Skip to content

Commit

Permalink
Differentiate between cargo and non-cargo runs
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Dec 20, 2024
1 parent 01528f9 commit 68af421
Show file tree
Hide file tree
Showing 58 changed files with 128 additions and 93 deletions.
42 changes: 30 additions & 12 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 @@ -2045,10 +2046,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
self.current_crate_outer_attr_insert_span,
format!("extern crate {ident};\n"),
)],
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",
),
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 @@ -2227,14 +2235,24 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let descr = binding.res().descr();
(format!("{descr} `{ident}` is not a crate or module"), suggestion)
} else {
let suggestion = suggestion.or(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,
)));
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
4 changes: 2 additions & 2 deletions tests/ui/attributes/field-attributes-vis-unresolved.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `non
LL | pub(in nonexistent) field: u8
| ^^^^^^^^^^^ use of unresolved module or unlinked crate `nonexistent`
|
help: if you wanted to use a crate named `nonexistent`, use `cargo add nonexistent` to add it to your `Cargo.toml` and import it in your code
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 All @@ -15,7 +15,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `non
LL | pub(in nonexistent) u8
| ^^^^^^^^^^^ use of unresolved module or unlinked crate `nonexistent`
|
help: if you wanted to use a crate named `nonexistent`, use `cargo add nonexistent` to add it to your `Cargo.toml` and import it in your code
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
4 changes: 2 additions & 2 deletions tests/ui/coherence/conflicting-impl-with-err.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `nop
LL | impl From<nope::Thing> for Error {
| ^^^^ use of unresolved module or unlinked crate `nope`
|
= help: if you wanted to use a crate named `nope`, use `cargo add nope` to add it to your `Cargo.toml`
= help: you might be missing a crate named `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 unresolved module or unlinked crate `nope`
|
= help: if you wanted to use a crate named `nope`, use `cargo add nope` to add it to your `Cargo.toml`
= 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.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `unr
LL | reuse unresolved_prefix::{a, b, c};
| ^^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved_prefix`
|
= help: if you wanted to use a crate named `unresolved_prefix`, use `cargo add unresolved_prefix` to add it to your `Cargo.toml`
= 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/error-codes/E0432.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `something`
LL | use something::Foo;
| ^^^^^^^^^ use of unresolved module or unlinked crate `something`
|
help: if you wanted to use a crate named `something`, use `cargo add something` to add it to your `Cargo.toml` and import it in your code
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
2 changes: 1 addition & 1 deletion tests/ui/extern-flag/multiple-opts.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `som
LL | somedep::somefun();
| ^^^^^^^ use of unresolved module or unlinked crate `somedep`
|
= help: if you wanted to use a crate named `somedep`, use `cargo add somedep` to add it to your `Cargo.toml`
= help: you might be missing a crate named `somedep`

error: aborting due to 1 previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/extern-flag/noprelude.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `som
LL | somedep::somefun();
| ^^^^^^^ use of unresolved module or unlinked crate `somedep`
|
= help: if you wanted to use a crate named `somedep`, use `cargo add somedep` to add it to your `Cargo.toml`
= help: you might be missing a crate named `somedep`

error: aborting due to 1 previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/foreign/stashed-issue-121451.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `lib
LL | extern "C" fn _f() -> libc::uintptr_t {}
| ^^^^ use of unresolved module or unlinked crate `libc`
|
= help: if you wanted to use a crate named `libc`, use `cargo add libc` to add it to your `Cargo.toml`
= help: you might be missing a crate named `libc`

error: aborting due to 1 previous error

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/hygiene/extern-prelude-from-opaque-fail-2018.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ LL | fn f() { my_core::mem::drop(0); }
LL | a!();
| ---- in this macro invocation
|
= help: if you wanted to use a crate named `my_core`, use `cargo add my_core` to add it to your `Cargo.toml`
= help: you might be missing a crate named `my_core`
= help: consider importing this module:
std::mem
= note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info)
Expand All @@ -35,7 +35,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `my_
LL | fn f() { my_core::mem::drop(0); }
| ^^^^^^^ use of unresolved module or unlinked crate `my_core`
|
= help: if you wanted to use a crate named `my_core`, use `cargo add my_core` to add it to your `Cargo.toml`
= help: you might be missing a crate named `my_core`
help: consider importing this module
|
LL + use std::mem;
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/hygiene/extern-prelude-from-opaque-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ LL | fn f() { my_core::mem::drop(0); }
LL | a!();
| ---- in this macro invocation
|
= help: if you wanted to use a crate named `my_core`, use `cargo add my_core` to add it to your `Cargo.toml`
= help: you might be missing a crate named `my_core`
= help: consider importing this module:
my_core::mem
= note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info)
Expand All @@ -35,7 +35,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `my_
LL | fn f() { my_core::mem::drop(0); }
| ^^^^^^^ use of unresolved module or unlinked crate `my_core`
|
= help: if you wanted to use a crate named `my_core`, use `cargo add my_core` to add it to your `Cargo.toml`
= help: you might be missing a crate named `my_core`
help: consider importing this module
|
LL + use my_core::mem;
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/impl-trait/issue-72911.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `foo
LL | fn gather_from_file(dir_entry: &foo::MissingItem) -> impl Iterator<Item = Lint> {
| ^^^ use of unresolved module or unlinked crate `foo`
|
= help: if you wanted to use a crate named `foo`, use `cargo add foo` to add it to your `Cargo.toml`
= help: you might be missing a crate named `foo`

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `foo`
--> $DIR/issue-72911.rs:16:41
|
LL | fn lint_files() -> impl Iterator<Item = foo::MissingItem> {
| ^^^ use of unresolved module or unlinked crate `foo`
|
= help: if you wanted to use a crate named `foo`, use `cargo add foo` to add it to your `Cargo.toml`
= help: you might be missing a crate named `foo`

error: aborting due to 2 previous errors

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/impl-trait/stashed-diag-issue-121504.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `xyz
LL | impl MyTrait for xyz::T {
| ^^^ use of unresolved module or unlinked crate `xyz`
|
= help: if you wanted to use a crate named `xyz`, use `cargo add xyz` to add it to your `Cargo.toml`
= help: you might be missing a crate named `xyz`

error: aborting due to 1 previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/import-from-missing-star-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `spam`
LL | use spam::*;
| ^^^^ use of unresolved module or unlinked crate `spam`
|
help: if you wanted to use a crate named `spam`, use `cargo add spam` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `spam`, add it to your project and import it in your code
|
LL + extern crate spam;
|
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/imports/import-from-missing-star-3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `spam`
LL | use spam::*;
| ^^^^ use of unresolved module or unlinked crate `spam`
|
help: if you wanted to use a crate named `spam`, use `cargo add spam` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `spam`, add it to your project and import it in your code
|
LL + extern crate spam;
|
Expand All @@ -15,7 +15,7 @@ error[E0432]: unresolved import `spam`
LL | use spam::*;
| ^^^^ use of unresolved module or unlinked crate `spam`
|
help: if you wanted to use a crate named `spam`, use `cargo add spam` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `spam`, add it to your project and import it in your code
|
LL + extern crate spam;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/import-from-missing-star.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `spam`
LL | use spam::*;
| ^^^^ use of unresolved module or unlinked crate `spam`
|
help: if you wanted to use a crate named `spam`, use `cargo add spam` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `spam`, add it to your project and import it in your code
|
LL + extern crate spam;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/import3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `main`
LL | use main::bar;
| ^^^^ use of unresolved module or unlinked crate `main`
|
help: if you wanted to use a crate named `main`, use `cargo add main` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `main`, add it to your project and import it in your code
|
LL + extern crate main;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-109343.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `unresolved`
LL | pub use unresolved::f;
| ^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved`
|
help: if you wanted to use a crate named `unresolved`, use `cargo add unresolved` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `unresolved`, add it to your project and import it in your code
|
LL + extern crate unresolved;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-1697.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
use unresolved::*;
//~^ ERROR unresolved import `unresolved` [E0432]
//~| NOTE use of unresolved module or unlinked crate `unresolved`
//~| HELP if you wanted to use a crate named `unresolved`, use `cargo add unresolved` to add it to your `Cargo.toml`
//~| HELP you might be missing a crate named `unresolved`

fn main() {}
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-1697.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `unresolved`
LL | use unresolved::*;
| ^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved`
|
help: if you wanted to use a crate named `unresolved`, use `cargo add unresolved` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `unresolved`, add it to your project and import it in your code
|
LL + extern crate unresolved;
|
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/imports/issue-33464.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `abc`
LL | use abc::one_el;
| ^^^ use of unresolved module or unlinked crate `abc`
|
help: if you wanted to use a crate named `abc`, use `cargo add abc` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `abc`, add it to your project and import it in your code
|
LL + extern crate abc;
|
Expand All @@ -15,7 +15,7 @@ error[E0432]: unresolved import `abc`
LL | use abc::{a, bbb, cccccc};
| ^^^ use of unresolved module or unlinked crate `abc`
|
help: if you wanted to use a crate named `abc`, use `cargo add abc` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `abc`, add it to your project and import it in your code
|
LL + extern crate abc;
|
Expand All @@ -26,7 +26,7 @@ error[E0432]: unresolved import `a_very_long_name`
LL | use a_very_long_name::{el, el2};
| ^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `a_very_long_name`
|
help: if you wanted to use a crate named `a_very_long_name`, use `cargo add a_very_long_name` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `a_very_long_name`, add it to your project and import it in your code
|
LL + extern crate a_very_long_name;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-36881.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `issue_36881_aux`
LL | use issue_36881_aux::Foo;
| ^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `issue_36881_aux`
|
help: if you wanted to use a crate named `issue_36881_aux`, use `cargo add issue_36881_aux` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `issue_36881_aux`, add it to your project and import it in your code
|
LL + extern crate issue_36881_aux;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-37887.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `test`
LL | use test::*;
| ^^^^ use of unresolved module or unlinked crate `test`
|
help: if you wanted to use a crate named `test`, use `cargo add test` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `test`, add it to your project and import it in your code
|
LL + extern crate test;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-53269.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `nonexistent_module`
LL | use nonexistent_module::mac;
| ^^^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `nonexistent_module`
|
help: if you wanted to use a crate named `nonexistent_module`, use `cargo add nonexistent_module` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `nonexistent_module`, add it to your project and import it in your code
|
LL + extern crate nonexistent_module;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-55457.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ error[E0432]: unresolved import `non_existent`
LL | use non_existent::non_existent;
| ^^^^^^^^^^^^ use of unresolved module or unlinked crate `non_existent`
|
help: if you wanted to use a crate named `non_existent`, use `cargo add non_existent` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `non_existent`, add it to your project and import it in your code
|
LL + extern crate non_existent;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-81413.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `doesnt_exist`
LL | pub use doesnt_exist::*;
| ^^^^^^^^^^^^ use of unresolved module or unlinked crate `doesnt_exist`
|
help: if you wanted to use a crate named `doesnt_exist`, use `cargo add doesnt_exist` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `doesnt_exist`, add it to your project and import it in your code
|
LL + extern crate doesnt_exist;
|
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/imports/tool-mod-child.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `cli
LL | use clippy::a::b;
| ^^^^^^ use of unresolved module or unlinked crate `clippy`
|
help: if you wanted to use a crate named `clippy`, use `cargo add clippy` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `clippy`, add it to your project and import it in your code
|
LL + extern crate clippy;
|
Expand All @@ -15,7 +15,7 @@ error[E0432]: unresolved import `clippy`
LL | use clippy::a;
| ^^^^^^ use of unresolved module or unlinked crate `clippy`
|
help: if you wanted to use a crate named `clippy`, use `cargo add clippy` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `clippy`, add it to your project and import it in your code
|
LL + extern crate clippy;
|
Expand All @@ -26,7 +26,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `rus
LL | use rustdoc::a::b;
| ^^^^^^^ use of unresolved module or unlinked crate `rustdoc`
|
help: if you wanted to use a crate named `rustdoc`, use `cargo add rustdoc` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `rustdoc`, add it to your project and import it in your code
|
LL + extern crate rustdoc;
|
Expand All @@ -37,7 +37,7 @@ error[E0432]: unresolved import `rustdoc`
LL | use rustdoc::a;
| ^^^^^^^ use of unresolved module or unlinked crate `rustdoc`
|
help: if you wanted to use a crate named `rustdoc`, use `cargo add rustdoc` to add it to your `Cargo.toml` and import it in your code
help: you might be missing a crate named `rustdoc`, add it to your project and import it in your code
|
LL + extern crate rustdoc;
|
Expand Down
Loading

0 comments on commit 68af421

Please sign in to comment.