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

Reword resolve errors caused by likely missing crate in dep tree #133154

Merged
merged 1 commit into from
Jan 25, 2025
Merged
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
2 changes: 1 addition & 1 deletion compiler/rustc_error_codes/src/error_codes/E0433.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ If you've expected to use a crate name:

```compile_fail
use ferris_wheel::BigO;
// error: failed to resolve: use of undeclared crate or module `ferris_wheel`
// error: failed to resolve: use of undeclared module or unlinked crate
```

Make sure the crate has been added as a dependency in `Cargo.toml`.
Expand Down
36 changes: 32 additions & 4 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use rustc_session::lint::builtin::{
MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
};
use rustc_session::lint::{AmbiguityErrorDiag, BuiltinLintDiag};
use rustc_session::utils::was_invoked_from_cargo;
use rustc_span::edit_distance::find_best_match_for_name;
use rustc_span::edition::Edition;
use rustc_span::hygiene::MacroKind;
Expand Down Expand Up @@ -809,7 +810,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
err.multipart_suggestion(msg, suggestions, applicability);
}

if let Some(ModuleOrUniformRoot::Module(module)) = module
&& let Some(module) = module.opt_def_id()
&& let Some(segment) = segment
Expand Down Expand Up @@ -2044,13 +2044,23 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
(format!("`_` is not a valid crate or module name"), None)
} else if self.tcx.sess.is_rust_2015() {
(
format!("you might be missing crate `{ident}`"),
format!("use of unresolved module or unlinked crate `{ident}`"),
Some((
vec![(
self.current_crate_outer_attr_insert_span,
format!("extern crate {ident};\n"),
)],
format!("consider importing the `{ident}` crate"),
if was_invoked_from_cargo() {
format!(
"if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
to add it to your `Cargo.toml` and import it in your code",
)
} else {
format!(
"you might be missing a crate named `{ident}`, add it to your \
project and import it in your code",
)
},
Applicability::MaybeIncorrect,
)),
)
Expand Down Expand Up @@ -2229,7 +2239,25 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let descr = binding.res().descr();
(format!("{descr} `{ident}` is not a crate or module"), suggestion)
} else {
(format!("use of undeclared crate or module `{ident}`"), suggestion)
let suggestion = if suggestion.is_some() {
suggestion
} else if was_invoked_from_cargo() {
Some((
vec![],
format!(
"if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
to add it to your `Cargo.toml`",
),
Applicability::MaybeIncorrect,
))
} else {
Some((
vec![],
format!("you might be missing a crate named `{ident}`",),
Applicability::MaybeIncorrect,
))
};
(format!("use of unresolved module or unlinked crate `{ident}`"), suggestion)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/fail/rustc-error2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct Struct<T>(T);
impl<T> std::ops::Deref for Struct<T> {
type Target = dyn Fn(T);
fn deref(&self) -> &assert_mem_uninitialized_valid::Target {
//~^ERROR: undeclared crate or module
//~^ERROR: use of unresolved module or unlinked crate
unimplemented!()
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/tools/miri/tests/fail/rustc-error2.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0433]: failed to resolve: use of undeclared crate or module `assert_mem_uninitialized_valid`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `assert_mem_uninitialized_valid`
--> tests/fail/rustc-error2.rs:LL:CC
|
LL | fn deref(&self) -> &assert_mem_uninitialized_valid::Target {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use of undeclared crate or module `assert_mem_uninitialized_valid`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `assert_mem_uninitialized_valid`
|
= help: you might be missing a crate named `assert_mem_uninitialized_valid`

error: aborting due to 1 previous error

Expand Down
4 changes: 2 additions & 2 deletions tests/rustdoc-ui/ice-unresolved-import-100241.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ error[E0432]: unresolved import `inner`
--> $DIR/ice-unresolved-import-100241.rs:9:13
|
LL | pub use inner::S;
| ^^^^^ you might be missing crate `inner`
| ^^^^^ use of unresolved module or unlinked crate `inner`
|
help: consider importing the `inner` crate
help: you might be missing a crate named `inner`, add it to your project and import it in your code
|
LL + extern crate inner;
|
Expand Down
6 changes: 3 additions & 3 deletions tests/rustdoc-ui/intra-doc/unresolved-import-recovery.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0433]: failed to resolve: you might be missing crate `unresolved_crate`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `unresolved_crate`
--> $DIR/unresolved-import-recovery.rs:3:5
|
LL | use unresolved_crate::module::Name;
| ^^^^^^^^^^^^^^^^ you might be missing crate `unresolved_crate`
| ^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved_crate`
|
help: consider importing the `unresolved_crate` crate
help: you might be missing a crate named `unresolved_crate`, add it to your project and import it in your code
|
LL + extern crate unresolved_crate;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-ui/issues/issue-61732.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This previously triggered an ICE.

pub(in crate::r#mod) fn main() {}
//~^ ERROR failed to resolve: you might be missing crate `r#mod`
//~^ ERROR failed to resolve: use of unresolved module or unlinked crate `r#mod`
6 changes: 3 additions & 3 deletions tests/rustdoc-ui/issues/issue-61732.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0433]: failed to resolve: you might be missing crate `r#mod`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `r#mod`
--> $DIR/issue-61732.rs:3:15
|
LL | pub(in crate::r#mod) fn main() {}
| ^^^^^ you might be missing crate `r#mod`
| ^^^^^ use of unresolved module or unlinked crate `r#mod`
|
help: consider importing the `r#mod` crate
help: you might be missing a crate named `r#mod`, add it to your project and import it in your code
|
LL + extern crate r#mod;
|
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/attributes/check-builtin-attr-ice.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
error[E0433]: failed to resolve: use of undeclared crate or module `should_panic`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `should_panic`
--> $DIR/check-builtin-attr-ice.rs:43:7
|
LL | #[should_panic::skip]
| ^^^^^^^^^^^^ use of undeclared crate or module `should_panic`
| ^^^^^^^^^^^^ use of unresolved module or unlinked crate `should_panic`

error[E0433]: failed to resolve: use of undeclared crate or module `should_panic`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `should_panic`
--> $DIR/check-builtin-attr-ice.rs:47:7
|
LL | #[should_panic::a::b::c]
| ^^^^^^^^^^^^ use of undeclared crate or module `should_panic`
| ^^^^^^^^^^^^ use of unresolved module or unlinked crate `should_panic`

error[E0433]: failed to resolve: use of undeclared crate or module `deny`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `deny`
--> $DIR/check-builtin-attr-ice.rs:55:7
|
LL | #[deny::skip]
| ^^^^ use of undeclared crate or module `deny`
| ^^^^ use of unresolved module or unlinked crate `deny`

error: aborting due to 3 previous errors

Expand Down
52 changes: 26 additions & 26 deletions tests/ui/attributes/check-cfg_attr-ice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,83 +17,83 @@ LL | #[cfg_attr::no_such_thing]
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0433]: failed to resolve: use of undeclared crate or module `cfg_attr`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `cfg_attr`
--> $DIR/check-cfg_attr-ice.rs:52:3
|
LL | #[cfg_attr::no_such_thing]
| ^^^^^^^^ use of undeclared crate or module `cfg_attr`
| ^^^^^^^^ use of unresolved module or unlinked crate `cfg_attr`

error[E0433]: failed to resolve: use of undeclared crate or module `cfg_attr`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `cfg_attr`
--> $DIR/check-cfg_attr-ice.rs:55:7
|
LL | #[cfg_attr::no_such_thing]
| ^^^^^^^^ use of undeclared crate or module `cfg_attr`
| ^^^^^^^^ use of unresolved module or unlinked crate `cfg_attr`

error[E0433]: failed to resolve: use of undeclared crate or module `cfg_attr`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `cfg_attr`
--> $DIR/check-cfg_attr-ice.rs:57:17
|
LL | GiveYouUp(#[cfg_attr::no_such_thing] u8),
| ^^^^^^^^ use of undeclared crate or module `cfg_attr`
| ^^^^^^^^ use of unresolved module or unlinked crate `cfg_attr`

error[E0433]: failed to resolve: use of undeclared crate or module `cfg_attr`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `cfg_attr`
--> $DIR/check-cfg_attr-ice.rs:64:11
|
LL | #[cfg_attr::no_such_thing]
| ^^^^^^^^ use of undeclared crate or module `cfg_attr`
| ^^^^^^^^ use of unresolved module or unlinked crate `cfg_attr`

error[E0433]: failed to resolve: use of undeclared crate or module `cfg_attr`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `cfg_attr`
--> $DIR/check-cfg_attr-ice.rs:41:7
|
LL | #[cfg_attr::no_such_thing]
| ^^^^^^^^ use of undeclared crate or module `cfg_attr`
| ^^^^^^^^ use of unresolved module or unlinked crate `cfg_attr`

error[E0433]: failed to resolve: use of undeclared crate or module `cfg_attr`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `cfg_attr`
--> $DIR/check-cfg_attr-ice.rs:43:15
|
LL | fn from(#[cfg_attr::no_such_thing] any_other_guy: AnyOtherGuy) -> This {
| ^^^^^^^^ use of undeclared crate or module `cfg_attr`
| ^^^^^^^^ use of unresolved module or unlinked crate `cfg_attr`

error[E0433]: failed to resolve: use of undeclared crate or module `cfg_attr`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `cfg_attr`
--> $DIR/check-cfg_attr-ice.rs:45:11
|
LL | #[cfg_attr::no_such_thing]
| ^^^^^^^^ use of undeclared crate or module `cfg_attr`
| ^^^^^^^^ use of unresolved module or unlinked crate `cfg_attr`

error[E0433]: failed to resolve: use of undeclared crate or module `cfg_attr`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `cfg_attr`
--> $DIR/check-cfg_attr-ice.rs:32:3
|
LL | #[cfg_attr::no_such_thing]
| ^^^^^^^^ use of undeclared crate or module `cfg_attr`
| ^^^^^^^^ use of unresolved module or unlinked crate `cfg_attr`

error[E0433]: failed to resolve: use of undeclared crate or module `cfg_attr`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `cfg_attr`
--> $DIR/check-cfg_attr-ice.rs:24:3
|
LL | #[cfg_attr::no_such_thing]
| ^^^^^^^^ use of undeclared crate or module `cfg_attr`
| ^^^^^^^^ use of unresolved module or unlinked crate `cfg_attr`

error[E0433]: failed to resolve: use of undeclared crate or module `cfg_attr`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `cfg_attr`
--> $DIR/check-cfg_attr-ice.rs:27:7
|
LL | #[cfg_attr::no_such_thing]
| ^^^^^^^^ use of undeclared crate or module `cfg_attr`
| ^^^^^^^^ use of unresolved module or unlinked crate `cfg_attr`

error[E0433]: failed to resolve: use of undeclared crate or module `cfg_attr`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `cfg_attr`
--> $DIR/check-cfg_attr-ice.rs:16:3
|
LL | #[cfg_attr::no_such_thing]
| ^^^^^^^^ use of undeclared crate or module `cfg_attr`
| ^^^^^^^^ use of unresolved module or unlinked crate `cfg_attr`

error[E0433]: failed to resolve: use of undeclared crate or module `cfg_attr`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `cfg_attr`
--> $DIR/check-cfg_attr-ice.rs:19:7
|
LL | #[cfg_attr::no_such_thing]
| ^^^^^^^^ use of undeclared crate or module `cfg_attr`
| ^^^^^^^^ use of unresolved module or unlinked crate `cfg_attr`

error[E0433]: failed to resolve: use of undeclared crate or module `cfg_attr`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `cfg_attr`
--> $DIR/check-cfg_attr-ice.rs:12:3
|
LL | #[cfg_attr::no_such_thing]
| ^^^^^^^^ use of undeclared crate or module `cfg_attr`
| ^^^^^^^^ use of unresolved module or unlinked crate `cfg_attr`

error: aborting due to 15 previous errors

Expand Down
12 changes: 6 additions & 6 deletions tests/ui/attributes/field-attributes-vis-unresolved.stderr
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
error[E0433]: failed to resolve: you might be missing crate `nonexistent`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `nonexistent`
--> $DIR/field-attributes-vis-unresolved.rs:17:12
|
LL | pub(in nonexistent) field: u8
| ^^^^^^^^^^^ you might be missing crate `nonexistent`
| ^^^^^^^^^^^ use of unresolved module or unlinked crate `nonexistent`
|
help: consider importing the `nonexistent` crate
help: you might be missing a crate named `nonexistent`, add it to your project and import it in your code
|
LL + extern crate nonexistent;
|

error[E0433]: failed to resolve: you might be missing crate `nonexistent`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `nonexistent`
--> $DIR/field-attributes-vis-unresolved.rs:22:12
|
LL | pub(in nonexistent) u8
| ^^^^^^^^^^^ you might be missing crate `nonexistent`
| ^^^^^^^^^^^ use of unresolved module or unlinked crate `nonexistent`
|
help: consider importing the `nonexistent` crate
help: you might be missing a crate named `nonexistent`, add it to your project and import it in your code
|
LL + extern crate nonexistent;
|
Expand Down
12 changes: 8 additions & 4 deletions tests/ui/coherence/conflicting-impl-with-err.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
error[E0433]: failed to resolve: use of undeclared crate or module `nope`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `nope`
--> $DIR/conflicting-impl-with-err.rs:4:11
|
LL | impl From<nope::Thing> for Error {
| ^^^^ use of undeclared crate or module `nope`
| ^^^^ use of unresolved module or unlinked crate `nope`
|
= help: you might be missing a crate named `nope`

error[E0433]: failed to resolve: use of undeclared crate or module `nope`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `nope`
--> $DIR/conflicting-impl-with-err.rs:5:16
|
LL | fn from(_: nope::Thing) -> Self {
| ^^^^ use of undeclared crate or module `nope`
| ^^^^ use of unresolved module or unlinked crate `nope`
|
= help: you might be missing a crate named `nope`

error: aborting due to 2 previous errors

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/delegation/bad-resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Trait for S {
}

mod prefix {}
reuse unresolved_prefix::{a, b, c}; //~ ERROR use of undeclared crate or module `unresolved_prefix`
reuse unresolved_prefix::{a, b, c}; //~ ERROR use of unresolved module or unlinked crate
reuse prefix::{self, super, crate}; //~ ERROR `crate` in paths can only be used in start position

fn main() {}
6 changes: 4 additions & 2 deletions tests/ui/delegation/bad-resolve.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ LL | type Type;
LL | impl Trait for S {
| ^^^^^^^^^^^^^^^^ missing `Type` in implementation

error[E0433]: failed to resolve: use of undeclared crate or module `unresolved_prefix`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `unresolved_prefix`
--> $DIR/bad-resolve.rs:43:7
|
LL | reuse unresolved_prefix::{a, b, c};
| ^^^^^^^^^^^^^^^^^ use of undeclared crate or module `unresolved_prefix`
| ^^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved_prefix`
|
= help: you might be missing a crate named `unresolved_prefix`

error[E0433]: failed to resolve: `crate` in paths can only be used in start position
--> $DIR/bad-resolve.rs:44:29
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/delegation/glob-bad-path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ trait Trait {}
struct S;

impl Trait for u8 {
reuse unresolved::*; //~ ERROR failed to resolve: use of undeclared crate or module `unresolved`
reuse unresolved::*; //~ ERROR failed to resolve: use of unresolved module or unlinked crate `unresolved`
reuse S::*; //~ ERROR expected trait, found struct `S`
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/delegation/glob-bad-path.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ error: expected trait, found struct `S`
LL | reuse S::*;
| ^ not a trait

error[E0433]: failed to resolve: use of undeclared crate or module `unresolved`
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `unresolved`
--> $DIR/glob-bad-path.rs:8:11
|
LL | reuse unresolved::*;
| ^^^^^^^^^^ use of undeclared crate or module `unresolved`
| ^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved`

error: aborting due to 2 previous errors

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/error-codes/E0432.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ error[E0432]: unresolved import `something`
--> $DIR/E0432.rs:1:5
|
LL | use something::Foo;
| ^^^^^^^^^ you might be missing crate `something`
| ^^^^^^^^^ use of unresolved module or unlinked crate `something`
|
help: consider importing the `something` crate
help: you might be missing a crate named `something`, add it to your project and import it in your code
|
LL + extern crate something;
|
Expand Down
Loading
Loading