Skip to content

Commit b32d0e7

Browse files
committed
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 b32d0e7

File tree

223 files changed

+516
-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

+516
-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
}

compiler/rustc_resolve/src/ident.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use Determinacy::*;
22
use Namespace::*;
33
use rustc_ast::{self as ast, NodeId};
4-
use rustc_errors::ErrorGuaranteed;
4+
use rustc_errors::{Applicability, ErrorGuaranteed};
55
use rustc_hir::def::{DefKind, Namespace, NonMacroAttrKind, PartialRes, PerNS};
66
use rustc_middle::{bug, ty};
77
use rustc_session::lint::BuiltinLintDiag;
@@ -1483,13 +1483,42 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14831483
Some(ModuleOrUniformRoot::Module(self.resolve_self(&mut ctxt, parent)));
14841484
continue;
14851485
}
1486+
let mut item_type = "module";
1487+
if path.len() == 1
1488+
&& let Some(ribs) = ribs
1489+
&& let RibKind::Normal = ribs[ValueNS][ribs[ValueNS].len() - 1].kind
1490+
{
1491+
item_type = "item";
1492+
}
14861493
return PathResult::failed(
14871494
ident,
14881495
false,
14891496
finalize.is_some(),
14901497
module_had_parse_errors,
14911498
module,
1492-
|| ("there are too many leading `super` keywords".to_string(), None),
1499+
|| {
1500+
let mut suggestion = None;
1501+
let label = if path.len() == 1
1502+
&& let Some(ribs) = ribs
1503+
&& let RibKind::Normal = ribs[ValueNS][ribs[ValueNS].len() - 1].kind
1504+
{
1505+
suggestion = Some((
1506+
vec![(ident.span.shrink_to_lo(), "r#".to_string())],
1507+
"if you still want to call your identifier `super`, use the \
1508+
raw identifier format"
1509+
.to_string(),
1510+
Applicability::MachineApplicable,
1511+
));
1512+
"can't use `super` as an identifier"
1513+
} else if segment_idx == 0 {
1514+
"can't use `super` on the crate root, there are no further modules \
1515+
to go \"up\" to"
1516+
} else {
1517+
"there are too many leading `super` keywords"
1518+
};
1519+
(label.to_string(), suggestion)
1520+
},
1521+
item_type,
14931522
);
14941523
}
14951524
if segment_idx == 0 {
@@ -1547,6 +1576,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15471576
};
15481577
(label, None)
15491578
},
1579+
"module",
15501580
);
15511581
}
15521582

@@ -1651,6 +1681,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16511681
);
16521682
(label, None)
16531683
},
1684+
"module",
16541685
);
16551686
}
16561687
}
@@ -1685,6 +1716,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16851716
ident,
16861717
)
16871718
},
1719+
match opt_ns {
1720+
Some(ValueNS) if path.len() == 1 => "item or value",
1721+
Some(ns) if path.len() - 1 == segment_idx => ns.descr(),
1722+
Some(_) | None => "item",
1723+
},
16881724
);
16891725
}
16901726
}

compiler/rustc_resolve/src/imports.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,16 +920,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
920920
suggestion,
921921
module,
922922
error_implied_by_parse_error: _,
923+
item_type,
923924
} => {
924925
if no_ambiguity {
925926
assert!(import.imported_module.get().is_none());
926927
self.report_error(
927928
span,
928929
ResolutionError::FailedToResolve {
929-
segment: Some(segment_name),
930+
segment: segment_name,
930931
label,
931932
suggestion,
932933
module,
934+
item_type,
933935
},
934936
);
935937
}

compiler/rustc_resolve/src/late.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4696,14 +4696,16 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
46964696
module,
46974697
segment_name,
46984698
error_implied_by_parse_error: _,
4699+
item_type,
46994700
} => {
47004701
return Err(respan(
47014702
span,
47024703
ResolutionError::FailedToResolve {
4703-
segment: Some(segment_name),
4704+
segment: segment_name,
47044705
label,
47054706
suggestion,
47064707
module,
4708+
item_type,
47074709
},
47084710
));
47094711
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,11 @@ enum ResolutionError<'ra> {
253253
SelfImportOnlyInImportListWithNonEmptyPrefix,
254254
/// Error E0433: failed to resolve.
255255
FailedToResolve {
256-
segment: Option<Symbol>,
256+
segment: Symbol,
257257
label: String,
258258
suggestion: Option<Suggestion>,
259259
module: Option<ModuleOrUniformRoot<'ra>>,
260+
item_type: &'static str,
260261
},
261262
/// Error E0434: can't capture dynamic environment in a fn item.
262263
CannotCaptureDynamicEnvironmentInFnItem,
@@ -315,7 +316,7 @@ enum ResolutionError<'ra> {
315316
enum VisResolutionError<'a> {
316317
Relative2018(Span, &'a ast::Path),
317318
AncestorOnly(Span),
318-
FailedToResolve(Span, String, Option<Suggestion>),
319+
FailedToResolve(Span, Symbol, String, Option<Suggestion>, &'static str),
319320
ExpectedFound(Span, String, Res),
320321
Indeterminate(Span),
321322
ModuleOnly(Span),
@@ -458,6 +459,7 @@ enum PathResult<'ra> {
458459
/// The segment name of target
459460
segment_name: Symbol,
460461
error_implied_by_parse_error: bool,
462+
item_type: &'static str,
461463
},
462464
}
463465

@@ -469,6 +471,7 @@ impl<'ra> PathResult<'ra> {
469471
error_implied_by_parse_error: bool,
470472
module: Option<ModuleOrUniformRoot<'ra>>,
471473
label_and_suggestion: impl FnOnce() -> (String, Option<Suggestion>),
474+
item_type: &'static str,
472475
) -> PathResult<'ra> {
473476
let (label, suggestion) =
474477
if finalize { label_and_suggestion() } else { (String::new(), None) };
@@ -480,6 +483,7 @@ impl<'ra> PathResult<'ra> {
480483
is_error_from_last_segment,
481484
module,
482485
error_implied_by_parse_error,
486+
item_type,
483487
}
484488
}
485489
}

compiler/rustc_resolve/src/macros.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -856,8 +856,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
856856
),
857857
path_res @ (PathResult::NonModule(..) | PathResult::Failed { .. }) => {
858858
let mut suggestion = None;
859-
let (span, label, module, segment) =
860-
if let PathResult::Failed { span, label, module, segment_name, .. } =
859+
let (span, label, module, segment, item_type) =
860+
if let PathResult::Failed { span, label, module, segment_name, item_type, .. } =
861861
path_res
862862
{
863863
// try to suggest if it's not a macro, maybe a function
@@ -877,26 +877,28 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
877877
Applicability::MaybeIncorrect,
878878
));
879879
}
880-
(span, label, module, segment_name)
880+
(span, label, module, segment_name, item_type)
881881
} else {
882882
(
883883
path_span,
884884
format!(
885-
"partially resolved path in {} {}",
886-
kind.article(),
887-
kind.descr()
885+
"{} is not a macro, but a {}, try to remove `!`",
886+
Segment::names_to_string(&path),
887+
kind.descr(),
888888
),
889889
None,
890890
path.last().map(|segment| segment.ident.name).unwrap(),
891+
"macro",
891892
)
892893
};
893894
self.report_error(
894895
span,
895896
ResolutionError::FailedToResolve {
896-
segment: Some(segment),
897+
segment,
897898
label,
898899
suggestion,
899900
module,
901+
item_type,
900902
},
901903
);
902904
}

src/tools/clippy/tests/ui/crashes/unreachable-array-or-slice.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0433]: failed to resolve: `Self` is only available in impls, traits, and type definitions
1+
error[E0433]: cannot find item `Self` in this scope
22
--> tests/ui/crashes/unreachable-array-or-slice.rs:4:9
33
|
44
LL | let Self::anything_here_kills_it(a, b, ..) = Foo(5, 5, 5, 5);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Regression test for issue #95879.
22

3-
use unresolved_crate::module::Name; //~ ERROR failed to resolve
3+
use unresolved_crate::module::Name; //~ ERROR cannot find item
44

55
/// [Name]
66
pub struct S;

tests/rustdoc-ui/intra-doc/unresolved-import-recovery.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `unresolved_crate`
1+
error[E0433]: cannot find item `unresolved_crate` in the crate root
22
--> $DIR/unresolved-import-recovery.rs:3:5
33
|
44
LL | use unresolved_crate::module::Name;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// This previously triggered an ICE.
22

33
pub(in crate::r#mod) fn main() {}
4-
//~^ ERROR failed to resolve: use of unresolved module or unlinked crate `r#mod`
4+
//~^ ERROR cannot find item `mod`

tests/rustdoc-ui/issues/issue-61732.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `r#mod`
1+
error[E0433]: cannot find item `mod` in this scope
22
--> $DIR/issue-61732.rs:3:15
33
|
44
LL | pub(in crate::r#mod) fn main() {}

tests/ui/asm/naked-invalid-attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn main() {
5555
// Check that the path of an attribute without a name is printed correctly (issue #140082)
5656
#[::a]
5757
//~^ ERROR attribute incompatible with `#[unsafe(naked)]`
58-
//~| ERROR failed to resolve: use of unresolved module or unlinked crate `a`
58+
//~| ERROR cannot find macro `a` in the crate root
5959
#[unsafe(naked)]
6060
extern "C" fn issue_140082() {
6161
naked_asm!("")

tests/ui/asm/naked-invalid-attr.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `a`
1+
error[E0433]: cannot find macro `a` in the crate root
22
--> $DIR/naked-invalid-attr.rs:56:5
33
|
44
LL | #[::a]

tests/ui/attributes/check-builtin-attr-ice.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@
4343

4444
struct Foo {
4545
#[should_panic::skip]
46-
//~^ ERROR failed to resolve
46+
//~^ ERROR cannot find
4747
//~| ERROR `#[should_panic::skip]` only has an effect on functions
4848
pub field: u8,
4949

5050
#[should_panic::a::b::c]
51-
//~^ ERROR failed to resolve
51+
//~^ ERROR cannot find
5252
//~| ERROR `#[should_panic::a::b::c]` only has an effect on functions
5353
pub field2: u8,
5454
}
@@ -57,6 +57,6 @@ fn foo() {}
5757

5858
fn main() {
5959
#[deny::skip]
60-
//~^ ERROR failed to resolve
60+
//~^ ERROR cannot find
6161
foo();
6262
}

tests/ui/attributes/check-builtin-attr-ice.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `should_panic`
1+
error[E0433]: cannot find item `should_panic` in this scope
22
--> $DIR/check-builtin-attr-ice.rs:45:7
33
|
44
LL | #[should_panic::skip]
55
| ^^^^^^^^^^^^ use of unresolved module or unlinked crate `should_panic`
66

7-
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `should_panic`
7+
error[E0433]: cannot find item `should_panic` in this scope
88
--> $DIR/check-builtin-attr-ice.rs:50:7
99
|
1010
LL | #[should_panic::a::b::c]
1111
| ^^^^^^^^^^^^ use of unresolved module or unlinked crate `should_panic`
1212

13-
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `deny`
13+
error[E0433]: cannot find item `deny` in this scope
1414
--> $DIR/check-builtin-attr-ice.rs:59:7
1515
|
1616
LL | #[deny::skip]

0 commit comments

Comments
 (0)