diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2021-01-07 16:44:08 -0800 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2021-01-07 16:52:44 -0800 |
| commit | 9a5dcaab673b5b5a2842b689daeb0b2968fec476 (patch) | |
| tree | 12017f0f4ef9df5dfc0a9195f2ceb659bf678395 /compiler/rustc_resolve | |
| parent | c8915eebeaaef9f7cc1cff6ffd97f578b03c2ac9 (diff) | |
| download | rust-9a5dcaab673b5b5a2842b689daeb0b2968fec476.tar.gz rust-9a5dcaab673b5b5a2842b689daeb0b2968fec476.zip | |
Use correct span for structured suggestion
On structured suggestion for `let` -> `const` and `const` -> `let`, use a proper `Span` and update tests to check the correct application. Follow up to #80012.
Diffstat (limited to 'compiler/rustc_resolve')
| -rw-r--r-- | compiler/rustc_resolve/src/diagnostics.rs | 26 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/lib.rs | 21 |
2 files changed, 31 insertions, 16 deletions
diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 6a181dbab5a..03b66a3f7b3 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -398,20 +398,30 @@ impl<'a> Resolver<'a> { err.help("use the `|| { ... }` closure form instead"); err } - ResolutionError::AttemptToUseNonConstantValueInConstant(ident, sugg) => { + ResolutionError::AttemptToUseNonConstantValueInConstant(ident, sugg, current) => { let mut err = struct_span_err!( self.session, span, E0435, "attempt to use a non-constant value in a constant" ); - err.span_suggestion( - ident.span, - &sugg, - "".to_string(), - Applicability::MaybeIncorrect, - ); - err.span_label(span, "non-constant value"); + // let foo =... + // ^^^ given this Span + // ------- get this Span to have an applicable suggestion + let sp = + self.session.source_map().span_extend_to_prev_str(ident.span, current, true); + if sp.lo().0 == 0 { + err.span_label(ident.span, &format!("this would need to be a `{}`", sugg)); + } else { + let sp = sp.with_lo(BytePos(sp.lo().0 - current.len() as u32)); + err.span_suggestion( + sp, + &format!("consider using `{}` instead of `{}`", sugg, current), + format!("{} {}", sugg, ident), + Applicability::MaybeIncorrect, + ); + err.span_label(span, "non-constant value"); + } err } ResolutionError::BindingShadowsSomethingUnacceptable(what_binding, name, binding) => { diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index a6d0240b6fd..2c68e041858 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -210,7 +210,11 @@ enum ResolutionError<'a> { /// Error E0434: can't capture dynamic environment in a fn item. CannotCaptureDynamicEnvironmentInFnItem, /// Error E0435: attempt to use a non-constant value in a constant. - AttemptToUseNonConstantValueInConstant(Ident, String), + AttemptToUseNonConstantValueInConstant( + Ident, + /* suggestion */ &'static str, + /* current */ &'static str, + ), /// Error E0530: `X` bindings cannot shadow `Y`s. BindingShadowsSomethingUnacceptable(&'static str, Symbol, &'a NameBinding<'a>), /// Error E0128: type parameters with a default cannot use forward-declared identifiers. @@ -2614,18 +2618,19 @@ impl<'a> Resolver<'a> { ConstantItemKind::Const => "const", ConstantItemKind::Static => "static", }; - let sugg = format!( - "consider using `let` instead of `{}`", - kind_str - ); - (span, AttemptToUseNonConstantValueInConstant(ident, sugg)) + ( + span, + AttemptToUseNonConstantValueInConstant( + ident, "let", kind_str, + ), + ) } else { - let sugg = "consider using `const` instead of `let`"; ( rib_ident.span, AttemptToUseNonConstantValueInConstant( original_rib_ident_def, - sugg.to_string(), + "const", + "let", ), ) }; |
