store the segment name when resolution fails

This commit is contained in:
bohan 2024-01-13 21:05:41 +08:00
parent 284cb714d2
commit c288cb1f74
10 changed files with 58 additions and 51 deletions

View File

@ -786,7 +786,7 @@ pub(crate) fn into_struct_error(
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 @@ pub(crate) fn into_struct_error(
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 @@ pub(crate) fn report_vis_error(
}
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 @@ pub(crate) fn check_for_module_export_macro(
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 @@ pub(crate) fn find_cfg_stripped(
};
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;
}

View File

@ -1381,13 +1381,9 @@ pub(crate) fn resolve_path_with_ribs(
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 @@ pub(crate) fn resolve_path_with_ribs(
// 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 @@ pub(crate) fn resolve_path_with_ribs(
));
} else {
return PathResult::failed(
ident.span,
ident,
is_last,
finalize.is_some(),
module,
@ -1541,24 +1537,18 @@ pub(crate) fn resolve_path_with_ribs(
}
}
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,
)
});
}
}
}

View File

@ -886,6 +886,7 @@ fn finalize_import(&mut self, import: Import<'a>) -> Option<UnresolvedImportErro
PathResult::Failed {
is_error_from_last_segment: false,
span,
segment_name,
label,
suggestion,
module,
@ -895,7 +896,7 @@ fn finalize_import(&mut self, import: Import<'a>) -> Option<UnresolvedImportErro
self.report_error(
span,
ResolutionError::FailedToResolve {
last_segment: None,
segment: Some(segment_name),
label,
suggestion,
module,

View File

@ -4054,11 +4054,12 @@ fn resolve_qpath(
label,
suggestion,
module,
segment_name,
} => {
return Err(respan(
span,
ResolutionError::FailedToResolve {
last_segment: None,
segment: Some(segment_name),
label,
suggestion,
module,

View File

@ -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>>,
@ -396,12 +396,14 @@ 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>>,
@ -409,7 +411,14 @@ fn failed(
) -> 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,
}
}
}

View File

@ -779,7 +779,7 @@ pub(crate) fn finalize_macro_resolutions(&mut self, krate: &Crate) {
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,

View File

@ -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

View File

@ -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

View File

@ -4,7 +4,7 @@ pub fn uwu() {}
//~^ 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`

View File

@ -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