From bd7688c3f11b1418ff9f411abf6ed6e4d5011a8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 12 Oct 2026 16:08:56 +0000 Subject: [PATCH 1/3] Fix incorrect span to point at private item that should be marked `pub` ``` error[E0364]: `f` is private, and cannot be re-exported --> $DIR/privacy-early.rs:10:13 | LL | pub macro m() { LL | use f as g; | ^^^^^^ ... LL | foo::m!(); | --------- in this macro invocation | note: consider marking `f` as `pub` in the imported module --> $DIR/privacy-early.rs:6:5 | LL | fn f() {} | ^^^^^^^^^ ``` where it was ``` error[E0364]: `f` is private, and cannot be re-exported --> $DIR/privacy-early.rs:10:13 | LL | pub macro m() { LL | use f as g; | ^^^^^^ ... LL | foo::m!(); | --------- in this macro invocation | note: consider marking `f` as `pub` in the imported module --> $DIR/privacy-early.rs:10:13 | LL | use f as g; | ^^^^^^ ... LL | foo::m!(); | --------- in this macro invocation ``` --- compiler/rustc_resolve/src/imports.rs | 2 +- tests/ui/hygiene/privacy-early.stderr | 19 ++---- .../private-from-decl-macro.fail.stderr | 9 +-- tests/ui/imports/reexports.stderr | 6 +- .../ui/privacy/crate-private-reexport.stderr | 66 +++++++++---------- ...46209-private-enum-variant-reexport.stderr | 12 ++-- .../ui/privacy/macro-private-reexport.stderr | 6 +- .../privacy/private-variant-reexport.stderr | 12 ++-- tests/ui/privacy/restricted/test.stderr | 6 +- tests/ui/resolve/decl-macro-use-no-ice.stderr | 9 +-- 10 files changed, 67 insertions(+), 80 deletions(-) diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 9afbb425eeb9d..47d3f6b1804de 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -1708,7 +1708,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { err.subdiagnostic(ConsiderMarkingAsPubCrate { vis_span: import.vis_span }); } _ => { - err.subdiagnostic(ConsiderMarkingAsPub { span: import.span, ident }); + err.subdiagnostic(ConsiderMarkingAsPub { span: decl.span, ident }); } } err.emit(); diff --git a/tests/ui/hygiene/privacy-early.stderr b/tests/ui/hygiene/privacy-early.stderr index 22a8db986ad3d..5faa3181f7dc8 100644 --- a/tests/ui/hygiene/privacy-early.stderr +++ b/tests/ui/hygiene/privacy-early.stderr @@ -9,13 +9,10 @@ LL | foo::m!(); | --------- in this macro invocation | note: consider marking `f` as `pub` in the imported module - --> $DIR/privacy-early.rs:10:13 + --> $DIR/privacy-early.rs:6:5 | -LL | use f as g; - | ^^^^^^ -... -LL | foo::m!(); - | --------- in this macro invocation +LL | fn f() {} + | ^^^^^^^^^ error[E0364]: `f` is private, and cannot be re-exported --> $DIR/privacy-early.rs:10:13 @@ -28,14 +25,10 @@ LL | foo::m!(); | --------- in this macro invocation | note: consider marking `f` as `pub` in the imported module - --> $DIR/privacy-early.rs:10:13 + --> $DIR/privacy-early.rs:7:5 | -LL | use f as g; - | ^^^^^^ -... -LL | foo::m!(); - | --------- in this macro invocation - = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` +LL | macro f() {} + | ^^^^^^^^^^^^ error: aborting due to 2 previous errors diff --git a/tests/ui/imports/private-from-decl-macro.fail.stderr b/tests/ui/imports/private-from-decl-macro.fail.stderr index 80b6fde5e8539..a6fac6b33e3d8 100644 --- a/tests/ui/imports/private-from-decl-macro.fail.stderr +++ b/tests/ui/imports/private-from-decl-macro.fail.stderr @@ -10,13 +10,10 @@ LL | crate::m::mac_glob!(); | --------------------- in this macro invocation | note: consider marking `S` as `pub` in the imported module - --> $DIR/private-from-decl-macro.rs:18:13 + --> $DIR/private-from-decl-macro.rs:10:5 | -LL | use crate::m::*; - | ^^^^^^^^^^^ -... -LL | crate::m::mac_glob!(); - | --------------------- in this macro invocation +LL | const S: u8 = 0; + | ^^^^^^^^^^^^^^^^ error[E0423]: cannot find value `S` in this scope --> $DIR/private-from-decl-macro.rs:28:17 diff --git a/tests/ui/imports/reexports.stderr b/tests/ui/imports/reexports.stderr index cb3836305c1ab..e85d938be2ff5 100644 --- a/tests/ui/imports/reexports.stderr +++ b/tests/ui/imports/reexports.stderr @@ -5,10 +5,10 @@ LL | pub use super::foo; | ^^^^^^^^^^ | note: consider marking `foo` as `pub` in the imported module - --> $DIR/reexports.rs:8:17 + --> $DIR/reexports.rs:4:5 | -LL | pub use super::foo; - | ^^^^^^^^^^ +LL | fn foo() {} + | ^^^^^^^^^^^ error[E0603]: module import `foo` is private --> $DIR/reexports.rs:36:22 diff --git a/tests/ui/privacy/crate-private-reexport.stderr b/tests/ui/privacy/crate-private-reexport.stderr index 9b2626efacd2d..39814835f6ff4 100644 --- a/tests/ui/privacy/crate-private-reexport.stderr +++ b/tests/ui/privacy/crate-private-reexport.stderr @@ -5,10 +5,10 @@ LL | pub use crate::f1; | ^^^^^^^^^ | note: consider marking `f1` as `pub` in the imported module - --> $DIR/crate-private-reexport.rs:8:13 + --> $DIR/crate-private-reexport.rs:1:1 | -LL | pub use crate::f1; - | ^^^^^^^^^ +LL | fn f1() {} + | ^^^^^^^^^^ error[E0365]: `S1` is only public within the crate, and cannot be re-exported outside --> $DIR/crate-private-reexport.rs:9:13 @@ -33,10 +33,10 @@ LL | pub use crate::E1::V; | ^^^^^^^^^^^^ | note: consider marking `V` as `pub` in the imported module - --> $DIR/crate-private-reexport.rs:11:13 + --> $DIR/crate-private-reexport.rs:2:11 | -LL | pub use crate::E1::V; - | ^^^^^^^^^^^^ +LL | enum E1 { V } + | ^ error[E0364]: `f2` is only public within the crate, and cannot be re-exported outside --> $DIR/crate-private-reexport.rs:23:13 @@ -45,10 +45,10 @@ LL | pub use crate::f2; | ^^^^^^^^^ | note: consider marking `f2` as `pub` in the imported module - --> $DIR/crate-private-reexport.rs:23:13 + --> $DIR/crate-private-reexport.rs:14:1 | -LL | pub use crate::f2; - | ^^^^^^^^^ +LL | pub(crate) fn f2() {} + | ^^^^^^^^^^^^^^^^^^^^^ error[E0365]: `S2` is only public within the crate, and cannot be re-exported outside --> $DIR/crate-private-reexport.rs:24:13 @@ -73,10 +73,10 @@ LL | pub use crate::E2::V; | ^^^^^^^^^^^^ | note: consider marking `V` as `pub` in the imported module - --> $DIR/crate-private-reexport.rs:26:13 + --> $DIR/crate-private-reexport.rs:16:5 | -LL | pub use crate::E2::V; - | ^^^^^^^^^^^^ +LL | V + | ^ error[E0364]: `f3` is only public within the crate, and cannot be re-exported outside --> $DIR/crate-private-reexport.rs:39:9 @@ -85,10 +85,10 @@ LL | pub use m3::f3; | ^^^^^^ | note: consider marking `f3` as `pub` in the imported module - --> $DIR/crate-private-reexport.rs:39:9 + --> $DIR/crate-private-reexport.rs:30:5 | -LL | pub use m3::f3; - | ^^^^^^ +LL | pub(crate) fn f3() {} + | ^^^^^^^^^^^^^^^^^^^^^ error[E0365]: `S3` is only public within the crate, and cannot be re-exported outside --> $DIR/crate-private-reexport.rs:40:9 @@ -113,10 +113,10 @@ LL | pub use m3::E3::V; | ^^^^^^^^^ | note: consider marking `V` as `pub` in the imported module - --> $DIR/crate-private-reexport.rs:42:9 + --> $DIR/crate-private-reexport.rs:32:9 | -LL | pub use m3::E3::V; - | ^^^^^^^^^ +LL | V + | ^ error[E0364]: `f4` is only public within the crate, and cannot be re-exported outside --> $DIR/crate-private-reexport.rs:45:9 @@ -125,10 +125,10 @@ LL | pub use crate::f4 as f5; | ^^^^^^^^^^^^^^^ | note: consider marking `f4` as `pub` in the imported module - --> $DIR/crate-private-reexport.rs:45:9 + --> $DIR/crate-private-reexport.rs:44:1 | -LL | pub use crate::f4 as f5; - | ^^^^^^^^^^^^^^^ +LL | pub(self) fn f4() {} + | ^^^^^^^^^^^^^^^^^^^^ error[E0364]: `f6` is private, and cannot be re-exported --> $DIR/crate-private-reexport.rs:53:13 @@ -137,10 +137,10 @@ LL | pub use self::m::f6; | ^^^^^^^^^^^ | note: consider marking `f6` as `pub` in the imported module - --> $DIR/crate-private-reexport.rs:53:13 + --> $DIR/crate-private-reexport.rs:49:9 | -LL | pub use self::m::f6; - | ^^^^^^^^^^^ +LL | pub(super) fn f6() {} + | ^^^^^^^^^^^^^^^^^^^^^ error[E0364]: `f7` is only public within the crate, and cannot be re-exported outside --> $DIR/crate-private-reexport.rs:54:13 @@ -149,10 +149,10 @@ LL | pub use self::m::f7; | ^^^^^^^^^^^ | note: consider marking `f7` as `pub` in the imported module - --> $DIR/crate-private-reexport.rs:54:13 + --> $DIR/crate-private-reexport.rs:50:9 | -LL | pub use self::m::f7; - | ^^^^^^^^^^^ +LL | pub(crate) fn f7() {} + | ^^^^^^^^^^^^^^^^^^^^^ error[E0364]: `f8` is private, and cannot be re-exported --> $DIR/crate-private-reexport.rs:55:13 @@ -161,10 +161,10 @@ LL | pub use self::m::f8; | ^^^^^^^^^^^ | note: consider marking `f8` as `pub` in the imported module - --> $DIR/crate-private-reexport.rs:55:13 + --> $DIR/crate-private-reexport.rs:51:9 | -LL | pub use self::m::f8; - | ^^^^^^^^^^^ +LL | pub(in crate::m10) fn f8() {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0364]: `f7` is only public within the crate, and cannot be re-exported outside --> $DIR/crate-private-reexport.rs:58:9 @@ -173,10 +173,10 @@ LL | pub use m10::m::f7; | ^^^^^^^^^^ | note: consider marking `f7` as `pub` in the imported module - --> $DIR/crate-private-reexport.rs:58:9 + --> $DIR/crate-private-reexport.rs:50:9 | -LL | pub use m10::m::f7; - | ^^^^^^^^^^ +LL | pub(crate) fn f7() {} + | ^^^^^^^^^^^^^^^^^^^^^ error[E0603]: function `f6` is private --> $DIR/crate-private-reexport.rs:57:17 diff --git a/tests/ui/privacy/issue-46209-private-enum-variant-reexport.stderr b/tests/ui/privacy/issue-46209-private-enum-variant-reexport.stderr index f1701d547a62f..0adf20c8f9fab 100644 --- a/tests/ui/privacy/issue-46209-private-enum-variant-reexport.stderr +++ b/tests/ui/privacy/issue-46209-private-enum-variant-reexport.stderr @@ -5,10 +5,10 @@ LL | pub use self::Lieutenant::{JuniorGrade, Full}; | ^^^^^^^^^^^ | note: consider marking `JuniorGrade` as `pub` in the imported module - --> $DIR/issue-46209-private-enum-variant-reexport.rs:6:32 + --> $DIR/issue-46209-private-enum-variant-reexport.rs:25:9 | -LL | pub use self::Lieutenant::{JuniorGrade, Full}; - | ^^^^^^^^^^^ +LL | JuniorGrade, + | ^^^^^^^^^^^ error[E0364]: `Full` is private, and cannot be re-exported --> $DIR/issue-46209-private-enum-variant-reexport.rs:6:45 @@ -17,10 +17,10 @@ LL | pub use self::Lieutenant::{JuniorGrade, Full}; | ^^^^ | note: consider marking `Full` as `pub` in the imported module - --> $DIR/issue-46209-private-enum-variant-reexport.rs:6:45 + --> $DIR/issue-46209-private-enum-variant-reexport.rs:26:9 | -LL | pub use self::Lieutenant::{JuniorGrade, Full}; - | ^^^^ +LL | Full, + | ^^^^ error: glob import doesn't reexport anything with visibility `pub` because no imported item is public enough --> $DIR/issue-46209-private-enum-variant-reexport.rs:3:13 diff --git a/tests/ui/privacy/macro-private-reexport.stderr b/tests/ui/privacy/macro-private-reexport.stderr index aa02715c20298..61af022b901e5 100644 --- a/tests/ui/privacy/macro-private-reexport.stderr +++ b/tests/ui/privacy/macro-private-reexport.stderr @@ -23,10 +23,10 @@ LL | pub use baz as _; | ^^^^^^^^ | note: consider marking `baz` as `pub` in the imported module - --> $DIR/macro-private-reexport.rs:14:13 + --> $DIR/macro-private-reexport.rs:12:5 | -LL | pub use baz as _; - | ^^^^^^^^ +LL | macro baz() {} + | ^^^^^^^^^^^^^^ error: aborting due to 2 previous errors diff --git a/tests/ui/privacy/private-variant-reexport.stderr b/tests/ui/privacy/private-variant-reexport.stderr index aaa42d8d30dc5..efc6d5c3ae0ff 100644 --- a/tests/ui/privacy/private-variant-reexport.stderr +++ b/tests/ui/privacy/private-variant-reexport.stderr @@ -5,10 +5,10 @@ LL | pub use ::E::V; | ^^^^^^ | note: consider marking `V` as `pub` in the imported module - --> $DIR/private-variant-reexport.rs:3:13 + --> $DIR/private-variant-reexport.rs:21:10 | -LL | pub use ::E::V; - | ^^^^^^ +LL | enum E { V } + | ^ error[E0364]: `V` is only public within the crate, and cannot be re-exported outside --> $DIR/private-variant-reexport.rs:7:19 @@ -17,10 +17,10 @@ LL | pub use ::E::{V}; | ^ | note: consider marking `V` as `pub` in the imported module - --> $DIR/private-variant-reexport.rs:7:19 + --> $DIR/private-variant-reexport.rs:21:10 | -LL | pub use ::E::{V}; - | ^ +LL | enum E { V } + | ^ error[E0432]: unresolved import `E::V` --> $DIR/private-variant-reexport.rs:11:18 diff --git a/tests/ui/privacy/restricted/test.stderr b/tests/ui/privacy/restricted/test.stderr index f380da1ca344e..5aebd55825eba 100644 --- a/tests/ui/privacy/restricted/test.stderr +++ b/tests/ui/privacy/restricted/test.stderr @@ -5,10 +5,10 @@ LL | pub(super) use foo::bar::f as g; | ^^^^^^^^^^^^^^^^ | note: consider marking `f` as `pub` in the imported module - --> $DIR/test.rs:22:24 + --> $DIR/test.rs:9:9 | -LL | pub(super) use foo::bar::f as g; - | ^^^^^^^^^^^^^^^^ +LL | pub(super) fn f() {} + | ^^^^^^^^^^^^^^^^^^^^ error[E0433]: cannot find module or crate `bad` in the crate root --> $DIR/test.rs:51:12 diff --git a/tests/ui/resolve/decl-macro-use-no-ice.stderr b/tests/ui/resolve/decl-macro-use-no-ice.stderr index 1f47f757f6153..35074c292ba08 100644 --- a/tests/ui/resolve/decl-macro-use-no-ice.stderr +++ b/tests/ui/resolve/decl-macro-use-no-ice.stderr @@ -9,13 +9,10 @@ LL | foo::m!(); | --------- in this macro invocation | note: consider marking `f` as `pub` in the imported module - --> $DIR/decl-macro-use-no-ice.rs:13:13 + --> $DIR/decl-macro-use-no-ice.rs:10:5 | -LL | use f; - | ^ -... -LL | foo::m!(); - | --------- in this macro invocation +LL | macro f() {} + | ^^^^^^^^^^^^ error: aborting due to 1 previous error From 43c52bd3afaf4b2c8480b5388007a4679e5cd525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 12 Oct 2026 17:15:20 +0000 Subject: [PATCH 2/3] Provide more context for private re-exported items Use span when suggesting making module public: ``` note: consider declaring module `foo` with `pub` --> $DIR/E0365.rs:1:1 | LL | / mod foo { LL | | pub const X: u32 = 1; LL | | } | |_^ ``` Show container when talking about enclosing module: ``` note: consider marking `f` as `pub` in the imported module --> $DIR/privacy-early.rs:6:5 | LL | mod foo { LL | fn f() {} | ^^^^^^^^^ ``` Use accurate descriptor for enclosing item: ``` note: consider declaring struct `S1` with `pub` --> $DIR/crate-private-reexport.rs:3:1 | LL | / struct S1 { LL | | #[rustfmt::skip] LL | | bar: i32, LL | | } | |_^ ``` ``` note: consider marking `Full` as `pub` in the imported variant --> $DIR/issue-46209-private-enum-variant-reexport.rs:26:9 | LL | enum Lieutenant { LL | JuniorGrade, LL | Full, | ^^^^ ``` --- compiler/rustc_resolve/src/diagnostics/mod.rs | 13 ++-- compiler/rustc_resolve/src/imports.rs | 46 +++++++++++-- tests/ui/error-codes/E0365.stderr | 8 ++- tests/ui/hygiene/privacy-early.stderr | 3 + .../private-from-decl-macro.fail.stderr | 2 + tests/ui/imports/reexports.stderr | 1 + tests/ui/modules/issue-56411.stderr | 9 ++- .../ui/privacy/crate-private-reexport.stderr | 65 ++++++++++++++++--- ...46209-private-enum-variant-reexport.stderr | 7 +- .../ui/privacy/macro-private-reexport.stderr | 2 + .../privacy/private-variant-reexport.stderr | 4 +- tests/ui/privacy/restricted/test.stderr | 1 + tests/ui/proc-macro/issue-79148.stderr | 2 +- tests/ui/resolve/decl-macro-use-no-ice.stderr | 1 + tests/ui/use/pub-use-self-super-crate.stderr | 45 +++++++++++-- 15 files changed, 178 insertions(+), 31 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics/mod.rs b/compiler/rustc_resolve/src/diagnostics/mod.rs index e33255eec46d8..4a54269a30137 100644 --- a/compiler/rustc_resolve/src/diagnostics/mod.rs +++ b/compiler/rustc_resolve/src/diagnostics/mod.rs @@ -831,22 +831,26 @@ pub(crate) struct CannotBeReexportedCratePublic { #[derive(Diagnostic)] #[diag("`{$ident}` is private, and cannot be re-exported", code = E0365)] -#[note("consider declaring type or module `{$ident}` with `pub`")] pub(crate) struct CannotBeReexportedPrivateNS { #[primary_span] #[label("re-export of private `{$ident}`")] pub(crate) span: Span, pub(crate) ident: Ident, + #[note("consider declaring {$descr} `{$ident}` with `pub`")] + pub(crate) parent: Span, + pub(crate) descr: &'static str, } #[derive(Diagnostic)] #[diag("`{$ident}` is only public within the crate, and cannot be re-exported outside", code = E0365)] -#[note("consider declaring type or module `{$ident}` with `pub`")] pub(crate) struct CannotBeReexportedCratePublicNS { #[primary_span] #[label("re-export of crate public `{$ident}`")] pub(crate) span: Span, pub(crate) ident: Ident, + #[note("consider declaring {$descr} `{$ident}` with `pub`")] + pub(crate) parent: Span, + pub(crate) descr: &'static str, } #[derive(Diagnostic)] @@ -881,11 +885,12 @@ pub(crate) struct ConsiderMarkingAsPubCrate { } #[derive(Subdiagnostic)] -#[note("consider marking `{$ident}` as `pub` in the imported module")] +#[note("consider marking `{$ident}` as `pub` in the imported {$descr}")] pub(crate) struct ConsiderMarkingAsPub { #[primary_span] - pub(crate) span: Span, + pub(crate) span: MultiSpan, pub(crate) ident: Ident, + pub(crate) descr: &'static str, } #[derive(Diagnostic)] diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 47d3f6b1804de..0205ad616cb08 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -6,10 +6,10 @@ use std::mem; use rustc_ast::NodeId; use rustc_data_structures::fx::{FxHashSet, FxIndexSet}; use rustc_data_structures::intern::Interned; -use rustc_errors::{Applicability, BufferedEarlyLint, Diagnostic}; +use rustc_errors::{Applicability, BufferedEarlyLint, Diagnostic, MultiSpan}; use rustc_expand::base::SyntaxExtensionKind; use rustc_hir::def::{self, DefKind}; -use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdMap}; +use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId, LocalDefIdMap}; use rustc_lint_defs::LintId; use rustc_lint_defs::builtin::{ AMBIGUOUS_GLOB_REEXPORTS, EXPORTED_PRIVATE_DEPENDENCIES, HIDDEN_GLOB_REEXPORTS, @@ -20,7 +20,7 @@ use rustc_middle::ty::Visibility; use rustc_session::diagnostics::feature_err; use rustc_span::edit_distance::find_best_match_for_name; use rustc_span::hygiene::LocalExpnId; -use rustc_span::{Ident, OrdSpan, Span, Symbol, kw, span_bug, sym}; +use rustc_span::{DUMMY_SP, Ident, OrdSpan, Span, Symbol, kw, span_bug, sym}; use tracing::debug; use crate::Namespace::{self, *}; @@ -1685,10 +1685,24 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { diagnostic: diagnostic.into(), }); } else if ns == TypeNS { + let def_id = decl.res().def_id(); + let parent = if !def_id.is_crate_root() { self.def_span(def_id) } else { DUMMY_SP }; + let descr = self.tcx().def_descr(def_id); + if crate_private_reexport { - self.dcx().emit_err(CannotBeReexportedCratePublicNS { span: import.span, ident }); + self.dcx().emit_err(CannotBeReexportedCratePublicNS { + span: import.span, + ident, + parent, + descr, + }); } else { - self.dcx().emit_err(CannotBeReexportedPrivateNS { span: import.span, ident }); + self.dcx().emit_err(CannotBeReexportedPrivateNS { + span: import.span, + ident, + parent, + descr, + }); } } else { let mut err = if crate_private_reexport { @@ -1708,7 +1722,27 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { err.subdiagnostic(ConsiderMarkingAsPubCrate { vis_span: import.vis_span }); } _ => { - err.subdiagnostic(ConsiderMarkingAsPub { span: decl.span, ident }); + let mut span: MultiSpan = decl.span.into(); + let descr = if decl.is_import() { + "re-export" + } else if let Some(def_id) = decl.res().opt_def_id() { + let parent = self.tcx().parent(def_id); + + if let DefKind::Variant = self.tcx().def_kind(parent) { + // We had the DefId for the variant's constructor, but we want to point at the enum. + span.push_span_context( + self.def_span(self.tcx().parent(parent)).shrink_to_lo(), + ); + } else if CRATE_DEF_ID.to_def_id() != parent { + span.push_span_context(self.def_span(parent).shrink_to_lo()); + } + self.tcx().def_descr(parent) + } else { + // unreachable, but provide fallback just in case + debug_assert!(false, "we have a DefId in {decl:?} with no parent?"); + "module" + }; + err.subdiagnostic(ConsiderMarkingAsPub { span, ident, descr }); } } err.emit(); diff --git a/tests/ui/error-codes/E0365.stderr b/tests/ui/error-codes/E0365.stderr index d6eb9d6c38339..4199459e12d1a 100644 --- a/tests/ui/error-codes/E0365.stderr +++ b/tests/ui/error-codes/E0365.stderr @@ -4,7 +4,13 @@ error[E0365]: `foo` is only public within the crate, and cannot be re-exported o LL | pub use foo as foo2; | ^^^^^^^^^^^ re-export of crate public `foo` | - = note: consider declaring type or module `foo` with `pub` +note: consider declaring module `foo` with `pub` + --> $DIR/E0365.rs:1:1 + | +LL | / mod foo { +LL | | pub const X: u32 = 1; +LL | | } + | |_^ error: aborting due to 1 previous error diff --git a/tests/ui/hygiene/privacy-early.stderr b/tests/ui/hygiene/privacy-early.stderr index 5faa3181f7dc8..2c1ac643b9a33 100644 --- a/tests/ui/hygiene/privacy-early.stderr +++ b/tests/ui/hygiene/privacy-early.stderr @@ -11,6 +11,7 @@ LL | foo::m!(); note: consider marking `f` as `pub` in the imported module --> $DIR/privacy-early.rs:6:5 | +LL | mod foo { LL | fn f() {} | ^^^^^^^^^ @@ -27,6 +28,8 @@ LL | foo::m!(); note: consider marking `f` as `pub` in the imported module --> $DIR/privacy-early.rs:7:5 | +LL | mod foo { +LL | fn f() {} LL | macro f() {} | ^^^^^^^^^^^^ diff --git a/tests/ui/imports/private-from-decl-macro.fail.stderr b/tests/ui/imports/private-from-decl-macro.fail.stderr index a6fac6b33e3d8..9468c3535f7f1 100644 --- a/tests/ui/imports/private-from-decl-macro.fail.stderr +++ b/tests/ui/imports/private-from-decl-macro.fail.stderr @@ -12,6 +12,8 @@ LL | crate::m::mac_glob!(); note: consider marking `S` as `pub` in the imported module --> $DIR/private-from-decl-macro.rs:10:5 | +LL | mod m { +... LL | const S: u8 = 0; | ^^^^^^^^^^^^^^^^ diff --git a/tests/ui/imports/reexports.stderr b/tests/ui/imports/reexports.stderr index e85d938be2ff5..18e7739622219 100644 --- a/tests/ui/imports/reexports.stderr +++ b/tests/ui/imports/reexports.stderr @@ -7,6 +7,7 @@ LL | pub use super::foo; note: consider marking `foo` as `pub` in the imported module --> $DIR/reexports.rs:4:5 | +LL | mod a { LL | fn foo() {} | ^^^^^^^^^^^ diff --git a/tests/ui/modules/issue-56411.stderr b/tests/ui/modules/issue-56411.stderr index 6e8912146883d..06f603bc55921 100644 --- a/tests/ui/modules/issue-56411.stderr +++ b/tests/ui/modules/issue-56411.stderr @@ -27,7 +27,14 @@ LL | pub use self::$name; LL | import!(("issue-56411-aux.rs", issue_56411_aux)); | ------------------------------------------------ in this macro invocation | - = note: consider declaring type or module `issue_56411_aux` with `pub` +note: consider declaring module `issue_56411_aux` with `pub` + --> $DIR/issue-56411.rs:5:13 + | +LL | mod $name; + | ^^^^^^^^^^ +... +LL | import!(("issue-56411-aux.rs", issue_56411_aux)); + | ------------------------------------------------ in this macro invocation error: aborting due to 2 previous errors diff --git a/tests/ui/privacy/crate-private-reexport.stderr b/tests/ui/privacy/crate-private-reexport.stderr index 39814835f6ff4..5962db9783ea1 100644 --- a/tests/ui/privacy/crate-private-reexport.stderr +++ b/tests/ui/privacy/crate-private-reexport.stderr @@ -16,7 +16,14 @@ error[E0365]: `S1` is only public within the crate, and cannot be re-exported ou LL | pub use crate::S1; | ^^^^^^^^^ re-export of crate public `S1` | - = note: consider declaring type or module `S1` with `pub` +note: consider declaring struct `S1` with `pub` + --> $DIR/crate-private-reexport.rs:3:1 + | +LL | / struct S1 { +LL | | #[rustfmt::skip] +LL | | bar: i32, +LL | | } + | |_^ error[E0365]: `E1` is only public within the crate, and cannot be re-exported outside --> $DIR/crate-private-reexport.rs:10:13 @@ -24,7 +31,11 @@ error[E0365]: `E1` is only public within the crate, and cannot be re-exported ou LL | pub use crate::E1; | ^^^^^^^^^ re-export of crate public `E1` | - = note: consider declaring type or module `E1` with `pub` +note: consider declaring enum `E1` with `pub` + --> $DIR/crate-private-reexport.rs:2:1 + | +LL | enum E1 { V } + | ^^^^^^^^^^^^^ error[E0364]: `V` is only public within the crate, and cannot be re-exported outside --> $DIR/crate-private-reexport.rs:11:13 @@ -32,7 +43,7 @@ error[E0364]: `V` is only public within the crate, and cannot be re-exported out LL | pub use crate::E1::V; | ^^^^^^^^^^^^ | -note: consider marking `V` as `pub` in the imported module +note: consider marking `V` as `pub` in the imported variant --> $DIR/crate-private-reexport.rs:2:11 | LL | enum E1 { V } @@ -56,7 +67,14 @@ error[E0365]: `S2` is only public within the crate, and cannot be re-exported ou LL | pub use crate::S2; | ^^^^^^^^^ re-export of crate public `S2` | - = note: consider declaring type or module `S2` with `pub` +note: consider declaring struct `S2` with `pub` + --> $DIR/crate-private-reexport.rs:18:1 + | +LL | / pub(crate) struct S2 { +LL | | #[rustfmt::skip] +LL | | bar: i32, +LL | | } + | |_^ error[E0365]: `E2` is only public within the crate, and cannot be re-exported outside --> $DIR/crate-private-reexport.rs:25:13 @@ -64,7 +82,13 @@ error[E0365]: `E2` is only public within the crate, and cannot be re-exported ou LL | pub use crate::E2; | ^^^^^^^^^ re-export of crate public `E2` | - = note: consider declaring type or module `E2` with `pub` +note: consider declaring enum `E2` with `pub` + --> $DIR/crate-private-reexport.rs:15:1 + | +LL | / pub(crate) enum E2 { +LL | | V +LL | | } + | |_^ error[E0364]: `V` is only public within the crate, and cannot be re-exported outside --> $DIR/crate-private-reexport.rs:26:13 @@ -72,9 +96,10 @@ error[E0364]: `V` is only public within the crate, and cannot be re-exported out LL | pub use crate::E2::V; | ^^^^^^^^^^^^ | -note: consider marking `V` as `pub` in the imported module +note: consider marking `V` as `pub` in the imported variant --> $DIR/crate-private-reexport.rs:16:5 | +LL | pub(crate) enum E2 { LL | V | ^ @@ -87,6 +112,7 @@ LL | pub use m3::f3; note: consider marking `f3` as `pub` in the imported module --> $DIR/crate-private-reexport.rs:30:5 | +LL | mod m3 { LL | pub(crate) fn f3() {} | ^^^^^^^^^^^^^^^^^^^^^ @@ -96,7 +122,14 @@ error[E0365]: `S3` is only public within the crate, and cannot be re-exported ou LL | pub use m3::S3; | ^^^^^^ re-export of crate public `S3` | - = note: consider declaring type or module `S3` with `pub` +note: consider declaring struct `S3` with `pub` + --> $DIR/crate-private-reexport.rs:34:5 + | +LL | / pub(crate) struct S3 { +LL | | #[rustfmt::skip] +LL | | bar: i32, +LL | | } + | |_____^ error[E0365]: `E3` is only public within the crate, and cannot be re-exported outside --> $DIR/crate-private-reexport.rs:41:9 @@ -104,7 +137,13 @@ error[E0365]: `E3` is only public within the crate, and cannot be re-exported ou LL | pub use m3::E3; | ^^^^^^ re-export of crate public `E3` | - = note: consider declaring type or module `E3` with `pub` +note: consider declaring enum `E3` with `pub` + --> $DIR/crate-private-reexport.rs:31:5 + | +LL | / pub(crate) enum E3 { +LL | | V +LL | | } + | |_____^ error[E0364]: `V` is only public within the crate, and cannot be re-exported outside --> $DIR/crate-private-reexport.rs:42:9 @@ -112,9 +151,10 @@ error[E0364]: `V` is only public within the crate, and cannot be re-exported out LL | pub use m3::E3::V; | ^^^^^^^^^ | -note: consider marking `V` as `pub` in the imported module +note: consider marking `V` as `pub` in the imported variant --> $DIR/crate-private-reexport.rs:32:9 | +LL | pub(crate) enum E3 { LL | V | ^ @@ -139,6 +179,7 @@ LL | pub use self::m::f6; note: consider marking `f6` as `pub` in the imported module --> $DIR/crate-private-reexport.rs:49:9 | +LL | pub mod m { LL | pub(super) fn f6() {} | ^^^^^^^^^^^^^^^^^^^^^ @@ -151,6 +192,8 @@ LL | pub use self::m::f7; note: consider marking `f7` as `pub` in the imported module --> $DIR/crate-private-reexport.rs:50:9 | +LL | pub mod m { +LL | pub(super) fn f6() {} LL | pub(crate) fn f7() {} | ^^^^^^^^^^^^^^^^^^^^^ @@ -163,6 +206,8 @@ LL | pub use self::m::f8; note: consider marking `f8` as `pub` in the imported module --> $DIR/crate-private-reexport.rs:51:9 | +LL | pub mod m { +... LL | pub(in crate::m10) fn f8() {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -175,6 +220,8 @@ LL | pub use m10::m::f7; note: consider marking `f7` as `pub` in the imported module --> $DIR/crate-private-reexport.rs:50:9 | +LL | pub mod m { +LL | pub(super) fn f6() {} LL | pub(crate) fn f7() {} | ^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/privacy/issue-46209-private-enum-variant-reexport.stderr b/tests/ui/privacy/issue-46209-private-enum-variant-reexport.stderr index 0adf20c8f9fab..74430f04817ca 100644 --- a/tests/ui/privacy/issue-46209-private-enum-variant-reexport.stderr +++ b/tests/ui/privacy/issue-46209-private-enum-variant-reexport.stderr @@ -4,9 +4,10 @@ error[E0364]: `JuniorGrade` is private, and cannot be re-exported LL | pub use self::Lieutenant::{JuniorGrade, Full}; | ^^^^^^^^^^^ | -note: consider marking `JuniorGrade` as `pub` in the imported module +note: consider marking `JuniorGrade` as `pub` in the imported variant --> $DIR/issue-46209-private-enum-variant-reexport.rs:25:9 | +LL | enum Lieutenant { LL | JuniorGrade, | ^^^^^^^^^^^ @@ -16,9 +17,11 @@ error[E0364]: `Full` is private, and cannot be re-exported LL | pub use self::Lieutenant::{JuniorGrade, Full}; | ^^^^ | -note: consider marking `Full` as `pub` in the imported module +note: consider marking `Full` as `pub` in the imported variant --> $DIR/issue-46209-private-enum-variant-reexport.rs:26:9 | +LL | enum Lieutenant { +LL | JuniorGrade, LL | Full, | ^^^^ diff --git a/tests/ui/privacy/macro-private-reexport.stderr b/tests/ui/privacy/macro-private-reexport.stderr index 61af022b901e5..33c68ce013d62 100644 --- a/tests/ui/privacy/macro-private-reexport.stderr +++ b/tests/ui/privacy/macro-private-reexport.stderr @@ -25,6 +25,8 @@ LL | pub use baz as _; note: consider marking `baz` as `pub` in the imported module --> $DIR/macro-private-reexport.rs:12:5 | +LL | mod foo { +... LL | macro baz() {} | ^^^^^^^^^^^^^^ diff --git a/tests/ui/privacy/private-variant-reexport.stderr b/tests/ui/privacy/private-variant-reexport.stderr index efc6d5c3ae0ff..4d76bbad6c3c5 100644 --- a/tests/ui/privacy/private-variant-reexport.stderr +++ b/tests/ui/privacy/private-variant-reexport.stderr @@ -4,7 +4,7 @@ error[E0364]: `V` is only public within the crate, and cannot be re-exported out LL | pub use ::E::V; | ^^^^^^ | -note: consider marking `V` as `pub` in the imported module +note: consider marking `V` as `pub` in the imported variant --> $DIR/private-variant-reexport.rs:21:10 | LL | enum E { V } @@ -16,7 +16,7 @@ error[E0364]: `V` is only public within the crate, and cannot be re-exported out LL | pub use ::E::{V}; | ^ | -note: consider marking `V` as `pub` in the imported module +note: consider marking `V` as `pub` in the imported variant --> $DIR/private-variant-reexport.rs:21:10 | LL | enum E { V } diff --git a/tests/ui/privacy/restricted/test.stderr b/tests/ui/privacy/restricted/test.stderr index 5aebd55825eba..529bc9cf96766 100644 --- a/tests/ui/privacy/restricted/test.stderr +++ b/tests/ui/privacy/restricted/test.stderr @@ -7,6 +7,7 @@ LL | pub(super) use foo::bar::f as g; note: consider marking `f` as `pub` in the imported module --> $DIR/test.rs:9:9 | +LL | pub mod bar { LL | pub(super) fn f() {} | ^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/proc-macro/issue-79148.stderr b/tests/ui/proc-macro/issue-79148.stderr index 8adc4c6e0dbe4..bcb7f7b2fe4b1 100644 --- a/tests/ui/proc-macro/issue-79148.stderr +++ b/tests/ui/proc-macro/issue-79148.stderr @@ -4,7 +4,7 @@ error[E0364]: `Variant` is only public within the crate, and cannot be re-export LL | cause_ice!(); | ^^^^^^^^^^^^ | -note: consider marking `Variant` as `pub` in the imported module +note: consider marking `Variant` as `pub` in the imported variant --> $DIR/issue-79148.rs:8:1 | LL | cause_ice!(); diff --git a/tests/ui/resolve/decl-macro-use-no-ice.stderr b/tests/ui/resolve/decl-macro-use-no-ice.stderr index 35074c292ba08..073537d1cabbd 100644 --- a/tests/ui/resolve/decl-macro-use-no-ice.stderr +++ b/tests/ui/resolve/decl-macro-use-no-ice.stderr @@ -11,6 +11,7 @@ LL | foo::m!(); note: consider marking `f` as `pub` in the imported module --> $DIR/decl-macro-use-no-ice.rs:10:5 | +LL | mod foo { LL | macro f() {} | ^^^^^^^^^^^^ diff --git a/tests/ui/use/pub-use-self-super-crate.stderr b/tests/ui/use/pub-use-self-super-crate.stderr index 0f9c87b93a098..9d6df994a4d79 100644 --- a/tests/ui/use/pub-use-self-super-crate.stderr +++ b/tests/ui/use/pub-use-self-super-crate.stderr @@ -4,7 +4,14 @@ error[E0365]: `self` is only public within the crate, and cannot be re-exported LL | pub use self as this; | ^^^^^^^^^^^^ re-export of crate public `self` | - = note: consider declaring type or module `self` with `pub` +note: consider declaring module `self` with `pub` + --> $DIR/pub-use-self-super-crate.rs:1:1 + | +LL | / mod foo { +LL | | pub use self as this; +... | +LL | | } + | |_^ error[E0365]: `super` is only public within the crate, and cannot be re-exported outside --> $DIR/pub-use-self-super-crate.rs:6:17 @@ -12,7 +19,14 @@ error[E0365]: `super` is only public within the crate, and cannot be re-exported LL | pub use super as parent; | ^^^^^^^^^^^^^^^ re-export of crate public `super` | - = note: consider declaring type or module `super` with `pub` +note: consider declaring module `super` with `pub` + --> $DIR/pub-use-self-super-crate.rs:1:1 + | +LL | / mod foo { +LL | | pub use self as this; +... | +LL | | } + | |_^ error[E0365]: `super` is only public within the crate, and cannot be re-exported outside --> $DIR/pub-use-self-super-crate.rs:8:17 @@ -20,7 +34,14 @@ error[E0365]: `super` is only public within the crate, and cannot be re-exported LL | pub use self::super as parent2; | ^^^^^^^^^^^^^^^^^^^^^^ re-export of crate public `super` | - = note: consider declaring type or module `super` with `pub` +note: consider declaring module `super` with `pub` + --> $DIR/pub-use-self-super-crate.rs:1:1 + | +LL | / mod foo { +LL | | pub use self as this; +... | +LL | | } + | |_^ error[E0365]: `self` is only public within the crate, and cannot be re-exported outside --> $DIR/pub-use-self-super-crate.rs:10:25 @@ -28,7 +49,14 @@ error[E0365]: `self` is only public within the crate, and cannot be re-exported LL | pub use super::{self as parent3}; | ^^^^^^^^^^^^^^^ re-export of crate public `self` | - = note: consider declaring type or module `self` with `pub` +note: consider declaring module `self` with `pub` + --> $DIR/pub-use-self-super-crate.rs:1:1 + | +LL | / mod foo { +LL | | pub use self as this; +... | +LL | | } + | |_^ error[E0365]: `super` is only public within the crate, and cannot be re-exported outside --> $DIR/pub-use-self-super-crate.rs:12:24 @@ -36,7 +64,14 @@ error[E0365]: `super` is only public within the crate, and cannot be re-exported LL | pub use self::{super as parent4}; | ^^^^^^^^^^^^^^^^ re-export of crate public `super` | - = note: consider declaring type or module `super` with `pub` +note: consider declaring module `super` with `pub` + --> $DIR/pub-use-self-super-crate.rs:1:1 + | +LL | / mod foo { +LL | | pub use self as this; +... | +LL | | } + | |_^ error: aborting due to 5 previous errors From bef8da0b44c5cf6c0d7d5539600c371d1f5451e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 9 Nov 2026 20:21:57 +0000 Subject: [PATCH 3/3] Tweak wording when encountering private variants --- compiler/rustc_resolve/src/imports.rs | 5 ++++- tests/ui/privacy/crate-private-reexport.stderr | 6 +++--- .../issue-46209-private-enum-variant-reexport.stderr | 4 ++-- tests/ui/privacy/private-variant-reexport.stderr | 4 ++-- tests/ui/proc-macro/issue-79148.stderr | 2 +- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 0205ad616cb08..f7e86925800a3 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -1733,10 +1733,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { span.push_span_context( self.def_span(self.tcx().parent(parent)).shrink_to_lo(), ); + self.tcx().def_descr(self.tcx().parent(parent)) } else if CRATE_DEF_ID.to_def_id() != parent { span.push_span_context(self.def_span(parent).shrink_to_lo()); + self.tcx().def_descr(parent) + } else { + self.tcx().def_descr(parent) } - self.tcx().def_descr(parent) } else { // unreachable, but provide fallback just in case debug_assert!(false, "we have a DefId in {decl:?} with no parent?"); diff --git a/tests/ui/privacy/crate-private-reexport.stderr b/tests/ui/privacy/crate-private-reexport.stderr index 5962db9783ea1..c092120972589 100644 --- a/tests/ui/privacy/crate-private-reexport.stderr +++ b/tests/ui/privacy/crate-private-reexport.stderr @@ -43,7 +43,7 @@ error[E0364]: `V` is only public within the crate, and cannot be re-exported out LL | pub use crate::E1::V; | ^^^^^^^^^^^^ | -note: consider marking `V` as `pub` in the imported variant +note: consider marking `V` as `pub` in the imported enum --> $DIR/crate-private-reexport.rs:2:11 | LL | enum E1 { V } @@ -96,7 +96,7 @@ error[E0364]: `V` is only public within the crate, and cannot be re-exported out LL | pub use crate::E2::V; | ^^^^^^^^^^^^ | -note: consider marking `V` as `pub` in the imported variant +note: consider marking `V` as `pub` in the imported enum --> $DIR/crate-private-reexport.rs:16:5 | LL | pub(crate) enum E2 { @@ -151,7 +151,7 @@ error[E0364]: `V` is only public within the crate, and cannot be re-exported out LL | pub use m3::E3::V; | ^^^^^^^^^ | -note: consider marking `V` as `pub` in the imported variant +note: consider marking `V` as `pub` in the imported enum --> $DIR/crate-private-reexport.rs:32:9 | LL | pub(crate) enum E3 { diff --git a/tests/ui/privacy/issue-46209-private-enum-variant-reexport.stderr b/tests/ui/privacy/issue-46209-private-enum-variant-reexport.stderr index 74430f04817ca..ea748ae5f0f60 100644 --- a/tests/ui/privacy/issue-46209-private-enum-variant-reexport.stderr +++ b/tests/ui/privacy/issue-46209-private-enum-variant-reexport.stderr @@ -4,7 +4,7 @@ error[E0364]: `JuniorGrade` is private, and cannot be re-exported LL | pub use self::Lieutenant::{JuniorGrade, Full}; | ^^^^^^^^^^^ | -note: consider marking `JuniorGrade` as `pub` in the imported variant +note: consider marking `JuniorGrade` as `pub` in the imported enum --> $DIR/issue-46209-private-enum-variant-reexport.rs:25:9 | LL | enum Lieutenant { @@ -17,7 +17,7 @@ error[E0364]: `Full` is private, and cannot be re-exported LL | pub use self::Lieutenant::{JuniorGrade, Full}; | ^^^^ | -note: consider marking `Full` as `pub` in the imported variant +note: consider marking `Full` as `pub` in the imported enum --> $DIR/issue-46209-private-enum-variant-reexport.rs:26:9 | LL | enum Lieutenant { diff --git a/tests/ui/privacy/private-variant-reexport.stderr b/tests/ui/privacy/private-variant-reexport.stderr index 4d76bbad6c3c5..6dd94028a9167 100644 --- a/tests/ui/privacy/private-variant-reexport.stderr +++ b/tests/ui/privacy/private-variant-reexport.stderr @@ -4,7 +4,7 @@ error[E0364]: `V` is only public within the crate, and cannot be re-exported out LL | pub use ::E::V; | ^^^^^^ | -note: consider marking `V` as `pub` in the imported variant +note: consider marking `V` as `pub` in the imported enum --> $DIR/private-variant-reexport.rs:21:10 | LL | enum E { V } @@ -16,7 +16,7 @@ error[E0364]: `V` is only public within the crate, and cannot be re-exported out LL | pub use ::E::{V}; | ^ | -note: consider marking `V` as `pub` in the imported variant +note: consider marking `V` as `pub` in the imported enum --> $DIR/private-variant-reexport.rs:21:10 | LL | enum E { V } diff --git a/tests/ui/proc-macro/issue-79148.stderr b/tests/ui/proc-macro/issue-79148.stderr index bcb7f7b2fe4b1..008a1d16df992 100644 --- a/tests/ui/proc-macro/issue-79148.stderr +++ b/tests/ui/proc-macro/issue-79148.stderr @@ -4,7 +4,7 @@ error[E0364]: `Variant` is only public within the crate, and cannot be re-export LL | cause_ice!(); | ^^^^^^^^^^^^ | -note: consider marking `Variant` as `pub` in the imported variant +note: consider marking `Variant` as `pub` in the imported enum --> $DIR/issue-79148.rs:8:1 | LL | cause_ice!();