Skip to content

Commit 4b24c4b

Browse files
committed
Tweak rendering of cfg'd out item
``` 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 | #[cfg(false)] | ----- the item is gated here LL | pub mod doesnt_exist { | ^^^^^^^^^^^^ ```
1 parent 77f75f9 commit 4b24c4b

16 files changed

+136
-222
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2845,16 +2845,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
28452845
continue;
28462846
}
28472847

2848-
let note = errors::FoundItemConfigureOut { span: ident.span };
2849-
err.subdiagnostic(note);
2850-
2851-
if let CfgEntry::NameValue { value: Some((feature, _)), .. } = cfg.0 {
2852-
let note = errors::ItemWasBehindFeature { feature, span: cfg.1 };
2853-
err.subdiagnostic(note);
2848+
let item_was = if let CfgEntry::NameValue { value: Some((feature, _)), .. } = cfg.0 {
2849+
errors::ItemWas::BehindFeature { feature, span: cfg.1 }
28542850
} else {
2855-
let note = errors::ItemWasCfgOut { span: cfg.1 };
2856-
err.subdiagnostic(note);
2857-
}
2851+
errors::ItemWas::CfgOut { span: cfg.1 }
2852+
};
2853+
let note = errors::FoundItemConfigureOut { span: ident.span, item_was };
2854+
err.subdiagnostic(note);
28582855
}
28592856
}
28602857
}

compiler/rustc_resolve/src/errors.rs

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use rustc_errors::codes::*;
2-
use rustc_errors::{Applicability, ElidedLifetimeInPathSubdiag, MultiSpan};
2+
use rustc_errors::{
3+
Applicability, Diag, ElidedLifetimeInPathSubdiag, EmissionGuarantee, IntoDiagArg, MultiSpan,
4+
Subdiagnostic,
5+
};
36
use rustc_macros::{Diagnostic, Subdiagnostic};
47
use rustc_span::{Ident, Span, Symbol};
58

6-
use crate::Res;
79
use crate::late::PatternSource;
10+
use crate::{Res, fluent_generated as fluent};
811

912
#[derive(Diagnostic)]
1013
#[diag(resolve_generic_params_from_outer_item, code = E0401)]
@@ -1201,26 +1204,35 @@ pub(crate) struct IdentInScopeButItIsDesc<'a> {
12011204
pub(crate) imported_ident_desc: &'a str,
12021205
}
12031206

1204-
#[derive(Subdiagnostic)]
1205-
#[note(resolve_found_an_item_configured_out)]
12061207
pub(crate) struct FoundItemConfigureOut {
1207-
#[primary_span]
1208-
pub(crate) span: Span,
1209-
}
1210-
1211-
#[derive(Subdiagnostic)]
1212-
#[note(resolve_item_was_behind_feature)]
1213-
pub(crate) struct ItemWasBehindFeature {
1214-
pub(crate) feature: Symbol,
1215-
#[primary_span]
1216-
pub(crate) span: Span,
1217-
}
1218-
1219-
#[derive(Subdiagnostic)]
1220-
#[note(resolve_item_was_cfg_out)]
1221-
pub(crate) struct ItemWasCfgOut {
1222-
#[primary_span]
12231208
pub(crate) span: Span,
1209+
pub(crate) item_was: ItemWas,
1210+
}
1211+
1212+
pub(crate) enum ItemWas {
1213+
BehindFeature { feature: Symbol, span: Span },
1214+
CfgOut { span: Span },
1215+
}
1216+
1217+
impl Subdiagnostic for FoundItemConfigureOut {
1218+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
1219+
let mut multispan: MultiSpan = self.span.into();
1220+
match self.item_was {
1221+
ItemWas::BehindFeature { feature, span } => {
1222+
let key = "feature".into();
1223+
let value = feature.into_diag_arg(&mut None);
1224+
let msg = diag.dcx.eagerly_translate_to_string(
1225+
fluent::resolve_item_was_behind_feature,
1226+
[(&key, &value)].into_iter(),
1227+
);
1228+
multispan.push_span_label(span, msg);
1229+
}
1230+
ItemWas::CfgOut { span } => {
1231+
multispan.push_span_label(span, fluent::resolve_item_was_cfg_out);
1232+
}
1233+
}
1234+
diag.span_note(multispan, fluent::resolve_found_an_item_configured_out);
1235+
}
12241236
}
12251237

12261238
#[derive(Diagnostic)]

tests/ui/cfg/both-true-false.stderr

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,18 @@ LL | foo();
77
note: found an item that was configured out
88
--> $DIR/both-true-false.rs:6:4
99
|
10+
LL | #[cfg(false)]
11+
| ----- the item is gated here
12+
LL | #[cfg(true)]
1013
LL | fn foo() {}
1114
| ^^^
12-
note: the item is gated here
13-
--> $DIR/both-true-false.rs:4:7
14-
|
15-
LL | #[cfg(false)]
16-
| ^^^^^
1715
note: found an item that was configured out
1816
--> $DIR/both-true-false.rs:10:4
1917
|
18+
LL | #[cfg(false)]
19+
| ----- the item is gated here
2020
LL | fn foo() {}
2121
| ^^^
22-
note: the item is gated here
23-
--> $DIR/both-true-false.rs:9:7
24-
|
25-
LL | #[cfg(false)]
26-
| ^^^^^
2722

2823
error: aborting due to 1 previous error
2924

tests/ui/cfg/cfg-version/syntax.stderr

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,11 @@ LL | key_value_form();
132132
note: found an item that was configured out
133133
--> $DIR/syntax.rs:32:4
134134
|
135+
LL | #[cfg(version = "1.43")]
136+
| ---------------- the item is gated behind the `1.43` feature
137+
LL |
135138
LL | fn key_value_form() {}
136139
| ^^^^^^^^^^^^^^
137-
note: the item is gated behind the `1.43` feature
138-
--> $DIR/syntax.rs:30:7
139-
|
140-
LL | #[cfg(version = "1.43")]
141-
| ^^^^^^^^^^^^^^^^
142140

143141
error[E0425]: cannot find function `not_numbers_or_periods` in this scope
144142
--> $DIR/syntax.rs:143:5
@@ -149,13 +147,11 @@ LL | not_numbers_or_periods();
149147
note: found an item that was configured out
150148
--> $DIR/syntax.rs:53:4
151149
|
150+
LL | #[cfg(version("foo"))]
151+
| ------- the item is gated here
152+
LL |
152153
LL | fn not_numbers_or_periods() {}
153154
| ^^^^^^^^^^^^^^^^^^^^^^
154-
note: the item is gated here
155-
--> $DIR/syntax.rs:51:14
156-
|
157-
LL | #[cfg(version("foo"))]
158-
| ^^^^^^^
159155

160156
error[E0425]: cannot find function `complex_semver_with_metadata` in this scope
161157
--> $DIR/syntax.rs:144:5
@@ -166,13 +162,11 @@ LL | complex_semver_with_metadata();
166162
note: found an item that was configured out
167163
--> $DIR/syntax.rs:57:4
168164
|
165+
LL | #[cfg(version("1.20.0-stable"))]
166+
| ----------------- the item is gated here
167+
LL |
169168
LL | fn complex_semver_with_metadata() {}
170169
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
171-
note: the item is gated here
172-
--> $DIR/syntax.rs:55:14
173-
|
174-
LL | #[cfg(version("1.20.0-stable"))]
175-
| ^^^^^^^^^^^^^^^^^
176170

177171
error[E0425]: cannot find function `invalid_major_only` in this scope
178172
--> $DIR/syntax.rs:145:5
@@ -183,13 +177,11 @@ LL | invalid_major_only();
183177
note: found an item that was configured out
184178
--> $DIR/syntax.rs:80:4
185179
|
180+
LL | #[cfg(version("1"))]
181+
| ----- the item is gated here
182+
LL |
186183
LL | fn invalid_major_only() {}
187184
| ^^^^^^^^^^^^^^^^^^
188-
note: the item is gated here
189-
--> $DIR/syntax.rs:78:14
190-
|
191-
LL | #[cfg(version("1"))]
192-
| ^^^^^
193185

194186
error[E0425]: cannot find function `invalid_major_only_zero` in this scope
195187
--> $DIR/syntax.rs:146:5
@@ -200,13 +192,11 @@ LL | invalid_major_only_zero();
200192
note: found an item that was configured out
201193
--> $DIR/syntax.rs:84:4
202194
|
195+
LL | #[cfg(version("0"))]
196+
| ----- the item is gated here
197+
LL |
203198
LL | fn invalid_major_only_zero() {}
204199
| ^^^^^^^^^^^^^^^^^^^^^^^
205-
note: the item is gated here
206-
--> $DIR/syntax.rs:82:14
207-
|
208-
LL | #[cfg(version("0"))]
209-
| ^^^^^
210200

211201
error[E0425]: cannot find function `invalid_major_only_negative` in this scope
212202
--> $DIR/syntax.rs:147:5
@@ -217,13 +207,11 @@ LL | invalid_major_only_negative();
217207
note: found an item that was configured out
218208
--> $DIR/syntax.rs:97:4
219209
|
210+
LL | #[cfg(version("-1"))]
211+
| ------ the item is gated here
212+
LL |
220213
LL | fn invalid_major_only_negative() {}
221214
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
222-
note: the item is gated here
223-
--> $DIR/syntax.rs:95:14
224-
|
225-
LL | #[cfg(version("-1"))]
226-
| ^^^^^^
227215

228216
error[E0425]: cannot find function `exceed_u16_major` in this scope
229217
--> $DIR/syntax.rs:148:5
@@ -234,13 +222,11 @@ LL | exceed_u16_major();
234222
note: found an item that was configured out
235223
--> $DIR/syntax.rs:103:4
236224
|
225+
LL | #[cfg(version("65536"))]
226+
| --------- the item is gated here
227+
LL |
237228
LL | fn exceed_u16_major() {}
238229
| ^^^^^^^^^^^^^^^^
239-
note: the item is gated here
240-
--> $DIR/syntax.rs:101:14
241-
|
242-
LL | #[cfg(version("65536"))]
243-
| ^^^^^^^^^
244230

245231
error[E0425]: cannot find function `exceed_u16_minor` in this scope
246232
--> $DIR/syntax.rs:149:5
@@ -251,13 +237,11 @@ LL | exceed_u16_minor();
251237
note: found an item that was configured out
252238
--> $DIR/syntax.rs:107:4
253239
|
240+
LL | #[cfg(version("1.65536.0"))]
241+
| ------------- the item is gated here
242+
LL |
254243
LL | fn exceed_u16_minor() {}
255244
| ^^^^^^^^^^^^^^^^
256-
note: the item is gated here
257-
--> $DIR/syntax.rs:105:14
258-
|
259-
LL | #[cfg(version("1.65536.0"))]
260-
| ^^^^^^^^^^^^^
261245

262246
error[E0425]: cannot find function `exceed_u16_patch` in this scope
263247
--> $DIR/syntax.rs:150:5
@@ -268,13 +252,11 @@ LL | exceed_u16_patch();
268252
note: found an item that was configured out
269253
--> $DIR/syntax.rs:111:4
270254
|
255+
LL | #[cfg(version("1.0.65536"))]
256+
| ------------- the item is gated here
257+
LL |
271258
LL | fn exceed_u16_patch() {}
272259
| ^^^^^^^^^^^^^^^^
273-
note: the item is gated here
274-
--> $DIR/syntax.rs:109:14
275-
|
276-
LL | #[cfg(version("1.0.65536"))]
277-
| ^^^^^^^^^^^^^
278260

279261
error[E0425]: cannot find function `exceed_u16_mixed` in this scope
280262
--> $DIR/syntax.rs:151:5
@@ -285,13 +267,11 @@ LL | exceed_u16_mixed();
285267
note: found an item that was configured out
286268
--> $DIR/syntax.rs:115:4
287269
|
270+
LL | #[cfg(version("65536.0.65536"))]
271+
| ----------------- the item is gated here
272+
LL |
288273
LL | fn exceed_u16_mixed() {}
289274
| ^^^^^^^^^^^^^^^^
290-
note: the item is gated here
291-
--> $DIR/syntax.rs:113:14
292-
|
293-
LL | #[cfg(version("65536.0.65536"))]
294-
| ^^^^^^^^^^^^^^^^^
295275

296276
error: aborting due to 14 previous errors; 14 warnings emitted
297277

tests/ui/cfg/cmdline-false.stderr

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@ LL | foo();
77
note: found an item that was configured out
88
--> $DIR/cmdline-false.rs:5:4
99
|
10+
LL | #[cfg(false)]
11+
| ----- the item is gated here
1012
LL | fn foo() {}
1113
| ^^^
12-
note: the item is gated here
13-
--> $DIR/cmdline-false.rs:4:7
14-
|
15-
LL | #[cfg(false)]
16-
| ^^^^^
1714

1815
error: aborting due to 1 previous error
1916

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,20 @@ 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
1514

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

2321
// It should find the one in the right module, not the wrong one.
2422
cfged_out::inner::right::meow(); //~ ERROR cannot find function
2523
//~^ NOTE found an item that was configured out
2624
//~| NOTE not found in `cfged_out::inner::right
27-
//~| NOTE the item is gated behind the `what-a-cool-feature` feature
2825

2926
// Exists in the crate root - diagnostic.
3027
cfged_out::vanished(); //~ ERROR cannot find function
3128
//~^ NOTE found an item that was configured out
3229
//~| NOTE not found in `cfged_out`
33-
//~| NOTE the item is gated here
3430
}

0 commit comments

Comments
 (0)