Skip to content

Commit 78d08a2

Browse files
authored
Rollup merge of #72789 - petrochenkov:impcand, r=davidtwco
resolve: Do not suggest imports from the same module in which we are resolving Based on the idea from #72623.
2 parents a70fb70 + 21fca7a commit 78d08a2

File tree

5 files changed

+14
-13
lines changed

5 files changed

+14
-13
lines changed

src/librustc_resolve/diagnostics.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ impl<'a> Resolver<'a> {
629629
&mut self,
630630
lookup_ident: Ident,
631631
namespace: Namespace,
632+
parent_scope: &ParentScope<'a>,
632633
start_module: Module<'a>,
633634
crate_name: Ident,
634635
filter_fn: FilterFn,
@@ -655,7 +656,11 @@ impl<'a> Resolver<'a> {
655656
}
656657

657658
// collect results based on the filter function
658-
if ident.name == lookup_ident.name && ns == namespace {
659+
// avoid suggesting anything from the same module in which we are resolving
660+
if ident.name == lookup_ident.name
661+
&& ns == namespace
662+
&& !ptr::eq(in_module, parent_scope.module)
663+
{
659664
let res = name_binding.res();
660665
if filter_fn(res) {
661666
// create the path
@@ -722,6 +727,7 @@ impl<'a> Resolver<'a> {
722727
&mut self,
723728
lookup_ident: Ident,
724729
namespace: Namespace,
730+
parent_scope: &ParentScope<'a>,
725731
filter_fn: FilterFn,
726732
) -> Vec<ImportSuggestion>
727733
where
@@ -730,6 +736,7 @@ impl<'a> Resolver<'a> {
730736
let mut suggestions = self.lookup_import_candidates_from_module(
731737
lookup_ident,
732738
namespace,
739+
parent_scope,
733740
self.graph_root,
734741
Ident::with_dummy_span(kw::Crate),
735742
&filter_fn,
@@ -754,6 +761,7 @@ impl<'a> Resolver<'a> {
754761
suggestions.extend(self.lookup_import_candidates_from_module(
755762
lookup_ident,
756763
namespace,
764+
parent_scope,
757765
crate_root,
758766
ident,
759767
&filter_fn,

src/librustc_resolve/late/diagnostics.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
212212
let ident = path.last().unwrap().ident;
213213
let candidates = self
214214
.r
215-
.lookup_import_candidates(ident, ns, is_expected)
215+
.lookup_import_candidates(ident, ns, &self.parent_scope, is_expected)
216216
.drain(..)
217217
.filter(|ImportSuggestion { did, .. }| {
218218
match (did, res.and_then(|res| res.opt_def_id())) {
@@ -223,7 +223,8 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
223223
.collect::<Vec<_>>();
224224
let crate_def_id = DefId::local(CRATE_DEF_INDEX);
225225
if candidates.is_empty() && is_expected(Res::Def(DefKind::Enum, crate_def_id)) {
226-
let enum_candidates = self.r.lookup_import_candidates(ident, ns, is_enum_variant);
226+
let enum_candidates =
227+
self.r.lookup_import_candidates(ident, ns, &self.parent_scope, is_enum_variant);
227228
let mut enum_candidates = enum_candidates
228229
.iter()
229230
.map(|suggestion| import_candidate_to_enum_paths(&suggestion))

src/librustc_resolve/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2235,7 +2235,8 @@ impl<'a> Resolver<'a> {
22352235
Res::Def(DefKind::Mod, _) => true,
22362236
_ => false,
22372237
};
2238-
let mut candidates = self.lookup_import_candidates(ident, TypeNS, is_mod);
2238+
let mut candidates =
2239+
self.lookup_import_candidates(ident, TypeNS, parent_scope, is_mod);
22392240
candidates.sort_by_cached_key(|c| {
22402241
(c.path.segments.len(), pprust::path_to_string(&c.path))
22412242
});

src/test/ui/lexical-scopes.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ error[E0574]: expected struct, variant or union type, found type parameter `T`
33
|
44
LL | let t = T { i: 0 };
55
| ^ not a struct, variant or union type
6-
|
7-
help: consider importing this struct instead
8-
|
9-
LL | use T;
10-
|
116

127
error[E0599]: no function or associated item named `f` found for type parameter `Foo` in the current scope
138
--> $DIR/lexical-scopes.rs:10:10

src/test/ui/proc-macro/mixed-site-span.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ LL | pass_dollar_crate!();
2727
| ^^^^^^^^^^^^^^^^^^^^^ not found in `$crate`
2828
|
2929
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
30-
help: consider importing this struct
31-
|
32-
LL | use ItemUse;
33-
|
3430

3531
error: aborting due to 4 previous errors
3632

0 commit comments

Comments
 (0)