Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify wording of "failed to resolve" errors with "cannot find" resolution errors #128086

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -307,9 +307,15 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
PathResult::NonModule(partial_res) => { PathResult::NonModule(partial_res) => {
expected_found_error(partial_res.expect_full_res()) expected_found_error(partial_res.expect_full_res())
} }
PathResult::Failed { span, label, suggestion, .. } => { PathResult::Failed {
Err(VisResolutionError::FailedToResolve(span, label, suggestion)) span, label, suggestion, segment_name, item_type, ..
} } => Err(VisResolutionError::FailedToResolve(
span,
segment_name,
label,
suggestion,
item_type,
)),
PathResult::Indeterminate => Err(VisResolutionError::Indeterminate(path.span)), PathResult::Indeterminate => Err(VisResolutionError::Indeterminate(path.span)),
} }
} }
Expand Down
44 changes: 37 additions & 7 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -796,9 +796,32 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
ResolutionError::SelfImportOnlyInImportListWithNonEmptyPrefix => { ResolutionError::SelfImportOnlyInImportListWithNonEmptyPrefix => {
self.dcx().create_err(errs::SelfImportOnlyInImportListWithNonEmptyPrefix { span }) self.dcx().create_err(errs::SelfImportOnlyInImportListWithNonEmptyPrefix { span })
} }
ResolutionError::FailedToResolve { segment, label, suggestion, module } => { ResolutionError::FailedToResolve { segment, label, suggestion, module, item_type } => {
let mut err = let mut err = struct_span_code_err!(
struct_span_code_err!(self.dcx(), span, E0433, "failed to resolve: {label}"); self.dcx(),
span,
E0433,
"cannot find {item_type} `{segment}` in {}",
match module {
Some(ModuleOrUniformRoot::CurrentScope) | None => "this scope".to_string(),
Some(ModuleOrUniformRoot::Module(module)) => {
match module.kind {
ModuleKind::Def(_, _, name) if name == kw::Empty => {
"the crate root".to_string()
}
ModuleKind::Def(kind, def_id, name) => {
format!("{} `{name}`", kind.descr(def_id))
}
ModuleKind::Block => "this scope".to_string(),
}
}
Some(ModuleOrUniformRoot::CrateRootAndExternPrelude) => {
"the crate root or the list of imported crates".to_string()
}
Some(ModuleOrUniformRoot::ExternPrelude) =>
"the list of imported crates".to_string(),
},
);
err.span_label(span, label); err.span_label(span, label);


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


if let Some(ModuleOrUniformRoot::Module(module)) = module if let Some(ModuleOrUniformRoot::Module(module)) = module
&& let Some(module) = module.opt_def_id() && let Some(module) = module.opt_def_id()
&& let Some(segment) = segment
{ {
self.find_cfg_stripped(&mut err, &segment, module); self.find_cfg_stripped(&mut err, &segment, module);
} }
Expand Down Expand Up @@ -998,10 +1020,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
VisResolutionError::AncestorOnly(span) => { VisResolutionError::AncestorOnly(span) => {
self.dcx().create_err(errs::AncestorOnly(span)) self.dcx().create_err(errs::AncestorOnly(span))
} }
VisResolutionError::FailedToResolve(span, label, suggestion) => self.into_struct_error( VisResolutionError::FailedToResolve(span, segment, label, suggestion, item_type) => {
self.into_struct_error(
span, span,
ResolutionError::FailedToResolve { segment: None, label, suggestion, module: None }, ResolutionError::FailedToResolve {
), segment,
label,
suggestion,
module: None,
item_type,
},
)
}
VisResolutionError::ExpectedFound(span, path_str, res) => { VisResolutionError::ExpectedFound(span, path_str, res) => {
self.dcx().create_err(errs::ExpectedModuleFound { span, res, path_str }) self.dcx().create_err(errs::ExpectedModuleFound { span, res, path_str })
} }
Expand Down
68 changes: 60 additions & 8 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc_ast::{self as ast, NodeId}; use rustc_ast::{self as ast, NodeId};
use rustc_errors::ErrorGuaranteed; use rustc_errors::{Applicability, ErrorGuaranteed};
use rustc_hir::def::{DefKind, Namespace, NonMacroAttrKind, PartialRes, PerNS}; use rustc_hir::def::{DefKind, Namespace, NonMacroAttrKind, PartialRes, PerNS};
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::ty; use rustc_middle::ty;
Expand Down Expand Up @@ -1483,9 +1483,42 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
continue; continue;
} }
} }
return PathResult::failed(ident, false, finalize.is_some(), module, || { let mut item_type = "module";
("there are too many leading `super` keywords".to_string(), None) if path.len() == 1
}); && let Some(ribs) = ribs
&& let RibKind::Normal = ribs[ValueNS][ribs[ValueNS].len() - 1].kind
{
item_type = "item";
}
return PathResult::failed(
ident,
false,
finalize.is_some(),
module,
|| {
let mut suggestion = None;
let label = if path.len() == 1
&& let Some(ribs) = ribs
&& let RibKind::Normal = ribs[ValueNS][ribs[ValueNS].len() - 1].kind
{
suggestion = Some((
vec![(ident.span.shrink_to_lo(), "r#".to_string())],
"if you still want to call your identifier `super`, use the \
raw identifier format"
.to_string(),
Applicability::MachineApplicable,
));
"can't use `super` as an identifier"
} else if segment_idx == 0 {
"can't use `super` on the crate root, there are no further modules \
to go \"up\" to"
} else {
"there are too many leading `super` keywords"
};
(label.to_string(), suggestion)
},
item_type,
);
} }
if segment_idx == 0 { if segment_idx == 0 {
if name == kw::SelfLower { if name == kw::SelfLower {
Expand Down Expand Up @@ -1517,7 +1550,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {


// Report special messages for path segment keywords in wrong positions. // Report special messages for path segment keywords in wrong positions.
if ident.is_path_segment_keyword() && segment_idx != 0 { if ident.is_path_segment_keyword() && segment_idx != 0 {
return PathResult::failed(ident, false, finalize.is_some(), module, || { return PathResult::failed(
ident,
false,
finalize.is_some(),
module,
|| {
let name_str = if name == kw::PathRoot { let name_str = if name == kw::PathRoot {
"crate root".to_string() "crate root".to_string()
} else { } else {
Expand All @@ -1529,7 +1567,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
format!("{name_str} in paths can only be used in start position") format!("{name_str} in paths can only be used in start position")
}; };
(label, None) (label, None)
}); },
"module",
);
} }


let binding = if let Some(module) = module { let binding = if let Some(module) = module {
Expand Down Expand Up @@ -1625,6 +1665,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
); );
(label, None) (label, None)
}, },
"module",
); );
} }
} }
Expand All @@ -1639,7 +1680,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
} }
} }


return PathResult::failed(ident, is_last, finalize.is_some(), module, || { return PathResult::failed(
ident,
is_last,
finalize.is_some(),
module,
|| {
self.report_path_resolution_error( self.report_path_resolution_error(
path, path,
opt_ns, opt_ns,
Expand All @@ -1650,7 +1696,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
segment_idx, segment_idx,
ident, ident,
) )
}); },
match opt_ns {
Some(ValueNS) if path.len() == 1 => "item or value",
Some(ns) if path.len() - 1 == segment_idx => ns.descr(),
Some(_) | None => "item",
},
);
} }
} }
} }
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_resolve/src/imports.rs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -900,16 +900,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
label, label,
suggestion, suggestion,
module, module,
item_type,
} => { } => {
if no_ambiguity { if no_ambiguity {
assert!(import.imported_module.get().is_none()); assert!(import.imported_module.get().is_none());
self.report_error( self.report_error(
span, span,
ResolutionError::FailedToResolve { ResolutionError::FailedToResolve {
segment: Some(segment_name), segment: segment_name,
label, label,
suggestion, suggestion,
module, module,
item_type,
}, },
); );
} }
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4312,14 +4312,16 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
suggestion, suggestion,
module, module,
segment_name, segment_name,
item_type,
} => { } => {
return Err(respan( return Err(respan(
span, span,
ResolutionError::FailedToResolve { ResolutionError::FailedToResolve {
segment: Some(segment_name), segment: segment_name,
label, label,
suggestion, suggestion,
module, module,
item_type,
}, },
)); ));
} }
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -228,10 +228,11 @@ enum ResolutionError<'a> {
SelfImportOnlyInImportListWithNonEmptyPrefix, SelfImportOnlyInImportListWithNonEmptyPrefix,
/// Error E0433: failed to resolve. /// Error E0433: failed to resolve.
FailedToResolve { FailedToResolve {
segment: Option<Symbol>, segment: Symbol,
label: String, label: String,
suggestion: Option<Suggestion>, suggestion: Option<Suggestion>,
module: Option<ModuleOrUniformRoot<'a>>, module: Option<ModuleOrUniformRoot<'a>>,
item_type: &'static str,
}, },
/// Error E0434: can't capture dynamic environment in a fn item. /// Error E0434: can't capture dynamic environment in a fn item.
CannotCaptureDynamicEnvironmentInFnItem, CannotCaptureDynamicEnvironmentInFnItem,
Expand Down Expand Up @@ -288,7 +289,7 @@ enum ResolutionError<'a> {
enum VisResolutionError<'a> { enum VisResolutionError<'a> {
Relative2018(Span, &'a ast::Path), Relative2018(Span, &'a ast::Path),
AncestorOnly(Span), AncestorOnly(Span),
FailedToResolve(Span, String, Option<Suggestion>), FailedToResolve(Span, Symbol, String, Option<Suggestion>, &'static str),
ExpectedFound(Span, String, Res), ExpectedFound(Span, String, Res),
Indeterminate(Span), Indeterminate(Span),
ModuleOnly(Span), ModuleOnly(Span),
Expand Down Expand Up @@ -430,6 +431,7 @@ enum PathResult<'a> {
module: Option<ModuleOrUniformRoot<'a>>, module: Option<ModuleOrUniformRoot<'a>>,
/// The segment name of target /// The segment name of target
segment_name: Symbol, segment_name: Symbol,
item_type: &'static str,
}, },
} }


Expand All @@ -440,6 +442,7 @@ impl<'a> PathResult<'a> {
finalize: bool, finalize: bool,
module: Option<ModuleOrUniformRoot<'a>>, module: Option<ModuleOrUniformRoot<'a>>,
label_and_suggestion: impl FnOnce() -> (String, Option<Suggestion>), label_and_suggestion: impl FnOnce() -> (String, Option<Suggestion>),
item_type: &'static str,
) -> PathResult<'a> { ) -> PathResult<'a> {
let (label, suggestion) = let (label, suggestion) =
if finalize { label_and_suggestion() } else { (String::new(), None) }; if finalize { label_and_suggestion() } else { (String::new(), None) };
Expand All @@ -450,6 +453,7 @@ impl<'a> PathResult<'a> {
suggestion, suggestion,
is_error_from_last_segment, is_error_from_last_segment,
module, module,
item_type,
} }
} }
} }
Expand Down
18 changes: 14 additions & 4 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -862,8 +862,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
), ),
path_res @ (PathResult::NonModule(..) | PathResult::Failed { .. }) => { path_res @ (PathResult::NonModule(..) | PathResult::Failed { .. }) => {
let mut suggestion = None; let mut suggestion = None;
let (span, label, module) = let (span, label, module, segment, item_type) = if let PathResult::Failed {
if let PathResult::Failed { span, label, module, .. } = path_res { span,
label,
module,
segment_name,
item_type,
..
} = path_res
{
// try to suggest if it's not a macro, maybe a function // try to suggest if it's not a macro, maybe a function
if let PathResult::NonModule(partial_res) = if let PathResult::NonModule(partial_res) =
self.maybe_resolve_path(&path, Some(ValueNS), &parent_scope) self.maybe_resolve_path(&path, Some(ValueNS), &parent_scope)
Expand All @@ -881,7 +888,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
)); ));
} }
(span, label, module) (span, label, module, segment_name, item_type)
} else { } else {
( (
path_span, path_span,
Expand All @@ -891,15 +898,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
kind.descr() kind.descr()
), ),
None, None,
path.last().map(|segment| segment.ident.name).unwrap(),
"macro",
) )
}; };
self.report_error( self.report_error(
span, span,
ResolutionError::FailedToResolve { ResolutionError::FailedToResolve {
segment: path.last().map(|segment| segment.ident.name), segment,
label, label,
suggestion, suggestion,
module, module,
item_type,
}, },
); );
} }
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0433]: failed to resolve: `Self` is only available in impls, traits, and type definitions error[E0433]: cannot find item `Self` in this scope
--> tests/ui/crashes/unreachable-array-or-slice.rs:4:9 --> tests/ui/crashes/unreachable-array-or-slice.rs:4:9
| |
LL | let Self::anything_here_kills_it(a, b, ..) = Foo(5, 5, 5, 5); LL | let Self::anything_here_kills_it(a, b, ..) = Foo(5, 5, 5, 5);
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-ui/intra-doc/unresolved-import-recovery.rs
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
// Regression test for issue #95879. // Regression test for issue #95879.


use unresolved_crate::module::Name; //~ ERROR failed to resolve use unresolved_crate::module::Name; //~ ERROR cannot find item


/// [Name] /// [Name]
pub struct S; pub struct S;
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0433]: failed to resolve: maybe a missing crate `unresolved_crate`? error[E0433]: cannot find item `unresolved_crate` in the crate root
--> $DIR/unresolved-import-recovery.rs:3:5 --> $DIR/unresolved-import-recovery.rs:3:5
| |
LL | use unresolved_crate::module::Name; LL | use unresolved_crate::module::Name;
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-ui/issues/issue-61732.rs
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
// This previously triggered an ICE. // This previously triggered an ICE.


pub(in crate::r#mod) fn main() {} pub(in crate::r#mod) fn main() {}
//~^ ERROR failed to resolve: maybe a missing crate `r#mod` //~^ ERROR cannot find item `mod`
2 changes: 1 addition & 1 deletion tests/rustdoc-ui/issues/issue-61732.stderr
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0433]: failed to resolve: maybe a missing crate `r#mod`? error[E0433]: cannot find item `mod` in this scope
--> $DIR/issue-61732.rs:3:15 --> $DIR/issue-61732.rs:3:15
| |
LL | pub(in crate::r#mod) fn main() {} LL | pub(in crate::r#mod) fn main() {}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/attributes/field-attributes-vis-unresolved.rs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ mod internal {


struct S { struct S {
#[rustfmt::skip] #[rustfmt::skip]
pub(in nonexistent) field: u8 //~ ERROR failed to resolve pub(in nonexistent) field: u8 //~ ERROR cannot find item `nonexistent` in this scope
} }


struct Z( struct Z(
#[rustfmt::skip] #[rustfmt::skip]
pub(in nonexistent) u8 //~ ERROR failed to resolve pub(in nonexistent) u8 //~ ERROR cannot find item `nonexistent` in this scope
); );


fn main() {} fn main() {}
4 changes: 2 additions & 2 deletions tests/ui/attributes/field-attributes-vis-unresolved.stderr
Original file line number Original file line Diff line number Diff line change
@@ -1,12 +1,12 @@
error[E0433]: failed to resolve: maybe a missing crate `nonexistent`? error[E0433]: cannot find item `nonexistent` in this scope
--> $DIR/field-attributes-vis-unresolved.rs:17:12 --> $DIR/field-attributes-vis-unresolved.rs:17:12
| |
LL | pub(in nonexistent) field: u8 LL | pub(in nonexistent) field: u8
| ^^^^^^^^^^^ maybe a missing crate `nonexistent`? | ^^^^^^^^^^^ maybe a missing crate `nonexistent`?
| |
= help: consider adding `extern crate nonexistent` to use the `nonexistent` crate = help: consider adding `extern crate nonexistent` to use the `nonexistent` crate


error[E0433]: failed to resolve: maybe a missing crate `nonexistent`? error[E0433]: cannot find item `nonexistent` in this scope
--> $DIR/field-attributes-vis-unresolved.rs:22:12 --> $DIR/field-attributes-vis-unresolved.rs:22:12
| |
LL | pub(in nonexistent) u8 LL | pub(in nonexistent) u8
Expand Down
Loading
Loading