Skip to content

Commit fd49d2a

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 350d0ef commit fd49d2a

File tree

226 files changed

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

+508
-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
@@ -1511,6 +1511,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15111511

15121512
allow_super &= ns == TypeNS && (name == kw::SelfLower || name == kw::Super);
15131513

1514+
let scope = match &path[..segment_idx] {
1515+
[.., prev] => {
1516+
if prev.ident.name == kw::PathRoot {
1517+
format!("the crate root")
1518+
} else {
1519+
format!("`{}`", prev.ident)
1520+
}
1521+
}
1522+
_ => format!("this scope"),
1523+
};
15141524
if ns == TypeNS {
15151525
if allow_super && name == kw::Super {
15161526
let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
@@ -1534,6 +1544,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15341544
finalize.is_some(),
15351545
module_had_parse_errors,
15361546
module,
1547+
scope,
15371548
|| ("there are too many leading `super` keywords".to_string(), None),
15381549
);
15391550
}
@@ -1580,6 +1591,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15801591
finalize.is_some(),
15811592
module_had_parse_errors,
15821593
module,
1594+
scope,
15831595
|| {
15841596
let name_str = if name == kw::PathRoot {
15851597
"crate root".to_string()
@@ -1703,6 +1715,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17031715
finalize.is_some(),
17041716
module_had_parse_errors,
17051717
module,
1718+
scope,
17061719
|| {
17071720
let label = format!(
17081721
"`{ident}` is {} {}, not a module",
@@ -1733,6 +1746,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17331746
finalize.is_some(),
17341747
module_had_parse_errors,
17351748
module,
1749+
scope,
17361750
|| {
17371751
this.get_mut().report_path_resolution_error(
17381752
path,

compiler/rustc_resolve/src/imports.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,16 +997,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
997997
suggestion,
998998
module,
999999
error_implied_by_parse_error: _,
1000+
scope,
10001001
} => {
10011002
if no_ambiguity {
10021003
assert!(import.imported_module.get().is_none());
10031004
self.report_error(
10041005
span,
10051006
ResolutionError::FailedToResolve {
1006-
segment: Some(segment_name),
1007+
segment: segment_name,
10071008
label,
10081009
suggestion,
10091010
module,
1011+
scope,
10101012
},
10111013
);
10121014
}

compiler/rustc_resolve/src/late.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4618,14 +4618,16 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
46184618
module,
46194619
segment_name,
46204620
error_implied_by_parse_error: _,
4621+
scope,
46214622
} => {
46224623
return Err(respan(
46234624
span,
46244625
ResolutionError::FailedToResolve {
4625-
segment: Some(segment_name),
4626+
segment: segment_name,
46264627
label,
46274628
suggestion,
46284629
module,
4630+
scope,
46294631
},
46304632
));
46314633
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,11 @@ enum ResolutionError<'ra> {
254254
SelfImportOnlyInImportListWithNonEmptyPrefix,
255255
/// Error E0433: failed to resolve.
256256
FailedToResolve {
257-
segment: Option<Symbol>,
257+
segment: Symbol,
258258
label: String,
259259
suggestion: Option<Suggestion>,
260260
module: Option<ModuleOrUniformRoot<'ra>>,
261+
scope: String,
261262
},
262263
/// Error E0434: can't capture dynamic environment in a fn item.
263264
CannotCaptureDynamicEnvironmentInFnItem,
@@ -316,7 +317,7 @@ enum ResolutionError<'ra> {
316317
enum VisResolutionError<'a> {
317318
Relative2018(Span, &'a ast::Path),
318319
AncestorOnly(Span),
319-
FailedToResolve(Span, String, Option<Suggestion>),
320+
FailedToResolve(Span, Symbol, String, Option<Suggestion>, String),
320321
ExpectedFound(Span, String, Res),
321322
Indeterminate(Span),
322323
ModuleOnly(Span),
@@ -461,6 +462,7 @@ enum PathResult<'ra> {
461462
/// The segment name of target
462463
segment_name: Symbol,
463464
error_implied_by_parse_error: bool,
465+
scope: String,
464466
},
465467
}
466468

@@ -471,6 +473,7 @@ impl<'ra> PathResult<'ra> {
471473
finalize: bool,
472474
error_implied_by_parse_error: bool,
473475
module: Option<ModuleOrUniformRoot<'ra>>,
476+
scope: String,
474477
label_and_suggestion: impl FnOnce() -> (String, Option<Suggestion>),
475478
) -> PathResult<'ra> {
476479
let (label, suggestion) =
@@ -483,6 +486,7 @@ impl<'ra> PathResult<'ra> {
483486
is_error_from_last_segment,
484487
module,
485488
error_implied_by_parse_error,
489+
scope,
486490
}
487491
}
488492
}

compiler/rustc_resolve/src/macros.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
911911
),
912912
path_res @ (PathResult::NonModule(..) | PathResult::Failed { .. }) => {
913913
let mut suggestion = None;
914+
let scope = match path_res {
915+
PathResult::Failed { ref scope, .. } => scope.clone(),
916+
PathResult::NonModule(partial_res) => match &path[..partial_res.unresolved_segments()] {
917+
[.., prev] => format!("`{}`", prev.ident),
918+
_ => "this scope".to_string(),
919+
},
920+
_ => "this scope".to_string(),
921+
};
914922
let (span, label, module, segment) =
915923
if let PathResult::Failed { span, label, module, segment_name, .. } =
916924
path_res
@@ -949,10 +957,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
949957
self.report_error(
950958
span,
951959
ResolutionError::FailedToResolve {
952-
segment: Some(segment),
960+
segment,
953961
label,
954962
suggestion,
955963
module,
964+
scope,
956965
},
957966
);
958967
}
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)