Skip to content

Commit 68af421

Browse files
committed
Differentiate between cargo and non-cargo runs
1 parent 01528f9 commit 68af421

File tree

58 files changed

+128
-93
lines changed

Some content is hidden

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

58 files changed

+128
-93
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+30-12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_session::lint::builtin::{
2424
MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
2525
};
2626
use rustc_session::lint::{AmbiguityErrorDiag, BuiltinLintDiag};
27+
use rustc_session::utils::was_invoked_from_cargo;
2728
use rustc_span::edit_distance::find_best_match_for_name;
2829
use rustc_span::edition::Edition;
2930
use rustc_span::hygiene::MacroKind;
@@ -2045,10 +2046,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20452046
self.current_crate_outer_attr_insert_span,
20462047
format!("extern crate {ident};\n"),
20472048
)],
2048-
format!(
2049-
"if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
2050-
to add it to your `Cargo.toml` and import it in your code",
2051-
),
2049+
if was_invoked_from_cargo() {
2050+
format!(
2051+
"if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
2052+
to add it to your `Cargo.toml` and import it in your code",
2053+
)
2054+
} else {
2055+
format!(
2056+
"you might be missing a crate named `{ident}`, add it to your \
2057+
project and import it in your code",
2058+
)
2059+
},
20522060
Applicability::MaybeIncorrect,
20532061
)),
20542062
)
@@ -2227,14 +2235,24 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22272235
let descr = binding.res().descr();
22282236
(format!("{descr} `{ident}` is not a crate or module"), suggestion)
22292237
} else {
2230-
let suggestion = suggestion.or(Some((
2231-
vec![],
2232-
format!(
2233-
"if you wanted to use a crate named `{ident}`, use `cargo add {ident}` to \
2234-
add it to your `Cargo.toml`",
2235-
),
2236-
Applicability::MaybeIncorrect,
2237-
)));
2238+
let suggestion = if suggestion.is_some() {
2239+
suggestion
2240+
} else if was_invoked_from_cargo() {
2241+
Some((
2242+
vec![],
2243+
format!(
2244+
"if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
2245+
to add it to your `Cargo.toml`",
2246+
),
2247+
Applicability::MaybeIncorrect,
2248+
))
2249+
} else {
2250+
Some((
2251+
vec![],
2252+
format!("you might be missing a crate named `{ident}`",),
2253+
Applicability::MaybeIncorrect,
2254+
))
2255+
};
22382256
(format!("use of unresolved module or unlinked crate `{ident}`"), suggestion)
22392257
}
22402258
}

tests/ui/attributes/field-attributes-vis-unresolved.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `non
44
LL | pub(in nonexistent) field: u8
55
| ^^^^^^^^^^^ use of unresolved module or unlinked crate `nonexistent`
66
|
7-
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
7+
help: you might be missing a crate named `nonexistent`, add it to your project and import it in your code
88
|
99
LL + extern crate nonexistent;
1010
|
@@ -15,7 +15,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `non
1515
LL | pub(in nonexistent) u8
1616
| ^^^^^^^^^^^ use of unresolved module or unlinked crate `nonexistent`
1717
|
18-
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
18+
help: you might be missing a crate named `nonexistent`, add it to your project and import it in your code
1919
|
2020
LL + extern crate nonexistent;
2121
|

tests/ui/coherence/conflicting-impl-with-err.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `nop
44
LL | impl From<nope::Thing> for Error {
55
| ^^^^ use of unresolved module or unlinked crate `nope`
66
|
7-
= help: if you wanted to use a crate named `nope`, use `cargo add nope` to add it to your `Cargo.toml`
7+
= help: you might be missing a crate named `nope`
88

99
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `nope`
1010
--> $DIR/conflicting-impl-with-err.rs:5:16
1111
|
1212
LL | fn from(_: nope::Thing) -> Self {
1313
| ^^^^ use of unresolved module or unlinked crate `nope`
1414
|
15-
= help: if you wanted to use a crate named `nope`, use `cargo add nope` to add it to your `Cargo.toml`
15+
= help: you might be missing a crate named `nope`
1616

1717
error: aborting due to 2 previous errors
1818

tests/ui/delegation/bad-resolve.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `unr
8787
LL | reuse unresolved_prefix::{a, b, c};
8888
| ^^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved_prefix`
8989
|
90-
= help: if you wanted to use a crate named `unresolved_prefix`, use `cargo add unresolved_prefix` to add it to your `Cargo.toml`
90+
= help: you might be missing a crate named `unresolved_prefix`
9191

9292
error[E0433]: failed to resolve: `crate` in paths can only be used in start position
9393
--> $DIR/bad-resolve.rs:44:29

tests/ui/error-codes/E0432.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `something`
44
LL | use something::Foo;
55
| ^^^^^^^^^ use of unresolved module or unlinked crate `something`
66
|
7-
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
7+
help: you might be missing a crate named `something`, add it to your project and import it in your code
88
|
99
LL + extern crate something;
1010
|

tests/ui/extern-flag/multiple-opts.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `som
44
LL | somedep::somefun();
55
| ^^^^^^^ use of unresolved module or unlinked crate `somedep`
66
|
7-
= help: if you wanted to use a crate named `somedep`, use `cargo add somedep` to add it to your `Cargo.toml`
7+
= help: you might be missing a crate named `somedep`
88

99
error: aborting due to 1 previous error
1010

tests/ui/extern-flag/noprelude.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `som
44
LL | somedep::somefun();
55
| ^^^^^^^ use of unresolved module or unlinked crate `somedep`
66
|
7-
= help: if you wanted to use a crate named `somedep`, use `cargo add somedep` to add it to your `Cargo.toml`
7+
= help: you might be missing a crate named `somedep`
88

99
error: aborting due to 1 previous error
1010

tests/ui/foreign/stashed-issue-121451.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `lib
44
LL | extern "C" fn _f() -> libc::uintptr_t {}
55
| ^^^^ use of unresolved module or unlinked crate `libc`
66
|
7-
= help: if you wanted to use a crate named `libc`, use `cargo add libc` to add it to your `Cargo.toml`
7+
= help: you might be missing a crate named `libc`
88

99
error: aborting due to 1 previous error
1010

tests/ui/hygiene/extern-prelude-from-opaque-fail-2018.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ LL | fn f() { my_core::mem::drop(0); }
2424
LL | a!();
2525
| ---- in this macro invocation
2626
|
27-
= help: if you wanted to use a crate named `my_core`, use `cargo add my_core` to add it to your `Cargo.toml`
27+
= help: you might be missing a crate named `my_core`
2828
= help: consider importing this module:
2929
std::mem
3030
= note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -35,7 +35,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `my_
3535
LL | fn f() { my_core::mem::drop(0); }
3636
| ^^^^^^^ use of unresolved module or unlinked crate `my_core`
3737
|
38-
= help: if you wanted to use a crate named `my_core`, use `cargo add my_core` to add it to your `Cargo.toml`
38+
= help: you might be missing a crate named `my_core`
3939
help: consider importing this module
4040
|
4141
LL + use std::mem;

tests/ui/hygiene/extern-prelude-from-opaque-fail.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ LL | fn f() { my_core::mem::drop(0); }
2424
LL | a!();
2525
| ---- in this macro invocation
2626
|
27-
= help: if you wanted to use a crate named `my_core`, use `cargo add my_core` to add it to your `Cargo.toml`
27+
= help: you might be missing a crate named `my_core`
2828
= help: consider importing this module:
2929
my_core::mem
3030
= note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -35,7 +35,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `my_
3535
LL | fn f() { my_core::mem::drop(0); }
3636
| ^^^^^^^ use of unresolved module or unlinked crate `my_core`
3737
|
38-
= help: if you wanted to use a crate named `my_core`, use `cargo add my_core` to add it to your `Cargo.toml`
38+
= help: you might be missing a crate named `my_core`
3939
help: consider importing this module
4040
|
4141
LL + use my_core::mem;

tests/ui/impl-trait/issue-72911.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `foo
44
LL | fn gather_from_file(dir_entry: &foo::MissingItem) -> impl Iterator<Item = Lint> {
55
| ^^^ use of unresolved module or unlinked crate `foo`
66
|
7-
= help: if you wanted to use a crate named `foo`, use `cargo add foo` to add it to your `Cargo.toml`
7+
= help: you might be missing a crate named `foo`
88

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

1717
error: aborting due to 2 previous errors
1818

tests/ui/impl-trait/stashed-diag-issue-121504.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `xyz
44
LL | impl MyTrait for xyz::T {
55
| ^^^ use of unresolved module or unlinked crate `xyz`
66
|
7-
= help: if you wanted to use a crate named `xyz`, use `cargo add xyz` to add it to your `Cargo.toml`
7+
= help: you might be missing a crate named `xyz`
88

99
error: aborting due to 1 previous error
1010

tests/ui/imports/import-from-missing-star-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `spam`
44
LL | use spam::*;
55
| ^^^^ use of unresolved module or unlinked crate `spam`
66
|
7-
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
7+
help: you might be missing a crate named `spam`, add it to your project and import it in your code
88
|
99
LL + extern crate spam;
1010
|

tests/ui/imports/import-from-missing-star-3.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `spam`
44
LL | use spam::*;
55
| ^^^^ use of unresolved module or unlinked crate `spam`
66
|
7-
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
7+
help: you might be missing a crate named `spam`, add it to your project and import it in your code
88
|
99
LL + extern crate spam;
1010
|
@@ -15,7 +15,7 @@ error[E0432]: unresolved import `spam`
1515
LL | use spam::*;
1616
| ^^^^ use of unresolved module or unlinked crate `spam`
1717
|
18-
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
18+
help: you might be missing a crate named `spam`, add it to your project and import it in your code
1919
|
2020
LL + extern crate spam;
2121
|

tests/ui/imports/import-from-missing-star.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `spam`
44
LL | use spam::*;
55
| ^^^^ use of unresolved module or unlinked crate `spam`
66
|
7-
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
7+
help: you might be missing a crate named `spam`, add it to your project and import it in your code
88
|
99
LL + extern crate spam;
1010
|

tests/ui/imports/import3.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `main`
44
LL | use main::bar;
55
| ^^^^ use of unresolved module or unlinked crate `main`
66
|
7-
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
7+
help: you might be missing a crate named `main`, add it to your project and import it in your code
88
|
99
LL + extern crate main;
1010
|

tests/ui/imports/issue-109343.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `unresolved`
44
LL | pub use unresolved::f;
55
| ^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved`
66
|
7-
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
7+
help: you might be missing a crate named `unresolved`, add it to your project and import it in your code
88
|
99
LL + extern crate unresolved;
1010
|

tests/ui/imports/issue-1697.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
use unresolved::*;
44
//~^ ERROR unresolved import `unresolved` [E0432]
55
//~| NOTE use of unresolved module or unlinked crate `unresolved`
6-
//~| HELP if you wanted to use a crate named `unresolved`, use `cargo add unresolved` to add it to your `Cargo.toml`
6+
//~| HELP you might be missing a crate named `unresolved`
77

88
fn main() {}

tests/ui/imports/issue-1697.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `unresolved`
44
LL | use unresolved::*;
55
| ^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved`
66
|
7-
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
7+
help: you might be missing a crate named `unresolved`, add it to your project and import it in your code
88
|
99
LL + extern crate unresolved;
1010
|

tests/ui/imports/issue-33464.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `abc`
44
LL | use abc::one_el;
55
| ^^^ use of unresolved module or unlinked crate `abc`
66
|
7-
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
7+
help: you might be missing a crate named `abc`, add it to your project and import it in your code
88
|
99
LL + extern crate abc;
1010
|
@@ -15,7 +15,7 @@ error[E0432]: unresolved import `abc`
1515
LL | use abc::{a, bbb, cccccc};
1616
| ^^^ use of unresolved module or unlinked crate `abc`
1717
|
18-
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
18+
help: you might be missing a crate named `abc`, add it to your project and import it in your code
1919
|
2020
LL + extern crate abc;
2121
|
@@ -26,7 +26,7 @@ error[E0432]: unresolved import `a_very_long_name`
2626
LL | use a_very_long_name::{el, el2};
2727
| ^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `a_very_long_name`
2828
|
29-
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
29+
help: you might be missing a crate named `a_very_long_name`, add it to your project and import it in your code
3030
|
3131
LL + extern crate a_very_long_name;
3232
|

tests/ui/imports/issue-36881.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `issue_36881_aux`
44
LL | use issue_36881_aux::Foo;
55
| ^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `issue_36881_aux`
66
|
7-
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
7+
help: you might be missing a crate named `issue_36881_aux`, add it to your project and import it in your code
88
|
99
LL + extern crate issue_36881_aux;
1010
|

tests/ui/imports/issue-37887.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `test`
44
LL | use test::*;
55
| ^^^^ use of unresolved module or unlinked crate `test`
66
|
7-
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
7+
help: you might be missing a crate named `test`, add it to your project and import it in your code
88
|
99
LL + extern crate test;
1010
|

tests/ui/imports/issue-53269.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `nonexistent_module`
44
LL | use nonexistent_module::mac;
55
| ^^^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `nonexistent_module`
66
|
7-
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
7+
help: you might be missing a crate named `nonexistent_module`, add it to your project and import it in your code
88
|
99
LL + extern crate nonexistent_module;
1010
|

tests/ui/imports/issue-55457.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ error[E0432]: unresolved import `non_existent`
1313
LL | use non_existent::non_existent;
1414
| ^^^^^^^^^^^^ use of unresolved module or unlinked crate `non_existent`
1515
|
16-
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
16+
help: you might be missing a crate named `non_existent`, add it to your project and import it in your code
1717
|
1818
LL + extern crate non_existent;
1919
|

tests/ui/imports/issue-81413.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `doesnt_exist`
44
LL | pub use doesnt_exist::*;
55
| ^^^^^^^^^^^^ use of unresolved module or unlinked crate `doesnt_exist`
66
|
7-
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
7+
help: you might be missing a crate named `doesnt_exist`, add it to your project and import it in your code
88
|
99
LL + extern crate doesnt_exist;
1010
|

tests/ui/imports/tool-mod-child.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `cli
44
LL | use clippy::a::b;
55
| ^^^^^^ use of unresolved module or unlinked crate `clippy`
66
|
7-
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
7+
help: you might be missing a crate named `clippy`, add it to your project and import it in your code
88
|
99
LL + extern crate clippy;
1010
|
@@ -15,7 +15,7 @@ error[E0432]: unresolved import `clippy`
1515
LL | use clippy::a;
1616
| ^^^^^^ use of unresolved module or unlinked crate `clippy`
1717
|
18-
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
18+
help: you might be missing a crate named `clippy`, add it to your project and import it in your code
1919
|
2020
LL + extern crate clippy;
2121
|
@@ -26,7 +26,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `rus
2626
LL | use rustdoc::a::b;
2727
| ^^^^^^^ use of unresolved module or unlinked crate `rustdoc`
2828
|
29-
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
29+
help: you might be missing a crate named `rustdoc`, add it to your project and import it in your code
3030
|
3131
LL + extern crate rustdoc;
3232
|
@@ -37,7 +37,7 @@ error[E0432]: unresolved import `rustdoc`
3737
LL | use rustdoc::a;
3838
| ^^^^^^^ use of unresolved module or unlinked crate `rustdoc`
3939
|
40-
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
40+
help: you might be missing a crate named `rustdoc`, add it to your project and import it in your code
4141
|
4242
LL + extern crate rustdoc;
4343
|

0 commit comments

Comments
 (0)