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 2abb31d

Browse files
committedJul 23, 2024·
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 2a1c384 commit 2abb31d

File tree

208 files changed

+541
-407
lines changed

Some content is hidden

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

208 files changed

+541
-407
lines changed
 

‎compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,15 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
307307
PathResult::NonModule(partial_res) => {
308308
expected_found_error(partial_res.expect_full_res())
309309
}
310-
PathResult::Failed { span, label, suggestion, .. } => {
311-
Err(VisResolutionError::FailedToResolve(span, label, suggestion))
312-
}
310+
PathResult::Failed {
311+
span, label, suggestion, segment_name, item_type, ..
312+
} => Err(VisResolutionError::FailedToResolve(
313+
span,
314+
segment_name,
315+
label,
316+
suggestion,
317+
item_type,
318+
)),
313319
PathResult::Indeterminate => Err(VisResolutionError::Indeterminate(path.span)),
314320
}
315321
}

‎compiler/rustc_resolve/src/diagnostics.rs

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

804827
if let Some((suggestions, msg, applicability)) = suggestion {
@@ -811,7 +834,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
811834

812835
if let Some(ModuleOrUniformRoot::Module(module)) = module
813836
&& let Some(module) = module.opt_def_id()
814-
&& let Some(segment) = segment
815837
{
816838
self.find_cfg_stripped(&mut err, &segment, module);
817839
}
@@ -998,10 +1020,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
9981020
VisResolutionError::AncestorOnly(span) => {
9991021
self.dcx().create_err(errs::AncestorOnly(span))
10001022
}
1001-
VisResolutionError::FailedToResolve(span, label, suggestion) => self.into_struct_error(
1002-
span,
1003-
ResolutionError::FailedToResolve { segment: None, label, suggestion, module: None },
1004-
),
1023+
VisResolutionError::FailedToResolve(span, segment, label, suggestion, item_type) => {
1024+
self.into_struct_error(
1025+
span,
1026+
ResolutionError::FailedToResolve {
1027+
segment,
1028+
label,
1029+
suggestion,
1030+
module: None,
1031+
item_type,
1032+
},
1033+
)
1034+
}
10051035
VisResolutionError::ExpectedFound(span, path_str, res) => {
10061036
self.dcx().create_err(errs::ExpectedModuleFound { span, res, path_str })
10071037
}

0 commit comments

Comments
 (0)
Please sign in to comment.