Skip to content

consider glob imports in cfg suggestion #141548

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,10 +1319,10 @@ impl InvocationCollectorNode for P<ast::Item> {

let mut idents = Vec::new();
collect_use_tree_leaves(ut, &mut idents);
return idents;
idents
} else {
self.kind.ident().into_iter().collect()
}

if let Some(ident) = self.kind.ident() { vec![ident] } else { vec![] }
}
}

Expand Down
50 changes: 48 additions & 2 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_ast::{
};
use rustc_ast_pretty::pprust;
use rustc_attr_data_structures::{self as attr, Stability};
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::unord::UnordSet;
use rustc_errors::codes::*;
use rustc_errors::{
Expand Down Expand Up @@ -2623,7 +2623,53 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
};

for &StrippedCfgItem { parent_module, ident, ref cfg } in symbols {
if parent_module != module || ident.name != *segment {
if ident.name != *segment {
continue;
}

fn comes_from_same_module_for_glob(
r: &Resolver<'_, '_>,
parent_module: DefId,
module: DefId,
visited: &mut FxHashMap<DefId, bool>,
) -> bool {
if let Some(&cached) = visited.get(&parent_module) {
// this branch is prevent from being called recursively infinity,
// because there has some cycles in globs imports,
// see more spec case at `tests/ui/cfg/diagnostics-reexport-2.rs#reexport32`
return cached;
}
visited.insert(parent_module, false);
let res = r.module_map.get(&parent_module).is_some_and(|m| {
for importer in m.glob_importers.borrow().iter() {
if let Some(next_parent_module) = importer.parent_scope.module.opt_def_id()
{
if next_parent_module == module
|| comes_from_same_module_for_glob(
r,
next_parent_module,
module,
visited,
)
{
return true;
}
}
}
false
});
visited.insert(parent_module, res);
res
}

let comes_from_same_module = parent_module == module
|| comes_from_same_module_for_glob(
self,
parent_module,
module,
&mut Default::default(),
);
if !comes_from_same_module {
continue;
}

Expand Down
61 changes: 61 additions & 0 deletions tests/ui/cfg/diagnostics-reexport-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// issue#141256

mod original {
#[cfg(false)]
//~^ NOTE the item is gated here
//~| NOTE the item is gated here
//~| NOTE the item is gated here
//~| NOTE the item is gated here
//~| NOTE the item is gated here
pub mod gated {
//~^ NOTE found an item that was configured out
//~| NOTE found an item that was configured out
//~| NOTE found an item that was configured out
//~| NOTE found an item that was configured out
//~| NOTE found an item that was configured out
pub fn foo() {}
}
}

mod reexport {
pub use super::original::*;
}

mod reexport2 {
pub use super::reexport::*;
}

mod reexport30 {
pub use super::original::*;
pub use super::reexport31::*;
}

mod reexport31 {
pub use super::reexport30::*;
}

mod reexport32 {
pub use super::reexport30::*;
}

fn main() {
reexport::gated::foo();
//~^ ERROR failed to resolve: could not find `gated` in `reexport`
//~| NOTE could not find `gated` in `reexport`

reexport2::gated::foo();
//~^ ERROR failed to resolve: could not find `gated` in `reexport2`
//~| NOTE could not find `gated` in `reexport2`

reexport30::gated::foo();
//~^ ERROR failed to resolve: could not find `gated` in `reexport30`
//~| NOTE could not find `gated` in `reexport30`

reexport31::gated::foo();
//~^ ERROR failed to resolve: could not find `gated` in `reexport31`
//~| NOTE could not find `gated` in `reexport31`

reexport32::gated::foo();
//~^ ERROR failed to resolve: could not find `gated` in `reexport32`
//~| NOTE could not find `gated` in `reexport32`
}
88 changes: 88 additions & 0 deletions tests/ui/cfg/diagnostics-reexport-2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
error[E0433]: failed to resolve: could not find `gated` in `reexport`
--> $DIR/diagnostics-reexport-2.rs:42:15
|
LL | reexport::gated::foo();
| ^^^^^ could not find `gated` in `reexport`
|
note: found an item that was configured out
--> $DIR/diagnostics-reexport-2.rs:10:13
|
LL | pub mod gated {
| ^^^^^
note: the item is gated here
--> $DIR/diagnostics-reexport-2.rs:4:5
|
LL | #[cfg(false)]
| ^^^^^^^^^^^^^

error[E0433]: failed to resolve: could not find `gated` in `reexport2`
--> $DIR/diagnostics-reexport-2.rs:46:16
|
LL | reexport2::gated::foo();
| ^^^^^ could not find `gated` in `reexport2`
|
note: found an item that was configured out
--> $DIR/diagnostics-reexport-2.rs:10:13
|
LL | pub mod gated {
| ^^^^^
note: the item is gated here
--> $DIR/diagnostics-reexport-2.rs:4:5
|
LL | #[cfg(false)]
| ^^^^^^^^^^^^^

error[E0433]: failed to resolve: could not find `gated` in `reexport30`
--> $DIR/diagnostics-reexport-2.rs:50:17
|
LL | reexport30::gated::foo();
| ^^^^^ could not find `gated` in `reexport30`
|
note: found an item that was configured out
--> $DIR/diagnostics-reexport-2.rs:10:13
|
LL | pub mod gated {
| ^^^^^
note: the item is gated here
--> $DIR/diagnostics-reexport-2.rs:4:5
|
LL | #[cfg(false)]
| ^^^^^^^^^^^^^

error[E0433]: failed to resolve: could not find `gated` in `reexport31`
--> $DIR/diagnostics-reexport-2.rs:54:17
|
LL | reexport31::gated::foo();
| ^^^^^ could not find `gated` in `reexport31`
|
note: found an item that was configured out
--> $DIR/diagnostics-reexport-2.rs:10:13
|
LL | pub mod gated {
| ^^^^^
note: the item is gated here
--> $DIR/diagnostics-reexport-2.rs:4:5
|
LL | #[cfg(false)]
| ^^^^^^^^^^^^^

error[E0433]: failed to resolve: could not find `gated` in `reexport32`
--> $DIR/diagnostics-reexport-2.rs:58:17
|
LL | reexport32::gated::foo();
| ^^^^^ could not find `gated` in `reexport32`
|
note: found an item that was configured out
--> $DIR/diagnostics-reexport-2.rs:10:13
|
LL | pub mod gated {
| ^^^^^
note: the item is gated here
--> $DIR/diagnostics-reexport-2.rs:4:5
|
LL | #[cfg(false)]
| ^^^^^^^^^^^^^

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0433`.
Loading