diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index c082455380ce..5664933673a3 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -2483,6 +2483,11 @@ fn report_privacy_error(&mut self, privacy_error: &PrivacyError<'ra>) { // `tests/ui/imports/issue-55884-2.rs` continue; } + // In nested imports `dedup_span` is just the inner ident, so a full path + // substitution would produce invalid code. See #156060. + if single_nested { + break; + } let path = join_path_idents(sugg); let sugg = if reexport { errors::ImportIdent::ThroughReExport { span: dedup_span, ident, path } diff --git a/tests/ui/imports/private-import-nested-suggestion-156060.rs b/tests/ui/imports/private-import-nested-suggestion-156060.rs new file mode 100644 index 000000000000..90a7b94aad55 --- /dev/null +++ b/tests/ui/imports/private-import-nested-suggestion-156060.rs @@ -0,0 +1,17 @@ +// Regression test for #156060. + +mod one { + pub struct One(); +} + +mod two { + use crate::one::One; + pub struct Two(); +} + +mod test { + use crate::two::{One, Two}; + //~^ ERROR struct import `One` is private [E0603] +} + +fn main() {} diff --git a/tests/ui/imports/private-import-nested-suggestion-156060.stderr b/tests/ui/imports/private-import-nested-suggestion-156060.stderr new file mode 100644 index 000000000000..09d5391bd58d --- /dev/null +++ b/tests/ui/imports/private-import-nested-suggestion-156060.stderr @@ -0,0 +1,20 @@ +error[E0603]: struct import `One` is private + --> $DIR/private-import-nested-suggestion-156060.rs:13:22 + | +LL | use crate::two::{One, Two}; + | ^^^ private struct import + | +note: the struct import `One` is defined here... + --> $DIR/private-import-nested-suggestion-156060.rs:8:9 + | +LL | use crate::one::One; + | ^^^^^^^^^^^^^^^ +note: ...and refers to the struct `One` which is defined here + --> $DIR/private-import-nested-suggestion-156060.rs:4:5 + | +LL | pub struct One(); + | ^^^^^^^^^^^^^^^^^ you could import this directly + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0603`.