Skip to content

Commit 2abb31d

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 2a1c384 commit 2abb31d

File tree

208 files changed

+541
-407
lines changed

Some content is hidden

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

208 files changed

+541
-407
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,15 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
307307
PathResult::NonModule(partial_res) => {
308308
expected_found_error(partial_res.expect_full_res())
309309
}
310-
PathResult::Failed { span, label, suggestion, .. } => {
311-
Err(VisResolutionError::FailedToResolve(span, label, suggestion))
312-
}
310+
PathResult::Failed {
311+
span, label, suggestion, segment_name, item_type, ..
312+
} => Err(VisResolutionError::FailedToResolve(
313+
span,
314+
segment_name,
315+
label,
316+
suggestion,
317+
item_type,
318+
)),
313319
PathResult::Indeterminate => Err(VisResolutionError::Indeterminate(path.span)),
314320
}
315321
}

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -796,9 +796,32 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
796796
ResolutionError::SelfImportOnlyInImportListWithNonEmptyPrefix => {
797797
self.dcx().create_err(errs::SelfImportOnlyInImportListWithNonEmptyPrefix { span })
798798
}
799-
ResolutionError::FailedToResolve { segment, label, suggestion, module } => {
800-
let mut err =
801-
struct_span_code_err!(self.dcx(), span, E0433, "failed to resolve: {label}");
799+
ResolutionError::FailedToResolve { segment, label, suggestion, module, item_type } => {
800+
let mut err = struct_span_code_err!(
801+
self.dcx(),
802+
span,
803+
E0433,
804+
"cannot find {item_type} `{segment}` in {}",
805+
match module {
806+
Some(ModuleOrUniformRoot::CurrentScope) | None => "this scope".to_string(),
807+
Some(ModuleOrUniformRoot::Module(module)) => {
808+
match module.kind {
809+
ModuleKind::Def(_, _, name) if name == kw::Empty => {
810+
"the crate root".to_string()
811+
}
812+
ModuleKind::Def(kind, def_id, name) => {
813+
format!("{} `{name}`", kind.descr(def_id))
814+
}
815+
ModuleKind::Block => "this scope".to_string(),
816+
}
817+
}
818+
Some(ModuleOrUniformRoot::CrateRootAndExternPrelude) => {
819+
"the crate root or the list of imported crates".to_string()
820+
}
821+
Some(ModuleOrUniformRoot::ExternPrelude) =>
822+
"the list of imported crates".to_string(),
823+
},
824+
);
802825
err.span_label(span, label);
803826

804827
if let Some((suggestions, msg, applicability)) = suggestion {
@@ -811,7 +834,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
811834

812835
if let Some(ModuleOrUniformRoot::Module(module)) = module
813836
&& let Some(module) = module.opt_def_id()
814-
&& let Some(segment) = segment
815837
{
816838
self.find_cfg_stripped(&mut err, &segment, module);
817839
}
@@ -998,10 +1020,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
9981020
VisResolutionError::AncestorOnly(span) => {
9991021
self.dcx().create_err(errs::AncestorOnly(span))
10001022
}
1001-
VisResolutionError::FailedToResolve(span, label, suggestion) => self.into_struct_error(
1002-
span,
1003-
ResolutionError::FailedToResolve { segment: None, label, suggestion, module: None },
1004-
),
1023+
VisResolutionError::FailedToResolve(span, segment, label, suggestion, item_type) => {
1024+
self.into_struct_error(
1025+
span,
1026+
ResolutionError::FailedToResolve {
1027+
segment,
1028+
label,
1029+
suggestion,
1030+
module: None,
1031+
item_type,
1032+
},
1033+
)
1034+
}
10051035
VisResolutionError::ExpectedFound(span, path_str, res) => {
10061036
self.dcx().create_err(errs::ExpectedModuleFound { span, res, path_str })
10071037
}

compiler/rustc_resolve/src/ident.rs

Lines changed: 81 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_ast::{self as ast, NodeId};
2-
use rustc_errors::ErrorGuaranteed;
2+
use rustc_errors::{Applicability, ErrorGuaranteed};
33
use rustc_hir::def::{DefKind, Namespace, NonMacroAttrKind, PartialRes, PerNS};
44
use rustc_middle::bug;
55
use rustc_middle::ty;
@@ -1483,9 +1483,42 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14831483
continue;
14841484
}
14851485
}
1486-
return PathResult::failed(ident, false, finalize.is_some(), module, || {
1487-
("there are too many leading `super` keywords".to_string(), None)
1488-
});
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+
}
1493+
return PathResult::failed(
1494+
ident,
1495+
false,
1496+
finalize.is_some(),
1497+
module,
1498+
|| {
1499+
let mut suggestion = None;
1500+
let label = if path.len() == 1
1501+
&& let Some(ribs) = ribs
1502+
&& let RibKind::Normal = ribs[ValueNS][ribs[ValueNS].len() - 1].kind
1503+
{
1504+
suggestion = Some((
1505+
vec![(ident.span.shrink_to_lo(), "r#".to_string())],
1506+
"if you still want to call your identifier `super`, use the \
1507+
raw identifier format"
1508+
.to_string(),
1509+
Applicability::MachineApplicable,
1510+
));
1511+
"can't use `super` as an identifier"
1512+
} else if segment_idx == 0 {
1513+
"can't use `super` on the crate root, there are no further modules \
1514+
to go \"up\" to"
1515+
} else {
1516+
"there are too many leading `super` keywords"
1517+
};
1518+
(label.to_string(), suggestion)
1519+
},
1520+
item_type,
1521+
);
14891522
}
14901523
if segment_idx == 0 {
14911524
if name == kw::SelfLower {
@@ -1517,19 +1550,26 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15171550

15181551
// Report special messages for path segment keywords in wrong positions.
15191552
if ident.is_path_segment_keyword() && segment_idx != 0 {
1520-
return PathResult::failed(ident, false, finalize.is_some(), module, || {
1521-
let name_str = if name == kw::PathRoot {
1522-
"crate root".to_string()
1523-
} else {
1524-
format!("`{name}`")
1525-
};
1526-
let label = if segment_idx == 1 && path[0].ident.name == kw::PathRoot {
1527-
format!("global paths cannot start with {name_str}")
1528-
} else {
1529-
format!("{name_str} in paths can only be used in start position")
1530-
};
1531-
(label, None)
1532-
});
1553+
return PathResult::failed(
1554+
ident,
1555+
false,
1556+
finalize.is_some(),
1557+
module,
1558+
|| {
1559+
let name_str = if name == kw::PathRoot {
1560+
"crate root".to_string()
1561+
} else {
1562+
format!("`{name}`")
1563+
};
1564+
let label = if segment_idx == 1 && path[0].ident.name == kw::PathRoot {
1565+
format!("global paths cannot start with {name_str}")
1566+
} else {
1567+
format!("{name_str} in paths can only be used in start position")
1568+
};
1569+
(label, None)
1570+
},
1571+
"module",
1572+
);
15331573
}
15341574

15351575
let binding = if let Some(module) = module {
@@ -1625,6 +1665,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16251665
);
16261666
(label, None)
16271667
},
1668+
"module",
16281669
);
16291670
}
16301671
}
@@ -1639,18 +1680,29 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16391680
}
16401681
}
16411682

1642-
return PathResult::failed(ident, is_last, finalize.is_some(), module, || {
1643-
self.report_path_resolution_error(
1644-
path,
1645-
opt_ns,
1646-
parent_scope,
1647-
ribs,
1648-
ignore_binding,
1649-
module,
1650-
segment_idx,
1651-
ident,
1652-
)
1653-
});
1683+
return PathResult::failed(
1684+
ident,
1685+
is_last,
1686+
finalize.is_some(),
1687+
module,
1688+
|| {
1689+
self.report_path_resolution_error(
1690+
path,
1691+
opt_ns,
1692+
parent_scope,
1693+
ribs,
1694+
ignore_binding,
1695+
module,
1696+
segment_idx,
1697+
ident,
1698+
)
1699+
},
1700+
match opt_ns {
1701+
Some(ValueNS) if path.len() == 1 => "item or value",
1702+
Some(ns) if path.len() - 1 == segment_idx => ns.descr(),
1703+
Some(_) | None => "item",
1704+
},
1705+
);
16541706
}
16551707
}
16561708
}

compiler/rustc_resolve/src/imports.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,16 +900,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
900900
label,
901901
suggestion,
902902
module,
903+
item_type,
903904
} => {
904905
if no_ambiguity {
905906
assert!(import.imported_module.get().is_none());
906907
self.report_error(
907908
span,
908909
ResolutionError::FailedToResolve {
909-
segment: Some(segment_name),
910+
segment: segment_name,
910911
label,
911912
suggestion,
912913
module,
914+
item_type,
913915
},
914916
);
915917
}

compiler/rustc_resolve/src/late.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4312,14 +4312,16 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
43124312
suggestion,
43134313
module,
43144314
segment_name,
4315+
item_type,
43154316
} => {
43164317
return Err(respan(
43174318
span,
43184319
ResolutionError::FailedToResolve {
4319-
segment: Some(segment_name),
4320+
segment: segment_name,
43204321
label,
43214322
suggestion,
43224323
module,
4324+
item_type,
43234325
},
43244326
));
43254327
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,11 @@ enum ResolutionError<'a> {
228228
SelfImportOnlyInImportListWithNonEmptyPrefix,
229229
/// Error E0433: failed to resolve.
230230
FailedToResolve {
231-
segment: Option<Symbol>,
231+
segment: Symbol,
232232
label: String,
233233
suggestion: Option<Suggestion>,
234234
module: Option<ModuleOrUniformRoot<'a>>,
235+
item_type: &'static str,
235236
},
236237
/// Error E0434: can't capture dynamic environment in a fn item.
237238
CannotCaptureDynamicEnvironmentInFnItem,
@@ -288,7 +289,7 @@ enum ResolutionError<'a> {
288289
enum VisResolutionError<'a> {
289290
Relative2018(Span, &'a ast::Path),
290291
AncestorOnly(Span),
291-
FailedToResolve(Span, String, Option<Suggestion>),
292+
FailedToResolve(Span, Symbol, String, Option<Suggestion>, &'static str),
292293
ExpectedFound(Span, String, Res),
293294
Indeterminate(Span),
294295
ModuleOnly(Span),
@@ -430,6 +431,7 @@ enum PathResult<'a> {
430431
module: Option<ModuleOrUniformRoot<'a>>,
431432
/// The segment name of target
432433
segment_name: Symbol,
434+
item_type: &'static str,
433435
},
434436
}
435437

@@ -440,6 +442,7 @@ impl<'a> PathResult<'a> {
440442
finalize: bool,
441443
module: Option<ModuleOrUniformRoot<'a>>,
442444
label_and_suggestion: impl FnOnce() -> (String, Option<Suggestion>),
445+
item_type: &'static str,
443446
) -> PathResult<'a> {
444447
let (label, suggestion) =
445448
if finalize { label_and_suggestion() } else { (String::new(), None) };
@@ -450,6 +453,7 @@ impl<'a> PathResult<'a> {
450453
suggestion,
451454
is_error_from_last_segment,
452455
module,
456+
item_type,
453457
}
454458
}
455459
}

compiler/rustc_resolve/src/macros.rs

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -862,44 +862,54 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
862862
),
863863
path_res @ (PathResult::NonModule(..) | PathResult::Failed { .. }) => {
864864
let mut suggestion = None;
865-
let (span, label, module) =
866-
if let PathResult::Failed { span, label, module, .. } = path_res {
867-
// try to suggest if it's not a macro, maybe a function
868-
if let PathResult::NonModule(partial_res) =
869-
self.maybe_resolve_path(&path, Some(ValueNS), &parent_scope)
870-
&& partial_res.unresolved_segments() == 0
871-
{
872-
let sm = self.tcx.sess.source_map();
873-
let exclamation_span = sm.next_point(span);
874-
suggestion = Some((
875-
vec![(exclamation_span, "".to_string())],
876-
format!(
877-
"{} is not a macro, but a {}, try to remove `!`",
878-
Segment::names_to_string(&path),
879-
partial_res.base_res().descr()
880-
),
881-
Applicability::MaybeIncorrect,
882-
));
883-
}
884-
(span, label, module)
885-
} else {
886-
(
887-
path_span,
865+
let (span, label, module, segment, item_type) = if let PathResult::Failed {
866+
span,
867+
label,
868+
module,
869+
segment_name,
870+
item_type,
871+
..
872+
} = path_res
873+
{
874+
// try to suggest if it's not a macro, maybe a function
875+
if let PathResult::NonModule(partial_res) =
876+
self.maybe_resolve_path(&path, Some(ValueNS), &parent_scope)
877+
&& partial_res.unresolved_segments() == 0
878+
{
879+
let sm = self.tcx.sess.source_map();
880+
let exclamation_span = sm.next_point(span);
881+
suggestion = Some((
882+
vec![(exclamation_span, "".to_string())],
888883
format!(
889-
"partially resolved path in {} {}",
890-
kind.article(),
891-
kind.descr()
884+
"{} is not a macro, but a {}, try to remove `!`",
885+
Segment::names_to_string(&path),
886+
partial_res.base_res().descr()
892887
),
893-
None,
894-
)
895-
};
888+
Applicability::MaybeIncorrect,
889+
));
890+
}
891+
(span, label, module, segment_name, item_type)
892+
} else {
893+
(
894+
path_span,
895+
format!(
896+
"partially resolved path in {} {}",
897+
kind.article(),
898+
kind.descr()
899+
),
900+
None,
901+
path.last().map(|segment| segment.ident.name).unwrap(),
902+
"macro",
903+
)
904+
};
896905
self.report_error(
897906
span,
898907
ResolutionError::FailedToResolve {
899-
segment: path.last().map(|segment| segment.ident.name),
908+
segment,
900909
label,
901910
suggestion,
902911
module,
912+
item_type,
903913
},
904914
);
905915
}

0 commit comments

Comments
 (0)