Skip to content

Commit f6ce33d

Browse files
committed
review comments
1 parent 72978a1 commit f6ce33d

File tree

124 files changed

+312
-274
lines changed

Some content is hidden

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

124 files changed

+312
-274
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -406,15 +406,15 @@ 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, scope, segment_name, .. } => {
410-
Err(VisResolutionError::FailedToResolve(
411-
span,
412-
segment_name,
413-
label,
414-
suggestion,
415-
scope,
416-
))
417-
}
409+
PathResult::Failed {
410+
span, label, suggestion, message, segment_name, ..
411+
} => Err(VisResolutionError::FailedToResolve(
412+
span,
413+
segment_name,
414+
label,
415+
suggestion,
416+
message,
417+
)),
418418
PathResult::Indeterminate => Err(VisResolutionError::Indeterminate(path.span)),
419419
}
420420
}

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -788,13 +788,8 @@ 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, scope } => {
792-
let mut err = struct_span_code_err!(
793-
self.dcx(),
794-
span,
795-
E0433,
796-
"cannot find `{segment}` in {scope}"
797-
);
791+
ResolutionError::FailedToResolve { segment, label, suggestion, module, message } => {
792+
let mut err = struct_span_code_err!(self.dcx(), span, E0433, "{message}");
798793
err.span_label(span, label);
799794

800795
if let Some((suggestions, msg, applicability)) = suggestion {
@@ -1003,15 +998,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1003998
VisResolutionError::AncestorOnly(span) => {
1004999
self.dcx().create_err(errs::AncestorOnly(span))
10051000
}
1006-
VisResolutionError::FailedToResolve(span, segment, label, suggestion, scope) => self
1001+
VisResolutionError::FailedToResolve(span, segment, label, suggestion, message) => self
10071002
.into_struct_error(
10081003
span,
10091004
ResolutionError::FailedToResolve {
10101005
segment,
10111006
label,
10121007
suggestion,
10131008
module: None,
1014-
scope,
1009+
message,
10151010
},
10161011
),
10171012
VisResolutionError::ExpectedFound(span, path_str, res) => {
@@ -2292,13 +2287,25 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22922287
module: Option<ModuleOrUniformRoot<'ra>>,
22932288
failed_segment_idx: usize,
22942289
ident: Ident,
2295-
) -> (String, Option<Suggestion>) {
2290+
) -> (String, String, Option<Suggestion>) {
22962291
let is_last = failed_segment_idx == path.len() - 1;
22972292
let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
22982293
let module_res = match module {
22992294
Some(ModuleOrUniformRoot::Module(module)) => module.res(),
23002295
_ => None,
23012296
};
2297+
let scope = match &path[..failed_segment_idx] {
2298+
[.., prev] => {
2299+
if prev.ident.name == kw::PathRoot {
2300+
format!("the crate root")
2301+
} else {
2302+
format!("`{}`", prev.ident)
2303+
}
2304+
}
2305+
_ => format!("this scope"),
2306+
};
2307+
let message = format!("cannot find `{ident}` in {scope}");
2308+
23022309
if module_res == self.graph_root.res() {
23032310
let is_mod = |res| matches!(res, Res::Def(DefKind::Mod, _));
23042311
let mut candidates = self.lookup_import_candidates(ident, TypeNS, parent_scope, is_mod);
@@ -2316,6 +2323,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23162323
Path { segments, span: Span::default(), tokens: None }
23172324
};
23182325
(
2326+
message,
23192327
String::from("unresolved import"),
23202328
Some((
23212329
vec![(ident.span, pprust::path_to_string(&path))],
@@ -2325,6 +2333,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23252333
)
23262334
} else if ident.name == sym::core {
23272335
(
2336+
message,
23282337
format!("you might be missing crate `{ident}`"),
23292338
Some((
23302339
vec![(ident.span, "std".to_string())],
@@ -2333,9 +2342,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23332342
)),
23342343
)
23352344
} else if ident.name == kw::Underscore {
2336-
(format!("`_` is not a valid crate or module name"), None)
2345+
(
2346+
"invalid crate or module name `_`".to_string(),
2347+
"`_` is not a valid crate or module name".to_string(),
2348+
None,
2349+
)
23372350
} else if self.tcx.sess.is_rust_2015() {
23382351
(
2352+
format!("cannot find module or crate `{ident}` in {scope}"),
23392353
format!("use of unresolved module or unlinked crate `{ident}`"),
23402354
Some((
23412355
vec![(
@@ -2344,8 +2358,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23442358
)],
23452359
if was_invoked_from_cargo() {
23462360
format!(
2347-
"if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
2348-
to add it to your `Cargo.toml` and import it in your code",
2361+
"if you wanted to use a crate named `{ident}`, use `cargo add \
2362+
{ident}` to add it to your `Cargo.toml` and import it in your \
2363+
code",
23492364
)
23502365
} else {
23512366
format!(
@@ -2357,7 +2372,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23572372
)),
23582373
)
23592374
} else {
2360-
(format!("could not find `{ident}` in the crate root"), None)
2375+
(message, format!("could not find `{ident}` in the crate root"), None)
23612376
}
23622377
} else if failed_segment_idx > 0 {
23632378
let parent = path[failed_segment_idx - 1].ident.name;
@@ -2423,15 +2438,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24232438
);
24242439
};
24252440
}
2426-
(msg, None)
2441+
(message, msg, None)
24272442
} else if ident.name == kw::SelfUpper {
24282443
// As mentioned above, `opt_ns` being `None` indicates a module path in import.
24292444
// We can use this to improve a confusing error for, e.g. `use Self::Variant` in an
24302445
// impl
24312446
if opt_ns.is_none() {
2432-
("`Self` cannot be used in imports".to_string(), None)
2447+
(message, "`Self` cannot be used in imports".to_string(), None)
24332448
} else {
24342449
(
2450+
message,
24352451
"`Self` is only available in impls, traits, and type definitions".to_string(),
24362452
None,
24372453
)
@@ -2485,7 +2501,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24852501
)
24862502
});
24872503

2488-
(format!("use of undeclared type `{ident}`"), suggestion)
2504+
(message, format!("use of undeclared type `{ident}`"), suggestion)
24892505
} else {
24902506
let mut suggestion = None;
24912507
if ident.name == sym::alloc {
@@ -2517,7 +2533,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
25172533
ignore_import,
25182534
) {
25192535
let descr = binding.res().descr();
2520-
(format!("{descr} `{ident}` is not a crate or module"), suggestion)
2536+
let message = format!("cannot find module or crate `{ident}` in {scope}");
2537+
(message, format!("{descr} `{ident}` is not a crate or module"), suggestion)
25212538
} else {
25222539
let suggestion = if suggestion.is_some() {
25232540
suggestion
@@ -2539,7 +2556,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
25392556
Applicability::MaybeIncorrect,
25402557
))
25412558
};
2542-
(format!("use of unresolved module or unlinked crate `{ident}`"), suggestion)
2559+
let message = format!("cannot find module or crate `{ident}` in {scope}");
2560+
(
2561+
message,
2562+
format!("use of unresolved module or unlinked crate `{ident}`"),
2563+
suggestion,
2564+
)
25432565
}
25442566
}
25452567
}

compiler/rustc_resolve/src/ident.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,16 +1513,6 @@ 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-
};
15261516
if ns == TypeNS {
15271517
if allow_super && name == kw::Super {
15281518
let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
@@ -1546,8 +1536,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15461536
finalize.is_some(),
15471537
module_had_parse_errors,
15481538
module,
1549-
scope,
1550-
|| ("there are too many leading `super` keywords".to_string(), None),
1539+
|| {
1540+
(
1541+
"too many leading `super` keywords".to_string(),
1542+
"there are too many leading `super` keywords".to_string(),
1543+
None,
1544+
)
1545+
},
15511546
);
15521547
}
15531548
if segment_idx == 0 {
@@ -1593,7 +1588,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15931588
finalize.is_some(),
15941589
module_had_parse_errors,
15951590
module,
1596-
scope,
15971591
|| {
15981592
let name_str = if name == kw::PathRoot {
15991593
"crate root".to_string()
@@ -1605,7 +1599,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16051599
} else {
16061600
format!("{name_str} in paths can only be used in start position")
16071601
};
1608-
(label, None)
1602+
(label.clone(), label, None)
16091603
},
16101604
);
16111605
}
@@ -1717,14 +1711,26 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17171711
finalize.is_some(),
17181712
module_had_parse_errors,
17191713
module,
1720-
scope,
17211714
|| {
17221715
let label = format!(
17231716
"`{ident}` is {} {}, not a module",
17241717
res.article(),
17251718
res.descr()
17261719
);
1727-
(label, None)
1720+
let scope = match &path[..segment_idx] {
1721+
[.., prev] => {
1722+
if prev.ident.name == kw::PathRoot {
1723+
format!("the crate root")
1724+
} else {
1725+
format!("`{}`", prev.ident)
1726+
}
1727+
}
1728+
_ => format!("this scope"),
1729+
};
1730+
// FIXME: reword, as the reason we expected a module is because of
1731+
// the following path segment.
1732+
let message = format!("cannot find module `{ident}` in {scope}");
1733+
(message, label, None)
17281734
},
17291735
);
17301736
}
@@ -1748,7 +1754,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17481754
finalize.is_some(),
17491755
module_had_parse_errors,
17501756
module,
1751-
scope,
17521757
|| {
17531758
this.get_mut().report_path_resolution_error(
17541759
path,

compiler/rustc_resolve/src/imports.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
993993
suggestion,
994994
module,
995995
error_implied_by_parse_error: _,
996-
scope,
996+
message,
997997
} => {
998998
if no_ambiguity {
999999
assert!(import.imported_module.get().is_none());
@@ -1004,7 +1004,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10041004
label,
10051005
suggestion,
10061006
module,
1007-
scope,
1007+
message,
10081008
},
10091009
);
10101010
}

compiler/rustc_resolve/src/late.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4599,7 +4599,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
45994599
module,
46004600
segment_name,
46014601
error_implied_by_parse_error: _,
4602-
scope,
4602+
message,
46034603
} => {
46044604
return Err(respan(
46054605
span,
@@ -4608,7 +4608,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
46084608
label,
46094609
suggestion,
46104610
module,
4611-
scope,
4611+
message,
46124612
},
46134613
));
46144614
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ enum ResolutionError<'ra> {
273273
label: String,
274274
suggestion: Option<Suggestion>,
275275
module: Option<ModuleOrUniformRoot<'ra>>,
276-
scope: String,
276+
message: String,
277277
},
278278
/// Error E0434: can't capture dynamic environment in a fn item.
279279
CannotCaptureDynamicEnvironmentInFnItem,
@@ -477,7 +477,7 @@ enum PathResult<'ra> {
477477
/// The segment name of target
478478
segment_name: Symbol,
479479
error_implied_by_parse_error: bool,
480-
scope: String,
480+
message: String,
481481
},
482482
}
483483

@@ -488,11 +488,14 @@ impl<'ra> PathResult<'ra> {
488488
finalize: bool,
489489
error_implied_by_parse_error: bool,
490490
module: Option<ModuleOrUniformRoot<'ra>>,
491-
scope: String,
492-
label_and_suggestion: impl FnOnce() -> (String, Option<Suggestion>),
491+
label_and_suggestion: impl FnOnce() -> (String, String, Option<Suggestion>),
493492
) -> PathResult<'ra> {
494-
let (label, suggestion) =
495-
if finalize { label_and_suggestion() } else { (String::new(), None) };
493+
let (message, label, suggestion) = if finalize {
494+
label_and_suggestion()
495+
} else {
496+
// FIXME: this output isn't actually present in the test suite.
497+
(format!("cannot find `{ident}` in this scope"), String::new(), None)
498+
};
496499
PathResult::Failed {
497500
span: ident.span,
498501
segment_name: ident.name,
@@ -501,7 +504,7 @@ impl<'ra> PathResult<'ra> {
501504
is_error_from_last_segment,
502505
module,
503506
error_implied_by_parse_error,
504-
scope,
507+
message,
505508
}
506509
}
507510
}

0 commit comments

Comments
 (0)