about summary refs log tree commit diff
path: root/src/librustc_resolve
diff options
context:
space:
mode:
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>2018-09-08 02:51:20 +0300
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>2018-09-08 14:15:11 +0300
commit2dce3779bbc0353ff9fb544774417a851027fcab (patch)
treeb1947e6ce697bd9b82be8a030d08f745e74375f7 /src/librustc_resolve
parent9beb5c3ef3062226783529f626fb8c4a7eb7f936 (diff)
downloadrust-2dce3779bbc0353ff9fb544774417a851027fcab.tar.gz
rust-2dce3779bbc0353ff9fb544774417a851027fcab.zip
resolve: More precise spans for ambiguous resolution errors
Add labels to ambiguous resolution errors
Diffstat (limited to 'src/librustc_resolve')
-rw-r--r--src/librustc_resolve/lib.rs32
-rw-r--r--src/librustc_resolve/macros.rs10
-rw-r--r--src/librustc_resolve/resolve_imports.rs8
3 files changed, 22 insertions, 28 deletions
diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs
index 6c9a924081a..b57a8a4d271 100644
--- a/src/librustc_resolve/lib.rs
+++ b/src/librustc_resolve/lib.rs
@@ -1164,8 +1164,7 @@ struct UseError<'a> {
 }
 
 struct AmbiguityError<'a> {
-    span: Span,
-    name: Name,
+    ident: Ident,
     b1: &'a NameBinding<'a>,
     b2: &'a NameBinding<'a>,
 }
@@ -1818,7 +1817,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
         self.arenas.alloc_module(module)
     }
 
-    fn record_use(&mut self, ident: Ident, ns: Namespace, binding: &'a NameBinding<'a>, span: Span)
+    fn record_use(&mut self, ident: Ident, ns: Namespace, binding: &'a NameBinding<'a>)
                   -> bool /* true if an error was reported */ {
         match binding.kind {
             NameBindingKind::Import { directive, binding, ref used }
@@ -1827,13 +1826,11 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
                 directive.used.set(true);
                 self.used_imports.insert((directive.id, ns));
                 self.add_to_glob_map(directive.id, ident);
-                self.record_use(ident, ns, binding, span)
+                self.record_use(ident, ns, binding)
             }
             NameBindingKind::Import { .. } => false,
             NameBindingKind::Ambiguity { b1, b2 } => {
-                self.ambiguity_errors.push(AmbiguityError {
-                    span, name: ident.name, b1, b2,
-                });
+                self.ambiguity_errors.push(AmbiguityError { ident, b1, b2 });
                 true
             }
             _ => false
@@ -2853,7 +2850,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
                             Def::Const(..) if is_syntactic_ambiguity => {
                                 // Disambiguate in favor of a unit struct/variant
                                 // or constant pattern.
-                                self.record_use(ident, ValueNS, binding.unwrap(), ident.span);
+                                self.record_use(ident, ValueNS, binding.unwrap());
                                 Some(PathResolution::new(def))
                             }
                             Def::StructCtor(..) | Def::VariantCtor(..) |
@@ -4532,12 +4529,12 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
         vis.is_accessible_from(module.normal_ancestor_id, self)
     }
 
-    fn report_ambiguity_error(&self, name: Name, span: Span, b1: &NameBinding, b2: &NameBinding) {
+    fn report_ambiguity_error(&self, ident: Ident, b1: &NameBinding, b2: &NameBinding) {
         let participle = |is_import: bool| if is_import { "imported" } else { "defined" };
         let msg1 =
-            format!("`{}` could refer to the name {} here", name, participle(b1.is_import()));
+            format!("`{}` could refer to the name {} here", ident, participle(b1.is_import()));
         let msg2 =
-            format!("`{}` could also refer to the name {} here", name, participle(b2.is_import()));
+            format!("`{}` could also refer to the name {} here", ident, participle(b2.is_import()));
         let note = if b1.expansion != Mark::root() {
             Some(if let Def::Macro(..) = b1.def() {
                 format!("macro-expanded {} do not shadow",
@@ -4547,16 +4544,17 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
                         if b1.is_import() { "imports" } else { "items" })
             })
         } else if b1.is_glob_import() {
-            Some(format!("consider adding an explicit import of `{}` to disambiguate", name))
+            Some(format!("consider adding an explicit import of `{}` to disambiguate", ident))
         } else {
             None
         };
 
-        let mut err = struct_span_err!(self.session, span, E0659, "`{}` is ambiguous", name);
+        let mut err = struct_span_err!(self.session, ident.span, E0659, "`{}` is ambiguous", ident);
+        err.span_label(ident.span, "ambiguous name");
         err.span_note(b1.span, &msg1);
         match b2.def() {
             Def::Macro(..) if b2.span.is_dummy() =>
-                err.note(&format!("`{}` is also a builtin macro", name)),
+                err.note(&format!("`{}` is also a builtin macro", ident)),
             _ => err.span_note(b2.span, &msg2),
         };
         if let Some(note) = note {
@@ -4581,9 +4579,9 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
             );
         }
 
-        for &AmbiguityError { span, name, b1, b2 } in &self.ambiguity_errors {
-            if reported_spans.insert(span) {
-                self.report_ambiguity_error(name, span, b1, b2);
+        for &AmbiguityError { ident, b1, b2 } in &self.ambiguity_errors {
+            if reported_spans.insert(ident.span) {
+                self.report_ambiguity_error(ident, b1, b2);
             }
         }
 
diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs
index 0359d62104c..96c2e0a6ba7 100644
--- a/src/librustc_resolve/macros.rs
+++ b/src/librustc_resolve/macros.rs
@@ -757,8 +757,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
                            (innermost_result.0.is_glob_import() ||
                             innermost_result.0.may_appear_after(invoc_id, result.0)) {
                             self.ambiguity_errors.push(AmbiguityError {
-                                span: path_span,
-                                name: ident.name,
+                                ident,
                                 b1: innermost_result.0,
                                 b2: result.0,
                             });
@@ -850,8 +849,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
                         if result.def() != innermost_result.def() &&
                            innermost_result.may_appear_after(invoc_id, result) {
                             self.ambiguity_errors.push(AmbiguityError {
-                                span: ident.span,
-                                name: ident.name,
+                                ident,
                                 b1: innermost_result,
                                 b2: result,
                             });
@@ -929,7 +927,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
                 (Some(legacy_binding), Ok((binding, FromPrelude(from_prelude))))
                         if !from_prelude || legacy_binding.may_appear_after(invoc_id, binding) => {
                     if legacy_binding.def_ignoring_ambiguity() != binding.def_ignoring_ambiguity() {
-                        self.report_ambiguity_error(ident.name, span, legacy_binding, binding);
+                        self.report_ambiguity_error(ident, legacy_binding, binding);
                     }
                 },
                 // OK, non-macro-expanded legacy wins over prelude even if defs are different
@@ -942,7 +940,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
                 (None, Ok((binding, FromPrelude(from_prelude)))) => {
                     check_consistency(self, binding.def_ignoring_ambiguity());
                     if from_prelude {
-                        self.record_use(ident, MacroNS, binding, span);
+                        self.record_use(ident, MacroNS, binding);
                         self.err_if_macro_use_proc_macro(ident.name, span, binding);
                     }
                 }
diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs
index 9d2e51069c7..ef7b5c58fdd 100644
--- a/src/librustc_resolve/resolve_imports.rs
+++ b/src/librustc_resolve/resolve_imports.rs
@@ -242,21 +242,19 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
         if record_used {
             if let Some(binding) = resolution.binding {
                 if let Some(shadowed_glob) = resolution.shadowed_glob {
-                    let name = ident.name;
                     // Forbid expanded shadowing to avoid time travel.
                     if restricted_shadowing &&
                        binding.expansion != Mark::root() &&
                        ns != MacroNS && // In MacroNS, `try_define` always forbids this shadowing
                        binding.def() != shadowed_glob.def() {
                         self.ambiguity_errors.push(AmbiguityError {
-                            span: path_span,
-                            name,
+                            ident,
                             b1: binding,
                             b2: shadowed_glob,
                         });
                     }
                 }
-                if self.record_use(ident, ns, binding, path_span) {
+                if self.record_use(ident, ns, binding) {
                     return Ok(self.dummy_binding);
                 }
                 if !self.is_accessible(binding.vis) {
@@ -936,7 +934,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
         self.per_ns(|this, ns| if !type_ns_only || ns == TypeNS {
             if let Ok(binding) = result[ns].get() {
                 all_ns_err = false;
-                if this.record_use(ident, ns, binding, directive.span) {
+                if this.record_use(ident, ns, binding) {
                     if let ModuleOrUniformRoot::Module(module) = module {
                         this.resolution(module, ident, ns).borrow_mut().binding =
                             Some(this.dummy_binding);