Skip to content

Commit 42e5621

Browse files
committed
Auto merge of #84113 - SNCPlay42:suggestion-extern-crate, r=petrochenkov
Detect when suggested paths enter extern crates more rigorously When reporting resolution errors, the compiler tries to avoid suggesting importing inaccessible paths from other crates. However, the search for suggestions only recognized when it was entering a crate root directly, and so failed to recognize a path like `crate::module::private_item`, where `module` was imported from another crate with `use other_crate::module`, as entering another crate. Fixes #80079 Fixes #84081
2 parents 84e9397 + a3ee0bb commit 42e5621

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -758,17 +758,14 @@ impl<'a> Resolver<'a> {
758758
{
759759
let mut candidates = Vec::new();
760760
let mut seen_modules = FxHashSet::default();
761-
let not_local_module = crate_name.name != kw::Crate;
762-
let mut worklist =
763-
vec![(start_module, Vec::<ast::PathSegment>::new(), true, not_local_module)];
761+
let mut worklist = vec![(start_module, Vec::<ast::PathSegment>::new(), true)];
764762
let mut worklist_via_import = vec![];
765763

766-
while let Some((in_module, path_segments, accessible, in_module_is_extern)) =
767-
match worklist.pop() {
768-
None => worklist_via_import.pop(),
769-
Some(x) => Some(x),
770-
}
771-
{
764+
while let Some((in_module, path_segments, accessible)) = match worklist.pop() {
765+
None => worklist_via_import.pop(),
766+
Some(x) => Some(x),
767+
} {
768+
let in_module_is_extern = !in_module.def_id().unwrap().is_local();
772769
// We have to visit module children in deterministic order to avoid
773770
// instabilities in reported imports (#43552).
774771
in_module.for_each_child(self, |this, ident, ns, name_binding| {
@@ -850,11 +847,10 @@ impl<'a> Resolver<'a> {
850847
name_binding.is_extern_crate() && lookup_ident.span.rust_2018();
851848

852849
if !is_extern_crate_that_also_appears_in_prelude {
853-
let is_extern = in_module_is_extern || name_binding.is_extern_crate();
854850
// add the module to the lookup
855851
if seen_modules.insert(module.def_id().unwrap()) {
856852
if via_import { &mut worklist_via_import } else { &mut worklist }
857-
.push((module, path_segments, child_accessible, is_extern));
853+
.push((module, path_segments, child_accessible));
858854
}
859855
}
860856
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![crate_type = "lib"]
2+
3+
pub mod public {
4+
use private_import;
5+
6+
// should not be suggested since it is private
7+
struct Foo;
8+
9+
mod private_module {
10+
// should not be suggested since it is private
11+
pub struct Foo;
12+
}
13+
}
14+
15+
mod private_import {
16+
// should not be suggested since it is private
17+
pub struct Foo;
18+
}

src/test/ui/resolve/issue-80079.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// aux-build:issue-80079.rs
2+
3+
// using a module from another crate should not cause errors to suggest private
4+
// items in that module
5+
6+
extern crate issue_80079;
7+
8+
use issue_80079::public;
9+
10+
fn main() {
11+
let _ = Foo; //~ ERROR cannot find value `Foo` in this scope
12+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0425]: cannot find value `Foo` in this scope
2+
--> $DIR/issue-80079.rs:11:13
3+
|
4+
LL | let _ = Foo;
5+
| ^^^ not found in this scope
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)