Skip to content

Commit 7ee91e0

Browse files
committed
consider glob imports in cfg suggestion
1 parent b5eb989 commit 7ee91e0

File tree

4 files changed

+170
-5
lines changed

4 files changed

+170
-5
lines changed

compiler/rustc_expand/src/expand.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,10 +1319,10 @@ impl InvocationCollectorNode for P<ast::Item> {
13191319

13201320
let mut idents = Vec::new();
13211321
collect_use_tree_leaves(ut, &mut idents);
1322-
return idents;
1322+
idents
1323+
} else {
1324+
self.kind.ident().into_iter().collect()
13231325
}
1324-
1325-
if let Some(ident) = self.kind.ident() { vec![ident] } else { vec![] }
13261326
}
13271327
}
13281328

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_ast::{
66
};
77
use rustc_ast_pretty::pprust;
88
use rustc_attr_data_structures::{self as attr, Stability};
9-
use rustc_data_structures::fx::FxHashSet;
9+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1010
use rustc_data_structures::unord::UnordSet;
1111
use rustc_errors::codes::*;
1212
use rustc_errors::{
@@ -2623,7 +2623,50 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
26232623
};
26242624

26252625
for &StrippedCfgItem { parent_module, ident, ref cfg } in symbols {
2626-
if parent_module != module || ident.name != *segment {
2626+
if ident.name != *segment {
2627+
continue;
2628+
}
2629+
2630+
fn comes_from_same_module_for_glob(
2631+
r: &Resolver<'_, '_>,
2632+
parent_module: DefId,
2633+
module: DefId,
2634+
visited: &mut FxHashMap<DefId, bool>,
2635+
) -> bool {
2636+
if let Some(&cached) = visited.get(&parent_module) {
2637+
return cached;
2638+
}
2639+
visited.insert(parent_module, false);
2640+
let res = r.module_map.get(&parent_module).is_some_and(|m| {
2641+
for importer in m.glob_importers.borrow().iter() {
2642+
if let Some(next_parent_module) = importer.parent_scope.module.opt_def_id()
2643+
{
2644+
if next_parent_module == module
2645+
|| comes_from_same_module_for_glob(
2646+
r,
2647+
next_parent_module,
2648+
module,
2649+
visited,
2650+
)
2651+
{
2652+
return true;
2653+
}
2654+
}
2655+
}
2656+
false
2657+
});
2658+
visited.insert(parent_module, res);
2659+
res
2660+
}
2661+
2662+
let comes_from_same_module = parent_module == module
2663+
|| comes_from_same_module_for_glob(
2664+
self,
2665+
parent_module,
2666+
module,
2667+
&mut Default::default(),
2668+
);
2669+
if !comes_from_same_module {
26272670
continue;
26282671
}
26292672

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// issue#141256
2+
3+
mod original {
4+
#[cfg(false)]
5+
//~^ NOTE the item is gated here
6+
//~| NOTE the item is gated here
7+
//~| NOTE the item is gated here
8+
//~| NOTE the item is gated here
9+
pub mod gated {
10+
//~^ NOTE found an item that was configured out
11+
//~| NOTE found an item that was configured out
12+
//~| NOTE found an item that was configured out
13+
//~| NOTE found an item that was configured out
14+
pub fn foo() {}
15+
}
16+
}
17+
18+
mod reexport {
19+
pub use super::original::*;
20+
}
21+
22+
mod reexport2 {
23+
pub use super::reexport::*;
24+
}
25+
26+
mod reexport30 {
27+
pub use super::original::*;
28+
pub use super::reexport31::*;
29+
}
30+
31+
mod reexport31 {
32+
pub use super::reexport30::*;
33+
}
34+
35+
fn main() {
36+
reexport::gated::foo();
37+
//~^ ERROR failed to resolve: could not find `gated` in `reexport`
38+
//~| NOTE could not find `gated` in `reexport`
39+
40+
reexport2::gated::foo();
41+
//~^ ERROR failed to resolve: could not find `gated` in `reexport2`
42+
//~| NOTE could not find `gated` in `reexport2`
43+
44+
reexport30::gated::foo();
45+
//~^ ERROR failed to resolve: could not find `gated` in `reexport30`
46+
//~| NOTE could not find `gated` in `reexport30`
47+
48+
reexport31::gated::foo();
49+
//~^ ERROR failed to resolve: could not find `gated` in `reexport31`
50+
//~| NOTE could not find `gated` in `reexport31`
51+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
error[E0433]: failed to resolve: could not find `gated` in `reexport`
2+
--> $DIR/diagnostics-reexport-2.rs:36:15
3+
|
4+
LL | reexport::gated::foo();
5+
| ^^^^^ could not find `gated` in `reexport`
6+
|
7+
note: found an item that was configured out
8+
--> $DIR/diagnostics-reexport-2.rs:9:13
9+
|
10+
LL | pub mod gated {
11+
| ^^^^^
12+
note: the item is gated here
13+
--> $DIR/diagnostics-reexport-2.rs:4:5
14+
|
15+
LL | #[cfg(false)]
16+
| ^^^^^^^^^^^^^
17+
18+
error[E0433]: failed to resolve: could not find `gated` in `reexport2`
19+
--> $DIR/diagnostics-reexport-2.rs:40:16
20+
|
21+
LL | reexport2::gated::foo();
22+
| ^^^^^ could not find `gated` in `reexport2`
23+
|
24+
note: found an item that was configured out
25+
--> $DIR/diagnostics-reexport-2.rs:9:13
26+
|
27+
LL | pub mod gated {
28+
| ^^^^^
29+
note: the item is gated here
30+
--> $DIR/diagnostics-reexport-2.rs:4:5
31+
|
32+
LL | #[cfg(false)]
33+
| ^^^^^^^^^^^^^
34+
35+
error[E0433]: failed to resolve: could not find `gated` in `reexport30`
36+
--> $DIR/diagnostics-reexport-2.rs:44:17
37+
|
38+
LL | reexport30::gated::foo();
39+
| ^^^^^ could not find `gated` in `reexport30`
40+
|
41+
note: found an item that was configured out
42+
--> $DIR/diagnostics-reexport-2.rs:9:13
43+
|
44+
LL | pub mod gated {
45+
| ^^^^^
46+
note: the item is gated here
47+
--> $DIR/diagnostics-reexport-2.rs:4:5
48+
|
49+
LL | #[cfg(false)]
50+
| ^^^^^^^^^^^^^
51+
52+
error[E0433]: failed to resolve: could not find `gated` in `reexport31`
53+
--> $DIR/diagnostics-reexport-2.rs:48:17
54+
|
55+
LL | reexport31::gated::foo();
56+
| ^^^^^ could not find `gated` in `reexport31`
57+
|
58+
note: found an item that was configured out
59+
--> $DIR/diagnostics-reexport-2.rs:9:13
60+
|
61+
LL | pub mod gated {
62+
| ^^^^^
63+
note: the item is gated here
64+
--> $DIR/diagnostics-reexport-2.rs:4:5
65+
|
66+
LL | #[cfg(false)]
67+
| ^^^^^^^^^^^^^
68+
69+
error: aborting due to 4 previous errors
70+
71+
For more information about this error, try `rustc --explain E0433`.

0 commit comments

Comments
 (0)