Skip to content

Commit 72978a1

Browse files
committed
Unify wording of resolve error
Remove "failed to resolve" and use the same format we use in other resolution errors "cannot find `name`". ``` error[E0433]: cannot find `nonexistent` in `existent` --> $DIR/custom_attr_multisegment_error.rs:5:13 | LL | #[existent::nonexistent] | ^^^^^^^^^^^ could not find `nonexistent` in `existent` ```
1 parent c018ae5 commit 72978a1

File tree

226 files changed

+510
-429
lines changed

Some content is hidden

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

226 files changed

+510
-429
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,14 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
406406
PathResult::NonModule(partial_res) => {
407407
expected_found_error(partial_res.expect_full_res())
408408
}
409-
PathResult::Failed { span, label, suggestion, .. } => {
410-
Err(VisResolutionError::FailedToResolve(span, label, suggestion))
409+
PathResult::Failed { span, label, suggestion, scope, segment_name, .. } => {
410+
Err(VisResolutionError::FailedToResolve(
411+
span,
412+
segment_name,
413+
label,
414+
suggestion,
415+
scope,
416+
))
411417
}
412418
PathResult::Indeterminate => Err(VisResolutionError::Indeterminate(path.span)),
413419
}

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -788,9 +788,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
788788
ResolutionError::SelfImportOnlyInImportListWithNonEmptyPrefix => {
789789
self.dcx().create_err(errs::SelfImportOnlyInImportListWithNonEmptyPrefix { span })
790790
}
791-
ResolutionError::FailedToResolve { segment, label, suggestion, module } => {
792-
let mut err =
793-
struct_span_code_err!(self.dcx(), span, E0433, "failed to resolve: {label}");
791+
ResolutionError::FailedToResolve { segment, label, suggestion, module, scope } => {
792+
let mut err = struct_span_code_err!(
793+
self.dcx(),
794+
span,
795+
E0433,
796+
"cannot find `{segment}` in {scope}"
797+
);
794798
err.span_label(span, label);
795799

796800
if let Some((suggestions, msg, applicability)) = suggestion {
@@ -801,13 +805,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
801805
err.multipart_suggestion(msg, suggestions, applicability);
802806
}
803807

804-
if let Some(segment) = segment {
805-
let module = match module {
806-
Some(ModuleOrUniformRoot::Module(m)) if let Some(id) = m.opt_def_id() => id,
807-
_ => CRATE_DEF_ID.to_def_id(),
808-
};
809-
self.find_cfg_stripped(&mut err, &segment, module);
810-
}
808+
let module = match module {
809+
Some(ModuleOrUniformRoot::Module(m)) if let Some(id) = m.opt_def_id() => id,
810+
_ => CRATE_DEF_ID.to_def_id(),
811+
};
812+
self.find_cfg_stripped(&mut err, &segment, module);
811813

812814
err
813815
}
@@ -1001,10 +1003,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10011003
VisResolutionError::AncestorOnly(span) => {
10021004
self.dcx().create_err(errs::AncestorOnly(span))
10031005
}
1004-
VisResolutionError::FailedToResolve(span, label, suggestion) => self.into_struct_error(
1005-
span,
1006-
ResolutionError::FailedToResolve { segment: None, label, suggestion, module: None },
1007-
),
1006+
VisResolutionError::FailedToResolve(span, segment, label, suggestion, scope) => self
1007+
.into_struct_error(
1008+
span,
1009+
ResolutionError::FailedToResolve {
1010+
segment,
1011+
label,
1012+
suggestion,
1013+
module: None,
1014+
scope,
1015+
},
1016+
),
10081017
VisResolutionError::ExpectedFound(span, path_str, res) => {
10091018
self.dcx().create_err(errs::ExpectedModuleFound { span, res, path_str })
10101019
}

compiler/rustc_resolve/src/ident.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15131513

15141514
allow_super &= ns == TypeNS && (name == kw::SelfLower || name == kw::Super);
15151515

1516+
let scope = match &path[..segment_idx] {
1517+
[.., prev] => {
1518+
if prev.ident.name == kw::PathRoot {
1519+
format!("the crate root")
1520+
} else {
1521+
format!("`{}`", prev.ident)
1522+
}
1523+
}
1524+
_ => format!("this scope"),
1525+
};
15161526
if ns == TypeNS {
15171527
if allow_super && name == kw::Super {
15181528
let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
@@ -1536,6 +1546,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15361546
finalize.is_some(),
15371547
module_had_parse_errors,
15381548
module,
1549+
scope,
15391550
|| ("there are too many leading `super` keywords".to_string(), None),
15401551
);
15411552
}
@@ -1582,6 +1593,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15821593
finalize.is_some(),
15831594
module_had_parse_errors,
15841595
module,
1596+
scope,
15851597
|| {
15861598
let name_str = if name == kw::PathRoot {
15871599
"crate root".to_string()
@@ -1705,6 +1717,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17051717
finalize.is_some(),
17061718
module_had_parse_errors,
17071719
module,
1720+
scope,
17081721
|| {
17091722
let label = format!(
17101723
"`{ident}` is {} {}, not a module",
@@ -1735,6 +1748,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17351748
finalize.is_some(),
17361749
module_had_parse_errors,
17371750
module,
1751+
scope,
17381752
|| {
17391753
this.get_mut().report_path_resolution_error(
17401754
path,

compiler/rustc_resolve/src/imports.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,16 +993,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
993993
suggestion,
994994
module,
995995
error_implied_by_parse_error: _,
996+
scope,
996997
} => {
997998
if no_ambiguity {
998999
assert!(import.imported_module.get().is_none());
9991000
self.report_error(
10001001
span,
10011002
ResolutionError::FailedToResolve {
1002-
segment: Some(segment_name),
1003+
segment: segment_name,
10031004
label,
10041005
suggestion,
10051006
module,
1007+
scope,
10061008
},
10071009
);
10081010
}

compiler/rustc_resolve/src/late.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4599,14 +4599,16 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
45994599
module,
46004600
segment_name,
46014601
error_implied_by_parse_error: _,
4602+
scope,
46024603
} => {
46034604
return Err(respan(
46044605
span,
46054606
ResolutionError::FailedToResolve {
4606-
segment: Some(segment_name),
4607+
segment: segment_name,
46074608
label,
46084609
suggestion,
46094610
module,
4611+
scope,
46104612
},
46114613
));
46124614
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,11 @@ enum ResolutionError<'ra> {
269269
SelfImportOnlyInImportListWithNonEmptyPrefix,
270270
/// Error E0433: failed to resolve.
271271
FailedToResolve {
272-
segment: Option<Symbol>,
272+
segment: Symbol,
273273
label: String,
274274
suggestion: Option<Suggestion>,
275275
module: Option<ModuleOrUniformRoot<'ra>>,
276+
scope: String,
276277
},
277278
/// Error E0434: can't capture dynamic environment in a fn item.
278279
CannotCaptureDynamicEnvironmentInFnItem,
@@ -331,7 +332,7 @@ enum ResolutionError<'ra> {
331332
enum VisResolutionError<'a> {
332333
Relative2018(Span, &'a ast::Path),
333334
AncestorOnly(Span),
334-
FailedToResolve(Span, String, Option<Suggestion>),
335+
FailedToResolve(Span, Symbol, String, Option<Suggestion>, String),
335336
ExpectedFound(Span, String, Res),
336337
Indeterminate(Span),
337338
ModuleOnly(Span),
@@ -476,6 +477,7 @@ enum PathResult<'ra> {
476477
/// The segment name of target
477478
segment_name: Symbol,
478479
error_implied_by_parse_error: bool,
480+
scope: String,
479481
},
480482
}
481483

@@ -486,6 +488,7 @@ impl<'ra> PathResult<'ra> {
486488
finalize: bool,
487489
error_implied_by_parse_error: bool,
488490
module: Option<ModuleOrUniformRoot<'ra>>,
491+
scope: String,
489492
label_and_suggestion: impl FnOnce() -> (String, Option<Suggestion>),
490493
) -> PathResult<'ra> {
491494
let (label, suggestion) =
@@ -498,6 +501,7 @@ impl<'ra> PathResult<'ra> {
498501
is_error_from_last_segment,
499502
module,
500503
error_implied_by_parse_error,
504+
scope,
501505
}
502506
}
503507
}

compiler/rustc_resolve/src/macros.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
899899
),
900900
path_res @ (PathResult::NonModule(..) | PathResult::Failed { .. }) => {
901901
let mut suggestion = None;
902+
let scope = match path_res {
903+
PathResult::Failed { ref scope, .. } => scope.clone(),
904+
PathResult::NonModule(partial_res) => {
905+
match &path[..partial_res.unresolved_segments()] {
906+
[.., prev] => format!("`{}`", prev.ident),
907+
_ => "this scope".to_string(),
908+
}
909+
}
910+
_ => "this scope".to_string(),
911+
};
902912
let (span, label, module, segment) =
903913
if let PathResult::Failed { span, label, module, segment_name, .. } =
904914
path_res
@@ -937,10 +947,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
937947
self.report_error(
938948
span,
939949
ResolutionError::FailedToResolve {
940-
segment: Some(segment),
950+
segment,
941951
label,
942952
suggestion,
943953
module,
954+
scope,
944955
},
945956
);
946957
}
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 `mod` in `crate`

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 `mod` in `crate`
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 `a` in the crate root
5959
#[unsafe(naked)]
6060
extern "C" fn issue_140082() {
6161
naked_asm!("")

0 commit comments

Comments
 (0)