From 52f1847bca00890aaf424634f9c0f52960866ee0 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 30 May 2020 19:54:44 +0300 Subject: [PATCH 1/2] resolve: Pass parent scope to the function providing import suggestions --- src/librustc_resolve/diagnostics.rs | 4 ++++ src/librustc_resolve/late/diagnostics.rs | 5 +++-- src/librustc_resolve/lib.rs | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index ea237f1a04f99..176204affde81 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -629,6 +629,7 @@ impl<'a> Resolver<'a> { &mut self, lookup_ident: Ident, namespace: Namespace, + _parent_scope: &ParentScope<'a>, start_module: Module<'a>, crate_name: Ident, filter_fn: FilterFn, @@ -722,6 +723,7 @@ impl<'a> Resolver<'a> { &mut self, lookup_ident: Ident, namespace: Namespace, + parent_scope: &ParentScope<'a>, filter_fn: FilterFn, ) -> Vec where @@ -730,6 +732,7 @@ impl<'a> Resolver<'a> { let mut suggestions = self.lookup_import_candidates_from_module( lookup_ident, namespace, + parent_scope, self.graph_root, Ident::with_dummy_span(kw::Crate), &filter_fn, @@ -754,6 +757,7 @@ impl<'a> Resolver<'a> { suggestions.extend(self.lookup_import_candidates_from_module( lookup_ident, namespace, + parent_scope, crate_root, ident, &filter_fn, diff --git a/src/librustc_resolve/late/diagnostics.rs b/src/librustc_resolve/late/diagnostics.rs index b1a1f8725a180..ed7adefcb8c9e 100644 --- a/src/librustc_resolve/late/diagnostics.rs +++ b/src/librustc_resolve/late/diagnostics.rs @@ -212,7 +212,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> { let ident = path.last().unwrap().ident; let candidates = self .r - .lookup_import_candidates(ident, ns, is_expected) + .lookup_import_candidates(ident, ns, &self.parent_scope, is_expected) .drain(..) .filter(|ImportSuggestion { did, .. }| { match (did, res.and_then(|res| res.opt_def_id())) { @@ -223,7 +223,8 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> { .collect::>(); let crate_def_id = DefId::local(CRATE_DEF_INDEX); if candidates.is_empty() && is_expected(Res::Def(DefKind::Enum, crate_def_id)) { - let enum_candidates = self.r.lookup_import_candidates(ident, ns, is_enum_variant); + let enum_candidates = + self.r.lookup_import_candidates(ident, ns, &self.parent_scope, is_enum_variant); let mut enum_candidates = enum_candidates .iter() .map(|suggestion| import_candidate_to_enum_paths(&suggestion)) diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index b50f9fe8e907d..3d6b0ea98bf47 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -2256,7 +2256,8 @@ impl<'a> Resolver<'a> { Res::Def(DefKind::Mod, _) => true, _ => false, }; - let mut candidates = self.lookup_import_candidates(ident, TypeNS, is_mod); + let mut candidates = + self.lookup_import_candidates(ident, TypeNS, parent_scope, is_mod); candidates.sort_by_cached_key(|c| { (c.path.segments.len(), pprust::path_to_string(&c.path)) }); From 21fca7aae7213b651399e95d3d83f85f00734036 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 30 May 2020 20:11:14 +0300 Subject: [PATCH 2/2] resolve: Do not suggest imports from the same module in which we are resolving --- src/librustc_resolve/diagnostics.rs | 8 ++++++-- src/test/ui/lexical-scopes.stderr | 5 ----- src/test/ui/proc-macro/mixed-site-span.stderr | 4 ---- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 176204affde81..a952e08c2d6eb 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -629,7 +629,7 @@ impl<'a> Resolver<'a> { &mut self, lookup_ident: Ident, namespace: Namespace, - _parent_scope: &ParentScope<'a>, + parent_scope: &ParentScope<'a>, start_module: Module<'a>, crate_name: Ident, filter_fn: FilterFn, @@ -656,7 +656,11 @@ impl<'a> Resolver<'a> { } // collect results based on the filter function - if ident.name == lookup_ident.name && ns == namespace { + // avoid suggesting anything from the same module in which we are resolving + if ident.name == lookup_ident.name + && ns == namespace + && !ptr::eq(in_module, parent_scope.module) + { let res = name_binding.res(); if filter_fn(res) { // create the path diff --git a/src/test/ui/lexical-scopes.stderr b/src/test/ui/lexical-scopes.stderr index dce7054517030..1e6a35ed479f6 100644 --- a/src/test/ui/lexical-scopes.stderr +++ b/src/test/ui/lexical-scopes.stderr @@ -3,11 +3,6 @@ error[E0574]: expected struct, variant or union type, found type parameter `T` | LL | let t = T { i: 0 }; | ^ not a struct, variant or union type - | -help: consider importing this struct instead - | -LL | use T; - | error[E0599]: no function or associated item named `f` found for type parameter `Foo` in the current scope --> $DIR/lexical-scopes.rs:10:10 diff --git a/src/test/ui/proc-macro/mixed-site-span.stderr b/src/test/ui/proc-macro/mixed-site-span.stderr index 2b851a76f6a0f..6244ffc47a64b 100644 --- a/src/test/ui/proc-macro/mixed-site-span.stderr +++ b/src/test/ui/proc-macro/mixed-site-span.stderr @@ -27,10 +27,6 @@ LL | pass_dollar_crate!(); | ^^^^^^^^^^^^^^^^^^^^^ not found in `$crate` | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) -help: consider importing this struct - | -LL | use ItemUse; - | error: aborting due to 4 previous errors