Skip to content

Commit 298dec7

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 30017c3 commit 298dec7

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
@@ -1515,6 +1515,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15151515

15161516
allow_super &= ns == TypeNS && (name == kw::SelfLower || name == kw::Super);
15171517

1518+
let scope = match &path[..segment_idx] {
1519+
[.., prev] => {
1520+
if prev.ident.name == kw::PathRoot {
1521+
format!("the crate root")
1522+
} else {
1523+
format!("`{}`", prev.ident)
1524+
}
1525+
}
1526+
_ => format!("this scope"),
1527+
};
15181528
if ns == TypeNS {
15191529
if allow_super && name == kw::Super {
15201530
let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
@@ -1538,6 +1548,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15381548
finalize.is_some(),
15391549
module_had_parse_errors,
15401550
module,
1551+
scope,
15411552
|| ("there are too many leading `super` keywords".to_string(), None),
15421553
);
15431554
}
@@ -1584,6 +1595,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15841595
finalize.is_some(),
15851596
module_had_parse_errors,
15861597
module,
1598+
scope,
15871599
|| {
15881600
let name_str = if name == kw::PathRoot {
15891601
"crate root".to_string()
@@ -1707,6 +1719,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17071719
finalize.is_some(),
17081720
module_had_parse_errors,
17091721
module,
1722+
scope,
17101723
|| {
17111724
let label = format!(
17121725
"`{ident}` is {} {}, not a module",
@@ -1737,6 +1750,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17371750
finalize.is_some(),
17381751
module_had_parse_errors,
17391752
module,
1753+
scope,
17401754
|| {
17411755
this.get_mut().report_path_resolution_error(
17421756
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
@@ -4606,14 +4606,16 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
46064606
module,
46074607
segment_name,
46084608
error_implied_by_parse_error: _,
4609+
scope,
46094610
} => {
46104611
return Err(respan(
46114612
span,
46124613
ResolutionError::FailedToResolve {
4613-
segment: Some(segment_name),
4614+
segment: segment_name,
46144615
label,
46154616
suggestion,
46164617
module,
4618+
scope,
46174619
},
46184620
));
46194621
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,11 @@ enum ResolutionError<'ra> {
268268
SelfImportOnlyInImportListWithNonEmptyPrefix,
269269
/// Error E0433: failed to resolve.
270270
FailedToResolve {
271-
segment: Option<Symbol>,
271+
segment: Symbol,
272272
label: String,
273273
suggestion: Option<Suggestion>,
274274
module: Option<ModuleOrUniformRoot<'ra>>,
275+
scope: String,
275276
},
276277
/// Error E0434: can't capture dynamic environment in a fn item.
277278
CannotCaptureDynamicEnvironmentInFnItem,
@@ -330,7 +331,7 @@ enum ResolutionError<'ra> {
330331
enum VisResolutionError<'a> {
331332
Relative2018(Span, &'a ast::Path),
332333
AncestorOnly(Span),
333-
FailedToResolve(Span, String, Option<Suggestion>),
334+
FailedToResolve(Span, Symbol, String, Option<Suggestion>, String),
334335
ExpectedFound(Span, String, Res),
335336
Indeterminate(Span),
336337
ModuleOnly(Span),
@@ -475,6 +476,7 @@ enum PathResult<'ra> {
475476
/// The segment name of target
476477
segment_name: Symbol,
477478
error_implied_by_parse_error: bool,
479+
scope: String,
478480
},
479481
}
480482

@@ -485,6 +487,7 @@ impl<'ra> PathResult<'ra> {
485487
finalize: bool,
486488
error_implied_by_parse_error: bool,
487489
module: Option<ModuleOrUniformRoot<'ra>>,
490+
scope: String,
488491
label_and_suggestion: impl FnOnce() -> (String, Option<Suggestion>),
489492
) -> PathResult<'ra> {
490493
let (label, suggestion) =
@@ -497,6 +500,7 @@ impl<'ra> PathResult<'ra> {
497500
is_error_from_last_segment,
498501
module,
499502
error_implied_by_parse_error,
503+
scope,
500504
}
501505
}
502506
}

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)