Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c397b99

Browse files
committedMay 27, 2025·
Unify wording of "failed to resolve" errors with "cannot find" resolution errors
* Use the same wording for all macro resolution errors * specify the scope in which the resolution failure happened Before ``` error[E0433]: failed to resolve: `crate` in paths can only be used in start position --> $DIR/crate-path-non-absolute.rs:5:22 | LL | let s = ::m::crate::S; | ^^^^^ `crate` in paths can only be used in start position ``` after ``` error[E0433]: cannot find module `crate` in module `m` --> $DIR/crate-path-non-absolute.rs:5:22 | LL | let s = ::m::crate::S; | ^^^^^ `crate` in paths can only be used in start position ```
1 parent 642e49b commit c397b99

File tree

223 files changed

+512
-409
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

223 files changed

+512
-409
lines changed
 

‎compiler/rustc_resolve/src/build_reduced_graph.rs‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,15 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
382382
PathResult::NonModule(partial_res) => {
383383
expected_found_error(partial_res.expect_full_res())
384384
}
385-
PathResult::Failed { span, label, suggestion, .. } => {
386-
Err(VisResolutionError::FailedToResolve(span, label, suggestion))
387-
}
385+
PathResult::Failed {
386+
span, label, suggestion, segment_name, item_type, ..
387+
} => Err(VisResolutionError::FailedToResolve(
388+
span,
389+
segment_name,
390+
label,
391+
suggestion,
392+
item_type,
393+
)),
388394
PathResult::Indeterminate => Err(VisResolutionError::Indeterminate(path.span)),
389395
}
390396
}

‎compiler/rustc_resolve/src/diagnostics.rs‎

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -792,9 +792,32 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
792792
ResolutionError::SelfImportOnlyInImportListWithNonEmptyPrefix => {
793793
self.dcx().create_err(errs::SelfImportOnlyInImportListWithNonEmptyPrefix { span })
794794
}
795-
ResolutionError::FailedToResolve { segment, label, suggestion, module } => {
796-
let mut err =
797-
struct_span_code_err!(self.dcx(), span, E0433, "failed to resolve: {label}");
795+
ResolutionError::FailedToResolve { segment, label, suggestion, module, item_type } => {
796+
let mut err = struct_span_code_err!(
797+
self.dcx(),
798+
span,
799+
E0433,
800+
"cannot find {item_type} `{segment}` in {}",
801+
match module {
802+
Some(ModuleOrUniformRoot::CurrentScope) | None => "this scope".to_string(),
803+
Some(ModuleOrUniformRoot::Module(module)) => {
804+
match module.kind {
805+
ModuleKind::Def(_, _, None) => {
806+
"the crate root".to_string()
807+
}
808+
ModuleKind::Def(kind, def_id, Some(name)) => {
809+
format!("{} `{name}`", kind.descr(def_id))
810+
}
811+
ModuleKind::Block => "this scope".to_string(),
812+
}
813+
}
814+
Some(ModuleOrUniformRoot::CrateRootAndExternPrelude) => {
815+
"the crate root or the list of imported crates".to_string()
816+
}
817+
Some(ModuleOrUniformRoot::ExternPrelude) =>
818+
"the list of imported crates".to_string(),
819+
},
820+
);
798821
err.span_label(span, label);
799822

800823
if let Some((suggestions, msg, applicability)) = suggestion {
@@ -806,7 +829,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
806829
}
807830
if let Some(ModuleOrUniformRoot::Module(module)) = module
808831
&& let Some(module) = module.opt_def_id()
809-
&& let Some(segment) = segment
810832
{
811833
self.find_cfg_stripped(&mut err, &segment, module);
812834
}
@@ -1003,10 +1025,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10031025
VisResolutionError::AncestorOnly(span) => {
10041026
self.dcx().create_err(errs::AncestorOnly(span))
10051027
}
1006-
VisResolutionError::FailedToResolve(span, label, suggestion) => self.into_struct_error(
1007-
span,
1008-
ResolutionError::FailedToResolve { segment: None, label, suggestion, module: None },
1009-
),
1028+
VisResolutionError::FailedToResolve(span, segment, label, suggestion, item_type) => {
1029+
self.into_struct_error(
1030+
span,
1031+
ResolutionError::FailedToResolve {
1032+
segment,
1033+
label,
1034+
suggestion,
1035+
module: None,
1036+
item_type,
1037+
},
1038+
)
1039+
}
10101040
VisResolutionError::ExpectedFound(span, path_str, res) => {
10111041
self.dcx().create_err(errs::ExpectedModuleFound { span, res, path_str })
10121042
}

0 commit comments

Comments
 (0)
Please sign in to comment.