Skip to content

Commit 98fdfcb

Browse files
authored
Rollup merge of #127662 - estebank:gate-span, r=TaKO8Ki
When finding item gated behind a `cfg` flag, point at it Previously we would only mention that the item was gated out, and opportunisitically mention the feature flag name when possible. We now point to the place where the item was gated, which can be behind layers of macro indirection, or in different modules. ``` error[E0433]: failed to resolve: could not find `doesnt_exist` in `inner` --> $DIR/diagnostics-cross-crate.rs:18:23 | LL | cfged_out::inner::doesnt_exist::hello(); | ^^^^^^^^^^^^ could not find `doesnt_exist` in `inner` | note: found an item that was configured out --> $DIR/auxiliary/cfged_out.rs:6:13 | LL | pub mod doesnt_exist { | ^^^^^^^^^^^^ note: the item is gated here --> $DIR/auxiliary/cfged_out.rs:5:5 | LL | #[cfg(FALSE)] | ^^^^^^^^^^^^^ ```
2 parents 7161e2d + cf09cba commit 98fdfcb

11 files changed

+122
-25
lines changed

compiler/rustc_resolve/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ resolve_is_private =
232232
resolve_item_was_behind_feature =
233233
the item is gated behind the `{$feature}` feature
234234
235+
resolve_item_was_cfg_out = the item is gated here
236+
235237
resolve_items_in_traits_are_not_importable =
236238
items in traits are not importable
237239

compiler/rustc_resolve/src/diagnostics.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2532,7 +2532,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
25322532
&& let NestedMetaItem::MetaItem(meta_item) = &nested[0]
25332533
&& let MetaItemKind::NameValue(feature_name) = &meta_item.kind
25342534
{
2535-
let note = errors::ItemWasBehindFeature { feature: feature_name.symbol };
2535+
let note = errors::ItemWasBehindFeature {
2536+
feature: feature_name.symbol,
2537+
span: meta_item.span,
2538+
};
2539+
err.subdiagnostic(note);
2540+
} else {
2541+
let note = errors::ItemWasCfgOut { span: cfg.span };
25362542
err.subdiagnostic(note);
25372543
}
25382544
}

compiler/rustc_resolve/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,15 @@ pub(crate) struct FoundItemConfigureOut {
12281228
#[note(resolve_item_was_behind_feature)]
12291229
pub(crate) struct ItemWasBehindFeature {
12301230
pub(crate) feature: Symbol,
1231+
#[primary_span]
1232+
pub(crate) span: Span,
1233+
}
1234+
1235+
#[derive(Subdiagnostic)]
1236+
#[note(resolve_item_was_cfg_out)]
1237+
pub(crate) struct ItemWasCfgOut {
1238+
#[primary_span]
1239+
pub(crate) span: Span,
12311240
}
12321241

12331242
#[derive(Diagnostic)]

tests/ui/cfg/diagnostics-cross-crate.rs

+3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ fn main() {
1111
cfged_out::inner::uwu(); //~ ERROR cannot find function
1212
//~^ NOTE found an item that was configured out
1313
//~| NOTE not found in `cfged_out::inner`
14+
//~| NOTE the item is gated here
1415

1516
// The module isn't found - we would like to get a diagnostic, but currently don't due to
1617
// the awkward way the resolver diagnostics are currently implemented.
1718
cfged_out::inner::doesnt_exist::hello(); //~ ERROR failed to resolve
1819
//~^ NOTE could not find `doesnt_exist` in `inner`
1920
//~| NOTE found an item that was configured out
21+
//~| NOTE the item is gated here
2022

2123
// It should find the one in the right module, not the wrong one.
2224
cfged_out::inner::right::meow(); //~ ERROR cannot find function
@@ -28,4 +30,5 @@ fn main() {
2830
cfged_out::vanished(); //~ ERROR cannot find function
2931
//~^ NOTE found an item that was configured out
3032
//~| NOTE not found in `cfged_out`
33+
//~| NOTE the item is gated here
3134
}

tests/ui/cfg/diagnostics-cross-crate.stderr

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0433]: failed to resolve: could not find `doesnt_exist` in `inner`
2-
--> $DIR/diagnostics-cross-crate.rs:17:23
2+
--> $DIR/diagnostics-cross-crate.rs:18:23
33
|
44
LL | cfged_out::inner::doesnt_exist::hello();
55
| ^^^^^^^^^^^^ could not find `doesnt_exist` in `inner`
@@ -9,6 +9,11 @@ note: found an item that was configured out
99
|
1010
LL | pub mod doesnt_exist {
1111
| ^^^^^^^^^^^^
12+
note: the item is gated here
13+
--> $DIR/auxiliary/cfged_out.rs:5:5
14+
|
15+
LL | #[cfg(FALSE)]
16+
| ^^^^^^^^^^^^^
1217

1318
error[E0425]: cannot find function `uwu` in crate `cfged_out`
1419
--> $DIR/diagnostics-cross-crate.rs:7:16
@@ -27,9 +32,14 @@ note: found an item that was configured out
2732
|
2833
LL | pub fn uwu() {}
2934
| ^^^
35+
note: the item is gated here
36+
--> $DIR/auxiliary/cfged_out.rs:2:5
37+
|
38+
LL | #[cfg(FALSE)]
39+
| ^^^^^^^^^^^^^
3040

3141
error[E0425]: cannot find function `meow` in module `cfged_out::inner::right`
32-
--> $DIR/diagnostics-cross-crate.rs:22:30
42+
--> $DIR/diagnostics-cross-crate.rs:24:30
3343
|
3444
LL | cfged_out::inner::right::meow();
3545
| ^^^^ not found in `cfged_out::inner::right`
@@ -39,10 +49,14 @@ note: found an item that was configured out
3949
|
4050
LL | pub fn meow() {}
4151
| ^^^^
42-
= note: the item is gated behind the `what-a-cool-feature` feature
52+
note: the item is gated behind the `what-a-cool-feature` feature
53+
--> $DIR/auxiliary/cfged_out.rs:16:15
54+
|
55+
LL | #[cfg(feature = "what-a-cool-feature")]
56+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4357

4458
error[E0425]: cannot find function `vanished` in crate `cfged_out`
45-
--> $DIR/diagnostics-cross-crate.rs:28:16
59+
--> $DIR/diagnostics-cross-crate.rs:30:16
4660
|
4761
LL | cfged_out::vanished();
4862
| ^^^^^^^^ not found in `cfged_out`
@@ -52,6 +66,11 @@ note: found an item that was configured out
5266
|
5367
LL | pub fn vanished() {}
5468
| ^^^^^^^^
69+
note: the item is gated here
70+
--> $DIR/auxiliary/cfged_out.rs:21:1
71+
|
72+
LL | #[cfg(i_dont_exist_and_you_can_do_nothing_about_it)]
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5574

5675
error: aborting due to 5 previous errors
5776

tests/ui/cfg/diagnostics-reexport.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub mod inner {
44
pub fn uwu() {}
55
}
66

7-
#[cfg(FALSE)]
7+
#[cfg(FALSE)] //~ NOTE the item is gated here
88
pub use super::uwu;
99
//~^ NOTE found an item that was configured out
1010
}
@@ -14,7 +14,7 @@ pub use a::x;
1414
//~| NOTE no `x` in `a`
1515

1616
mod a {
17-
#[cfg(FALSE)]
17+
#[cfg(FALSE)] //~ NOTE the item is gated here
1818
pub fn x() {}
1919
//~^ NOTE found an item that was configured out
2020
}
@@ -25,10 +25,10 @@ pub use b::{x, y};
2525
//~| NOTE no `y` in `b`
2626

2727
mod b {
28-
#[cfg(FALSE)]
28+
#[cfg(FALSE)] //~ NOTE the item is gated here
2929
pub fn x() {}
3030
//~^ NOTE found an item that was configured out
31-
#[cfg(FALSE)]
31+
#[cfg(FALSE)] //~ NOTE the item is gated here
3232
pub fn y() {}
3333
//~^ NOTE found an item that was configured out
3434
}

tests/ui/cfg/diagnostics-reexport.stderr

+20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ note: found an item that was configured out
99
|
1010
LL | pub fn x() {}
1111
| ^
12+
note: the item is gated here
13+
--> $DIR/diagnostics-reexport.rs:17:5
14+
|
15+
LL | #[cfg(FALSE)]
16+
| ^^^^^^^^^^^^^
1217

1318
error[E0432]: unresolved imports `b::x`, `b::y`
1419
--> $DIR/diagnostics-reexport.rs:22:13
@@ -23,11 +28,21 @@ note: found an item that was configured out
2328
|
2429
LL | pub fn x() {}
2530
| ^
31+
note: the item is gated here
32+
--> $DIR/diagnostics-reexport.rs:28:5
33+
|
34+
LL | #[cfg(FALSE)]
35+
| ^^^^^^^^^^^^^
2636
note: found an item that was configured out
2737
--> $DIR/diagnostics-reexport.rs:32:12
2838
|
2939
LL | pub fn y() {}
3040
| ^
41+
note: the item is gated here
42+
--> $DIR/diagnostics-reexport.rs:31:5
43+
|
44+
LL | #[cfg(FALSE)]
45+
| ^^^^^^^^^^^^^
3146

3247
error[E0425]: cannot find function `uwu` in module `inner`
3348
--> $DIR/diagnostics-reexport.rs:38:12
@@ -40,6 +55,11 @@ note: found an item that was configured out
4055
|
4156
LL | pub use super::uwu;
4257
| ^^^
58+
note: the item is gated here
59+
--> $DIR/diagnostics-reexport.rs:7:5
60+
|
61+
LL | #[cfg(FALSE)]
62+
| ^^^^^^^^^^^^^
4363

4464
error: aborting due to 3 previous errors
4565

tests/ui/cfg/diagnostics-same-crate.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#![allow(unexpected_cfgs)] // since we want to recognize them as unexpected
22

33
pub mod inner {
4-
#[cfg(FALSE)]
4+
#[cfg(FALSE)] //~ NOTE the item is gated here
55
pub fn uwu() {}
66
//~^ NOTE found an item that was configured out
77

8-
#[cfg(FALSE)]
8+
#[cfg(FALSE)] //~ NOTE the item is gated here
9+
//~^ NOTE the item is gated here
10+
//~| NOTE the item is gated here
911
pub mod doesnt_exist {
1012
//~^ NOTE found an item that was configured out
1113
//~| NOTE found an item that was configured out
@@ -20,7 +22,7 @@ pub mod inner {
2022
}
2123

2224
pub mod right {
23-
#[cfg(feature = "what-a-cool-feature")]
25+
#[cfg(feature = "what-a-cool-feature")] //~ NOTE the item is gated behind the `what-a-cool-feature` feature
2426
pub fn meow() {}
2527
//~^ NOTE found an item that was configured out
2628
}
@@ -55,7 +57,6 @@ fn main() {
5557
// It should find the one in the right module, not the wrong one.
5658
inner::right::meow(); //~ ERROR cannot find function
5759
//~| NOTE not found in `inner::right
58-
//~| NOTE the item is gated behind the `what-a-cool-feature` feature
5960

6061
// Exists in the crate root - we would generally want a diagnostic,
6162
// but currently don't have one.

tests/ui/cfg/diagnostics-same-crate.stderr

+36-12
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,56 @@
11
error[E0432]: unresolved import `super::inner::doesnt_exist`
2-
--> $DIR/diagnostics-same-crate.rs:30:9
2+
--> $DIR/diagnostics-same-crate.rs:32:9
33
|
44
LL | use super::inner::doesnt_exist;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ no `doesnt_exist` in `inner`
66
|
77
note: found an item that was configured out
8-
--> $DIR/diagnostics-same-crate.rs:9:13
8+
--> $DIR/diagnostics-same-crate.rs:11:13
99
|
1010
LL | pub mod doesnt_exist {
1111
| ^^^^^^^^^^^^
12+
note: the item is gated here
13+
--> $DIR/diagnostics-same-crate.rs:8:5
14+
|
15+
LL | #[cfg(FALSE)]
16+
| ^^^^^^^^^^^^^
1217

1318
error[E0432]: unresolved import `super::inner::doesnt_exist`
14-
--> $DIR/diagnostics-same-crate.rs:33:23
19+
--> $DIR/diagnostics-same-crate.rs:35:23
1520
|
1621
LL | use super::inner::doesnt_exist::hi;
1722
| ^^^^^^^^^^^^ could not find `doesnt_exist` in `inner`
1823
|
1924
note: found an item that was configured out
20-
--> $DIR/diagnostics-same-crate.rs:9:13
25+
--> $DIR/diagnostics-same-crate.rs:11:13
2126
|
2227
LL | pub mod doesnt_exist {
2328
| ^^^^^^^^^^^^
29+
note: the item is gated here
30+
--> $DIR/diagnostics-same-crate.rs:8:5
31+
|
32+
LL | #[cfg(FALSE)]
33+
| ^^^^^^^^^^^^^
2434

2535
error[E0433]: failed to resolve: could not find `doesnt_exist` in `inner`
26-
--> $DIR/diagnostics-same-crate.rs:52:12
36+
--> $DIR/diagnostics-same-crate.rs:54:12
2737
|
2838
LL | inner::doesnt_exist::hello();
2939
| ^^^^^^^^^^^^ could not find `doesnt_exist` in `inner`
3040
|
3141
note: found an item that was configured out
32-
--> $DIR/diagnostics-same-crate.rs:9:13
42+
--> $DIR/diagnostics-same-crate.rs:11:13
3343
|
3444
LL | pub mod doesnt_exist {
3545
| ^^^^^^^^^^^^
46+
note: the item is gated here
47+
--> $DIR/diagnostics-same-crate.rs:8:5
48+
|
49+
LL | #[cfg(FALSE)]
50+
| ^^^^^^^^^^^^^
3651

3752
error[E0425]: cannot find function `uwu` in module `inner`
38-
--> $DIR/diagnostics-same-crate.rs:47:12
53+
--> $DIR/diagnostics-same-crate.rs:49:12
3954
|
4055
LL | inner::uwu();
4156
| ^^^ not found in `inner`
@@ -45,28 +60,37 @@ note: found an item that was configured out
4560
|
4661
LL | pub fn uwu() {}
4762
| ^^^
63+
note: the item is gated here
64+
--> $DIR/diagnostics-same-crate.rs:4:5
65+
|
66+
LL | #[cfg(FALSE)]
67+
| ^^^^^^^^^^^^^
4868

4969
error[E0425]: cannot find function `meow` in module `inner::right`
50-
--> $DIR/diagnostics-same-crate.rs:56:19
70+
--> $DIR/diagnostics-same-crate.rs:58:19
5171
|
5272
LL | inner::right::meow();
5373
| ^^^^ not found in `inner::right`
5474
|
5575
note: found an item that was configured out
56-
--> $DIR/diagnostics-same-crate.rs:24:16
76+
--> $DIR/diagnostics-same-crate.rs:26:16
5777
|
5878
LL | pub fn meow() {}
5979
| ^^^^
60-
= note: the item is gated behind the `what-a-cool-feature` feature
80+
note: the item is gated behind the `what-a-cool-feature` feature
81+
--> $DIR/diagnostics-same-crate.rs:25:15
82+
|
83+
LL | #[cfg(feature = "what-a-cool-feature")]
84+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6185

6286
error[E0425]: cannot find function `uwu` in this scope
63-
--> $DIR/diagnostics-same-crate.rs:43:5
87+
--> $DIR/diagnostics-same-crate.rs:45:5
6488
|
6589
LL | uwu();
6690
| ^^^ not found in this scope
6791

6892
error[E0425]: cannot find function `vanished` in this scope
69-
--> $DIR/diagnostics-same-crate.rs:63:5
93+
--> $DIR/diagnostics-same-crate.rs:64:5
7094
|
7195
LL | vanished();
7296
| ^^^^^^^^ not found in this scope

tests/ui/macros/builtin-std-paths-fail.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ LL | #[std::test]
104104
|
105105
note: found an item that was configured out
106106
--> $SRC_DIR/std/src/lib.rs:LL:COL
107+
note: the item is gated here
108+
--> $SRC_DIR/std/src/lib.rs:LL:COL
107109

108110
error: aborting due to 16 previous errors
109111

tests/ui/macros/macro-outer-attributes.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ note: found an item that was configured out
99
|
1010
LL | pub fn bar() { });
1111
| ^^^
12+
note: the item is gated here
13+
--> $DIR/macro-outer-attributes.rs:5:45
14+
|
15+
LL | $i:item) => (mod $nm { #[$a] $i }); }
16+
| ^^^^^
17+
LL |
18+
LL | / test!(a,
19+
LL | | #[cfg(FALSE)],
20+
LL | | pub fn bar() { });
21+
| |_______________________- in this macro invocation
22+
= note: this error originates in the macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)
1223
help: consider importing this function
1324
|
1425
LL + use b::bar;

0 commit comments

Comments
 (0)