Skip to content

Commit

Permalink
Rollup merge of rust-lang#119925 - bvanjoi:fix-112672, r=Nilstrieb
Browse files Browse the repository at this point in the history
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```
  • Loading branch information
matthiaskrgr committed Jan 13, 2024
2 parents 0bbc888 + c288cb1 commit 70bc26d
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 51 deletions.
17 changes: 6 additions & 11 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
ResolutionError::SelfImportOnlyInImportListWithNonEmptyPrefix => {
self.dcx().create_err(errs::SelfImportOnlyInImportListWithNonEmptyPrefix { span })
}
ResolutionError::FailedToResolve { last_segment, label, suggestion, module } => {
ResolutionError::FailedToResolve { segment, label, suggestion, module } => {
let mut err =
struct_span_code_err!(self.dcx(), span, E0433, "failed to resolve: {}", &label);
err.span_label(span, label);
Expand All @@ -801,9 +801,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {

if let Some(ModuleOrUniformRoot::Module(module)) = module
&& let Some(module) = module.opt_def_id()
&& let Some(last_segment) = last_segment
&& let Some(segment) = segment
{
self.find_cfg_stripped(&mut err, &last_segment, module);
self.find_cfg_stripped(&mut err, &segment, module);
}

err
Expand Down Expand Up @@ -981,12 +981,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
VisResolutionError::FailedToResolve(span, label, suggestion) => self.into_struct_error(
span,
ResolutionError::FailedToResolve {
last_segment: None,
label,
suggestion,
module: None,
},
ResolutionError::FailedToResolve { segment: None, label, suggestion, module: None },
),
VisResolutionError::ExpectedFound(span, path_str, res) => {
self.dcx().create_err(errs::ExpectedFound { span, res, path_str })
Expand Down Expand Up @@ -2450,7 +2445,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
pub(crate) fn find_cfg_stripped(
&mut self,
err: &mut Diagnostic,
last_segment: &Symbol,
segment: &Symbol,
module: DefId,
) {
let local_items;
Expand All @@ -2469,7 +2464,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
};

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

Expand Down
44 changes: 17 additions & 27 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1381,13 +1381,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
continue;
}
}
return PathResult::failed(
ident.span,
false,
finalize.is_some(),
module,
|| ("there are too many leading `super` keywords".to_string(), None),
);
return PathResult::failed(ident, false, finalize.is_some(), module, || {
("there are too many leading `super` keywords".to_string(), None)
});
}
if segment_idx == 0 {
if name == kw::SelfLower {
Expand Down Expand Up @@ -1419,7 +1415,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {

// Report special messages for path segment keywords in wrong positions.
if ident.is_path_segment_keyword() && segment_idx != 0 {
return PathResult::failed(ident.span, false, finalize.is_some(), module, || {
return PathResult::failed(ident, false, finalize.is_some(), module, || {
let name_str = if name == kw::PathRoot {
"crate root".to_string()
} else {
Expand Down Expand Up @@ -1515,7 +1511,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
));
} else {
return PathResult::failed(
ident.span,
ident,
is_last,
finalize.is_some(),
module,
Expand All @@ -1541,24 +1537,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
}

return PathResult::failed(
ident.span,
is_last,
finalize.is_some(),
module,
|| {
self.report_path_resolution_error(
path,
opt_ns,
parent_scope,
ribs,
ignore_binding,
module,
segment_idx,
ident,
)
},
);
return PathResult::failed(ident, is_last, finalize.is_some(), module, || {
self.report_path_resolution_error(
path,
opt_ns,
parent_scope,
ribs,
ignore_binding,
module,
segment_idx,
ident,
)
});
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
PathResult::Failed {
is_error_from_last_segment: false,
span,
segment_name,
label,
suggestion,
module,
Expand All @@ -895,7 +896,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
self.report_error(
span,
ResolutionError::FailedToResolve {
last_segment: None,
segment: Some(segment_name),
label,
suggestion,
module,
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4054,11 +4054,12 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
label,
suggestion,
module,
segment_name,
} => {
return Err(respan(
span,
ResolutionError::FailedToResolve {
last_segment: None,
segment: Some(segment_name),
label,
suggestion,
module,
Expand Down
15 changes: 12 additions & 3 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ enum ResolutionError<'a> {
SelfImportOnlyInImportListWithNonEmptyPrefix,
/// Error E0433: failed to resolve.
FailedToResolve {
last_segment: Option<Symbol>,
segment: Option<Symbol>,
label: String,
suggestion: Option<Suggestion>,
module: Option<ModuleOrUniformRoot<'a>>,
Expand Down Expand Up @@ -396,20 +396,29 @@ enum PathResult<'a> {
suggestion: Option<Suggestion>,
is_error_from_last_segment: bool,
module: Option<ModuleOrUniformRoot<'a>>,
/// The segment name of target
segment_name: Symbol,
},
}

impl<'a> PathResult<'a> {
fn failed(
span: Span,
ident: Ident,
is_error_from_last_segment: bool,
finalize: bool,
module: Option<ModuleOrUniformRoot<'a>>,
label_and_suggestion: impl FnOnce() -> (String, Option<Suggestion>),
) -> PathResult<'a> {
let (label, suggestion) =
if finalize { label_and_suggestion() } else { (String::new(), None) };
PathResult::Failed { span, label, suggestion, is_error_from_last_segment, module }
PathResult::Failed {
span: ident.span,
segment_name: ident.name,
label,
suggestion,
is_error_from_last_segment,
module,
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
self.report_error(
span,
ResolutionError::FailedToResolve {
last_segment: path.last().map(|segment| segment.ident.name),
segment: path.last().map(|segment| segment.ident.name),
label,
suggestion,
module,
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/cfg/diagnostics-cross-crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ fn main() {

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

// It should find the one in the right module, not the wrong one.
cfged_out::inner::right::meow(); //~ ERROR cannot find function
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/cfg/diagnostics-cross-crate.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
error[E0433]: failed to resolve: could not find `doesnt_exist` in `inner`
--> $DIR/diagnostics-cross-crate.rs:18:23
--> $DIR/diagnostics-cross-crate.rs:17: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 {
| ^^^^^^^^^^^^

error[E0425]: cannot find function `uwu` in crate `cfged_out`
--> $DIR/diagnostics-cross-crate.rs:7:16
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/cfg/diagnostics-same-crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod inner {
//~^ NOTE found an item that was configured out

#[cfg(FALSE)]
pub mod doesnt_exist {
pub mod doesnt_exist { //~ NOTE found an item that was configured out
pub fn hello() {}
}

Expand Down Expand Up @@ -34,7 +34,6 @@ fn main() {

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

Expand Down
12 changes: 9 additions & 3 deletions tests/ui/cfg/diagnostics-same-crate.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
error[E0433]: failed to resolve: could not find `doesnt_exist` in `inner`
--> $DIR/diagnostics-same-crate.rs:38:12
--> $DIR/diagnostics-same-crate.rs:37:12
|
LL | inner::doesnt_exist::hello();
| ^^^^^^^^^^^^ could not find `doesnt_exist` in `inner`
|
note: found an item that was configured out
--> $DIR/diagnostics-same-crate.rs:7:13
|
LL | pub mod doesnt_exist {
| ^^^^^^^^^^^^

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

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

error[E0425]: cannot find function `vanished` in this scope
--> $DIR/diagnostics-same-crate.rs:49:5
--> $DIR/diagnostics-same-crate.rs:48:5
|
LL | vanished();
| ^^^^^^^^ not found in this scope
Expand Down

0 comments on commit 70bc26d

Please sign in to comment.