Importing rustc-1.51.0
Change-Id: Ie4f520eabee71971211e4637a45cbc9bf4a91e95
diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs
index 809de9b..3b02f74 100644
--- a/compiler/rustc_resolve/src/diagnostics.rs
+++ b/compiler/rustc_resolve/src/diagnostics.rs
@@ -398,14 +398,30 @@
err.help("use the `|| { ... }` closure form instead");
err
}
- ResolutionError::AttemptToUseNonConstantValueInConstant => {
+ 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_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) => {
@@ -595,7 +611,8 @@
filter_fn: &impl Fn(Res) -> bool,
) -> Option<TypoSuggestion> {
let mut suggestions = Vec::new();
- self.visit_scopes(scope_set, parent_scope, ident, |this, scope, use_prelude, _| {
+ let ctxt = ident.span.ctxt();
+ self.visit_scopes(scope_set, parent_scope, ctxt, |this, scope, use_prelude, _| {
match scope {
Scope::DeriveHelpers(expn_id) => {
let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
@@ -666,7 +683,7 @@
));
}
Scope::BuiltinAttrs => {
- let res = Res::NonMacroAttr(NonMacroAttrKind::Builtin);
+ let res = Res::NonMacroAttr(NonMacroAttrKind::Builtin(kw::Empty));
if filter_fn(res) {
suggestions.extend(
BUILTIN_ATTRIBUTES
@@ -960,7 +977,7 @@
});
if let Some(def_span) = def_span {
if span.overlaps(def_span) {
- // Don't suggest typo suggestion for itself like in the followoing:
+ // Don't suggest typo suggestion for itself like in the following:
// error[E0423]: expected function, tuple struct or tuple variant, found struct `X`
// --> $DIR/issue-64792-bad-unicode-ctor.rs:3:14
// |
@@ -1094,10 +1111,9 @@
_,
) = binding.kind
{
- let def_id = (&*self).parent(ctor_def_id).expect("no parent for a constructor");
+ let def_id = self.parent(ctor_def_id).expect("no parent for a constructor");
let fields = self.field_names.get(&def_id)?;
- let first_field = fields.first()?; // Handle `struct Foo()`
- return Some(fields.iter().fold(first_field.span, |acc, field| acc.to(field.span)));
+ return fields.iter().map(|name| name.span).reduce(Span::to); // None for `struct Foo()`
}
None
}