Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 68af421

Browse files
committedDec 20, 2024·
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

Lines changed: 30 additions & 12 deletions
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

Lines changed: 2 additions & 2 deletions
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
|

0 commit comments

Comments
 (0)