Skip to content

Commit 70bc26d

Browse files
authored
Rollup merge of rust-lang#119925 - bvanjoi:fix-112672, r=Nilstrieb
store the segment name when resolution fails Fixes rust-lang#112672 The `find_cfg_stripped` does indeed get executed within `smart_resolve_report_errors`. However, this error is not reported as it is subsequently overridden by `parent_err`. (See: https://github.com/rust-lang/rust/blob/master/compiler/rustc_resolve/src/late.rs#L3760) This PR changes `last_segment` to `segment`, which stores the name of the failed resolution, and ensures that the result of `find_cfg_stripped` is also included in `parent_err`. r? ```@Nilstrieb```
2 parents 0bbc888 + c288cb1 commit 70bc26d

10 files changed

+58
-51
lines changed

Diff for: compiler/rustc_resolve/src/diagnostics.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
786786
ResolutionError::SelfImportOnlyInImportListWithNonEmptyPrefix => {
787787
self.dcx().create_err(errs::SelfImportOnlyInImportListWithNonEmptyPrefix { span })
788788
}
789-
ResolutionError::FailedToResolve { last_segment, label, suggestion, module } => {
789+
ResolutionError::FailedToResolve { segment, label, suggestion, module } => {
790790
let mut err =
791791
struct_span_code_err!(self.dcx(), span, E0433, "failed to resolve: {}", &label);
792792
err.span_label(span, label);
@@ -801,9 +801,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
801801

802802
if let Some(ModuleOrUniformRoot::Module(module)) = module
803803
&& let Some(module) = module.opt_def_id()
804-
&& let Some(last_segment) = last_segment
804+
&& let Some(segment) = segment
805805
{
806-
self.find_cfg_stripped(&mut err, &last_segment, module);
806+
self.find_cfg_stripped(&mut err, &segment, module);
807807
}
808808

809809
err
@@ -981,12 +981,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
981981
}
982982
VisResolutionError::FailedToResolve(span, label, suggestion) => self.into_struct_error(
983983
span,
984-
ResolutionError::FailedToResolve {
985-
last_segment: None,
986-
label,
987-
suggestion,
988-
module: None,
989-
},
984+
ResolutionError::FailedToResolve { segment: None, label, suggestion, module: None },
990985
),
991986
VisResolutionError::ExpectedFound(span, path_str, res) => {
992987
self.dcx().create_err(errs::ExpectedFound { span, res, path_str })
@@ -2450,7 +2445,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
24502445
pub(crate) fn find_cfg_stripped(
24512446
&mut self,
24522447
err: &mut Diagnostic,
2453-
last_segment: &Symbol,
2448+
segment: &Symbol,
24542449
module: DefId,
24552450
) {
24562451
let local_items;
@@ -2469,7 +2464,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
24692464
};
24702465

24712466
for &StrippedCfgItem { parent_module, name, ref cfg } in symbols {
2472-
if parent_module != module || name.name != *last_segment {
2467+
if parent_module != module || name.name != *segment {
24732468
continue;
24742469
}
24752470

Diff for: compiler/rustc_resolve/src/ident.rs

+17-27
Original file line numberDiff line numberDiff line change
@@ -1381,13 +1381,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13811381
continue;
13821382
}
13831383
}
1384-
return PathResult::failed(
1385-
ident.span,
1386-
false,
1387-
finalize.is_some(),
1388-
module,
1389-
|| ("there are too many leading `super` keywords".to_string(), None),
1390-
);
1384+
return PathResult::failed(ident, false, finalize.is_some(), module, || {
1385+
("there are too many leading `super` keywords".to_string(), None)
1386+
});
13911387
}
13921388
if segment_idx == 0 {
13931389
if name == kw::SelfLower {
@@ -1419,7 +1415,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14191415

14201416
// Report special messages for path segment keywords in wrong positions.
14211417
if ident.is_path_segment_keyword() && segment_idx != 0 {
1422-
return PathResult::failed(ident.span, false, finalize.is_some(), module, || {
1418+
return PathResult::failed(ident, false, finalize.is_some(), module, || {
14231419
let name_str = if name == kw::PathRoot {
14241420
"crate root".to_string()
14251421
} else {
@@ -1515,7 +1511,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15151511
));
15161512
} else {
15171513
return PathResult::failed(
1518-
ident.span,
1514+
ident,
15191515
is_last,
15201516
finalize.is_some(),
15211517
module,
@@ -1541,24 +1537,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15411537
}
15421538
}
15431539

1544-
return PathResult::failed(
1545-
ident.span,
1546-
is_last,
1547-
finalize.is_some(),
1548-
module,
1549-
|| {
1550-
self.report_path_resolution_error(
1551-
path,
1552-
opt_ns,
1553-
parent_scope,
1554-
ribs,
1555-
ignore_binding,
1556-
module,
1557-
segment_idx,
1558-
ident,
1559-
)
1560-
},
1561-
);
1540+
return PathResult::failed(ident, is_last, finalize.is_some(), module, || {
1541+
self.report_path_resolution_error(
1542+
path,
1543+
opt_ns,
1544+
parent_scope,
1545+
ribs,
1546+
ignore_binding,
1547+
module,
1548+
segment_idx,
1549+
ident,
1550+
)
1551+
});
15621552
}
15631553
}
15641554
}

Diff for: compiler/rustc_resolve/src/imports.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
886886
PathResult::Failed {
887887
is_error_from_last_segment: false,
888888
span,
889+
segment_name,
889890
label,
890891
suggestion,
891892
module,
@@ -895,7 +896,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
895896
self.report_error(
896897
span,
897898
ResolutionError::FailedToResolve {
898-
last_segment: None,
899+
segment: Some(segment_name),
899900
label,
900901
suggestion,
901902
module,

Diff for: compiler/rustc_resolve/src/late.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4054,11 +4054,12 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
40544054
label,
40554055
suggestion,
40564056
module,
4057+
segment_name,
40574058
} => {
40584059
return Err(respan(
40594060
span,
40604061
ResolutionError::FailedToResolve {
4061-
last_segment: None,
4062+
segment: Some(segment_name),
40624063
label,
40634064
suggestion,
40644065
module,

Diff for: compiler/rustc_resolve/src/lib.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ enum ResolutionError<'a> {
213213
SelfImportOnlyInImportListWithNonEmptyPrefix,
214214
/// Error E0433: failed to resolve.
215215
FailedToResolve {
216-
last_segment: Option<Symbol>,
216+
segment: Option<Symbol>,
217217
label: String,
218218
suggestion: Option<Suggestion>,
219219
module: Option<ModuleOrUniformRoot<'a>>,
@@ -396,20 +396,29 @@ enum PathResult<'a> {
396396
suggestion: Option<Suggestion>,
397397
is_error_from_last_segment: bool,
398398
module: Option<ModuleOrUniformRoot<'a>>,
399+
/// The segment name of target
400+
segment_name: Symbol,
399401
},
400402
}
401403

402404
impl<'a> PathResult<'a> {
403405
fn failed(
404-
span: Span,
406+
ident: Ident,
405407
is_error_from_last_segment: bool,
406408
finalize: bool,
407409
module: Option<ModuleOrUniformRoot<'a>>,
408410
label_and_suggestion: impl FnOnce() -> (String, Option<Suggestion>),
409411
) -> PathResult<'a> {
410412
let (label, suggestion) =
411413
if finalize { label_and_suggestion() } else { (String::new(), None) };
412-
PathResult::Failed { span, label, suggestion, is_error_from_last_segment, module }
414+
PathResult::Failed {
415+
span: ident.span,
416+
segment_name: ident.name,
417+
label,
418+
suggestion,
419+
is_error_from_last_segment,
420+
module,
421+
}
413422
}
414423
}
415424

Diff for: compiler/rustc_resolve/src/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
779779
self.report_error(
780780
span,
781781
ResolutionError::FailedToResolve {
782-
last_segment: path.last().map(|segment| segment.ident.name),
782+
segment: path.last().map(|segment| segment.ident.name),
783783
label,
784784
suggestion,
785785
module,

Diff for: tests/ui/cfg/diagnostics-cross-crate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ fn main() {
1414

1515
// The module isn't found - we would like to get a diagnostic, but currently don't due to
1616
// the awkward way the resolver diagnostics are currently implemented.
17-
// FIXME(Nilstrieb): Also add a note to the cfg diagnostic here
1817
cfged_out::inner::doesnt_exist::hello(); //~ ERROR failed to resolve
1918
//~^ NOTE could not find `doesnt_exist` in `inner`
19+
//~| NOTE found an item that was configured out
2020

2121
// It should find the one in the right module, not the wrong one.
2222
cfged_out::inner::right::meow(); //~ ERROR cannot find function

Diff for: tests/ui/cfg/diagnostics-cross-crate.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
error[E0433]: failed to resolve: could not find `doesnt_exist` in `inner`
2-
--> $DIR/diagnostics-cross-crate.rs:18:23
2+
--> $DIR/diagnostics-cross-crate.rs:17:23
33
|
44
LL | cfged_out::inner::doesnt_exist::hello();
55
| ^^^^^^^^^^^^ could not find `doesnt_exist` in `inner`
6+
|
7+
note: found an item that was configured out
8+
--> $DIR/auxiliary/cfged_out.rs:6:13
9+
|
10+
LL | pub mod doesnt_exist {
11+
| ^^^^^^^^^^^^
612

713
error[E0425]: cannot find function `uwu` in crate `cfged_out`
814
--> $DIR/diagnostics-cross-crate.rs:7:16

Diff for: tests/ui/cfg/diagnostics-same-crate.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub mod inner {
44
//~^ NOTE found an item that was configured out
55

66
#[cfg(FALSE)]
7-
pub mod doesnt_exist {
7+
pub mod doesnt_exist { //~ NOTE found an item that was configured out
88
pub fn hello() {}
99
}
1010

@@ -34,7 +34,6 @@ fn main() {
3434

3535
// The module isn't found - we would like to get a diagnostic, but currently don't due to
3636
// the awkward way the resolver diagnostics are currently implemented.
37-
// FIXME(Nilstrieb): Also add a note to the cfg diagnostic here
3837
inner::doesnt_exist::hello(); //~ ERROR failed to resolve
3938
//~| NOTE could not find `doesnt_exist` in `inner`
4039

Diff for: tests/ui/cfg/diagnostics-same-crate.stderr

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
error[E0433]: failed to resolve: could not find `doesnt_exist` in `inner`
2-
--> $DIR/diagnostics-same-crate.rs:38:12
2+
--> $DIR/diagnostics-same-crate.rs:37:12
33
|
44
LL | inner::doesnt_exist::hello();
55
| ^^^^^^^^^^^^ could not find `doesnt_exist` in `inner`
6+
|
7+
note: found an item that was configured out
8+
--> $DIR/diagnostics-same-crate.rs:7:13
9+
|
10+
LL | pub mod doesnt_exist {
11+
| ^^^^^^^^^^^^
612

713
error[E0425]: cannot find function `uwu` in module `inner`
814
--> $DIR/diagnostics-same-crate.rs:32:12
@@ -17,7 +23,7 @@ LL | pub fn uwu() {}
1723
| ^^^
1824

1925
error[E0425]: cannot find function `meow` in module `inner::right`
20-
--> $DIR/diagnostics-same-crate.rs:42:19
26+
--> $DIR/diagnostics-same-crate.rs:41:19
2127
|
2228
LL | inner::right::meow();
2329
| ^^^^ not found in `inner::right`
@@ -36,7 +42,7 @@ LL | uwu();
3642
| ^^^ not found in this scope
3743

3844
error[E0425]: cannot find function `vanished` in this scope
39-
--> $DIR/diagnostics-same-crate.rs:49:5
45+
--> $DIR/diagnostics-same-crate.rs:48:5
4046
|
4147
LL | vanished();
4248
| ^^^^^^^^ not found in this scope

0 commit comments

Comments
 (0)