From 59bd98f2c0dc0d9184d0e237ac441cc1a16379ca Mon Sep 17 00:00:00 2001 From: Yukang Date: Mon, 21 Sep 2026 00:41:27 +0800 Subject: [PATCH 1/4] Add regression tests for private import diagnostics --- ...mport-alias-private-parent-issue-149418.rs | 28 +++ ...t-alias-private-parent-issue-149418.stderr | 42 ++++ ...import-alias-public-parent-issue-149418.rs | 113 +++++++++ ...rt-alias-public-parent-issue-149418.stderr | 218 ++++++++++++++++++ 4 files changed, 401 insertions(+) create mode 100644 tests/ui/imports/private-import-alias-private-parent-issue-149418.rs create mode 100644 tests/ui/imports/private-import-alias-private-parent-issue-149418.stderr create mode 100644 tests/ui/imports/private-import-alias-public-parent-issue-149418.rs create mode 100644 tests/ui/imports/private-import-alias-public-parent-issue-149418.stderr diff --git a/tests/ui/imports/private-import-alias-private-parent-issue-149418.rs b/tests/ui/imports/private-import-alias-private-parent-issue-149418.rs new file mode 100644 index 0000000000000..017b09def95f1 --- /dev/null +++ b/tests/ui/imports/private-import-alias-private-parent-issue-149418.rs @@ -0,0 +1,28 @@ +//@ check-fail +//@ edition: 2024 + +// Private import aliases should not suggest paths through private parent modules. + +mod delicious_snacks { + use self::fruits::PEAR as fruit; + + mod fruits { + pub const PEAR: &str = "Pear"; + pub const APPLE: &str = "Apple"; + } +} + +mod delicious_snacks_without_self { + use fruits::PEAR as fruit; + + mod fruits { + pub const PEAR: &str = "Pear"; + } +} + +fn main() { + let _ = delicious_snacks::fruit; + //~^ ERROR constant import `fruit` is private + let _ = delicious_snacks_without_self::fruit; + //~^ ERROR constant import `fruit` is private +} diff --git a/tests/ui/imports/private-import-alias-private-parent-issue-149418.stderr b/tests/ui/imports/private-import-alias-private-parent-issue-149418.stderr new file mode 100644 index 0000000000000..8830fe621acb3 --- /dev/null +++ b/tests/ui/imports/private-import-alias-private-parent-issue-149418.stderr @@ -0,0 +1,42 @@ +error[E0603]: constant import `fruit` is private + --> $DIR/private-import-alias-private-parent-issue-149418.rs:24:31 + | +LL | let _ = delicious_snacks::fruit; + | ^^^^^ private constant import + | +note: the constant import `fruit` is defined here... + --> $DIR/private-import-alias-private-parent-issue-149418.rs:7:9 + | +LL | use self::fruits::PEAR as fruit; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: ...and refers to the constant `PEAR` which is defined here + --> $DIR/private-import-alias-private-parent-issue-149418.rs:10:9 + | +LL | pub const PEAR: &str = "Pear"; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly + +error[E0603]: constant import `fruit` is private + --> $DIR/private-import-alias-private-parent-issue-149418.rs:26:44 + | +LL | let _ = delicious_snacks_without_self::fruit; + | ^^^^^ private constant import + | +note: the constant import `fruit` is defined here... + --> $DIR/private-import-alias-private-parent-issue-149418.rs:16:9 + | +LL | use fruits::PEAR as fruit; + | ^^^^^^^^^^^^^^^^^^^^^ +note: ...and refers to the constant `PEAR` which is defined here + --> $DIR/private-import-alias-private-parent-issue-149418.rs:19:9 + | +LL | pub const PEAR: &str = "Pear"; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly +help: import `fruit` directly + | +LL - let _ = delicious_snacks_without_self::fruit; +LL + let _ = fruits::fruit; + | + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0603`. diff --git a/tests/ui/imports/private-import-alias-public-parent-issue-149418.rs b/tests/ui/imports/private-import-alias-public-parent-issue-149418.rs new file mode 100644 index 0000000000000..9a1bcc4d5856c --- /dev/null +++ b/tests/ui/imports/private-import-alias-public-parent-issue-149418.rs @@ -0,0 +1,113 @@ + +//@ edition: 2024 + +// Private import aliases should suggest their source through an accessible parent module or +// re-export. + +#![allow(unused_imports, dead_code)] + +mod accessible_parent { + use self::fruits::PEAR as fruit; + + pub mod fruits { + pub const PEAR: &str = "Pear"; + } +} + +mod accessible_parent_without_self { + use fruits::PEAR as fruit; + + pub mod fruits { + pub const PEAR: &str = "Pear"; + } +} + +mod public_reexport { + mod private { + pub struct Item; + } + + pub use self::private::Item; +} + +mod nested_alias { + pub mod inner { + use super::super::public_reexport::Item as Alias; + } +} + +mod absolute_alias { + use crate::public_reexport::Item as Alias; +} + +mod hidden_reexport { + mod private { + pub use crate::public_reexport::Item; + } + + pub mod inner { + use super::private::Item as Alias; + } +} + +mod use_site { + fn check() { + let _: super::nested_alias::inner::Alias; + //~^ ERROR struct import `Alias` is private + // Keep pointing at a re-export when the original import path is accessible here. + let _: super::absolute_alias::Alias; + //~^ ERROR struct import `Alias` is private + // Finding another public re-export does not make `hidden_reexport::private` accessible. + let _: super::hidden_reexport::inner::Alias; + //~^ ERROR struct import `Alias` is private + } +} + +fn main() { + let _ = accessible_parent::fruit; + //~^ ERROR constant import `fruit` is private + let _ = accessible_parent_without_self::fruit; + //~^ ERROR constant import `fruit` is private +} + +mod crate_visible { + pub(crate) struct Item; + + pub mod nested { + use crate::crate_visible::Item as Alias; + } +} + +fn check_crate_visible() { + // A crate-visible definition can be recommended from within the same crate. + let _: crate_visible::nested::Alias; + //~^ ERROR struct import `Alias` is private +} + +mod inaccessible_import { + use crate::public_reexport::Item; + + pub mod nested { + use super::Item as Alias; + } +} + +mod accessible_import { + use crate::public_reexport::Item; + + mod nested { + use super::Item as Alias; + } + + mod use_site { + fn check() { + // The private import in our parent module is accessible here. + let _: super::nested::Alias; + //~^ ERROR struct import `Alias` is private + // `super::Item` resolves here, but not through `inaccessible_import::Item`. + // Do not recommend that inaccessible binding merely because its `Res` matches. + let _: crate::inaccessible_import::nested::Alias; + //~^ ERROR struct import `Alias` is private + } + } +} diff --git a/tests/ui/imports/private-import-alias-public-parent-issue-149418.stderr b/tests/ui/imports/private-import-alias-public-parent-issue-149418.stderr new file mode 100644 index 0000000000000..3eb39069274f4 --- /dev/null +++ b/tests/ui/imports/private-import-alias-public-parent-issue-149418.stderr @@ -0,0 +1,218 @@ +error[E0603]: struct import `Alias` is private + --> $DIR/private-import-alias-public-parent-issue-149418.rs:55:44 + | +LL | let _: super::nested_alias::inner::Alias; + | ^^^^^ private struct import + | +note: the struct import `Alias` is defined here... + --> $DIR/private-import-alias-public-parent-issue-149418.rs:35:13 + | +LL | use super::super::public_reexport::Item as Alias; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: ...and refers to the struct import `Item` which is defined here... + --> $DIR/private-import-alias-public-parent-issue-149418.rs:30:13 + | +LL | pub use self::private::Item; + | ^^^^^^^^^^^^^^^^^^^ you could import this re-export +note: ...and refers to the struct `Item` which is defined here + --> $DIR/private-import-alias-public-parent-issue-149418.rs:27:9 + | +LL | pub struct Item; + | ^^^^^^^^^^^^^^^^ you could import this directly +help: import `Alias` through the re-export + | +LL - let _: super::nested_alias::inner::Alias; +LL + let _: crate::public_reexport::Alias; + | + +error[E0603]: struct import `Alias` is private + --> $DIR/private-import-alias-public-parent-issue-149418.rs:58:39 + | +LL | let _: super::absolute_alias::Alias; + | ^^^^^ private struct import + | +note: the struct import `Alias` is defined here... + --> $DIR/private-import-alias-public-parent-issue-149418.rs:40:9 + | +LL | use crate::public_reexport::Item as Alias; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: ...and refers to the struct import `Item` which is defined here... + --> $DIR/private-import-alias-public-parent-issue-149418.rs:30:13 + | +LL | pub use self::private::Item; + | ^^^^^^^^^^^^^^^^^^^ you could import this re-export +note: ...and refers to the struct `Item` which is defined here + --> $DIR/private-import-alias-public-parent-issue-149418.rs:27:9 + | +LL | pub struct Item; + | ^^^^^^^^^^^^^^^^ you could import this directly +help: import `Alias` through the re-export + | +LL - let _: super::absolute_alias::Alias; +LL + let _: crate::public_reexport::Alias; + | + +error[E0603]: struct import `Alias` is private + --> $DIR/private-import-alias-public-parent-issue-149418.rs:61:47 + | +LL | let _: super::hidden_reexport::inner::Alias; + | ^^^^^ private struct import + | +note: the struct import `Alias` is defined here... + --> $DIR/private-import-alias-public-parent-issue-149418.rs:49:13 + | +LL | use super::private::Item as Alias; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: ...and refers to the struct import `Item` which is defined here... + --> $DIR/private-import-alias-public-parent-issue-149418.rs:45:17 + | +LL | pub use crate::public_reexport::Item; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this re-export +note: ...and refers to the struct import `Item` which is defined here... + --> $DIR/private-import-alias-public-parent-issue-149418.rs:30:13 + | +LL | pub use self::private::Item; + | ^^^^^^^^^^^^^^^^^^^ you could import this re-export +note: ...and refers to the struct `Item` which is defined here + --> $DIR/private-import-alias-public-parent-issue-149418.rs:27:9 + | +LL | pub struct Item; + | ^^^^^^^^^^^^^^^^ you could import this directly +help: import `Alias` through the re-export + | +LL - let _: super::hidden_reexport::inner::Alias; +LL + let _: crate::public_reexport::Alias; + | + +error[E0603]: constant import `fruit` is private + --> $DIR/private-import-alias-public-parent-issue-149418.rs:67:32 + | +LL | let _ = accessible_parent::fruit; + | ^^^^^ private constant import + | +note: the constant import `fruit` is defined here... + --> $DIR/private-import-alias-public-parent-issue-149418.rs:10:9 + | +LL | use self::fruits::PEAR as fruit; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: ...and refers to the constant `PEAR` which is defined here + --> $DIR/private-import-alias-public-parent-issue-149418.rs:13:9 + | +LL | pub const PEAR: &str = "Pear"; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly +help: import `fruit` directly + | +LL | let _ = accessible_parent::fruits::fruit; + | ++++++++ + +error[E0603]: constant import `fruit` is private + --> $DIR/private-import-alias-public-parent-issue-149418.rs:69:45 + | +LL | let _ = accessible_parent_without_self::fruit; + | ^^^^^ private constant import + | +note: the constant import `fruit` is defined here... + --> $DIR/private-import-alias-public-parent-issue-149418.rs:18:9 + | +LL | use fruits::PEAR as fruit; + | ^^^^^^^^^^^^^^^^^^^^^ +note: ...and refers to the constant `PEAR` which is defined here + --> $DIR/private-import-alias-public-parent-issue-149418.rs:21:9 + | +LL | pub const PEAR: &str = "Pear"; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly +help: import `fruit` directly + | +LL - let _ = accessible_parent_without_self::fruit; +LL + let _ = fruits::fruit; + | + +error[E0603]: struct import `Alias` is private + --> $DIR/private-import-alias-public-parent-issue-149418.rs:83:35 + | +LL | let _: crate_visible::nested::Alias; + | ^^^^^ private struct import + | +note: the struct import `Alias` is defined here... + --> $DIR/private-import-alias-public-parent-issue-149418.rs:77:13 + | +LL | use crate::crate_visible::Item as Alias; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: ...and refers to the struct `Item` which is defined here + --> $DIR/private-import-alias-public-parent-issue-149418.rs:74:5 + | +LL | pub(crate) struct Item; + | ^^^^^^^^^^^^^^^^^^^^^^^ +help: import `Alias` directly + | +LL - let _: crate_visible::nested::Alias; +LL + let _: crate::crate_visible::Alias; + | + +error[E0603]: struct import `Alias` is private + --> $DIR/private-import-alias-public-parent-issue-149418.rs:105:35 + | +LL | let _: super::nested::Alias; + | ^^^^^ private struct import + | +note: the struct import `Alias` is defined here... + --> $DIR/private-import-alias-public-parent-issue-149418.rs:99:13 + | +LL | use super::Item as Alias; + | ^^^^^^^^^^^^^^^^^^^^ +note: ...and refers to the struct import `Item` which is defined here... + --> $DIR/private-import-alias-public-parent-issue-149418.rs:96:9 + | +LL | use crate::public_reexport::Item; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: ...and refers to the struct import `Item` which is defined here... + --> $DIR/private-import-alias-public-parent-issue-149418.rs:30:13 + | +LL | pub use self::private::Item; + | ^^^^^^^^^^^^^^^^^^^ you could import this re-export +note: ...and refers to the struct `Item` which is defined here + --> $DIR/private-import-alias-public-parent-issue-149418.rs:27:9 + | +LL | pub struct Item; + | ^^^^^^^^^^^^^^^^ you could import this directly +help: import `Alias` through the re-export + | +LL - let _: super::nested::Alias; +LL + let _: super::Alias; + | + +error[E0603]: struct import `Alias` is private + --> $DIR/private-import-alias-public-parent-issue-149418.rs:109:56 + | +LL | let _: crate::inaccessible_import::nested::Alias; + | ^^^^^ private struct import + | +note: the struct import `Alias` is defined here... + --> $DIR/private-import-alias-public-parent-issue-149418.rs:91:13 + | +LL | use super::Item as Alias; + | ^^^^^^^^^^^^^^^^^^^^ +note: ...and refers to the struct import `Item` which is defined here... + --> $DIR/private-import-alias-public-parent-issue-149418.rs:88:9 + | +LL | use crate::public_reexport::Item; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: ...and refers to the struct import `Item` which is defined here... + --> $DIR/private-import-alias-public-parent-issue-149418.rs:30:13 + | +LL | pub use self::private::Item; + | ^^^^^^^^^^^^^^^^^^^ you could import this re-export +note: ...and refers to the struct `Item` which is defined here + --> $DIR/private-import-alias-public-parent-issue-149418.rs:27:9 + | +LL | pub struct Item; + | ^^^^^^^^^^^^^^^^ you could import this directly +help: import `Alias` through the re-export + | +LL - let _: crate::inaccessible_import::nested::Alias; +LL + let _: crate::inaccessible_import::Alias; + | + +error: aborting due to 8 previous errors + +For more information about this error, try `rustc --explain E0603`. From 74941f9e8ac4e608f685b692805c47a7afe23f63 Mon Sep 17 00:00:00 2001 From: Yukang Date: Mon, 21 Sep 2026 00:49:31 +0800 Subject: [PATCH 2/4] Fix private import suggestions and accessibility notes --- .../rustc_resolve/src/diagnostics/impls.rs | 220 ++++++++++-------- tests/ui/imports/issue-55884-2.fixed | 22 ++ tests/ui/imports/issue-55884-2.rs | 6 + tests/ui/imports/issue-55884-2.stderr | 14 +- ...t-alias-private-parent-issue-149418.stderr | 9 +- ...ort-alias-public-parent-issue-149418.fixed | 113 +++++++++ ...import-alias-public-parent-issue-149418.rs | 2 +- ...rt-alias-public-parent-issue-149418.stderr | 53 +++-- ...suggestion-path-156244.edition_2015.stderr | 2 +- ...suggestion-path-156244.edition_2018.stderr | 2 +- .../shadowed/shadowed-use-visibility.stderr | 4 +- 11 files changed, 310 insertions(+), 137 deletions(-) create mode 100644 tests/ui/imports/issue-55884-2.fixed create mode 100644 tests/ui/imports/private-import-alias-public-parent-issue-149418.fixed diff --git a/compiler/rustc_resolve/src/diagnostics/impls.rs b/compiler/rustc_resolve/src/diagnostics/impls.rs index 898b635b474b6..d626faba8024b 100644 --- a/compiler/rustc_resolve/src/diagnostics/impls.rs +++ b/compiler/rustc_resolve/src/diagnostics/impls.rs @@ -2590,6 +2590,52 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { Some(path) } + /// Returns the original import's source path if it resolves to `source_decl` from the use + /// site. + /// + /// The same `Res` may be reachable through several bindings, so resolve the last segment in + /// the module named by the rest of the path and compare the binding itself. + fn import_source_suggestion_path( + &self, + import: Import<'ra>, + source: Ident, + source_decl: Decl<'ra>, + parent_scope: &ParentScope<'ra>, + ) -> Option> { + let ns = source_decl.res().ns()?; + let path = Path { + span: source.span, + segments: import + .module_path + .iter() + // A leading `::` is only equivalent to a relative path before the 2018 edition. + .filter(|segment| { + segment.ident.name != kw::PathRoot || segment.ident.span.at_least_rust_2018() + }) + .map(|segment| segment.ident.clone()) + .chain(std::iter::once(source)) + .map(ast::PathSegment::from_ident) + .collect(), + }; + let segments = Segment::from_path(&path); + let (last, prefix) = segments.split_last()?; + let module = if prefix.is_empty() { + ModuleOrUniformRoot::CurrentScope + } else { + match self.cm().maybe_resolve_path(prefix, None, parent_scope, None) { + PathResult::Module(module) => module, + PathResult::NonModule(_) + | PathResult::Indeterminate + | PathResult::Failed { .. } => return None, + } + }; + let decl = self + .cm() + .maybe_resolve_ident_in_module(module, last.ident, ns, parent_scope, None) + .ok()?; + (decl == source_decl).then(|| path.segments.iter().map(|segment| segment.ident).collect()) + } + fn shorten_candidate_path( &self, suggestion: &mut ImportSuggestion, @@ -2806,111 +2852,101 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { } } + let has_direct_suggestion = !sugg_paths.is_empty(); // Print the whole import chain to make it easier to see what happens. let first_binding = decl; - let mut next_binding = Some(decl); + let mut next_binding = Some((decl, false)); let mut next_ident = ident; - while let Some(binding) = next_binding { + while let Some((binding, path_to_binding_accessible)) = next_binding { let name = next_ident; next_binding = match binding.kind { _ if res == Res::Err => None, - DeclKind::Import { source_decl, import, .. } => match import.kind { - _ if source_decl.span.is_dummy() => None, - ImportKind::Single { source, .. } => { - next_ident = source; - Some(source_decl) - } - ImportKind::Glob { .. } - | ImportKind::MacroUse { .. } - | ImportKind::MacroExport => Some(source_decl), - ImportKind::ExternCrate { .. } => None, - }, - _ => None, - }; - - match binding.kind { + DeclKind::Import { source_decl, .. } if source_decl.span.is_dummy() => None, DeclKind::Import { source_decl, import, .. } => { - let through_reexport = !matches!(source_decl.kind, DeclKind::Def(..)); - let uses_relative_path = import - .module_path - .first() - .is_some_and(|seg| matches!(seg.ident.name, kw::SelfLower | kw::Super)); - let res_def_id = res.opt_def_id(); - let path = if uses_relative_path { - // A path recovered from `self`/`super` is only useful if both the - // target and every module segment can be named from the failing use site. - let module_path = if let Some(ModuleOrUniformRoot::Module(module)) = - import.imported_module.get() - && module.is_local() - && let Some(module_path) = self.module_path_names(module) - && let Some(mut def_id) = module.opt_def_id() - && res_def_id.is_none_or(|def_id| { - self.is_accessible_from( - self.tcx.visibility(def_id), - parent_scope.module, - ) - }) { - // `module_path_names` tells us the resolved module's canonical path. - // Before suggesting that path from the failing use site, make sure - // every segment in it can actually be named from there. - let mut visible_from_use_site = true; - while let Some(parent) = self.tcx.opt_parent(def_id) { - if !self.is_accessible_from( - self.tcx.visibility(def_id), - parent_scope.module, - ) { - visible_from_use_site = false; - break; - } - if parent.is_top_level_module() { - break; - } - def_id = parent; + let source = match import.kind { + ImportKind::Single { source, .. } => Some(source), + ImportKind::Glob { .. } + | ImportKind::MacroUse { .. } + | ImportKind::MacroExport => Some(next_ident), + ImportKind::ExternCrate { .. } => None, + }; + if let Some(source) = source { + next_ident = source; + let source_res = source_decl.res(); + let through_reexport = source_decl.is_import(); + let mut path_is_accessible = has_direct_suggestion && !through_reexport; + if let Some(path) = self.import_source_suggestion_path( + import, + source, + source_decl, + &parent_scope, + ) { + path_is_accessible = true; + sugg_paths.push((path, through_reexport)); + } else if !has_direct_suggestion && let Some(ns) = source_res.ns() { + let candidate = self + .lookup_import_candidates(source, ns, &parent_scope, |res| { + res == source_res + }) + .into_iter() + .filter(|candidate| candidate.accessible) + .map(|mut candidate| { + self.shorten_candidate_path( + &mut candidate, + parent_scope.module, + ); + candidate + }) + .min_by_key(|candidate| { + ( + candidate.path.segments.len(), + candidate.path.segments[0].ident.name == sym::core, + ) + }); + if let Some(candidate) = candidate { + // The same item may have several re-exports. Only label this one + // if the suggested path resolves to its binding. + let segments = Segment::from_path(&candidate.path); + path_is_accessible = if let Some((last, prefix)) = + segments.split_last() + && let PathResult::Module(module) = self + .cm() + .maybe_resolve_path(prefix, None, &parent_scope, None) + { + self.cm() + .maybe_resolve_ident_in_module( + module, + last.ident, + ns, + &parent_scope, + None, + ) + .is_ok_and(|decl| decl == source_decl) + } else { + false + }; + let path = + candidate.path.segments.iter().map(|seg| seg.ident).collect(); + sugg_paths.push((path, candidate.via_import)); } - if visible_from_use_site { Some(module_path) } else { None } - } else { - None - }; - - module_path.map(|module_path| { - // `import.module_path` is relative to the import's module, not to the - // failing use site. - let mut path = Path { - span: ident.span, - segments: module_path - .into_iter() - .chain(std::iter::once(ident.name)) - .map(|name| { - ast::PathSegment::from_ident(Ident::with_dummy_span(name)) - }) - .collect(), - }; - self.shorten_import_path(res_def_id, &mut path, parent_scope.module); - path.segments.iter().map(|seg| seg.ident).collect() - }) + } + Some((source_decl, path_is_accessible)) } else { - // Don't include `{{root}}` in suggestions - it's an internal symbol - // that should never be shown to users. - Some( - import - .module_path - .iter() - .filter(|seg| seg.ident.name != kw::PathRoot) - .map(|seg| seg.ident.clone()) - .chain(std::iter::once(ident)) - .collect::>(), - ) - }; - if let Some(path) = path { - sugg_paths.push((path, through_reexport)); + None } } - DeclKind::Def(..) => {} - } + _ => None, + }; let first = binding == first_binding; let def_span = self.tcx.sess.source_map().guess_head_span(binding.span); let mut note_span = MultiSpan::from_span(def_span); - if !first && binding.vis().is_public() { + if !first + // The same `Res` may be reachable through a different import binding. + && self.is_accessible_from(binding.vis(), parent_scope.module) + && path_to_binding_accessible + // A circular import chain can lead back to the failing import itself. + && !binding.span.contains(ident.span) + { let desc = match binding.kind { DeclKind::Import { .. } => "re-export", _ => "directly", @@ -2956,6 +2992,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { // imports. `tests/ui/imports/issue-55884-2.rs` continue; } + // The suggested path may use a different name than the private alias. + let ident = *sugg.last().expect("at least one segment"); let path = join_path_idents(sugg); let sugg = if reexport { diagnostics::ImportIdent::ThroughReExport { span: dedup_span, ident, path } diff --git a/tests/ui/imports/issue-55884-2.fixed b/tests/ui/imports/issue-55884-2.fixed new file mode 100644 index 0000000000000..e77bfa658197a --- /dev/null +++ b/tests/ui/imports/issue-55884-2.fixed @@ -0,0 +1,22 @@ +//@ edition:2015 +//@ run-rustfix + +// Suggest a usable import path instead of a shadowed or circular re-export. + +#![allow(unused_imports)] + +mod options { + pub struct ParseOptions {} +} + +mod parser { + pub use options::*; + // Private single import shadows public glob import, but arrives too late for initial + // resolution of `use parser::ParseOptions` because it depends on that resolution itself. + #[allow(hidden_glob_reexports)] + use ParseOptions; +} + +pub use options::ParseOptions; //~ ERROR struct import `ParseOptions` is private + +fn main() {} diff --git a/tests/ui/imports/issue-55884-2.rs b/tests/ui/imports/issue-55884-2.rs index 1355a6c52b89a..699abe68d4dc7 100644 --- a/tests/ui/imports/issue-55884-2.rs +++ b/tests/ui/imports/issue-55884-2.rs @@ -1,4 +1,10 @@ //@ edition:2015 +//@ run-rustfix + +// Suggest a usable import path instead of a shadowed or circular re-export. + +#![allow(unused_imports)] + mod options { pub struct ParseOptions {} } diff --git a/tests/ui/imports/issue-55884-2.stderr b/tests/ui/imports/issue-55884-2.stderr index 55b5158ffca89..02ef97c7402bb 100644 --- a/tests/ui/imports/issue-55884-2.stderr +++ b/tests/ui/imports/issue-55884-2.stderr @@ -1,26 +1,26 @@ error[E0603]: struct import `ParseOptions` is private - --> $DIR/issue-55884-2.rs:14:17 + --> $DIR/issue-55884-2.rs:20:17 | LL | pub use parser::ParseOptions; | ^^^^^^^^^^^^ private struct import | note: the struct import `ParseOptions` is defined here... - --> $DIR/issue-55884-2.rs:11:9 + --> $DIR/issue-55884-2.rs:17:9 | LL | use ParseOptions; | ^^^^^^^^^^^^ note: ...and refers to the struct import `ParseOptions` which is defined here... - --> $DIR/issue-55884-2.rs:14:9 + --> $DIR/issue-55884-2.rs:20:9 | LL | pub use parser::ParseOptions; - | ^^^^^^^^^^^^^^^^^^^^ you could import this re-export + | ^^^^^^^^^^^^^^^^^^^^ note: ...and refers to the struct import `ParseOptions` which is defined here... - --> $DIR/issue-55884-2.rs:7:13 + --> $DIR/issue-55884-2.rs:13:13 | LL | pub use options::*; - | ^^^^^^^^^^ you could import this re-export + | ^^^^^^^^^^ note: ...and refers to the struct `ParseOptions` which is defined here - --> $DIR/issue-55884-2.rs:3:5 + --> $DIR/issue-55884-2.rs:9:5 | LL | pub struct ParseOptions {} | ^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly diff --git a/tests/ui/imports/private-import-alias-private-parent-issue-149418.stderr b/tests/ui/imports/private-import-alias-private-parent-issue-149418.stderr index 8830fe621acb3..bd19951b72631 100644 --- a/tests/ui/imports/private-import-alias-private-parent-issue-149418.stderr +++ b/tests/ui/imports/private-import-alias-private-parent-issue-149418.stderr @@ -13,7 +13,7 @@ note: ...and refers to the constant `PEAR` which is defined here --> $DIR/private-import-alias-private-parent-issue-149418.rs:10:9 | LL | pub const PEAR: &str = "Pear"; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0603]: constant import `fruit` is private --> $DIR/private-import-alias-private-parent-issue-149418.rs:26:44 @@ -30,12 +30,7 @@ note: ...and refers to the constant `PEAR` which is defined here --> $DIR/private-import-alias-private-parent-issue-149418.rs:19:9 | LL | pub const PEAR: &str = "Pear"; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly -help: import `fruit` directly - | -LL - let _ = delicious_snacks_without_self::fruit; -LL + let _ = fruits::fruit; - | + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to 2 previous errors diff --git a/tests/ui/imports/private-import-alias-public-parent-issue-149418.fixed b/tests/ui/imports/private-import-alias-public-parent-issue-149418.fixed new file mode 100644 index 0000000000000..ca30e9f03a574 --- /dev/null +++ b/tests/ui/imports/private-import-alias-public-parent-issue-149418.fixed @@ -0,0 +1,113 @@ +//@ run-rustfix +//@ edition: 2024 + +// Private import aliases should suggest their source through an accessible parent module or +// re-export. + +#![allow(unused_imports, dead_code)] + +mod accessible_parent { + use self::fruits::PEAR as fruit; + + pub mod fruits { + pub const PEAR: &str = "Pear"; + } +} + +mod accessible_parent_without_self { + use fruits::PEAR as fruit; + + pub mod fruits { + pub const PEAR: &str = "Pear"; + } +} + +mod public_reexport { + mod private { + pub struct Item; + } + + pub use self::private::Item; +} + +mod nested_alias { + pub mod inner { + use super::super::public_reexport::Item as Alias; + } +} + +mod absolute_alias { + use crate::public_reexport::Item as Alias; +} + +mod hidden_reexport { + mod private { + pub use crate::public_reexport::Item; + } + + pub mod inner { + use super::private::Item as Alias; + } +} + +mod use_site { + fn check() { + let _: crate::public_reexport::Item; + //~^ ERROR struct import `Alias` is private + // Keep pointing at a re-export when the original import path is accessible here. + let _: crate::public_reexport::Item; + //~^ ERROR struct import `Alias` is private + // Finding another public re-export does not make `hidden_reexport::private` accessible. + let _: crate::public_reexport::Item; + //~^ ERROR struct import `Alias` is private + } +} + +fn main() { + let _ = accessible_parent::fruits::PEAR; + //~^ ERROR constant import `fruit` is private + let _ = accessible_parent_without_self::fruits::PEAR; + //~^ ERROR constant import `fruit` is private +} + +mod crate_visible { + pub(crate) struct Item; + + pub mod nested { + use crate::crate_visible::Item as Alias; + } +} + +fn check_crate_visible() { + // A crate-visible definition can be recommended from within the same crate. + let _: crate::crate_visible::Item; + //~^ ERROR struct import `Alias` is private +} + +mod inaccessible_import { + use crate::public_reexport::Item; + + pub mod nested { + use super::Item as Alias; + } +} + +mod accessible_import { + use crate::public_reexport::Item; + + mod nested { + use super::Item as Alias; + } + + mod use_site { + fn check() { + // The private import in our parent module is accessible here. + let _: super::Item; + //~^ ERROR struct import `Alias` is private + // `super::Item` resolves here, but not through `inaccessible_import::Item`. + // Do not recommend that inaccessible binding merely because its `Res` matches. + let _: super::Item; + //~^ ERROR struct import `Alias` is private + } + } +} diff --git a/tests/ui/imports/private-import-alias-public-parent-issue-149418.rs b/tests/ui/imports/private-import-alias-public-parent-issue-149418.rs index 9a1bcc4d5856c..fba9c7508b48d 100644 --- a/tests/ui/imports/private-import-alias-public-parent-issue-149418.rs +++ b/tests/ui/imports/private-import-alias-public-parent-issue-149418.rs @@ -1,4 +1,4 @@ - +//@ run-rustfix //@ edition: 2024 // Private import aliases should suggest their source through an accessible parent module or diff --git a/tests/ui/imports/private-import-alias-public-parent-issue-149418.stderr b/tests/ui/imports/private-import-alias-public-parent-issue-149418.stderr index 3eb39069274f4..01d4067b1403e 100644 --- a/tests/ui/imports/private-import-alias-public-parent-issue-149418.stderr +++ b/tests/ui/imports/private-import-alias-public-parent-issue-149418.stderr @@ -18,11 +18,11 @@ note: ...and refers to the struct `Item` which is defined here --> $DIR/private-import-alias-public-parent-issue-149418.rs:27:9 | LL | pub struct Item; - | ^^^^^^^^^^^^^^^^ you could import this directly -help: import `Alias` through the re-export + | ^^^^^^^^^^^^^^^^ +help: import `Item` through the re-export | LL - let _: super::nested_alias::inner::Alias; -LL + let _: crate::public_reexport::Alias; +LL + let _: crate::public_reexport::Item; | error[E0603]: struct import `Alias` is private @@ -45,11 +45,11 @@ note: ...and refers to the struct `Item` which is defined here --> $DIR/private-import-alias-public-parent-issue-149418.rs:27:9 | LL | pub struct Item; - | ^^^^^^^^^^^^^^^^ you could import this directly -help: import `Alias` through the re-export + | ^^^^^^^^^^^^^^^^ +help: import `Item` through the re-export | LL - let _: super::absolute_alias::Alias; -LL + let _: crate::public_reexport::Alias; +LL + let _: crate::public_reexport::Item; | error[E0603]: struct import `Alias` is private @@ -67,7 +67,7 @@ note: ...and refers to the struct import `Item` which is defined here... --> $DIR/private-import-alias-public-parent-issue-149418.rs:45:17 | LL | pub use crate::public_reexport::Item; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this re-export + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: ...and refers to the struct import `Item` which is defined here... --> $DIR/private-import-alias-public-parent-issue-149418.rs:30:13 | @@ -77,11 +77,11 @@ note: ...and refers to the struct `Item` which is defined here --> $DIR/private-import-alias-public-parent-issue-149418.rs:27:9 | LL | pub struct Item; - | ^^^^^^^^^^^^^^^^ you could import this directly -help: import `Alias` through the re-export + | ^^^^^^^^^^^^^^^^ +help: import `Item` through the re-export | LL - let _: super::hidden_reexport::inner::Alias; -LL + let _: crate::public_reexport::Alias; +LL + let _: crate::public_reexport::Item; | error[E0603]: constant import `fruit` is private @@ -100,10 +100,10 @@ note: ...and refers to the constant `PEAR` which is defined here | LL | pub const PEAR: &str = "Pear"; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly -help: import `fruit` directly +help: import `PEAR` directly | -LL | let _ = accessible_parent::fruits::fruit; - | ++++++++ +LL | let _ = accessible_parent::fruits::PEAR; + | +++++++ error[E0603]: constant import `fruit` is private --> $DIR/private-import-alias-public-parent-issue-149418.rs:69:45 @@ -121,11 +121,10 @@ note: ...and refers to the constant `PEAR` which is defined here | LL | pub const PEAR: &str = "Pear"; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly -help: import `fruit` directly - | -LL - let _ = accessible_parent_without_self::fruit; -LL + let _ = fruits::fruit; +help: import `PEAR` directly | +LL | let _ = accessible_parent_without_self::fruits::PEAR; + | +++++++ error[E0603]: struct import `Alias` is private --> $DIR/private-import-alias-public-parent-issue-149418.rs:83:35 @@ -142,11 +141,11 @@ note: ...and refers to the struct `Item` which is defined here --> $DIR/private-import-alias-public-parent-issue-149418.rs:74:5 | LL | pub(crate) struct Item; - | ^^^^^^^^^^^^^^^^^^^^^^^ -help: import `Alias` directly + | ^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly +help: import `Item` directly | LL - let _: crate_visible::nested::Alias; -LL + let _: crate::crate_visible::Alias; +LL + let _: crate::crate_visible::Item; | error[E0603]: struct import `Alias` is private @@ -164,7 +163,7 @@ note: ...and refers to the struct import `Item` which is defined here... --> $DIR/private-import-alias-public-parent-issue-149418.rs:96:9 | LL | use crate::public_reexport::Item; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this re-export note: ...and refers to the struct import `Item` which is defined here... --> $DIR/private-import-alias-public-parent-issue-149418.rs:30:13 | @@ -174,11 +173,11 @@ note: ...and refers to the struct `Item` which is defined here --> $DIR/private-import-alias-public-parent-issue-149418.rs:27:9 | LL | pub struct Item; - | ^^^^^^^^^^^^^^^^ you could import this directly -help: import `Alias` through the re-export + | ^^^^^^^^^^^^^^^^ +help: import `Item` through the re-export | LL - let _: super::nested::Alias; -LL + let _: super::Alias; +LL + let _: super::Item; | error[E0603]: struct import `Alias` is private @@ -206,11 +205,11 @@ note: ...and refers to the struct `Item` which is defined here --> $DIR/private-import-alias-public-parent-issue-149418.rs:27:9 | LL | pub struct Item; - | ^^^^^^^^^^^^^^^^ you could import this directly -help: import `Alias` through the re-export + | ^^^^^^^^^^^^^^^^ +help: import `Item` through the re-export | LL - let _: crate::inaccessible_import::nested::Alias; -LL + let _: crate::inaccessible_import::Alias; +LL + let _: super::Item; | error: aborting due to 8 previous errors diff --git a/tests/ui/imports/private-import-suggestion-path-156244.edition_2015.stderr b/tests/ui/imports/private-import-suggestion-path-156244.edition_2015.stderr index 2a8795b6ab93c..cf865e86f39b2 100644 --- a/tests/ui/imports/private-import-suggestion-path-156244.edition_2015.stderr +++ b/tests/ui/imports/private-import-suggestion-path-156244.edition_2015.stderr @@ -91,7 +91,7 @@ note: ...and refers to the struct `Hi` which is defined here --> $DIR/private-import-suggestion-path-156244.rs:46:13 | LL | pub struct Hi; - | ^^^^^^^^^^^^^^ you could import this directly + | ^^^^^^^^^^^^^^ error[E0603]: module import `mem` is private --> $DIR/private-import-suggestion-path-156244.rs:65:21 diff --git a/tests/ui/imports/private-import-suggestion-path-156244.edition_2018.stderr b/tests/ui/imports/private-import-suggestion-path-156244.edition_2018.stderr index 9f112fe4e7551..65bca594bf5ae 100644 --- a/tests/ui/imports/private-import-suggestion-path-156244.edition_2018.stderr +++ b/tests/ui/imports/private-import-suggestion-path-156244.edition_2018.stderr @@ -91,7 +91,7 @@ note: ...and refers to the struct `Hi` which is defined here --> $DIR/private-import-suggestion-path-156244.rs:46:13 | LL | pub struct Hi; - | ^^^^^^^^^^^^^^ you could import this directly + | ^^^^^^^^^^^^^^ error[E0603]: module import `mem` is private --> $DIR/private-import-suggestion-path-156244.rs:65:21 diff --git a/tests/ui/shadowed/shadowed-use-visibility.stderr b/tests/ui/shadowed/shadowed-use-visibility.stderr index 1980437ab9242..091cbb3651232 100644 --- a/tests/ui/shadowed/shadowed-use-visibility.stderr +++ b/tests/ui/shadowed/shadowed-use-visibility.stderr @@ -13,7 +13,7 @@ note: ...and refers to the module `foo` which is defined here --> $DIR/shadowed-use-visibility.rs:1:1 | LL | mod foo { - | ^^^^^^^ + | ^^^^^^^ you could import this directly help: consider importing one of these functions instead | LL - use crate::foo::bar::f as g; @@ -38,7 +38,7 @@ note: ...and refers to the module `foo` which is defined here --> $DIR/shadowed-use-visibility.rs:1:1 | LL | mod foo { - | ^^^^^^^ + | ^^^^^^^ you could import this directly help: consider importing one of these functions instead | LL - use bar::f::f; From 17b39383dfa721d71d64ceea59fecd59e7fd2311 Mon Sep 17 00:00:00 2001 From: Yukang Date: Mon, 2 Nov 2026 09:19:14 +0800 Subject: [PATCH 3/4] code refactor on report_privacy_error --- .../rustc_resolve/src/diagnostics/impls.rs | 138 ++++++++---------- 1 file changed, 62 insertions(+), 76 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics/impls.rs b/compiler/rustc_resolve/src/diagnostics/impls.rs index d626faba8024b..d8a16791f5f33 100644 --- a/compiler/rustc_resolve/src/diagnostics/impls.rs +++ b/compiler/rustc_resolve/src/diagnostics/impls.rs @@ -2859,84 +2859,70 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { let mut next_ident = ident; while let Some((binding, path_to_binding_accessible)) = next_binding { let name = next_ident; - next_binding = match binding.kind { - _ if res == Res::Err => None, - DeclKind::Import { source_decl, .. } if source_decl.span.is_dummy() => None, - DeclKind::Import { source_decl, import, .. } => { - let source = match import.kind { - ImportKind::Single { source, .. } => Some(source), - ImportKind::Glob { .. } - | ImportKind::MacroUse { .. } - | ImportKind::MacroExport => Some(next_ident), - ImportKind::ExternCrate { .. } => None, - }; - if let Some(source) = source { - next_ident = source; - let source_res = source_decl.res(); - let through_reexport = source_decl.is_import(); - let mut path_is_accessible = has_direct_suggestion && !through_reexport; - if let Some(path) = self.import_source_suggestion_path( - import, - source, - source_decl, - &parent_scope, - ) { - path_is_accessible = true; - sugg_paths.push((path, through_reexport)); - } else if !has_direct_suggestion && let Some(ns) = source_res.ns() { - let candidate = self - .lookup_import_candidates(source, ns, &parent_scope, |res| { - res == source_res - }) - .into_iter() - .filter(|candidate| candidate.accessible) - .map(|mut candidate| { - self.shorten_candidate_path( - &mut candidate, - parent_scope.module, - ); - candidate - }) - .min_by_key(|candidate| { - ( - candidate.path.segments.len(), - candidate.path.segments[0].ident.name == sym::core, - ) - }); - if let Some(candidate) = candidate { - // The same item may have several re-exports. Only label this one - // if the suggested path resolves to its binding. - let segments = Segment::from_path(&candidate.path); - path_is_accessible = if let Some((last, prefix)) = - segments.split_last() - && let PathResult::Module(module) = self - .cm() - .maybe_resolve_path(prefix, None, &parent_scope, None) - { - self.cm() - .maybe_resolve_ident_in_module( - module, - last.ident, - ns, - &parent_scope, - None, - ) - .is_ok_and(|decl| decl == source_decl) - } else { - false - }; - let path = - candidate.path.segments.iter().map(|seg| seg.ident).collect(); - sugg_paths.push((path, candidate.via_import)); - } - } - Some((source_decl, path_is_accessible)) - } else { - None + next_binding = None; + if res != Res::Err + && let DeclKind::Import { source_decl, import, .. } = binding.kind + && !source_decl.span.is_dummy() + && let Some(source) = match import.kind { + ImportKind::Single { source, .. } => Some(source), + ImportKind::Glob { .. } + | ImportKind::MacroUse { .. } + | ImportKind::MacroExport => Some(next_ident), + ImportKind::ExternCrate { .. } => None, + } + { + next_ident = source; + let source_res = source_decl.res(); + let through_reexport = source_decl.is_import(); + let mut path_is_accessible = has_direct_suggestion && !through_reexport; + if let Some(path) = + self.import_source_suggestion_path(import, source, source_decl, &parent_scope) + { + path_is_accessible = true; + sugg_paths.push((path, through_reexport)); + } else if !has_direct_suggestion && let Some(ns) = source_res.ns() { + let candidate = self + .lookup_import_candidates(source, ns, &parent_scope, |res| { + res == source_res + }) + .into_iter() + .filter(|candidate| candidate.accessible) + .map(|mut candidate| { + self.shorten_candidate_path(&mut candidate, parent_scope.module); + candidate + }) + .min_by_key(|candidate| { + ( + candidate.path.segments.len(), + candidate.path.segments[0].ident.name == sym::core, + ) + }); + if let Some(candidate) = candidate { + // The same item may have several re-exports. Only label this one + // if the suggested path resolves to its binding. + let segments = Segment::from_path(&candidate.path); + path_is_accessible = if let Some((last, prefix)) = segments.split_last() + && let PathResult::Module(module) = + self.cm().maybe_resolve_path(prefix, None, &parent_scope, None) + { + self.cm() + .maybe_resolve_ident_in_module( + module, + last.ident, + ns, + &parent_scope, + None, + ) + .is_ok_and(|decl| decl == source_decl) + } else { + false + }; + let path = candidate.path.segments.iter().map(|seg| seg.ident).collect(); + sugg_paths.push((path, candidate.via_import)); } } - _ => None, - }; + next_binding = Some((source_decl, path_is_accessible)); + } let first = binding == first_binding; let def_span = self.tcx.sess.source_map().guess_head_span(binding.span); let mut note_span = MultiSpan::from_span(def_span); From 476a2be95651603948225c02fee1bc4f74fa650f Mon Sep 17 00:00:00 2001 From: Yukang Date: Mon, 2 Nov 2026 11:41:13 +0800 Subject: [PATCH 4/4] Update compiler/rustc_resolve/src/diagnostics/impls.rs Co-authored-by: Roland Xu --- .../rustc_resolve/src/diagnostics/impls.rs | 103 +++++++++--------- 1 file changed, 49 insertions(+), 54 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics/impls.rs b/compiler/rustc_resolve/src/diagnostics/impls.rs index d8a16791f5f33..7b4ecee52dad1 100644 --- a/compiler/rustc_resolve/src/diagnostics/impls.rs +++ b/compiler/rustc_resolve/src/diagnostics/impls.rs @@ -2603,37 +2603,32 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { parent_scope: &ParentScope<'ra>, ) -> Option> { let ns = source_decl.res().ns()?; - let path = Path { - span: source.span, - segments: import - .module_path - .iter() - // A leading `::` is only equivalent to a relative path before the 2018 edition. - .filter(|segment| { - segment.ident.name != kw::PathRoot || segment.ident.span.at_least_rust_2018() - }) - .map(|segment| segment.ident.clone()) - .chain(std::iter::once(source)) - .map(ast::PathSegment::from_ident) - .collect(), - }; - let segments = Segment::from_path(&path); - let (last, prefix) = segments.split_last()?; - let module = if prefix.is_empty() { + let module_path = import + .module_path + .iter() + .filter(|seg| seg.ident.name != kw::PathRoot || seg.ident.span.at_least_rust_2018()) + .cloned() + .collect::>(); + + let module = if module_path.is_empty() { ModuleOrUniformRoot::CurrentScope + } else if let PathResult::Module(module) = + self.cm().maybe_resolve_path(&module_path, None, parent_scope, None) + { + module } else { - match self.cm().maybe_resolve_path(prefix, None, parent_scope, None) { - PathResult::Module(module) => module, - PathResult::NonModule(_) - | PathResult::Indeterminate - | PathResult::Failed { .. } => return None, - } + return None; }; - let decl = self - .cm() - .maybe_resolve_ident_in_module(module, last.ident, ns, parent_scope, None) - .ok()?; - (decl == source_decl).then(|| path.segments.iter().map(|segment| segment.ident).collect()) + + let decl = + self.cm().maybe_resolve_ident_in_module(module, source, ns, parent_scope, None).ok()?; + (decl == source_decl).then(|| { + module_path + .into_iter() + .map(|segment| segment.ident) + .chain(std::iter::once(source)) + .collect() + }) } fn shorten_candidate_path( @@ -2880,8 +2875,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { { path_is_accessible = true; sugg_paths.push((path, through_reexport)); - } else if !has_direct_suggestion && let Some(ns) = source_res.ns() { - let candidate = self + } else if !has_direct_suggestion + && let Some(ns) = source_res.ns() + && let Some(candidate) = self .lookup_import_candidates(source, ns, &parent_scope, |res| { res == source_res }) @@ -2896,30 +2892,29 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { candidate.path.segments.len(), candidate.path.segments[0].ident.name == sym::core, ) - }); - if let Some(candidate) = candidate { - // The same item may have several re-exports. Only label this one - // if the suggested path resolves to its binding. - let segments = Segment::from_path(&candidate.path); - path_is_accessible = if let Some((last, prefix)) = segments.split_last() - && let PathResult::Module(module) = - self.cm().maybe_resolve_path(prefix, None, &parent_scope, None) - { - self.cm() - .maybe_resolve_ident_in_module( - module, - last.ident, - ns, - &parent_scope, - None, - ) - .is_ok_and(|decl| decl == source_decl) - } else { - false - }; - let path = candidate.path.segments.iter().map(|seg| seg.ident).collect(); - sugg_paths.push((path, candidate.via_import)); - } + }) + { + // The same item may have several re-exports. Only label this one + // if the suggested path resolves to its binding. + let segments = Segment::from_path(&candidate.path); + path_is_accessible = if let Some((last, prefix)) = segments.split_last() + && let PathResult::Module(module) = + self.cm().maybe_resolve_path(prefix, None, &parent_scope, None) + { + self.cm() + .maybe_resolve_ident_in_module( + module, + last.ident, + ns, + &parent_scope, + None, + ) + .is_ok_and(|decl| decl == source_decl) + } else { + false + }; + let path = candidate.path.segments.iter().map(|seg| seg.ident).collect(); + sugg_paths.push((path, candidate.via_import)); } next_binding = Some((source_decl, path_is_accessible)); }