Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add re-exports to use suggestions #73652

Merged
merged 2 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/librustc_resolve/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,22 +643,24 @@ impl<'a> Resolver<'a> {
let not_local_module = crate_name.name != kw::Crate;
let mut worklist =
vec![(start_module, Vec::<ast::PathSegment>::new(), true, not_local_module)];
let mut worklist_via_import = vec![];

while let Some((in_module, path_segments, accessible, in_module_is_extern)) = worklist.pop()
while let Some((in_module, path_segments, accessible, in_module_is_extern)) =
match worklist.pop() {
None => worklist_via_import.pop(),
Some(x) => Some(x),
}
petrochenkov marked this conversation as resolved.
Show resolved Hide resolved
{
// We have to visit module children in deterministic order to avoid
// instabilities in reported imports (#43552).
in_module.for_each_child(self, |this, ident, ns, name_binding| {
// avoid imports entirely
if name_binding.is_import() && !name_binding.is_extern_crate() {
return;
}

// avoid non-importable candidates as well
// avoid non-importable candidates
if !name_binding.is_importable() {
return;
}

let via_import = name_binding.is_import() && !name_binding.is_extern_crate();
petrochenkov marked this conversation as resolved.
Show resolved Hide resolved

let child_accessible =
accessible && this.is_accessible_from(name_binding.vis, parent_scope.module);

Expand All @@ -667,6 +669,10 @@ impl<'a> Resolver<'a> {
return;
}

if via_import && name_binding.is_possibly_imported_variant() {
petrochenkov marked this conversation as resolved.
Show resolved Hide resolved
return;
}

// collect results based on the filter function
// avoid suggesting anything from the same module in which we are resolving
if ident.name == lookup_ident.name
Expand Down Expand Up @@ -724,7 +730,8 @@ impl<'a> Resolver<'a> {
let is_extern = in_module_is_extern || name_binding.is_extern_crate();
// add the module to the lookup
if seen_modules.insert(module.def_id().unwrap()) {
worklist.push((module, path_segments, child_accessible, is_extern));
if via_import { &mut worklist_via_import } else { &mut worklist }
.push((module, path_segments, child_accessible, is_extern));
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,13 @@ impl<'a> NameBinding<'a> {
}
}

fn is_possibly_imported_variant(&self) -> bool {
match self.kind {
NameBindingKind::Import { binding, .. } => binding.is_possibly_imported_variant(),
_ => self.is_variant(),
}
}

// We sometimes need to treat variants as `pub` for backwards compatibility.
fn pseudo_vis(&self) -> ty::Visibility {
if self.is_variant() && self.res().def_id().is_local() {
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/glob-resolve1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ fn main() {
foo::<C>(); //~ ERROR: cannot find type `C` in this scope
foo::<D>(); //~ ERROR: cannot find type `D` in this scope
}

mod other {
pub fn import() {}
}
5 changes: 5 additions & 0 deletions src/test/ui/glob-resolve1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ error[E0425]: cannot find function `import` in this scope
|
LL | import();
| ^^^^^^ not found in this scope
|
help: consider importing this function
|
LL | use other::import;
|

error[E0412]: cannot find type `A` in this scope
--> $DIR/glob-resolve1.rs:28:11
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/namespace/namespace-mix.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ help: consider importing one of these items instead
|
LL | use m2::S;
|
LL | use namespace_mix::xm2::S;
LL | use xm2::S;
|

error[E0423]: expected value, found type alias `xm1::S`
Expand All @@ -39,7 +39,7 @@ help: consider importing one of these items instead
|
LL | use m2::S;
|
LL | use namespace_mix::xm2::S;
LL | use xm2::S;
|

error[E0423]: expected value, found struct variant `m7::V`
Expand All @@ -61,7 +61,7 @@ help: consider importing one of these items instead
|
LL | use m8::V;
|
LL | use namespace_mix::xm8::V;
LL | use xm8::V;
|

error[E0423]: expected value, found struct variant `xm7::V`
Expand All @@ -83,7 +83,7 @@ help: consider importing one of these items instead
|
LL | use m8::V;
|
LL | use namespace_mix::xm8::V;
LL | use xm8::V;
|

error[E0277]: the trait bound `c::Item: Impossible` is not satisfied
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/resolve/issue-21221-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ error[E0405]: cannot find trait `T` in this scope
LL | impl T for Foo { }
| ^ not found in this scope
|
help: consider importing this trait
help: consider importing one of these items
|
LL | use baz::T;
|
LL | use foo::bar::T;
|
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/resolve/privacy-enum-ctor.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ LL | let _: E = m::n::Z;
| ^
help: consider importing this enum
|
LL | use m::n::Z;
LL | use m::Z;
|

error[E0423]: expected value, found enum `m::n::Z`
Expand Down Expand Up @@ -165,7 +165,7 @@ LL | let _: E = m::n::Z::Fn;
| ^
help: consider importing this enum
|
LL | use m::n::Z;
LL | use m::Z;
|

error[E0412]: cannot find type `Z` in this scope
Expand All @@ -183,7 +183,7 @@ LL | let _: E = m::n::Z::Struct;
| ^
help: consider importing this enum
|
LL | use m::n::Z;
LL | use m::Z;
|

error[E0423]: expected value, found struct variant `m::n::Z::Struct`
Expand Down Expand Up @@ -212,7 +212,7 @@ LL | let _: E = m::n::Z::Unit {};
| ^
help: consider importing this enum
|
LL | use m::n::Z;
LL | use m::Z;
|

error[E0603]: enum `Z` is private
Expand Down