From c288cb1f7437ab052613409f34b03afbdf30eeff Mon Sep 17 00:00:00 2001 From: bohan Date: Sat, 13 Jan 2024 21:05:41 +0800 Subject: [PATCH] store the segment name when resolution fails --- compiler/rustc_resolve/src/diagnostics.rs | 17 +++----- compiler/rustc_resolve/src/ident.rs | 44 ++++++++------------- compiler/rustc_resolve/src/imports.rs | 3 +- compiler/rustc_resolve/src/late.rs | 3 +- compiler/rustc_resolve/src/lib.rs | 15 +++++-- compiler/rustc_resolve/src/macros.rs | 2 +- tests/ui/cfg/diagnostics-cross-crate.rs | 2 +- tests/ui/cfg/diagnostics-cross-crate.stderr | 8 +++- tests/ui/cfg/diagnostics-same-crate.rs | 3 +- tests/ui/cfg/diagnostics-same-crate.stderr | 12 ++++-- 10 files changed, 58 insertions(+), 51 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 0d744238eeb42..b570066138502 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -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); @@ -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 @@ -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 }) @@ -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; @@ -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; } diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs index 3a31addb10933..7fb9db16e9c7b 100644 --- a/compiler/rustc_resolve/src/ident.rs +++ b/compiler/rustc_resolve/src/ident.rs @@ -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 { @@ -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 { @@ -1515,7 +1511,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { )); } else { return PathResult::failed( - ident.span, + ident, is_last, finalize.is_some(), module, @@ -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, + ) + }); } } } diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 2ebf4c2056266..f846dbec2c697 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -886,6 +886,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { PathResult::Failed { is_error_from_last_segment: false, span, + segment_name, label, suggestion, module, @@ -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, diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index b9e603a499266..82f502279112b 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -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, diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 0adea65ee58ea..90aa7d79bf045 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -213,7 +213,7 @@ enum ResolutionError<'a> { SelfImportOnlyInImportListWithNonEmptyPrefix, /// Error E0433: failed to resolve. FailedToResolve { - last_segment: Option, + segment: Option, label: String, suggestion: Option, module: Option>, @@ -396,12 +396,14 @@ enum PathResult<'a> { suggestion: Option, is_error_from_last_segment: bool, module: Option>, + /// 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>, @@ -409,7 +411,14 @@ impl<'a> PathResult<'a> { ) -> 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, + } } } diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 66ecaeb4449fc..aa9a02d7c44f8 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -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, diff --git a/tests/ui/cfg/diagnostics-cross-crate.rs b/tests/ui/cfg/diagnostics-cross-crate.rs index d2725c94b083b..ad4e47b7b2e06 100644 --- a/tests/ui/cfg/diagnostics-cross-crate.rs +++ b/tests/ui/cfg/diagnostics-cross-crate.rs @@ -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 diff --git a/tests/ui/cfg/diagnostics-cross-crate.stderr b/tests/ui/cfg/diagnostics-cross-crate.stderr index 046929bc26023..8a238f3640445 100644 --- a/tests/ui/cfg/diagnostics-cross-crate.stderr +++ b/tests/ui/cfg/diagnostics-cross-crate.stderr @@ -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 diff --git a/tests/ui/cfg/diagnostics-same-crate.rs b/tests/ui/cfg/diagnostics-same-crate.rs index f76ace06a762d..2d0907c6dfb8c 100644 --- a/tests/ui/cfg/diagnostics-same-crate.rs +++ b/tests/ui/cfg/diagnostics-same-crate.rs @@ -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() {} } @@ -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` diff --git a/tests/ui/cfg/diagnostics-same-crate.stderr b/tests/ui/cfg/diagnostics-same-crate.stderr index 30ee6479bd26c..62a9d132de09d 100644 --- a/tests/ui/cfg/diagnostics-same-crate.stderr +++ b/tests/ui/cfg/diagnostics-same-crate.stderr @@ -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 @@ -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` @@ -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