Skip to content

Commit 92fe403

Browse files
authored
Unrolled build for #152302
Rollup merge of #152302 - ShE3py:cargo-envs, r=davidtwco fix: don't suggest replacing `env!("CARGO_BIN_NAME")` with itself Some environment variables (e.g. `CARGO_BIN_NAME`) are only available for some targets ([Cargo Targets](https://doc.rust-lang.org/cargo/reference/cargo-targets.html), [§ Environment variables Cargo sets for crates](https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates)), and the current diagnostic when copy-pasting code from a binary to a library is kinda confusing: ```rs const _: &str = env!("CARGO_BIN_NAME"); ``` Before: ``` error: environment variable `CARGO_BIN_NAME` not defined at compile time --> lib.rs:1:17 | 1 | const _: &str = env!("CARGO_BIN_NAME"); | ^^^^^^^^^^^^^^^^^^^^^^ | = help: there is a similar Cargo environment variable: `CARGO_BIN_NAME` ``` After: ``` error: environment variable `CARGO_BIN_NAME` not defined at compile time --> lib.rs:1:17 | 1 | const _: &str = env!("CARGO_BIN_NAME"); | ^^^^^^^^^^^^^^^^^^^^^^ | = help: `CARGO_BIN_NAME` may not be available for the current Cargo target = help: Cargo sets build script variables at run time. Use `std::env::var("CARGO_BIN_NAME")` instead ``` @rustbot label +T-compiler +A-diagnostics +D-confusing
2 parents 0c68443 + 57650ee commit 92fe403

File tree

5 files changed

+57
-25
lines changed

5 files changed

+57
-25
lines changed

compiler/rustc_builtin_macros/src/env.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,21 @@ pub(crate) fn expand_env<'cx>(
140140
unreachable!("`expr_to_string` ensures this is a string lit")
141141
};
142142

143+
let var = var.as_str();
143144
let guar = match err {
144145
VarError::NotPresent => {
145146
if let Some(msg_from_user) = custom_msg {
146147
cx.dcx()
147148
.emit_err(errors::EnvNotDefinedWithUserMessage { span, msg_from_user })
148-
} else if let Some(suggested_var) = find_similar_cargo_var(var.as_str()) {
149+
} else if let Some(suggested_var) = find_similar_cargo_var(var)
150+
&& suggested_var != var
151+
{
149152
cx.dcx().emit_err(errors::EnvNotDefined::CargoEnvVarTypo {
150153
span,
151154
var: *symbol,
152155
suggested_var: Symbol::intern(suggested_var),
153156
})
154-
} else if is_cargo_env_var(var.as_str()) {
157+
} else if is_cargo_env_var(var) {
155158
cx.dcx().emit_err(errors::EnvNotDefined::CargoEnvVar {
156159
span,
157160
var: *symbol,
@@ -177,7 +180,7 @@ pub(crate) fn expand_env<'cx>(
177180
ExpandResult::Ready(MacEager::expr(e))
178181
}
179182

180-
/// Returns `true` if an environment variable from `env!` is one used by Cargo.
183+
/// Returns `true` if an environment variable from `env!` could be one used by Cargo.
181184
fn is_cargo_env_var(var: &str) -> bool {
182185
var.starts_with("CARGO_")
183186
|| var.starts_with("DEP_")
@@ -187,25 +190,28 @@ fn is_cargo_env_var(var: &str) -> bool {
187190
const KNOWN_CARGO_VARS: &[&str] = &[
188191
// List of known Cargo environment variables that are set for crates (not build scripts, OUT_DIR etc).
189192
// See: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates
190-
"CARGO_PKG_VERSION",
191-
"CARGO_PKG_VERSION_MAJOR",
192-
"CARGO_PKG_VERSION_MINOR",
193-
"CARGO_PKG_VERSION_PATCH",
194-
"CARGO_PKG_VERSION_PRE",
193+
// tidy-alphabetical-start
194+
"CARGO_BIN_NAME",
195+
"CARGO_CRATE_NAME",
196+
"CARGO_MANIFEST_DIR",
197+
"CARGO_MANIFEST_PATH",
195198
"CARGO_PKG_AUTHORS",
196-
"CARGO_PKG_NAME",
197199
"CARGO_PKG_DESCRIPTION",
198200
"CARGO_PKG_HOMEPAGE",
199-
"CARGO_PKG_REPOSITORY",
200201
"CARGO_PKG_LICENSE",
201202
"CARGO_PKG_LICENSE_FILE",
202-
"CARGO_PKG_RUST_VERSION",
203+
"CARGO_PKG_NAME",
203204
"CARGO_PKG_README",
204-
"CARGO_MANIFEST_DIR",
205-
"CARGO_MANIFEST_PATH",
206-
"CARGO_CRATE_NAME",
207-
"CARGO_BIN_NAME",
205+
"CARGO_PKG_REPOSITORY",
206+
"CARGO_PKG_RUST_VERSION",
207+
"CARGO_PKG_VERSION",
208+
"CARGO_PKG_VERSION_MAJOR",
209+
"CARGO_PKG_VERSION_MINOR",
210+
"CARGO_PKG_VERSION_PATCH",
211+
"CARGO_PKG_VERSION_PRE",
208212
"CARGO_PRIMARY_PACKAGE",
213+
"CARGO_TARGET_TMPDIR",
214+
// tidy-alphabetical-end
209215
];
210216

211217
fn find_similar_cargo_var(var: &str) -> Option<&'static str> {
@@ -219,7 +225,13 @@ fn find_similar_cargo_var(var: &str) -> Option<&'static str> {
219225
let mut best_distance = usize::MAX;
220226

221227
for &known_var in KNOWN_CARGO_VARS {
222-
if let Some(distance) = edit_distance(var, known_var, max_dist) {
228+
if let Some(mut distance) = edit_distance(var, known_var, max_dist) {
229+
// assume `PACKAGE` to equals `PKG`
230+
// (otherwise, `d("CARGO_PACKAGE_NAME", "CARGO_PKG_NAME") == d("CARGO_PACKAGE_NAME", "CARGO_CRATE_NAME") == 4`)
231+
if var.contains("PACKAGE") && known_var.contains("PKG") {
232+
distance = distance.saturating_sub(const { "PACKAGE".len() - "PKG".len() }) // == d("PACKAGE", "PKG")
233+
}
234+
223235
if distance < best_distance {
224236
best_distance = distance;
225237
best_match = Some(known_var);

compiler/rustc_builtin_macros/src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for EnvNotDefinedWithUserMessag
545545
#[derive(Diagnostic)]
546546
pub(crate) enum EnvNotDefined<'a> {
547547
#[diag("environment variable `{$var}` not defined at compile time")]
548+
#[help("`{$var}` may not be available for the current Cargo target")]
548549
#[help(
549550
"Cargo sets build script variables at run time. Use `std::env::var({$var_expr})` instead"
550551
)]

tests/ui/env-macro/env-cargo-var-typo-issue-148439.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@ edition: 2021
2+
//@ compile-flags: --crate-type=lib
23

34
// Regression test for issue #148439
45
// Ensure that when using misspelled Cargo environment variables in env!(),
@@ -44,7 +45,14 @@ fn test_cargo_unknown_var() {
4445
// Cargo-prefixed but not similar to any known variable
4546
let _ = env!("CARGO_SOMETHING_TOTALLY_UNKNOWN");
4647
//~^ ERROR environment variable `CARGO_SOMETHING_TOTALLY_UNKNOWN` not defined at compile time
48+
//~| HELP `CARGO_SOMETHING_TOTALLY_UNKNOWN` may not be available for the current Cargo target
4749
//~| HELP Cargo sets build script variables at run time. Use `std::env::var("CARGO_SOMETHING_TOTALLY_UNKNOWN")` instead
4850
}
4951

50-
fn main() {}
52+
fn test_cargo_conditional_var() {
53+
// Only set for binairies
54+
let _ = env!("CARGO_BIN_NAME");
55+
//~^ ERROR environment variable `CARGO_BIN_NAME` not defined at compile time
56+
//~| HELP `CARGO_BIN_NAME` may not be available for the current Cargo target
57+
//~| HELP Cargo sets build script variables at run time. Use `std::env::var("CARGO_BIN_NAME")` instead
58+
}
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,68 @@
11
error: environment variable `CARGO_PACKAGE_VERSION` not defined at compile time
2-
--> $DIR/env-cargo-var-typo-issue-148439.rs:7:13
2+
--> $DIR/env-cargo-var-typo-issue-148439.rs:8:13
33
|
44
LL | let _ = env!("CARGO_PACKAGE_VERSION");
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= help: there is a similar Cargo environment variable: `CARGO_PKG_VERSION`
88

99
error: environment variable `CARGO_PACKAGE_NAME` not defined at compile time
10-
--> $DIR/env-cargo-var-typo-issue-148439.rs:13:13
10+
--> $DIR/env-cargo-var-typo-issue-148439.rs:14:13
1111
|
1212
LL | let _ = env!("CARGO_PACKAGE_NAME");
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1414
|
1515
= help: there is a similar Cargo environment variable: `CARGO_PKG_NAME`
1616

1717
error: environment variable `CARGO_PACKAGE_AUTHORS` not defined at compile time
18-
--> $DIR/env-cargo-var-typo-issue-148439.rs:19:13
18+
--> $DIR/env-cargo-var-typo-issue-148439.rs:20:13
1919
|
2020
LL | let _ = env!("CARGO_PACKAGE_AUTHORS");
2121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2222
|
2323
= help: there is a similar Cargo environment variable: `CARGO_PKG_AUTHORS`
2424

2525
error: environment variable `CARGO_MANIFEST_DIRECTORY` not defined at compile time
26-
--> $DIR/env-cargo-var-typo-issue-148439.rs:25:13
26+
--> $DIR/env-cargo-var-typo-issue-148439.rs:26:13
2727
|
2828
LL | let _ = env!("CARGO_MANIFEST_DIRECTORY");
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3030
|
3131
= help: there is a similar Cargo environment variable: `CARGO_MANIFEST_DIR`
3232

3333
error: environment variable `CARGO_PKG_VERSIO` not defined at compile time
34-
--> $DIR/env-cargo-var-typo-issue-148439.rs:31:13
34+
--> $DIR/env-cargo-var-typo-issue-148439.rs:32:13
3535
|
3636
LL | let _ = env!("CARGO_PKG_VERSIO");
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^
3838
|
3939
= help: there is a similar Cargo environment variable: `CARGO_PKG_VERSION`
4040

4141
error: environment variable `MY_CUSTOM_VAR` not defined at compile time
42-
--> $DIR/env-cargo-var-typo-issue-148439.rs:38:13
42+
--> $DIR/env-cargo-var-typo-issue-148439.rs:39:13
4343
|
4444
LL | let _ = env!("MY_CUSTOM_VAR");
4545
| ^^^^^^^^^^^^^^^^^^^^^
4646
|
4747
= help: use `std::env::var("MY_CUSTOM_VAR")` to read the variable at run time
4848

4949
error: environment variable `CARGO_SOMETHING_TOTALLY_UNKNOWN` not defined at compile time
50-
--> $DIR/env-cargo-var-typo-issue-148439.rs:45:13
50+
--> $DIR/env-cargo-var-typo-issue-148439.rs:46:13
5151
|
5252
LL | let _ = env!("CARGO_SOMETHING_TOTALLY_UNKNOWN");
5353
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5454
|
55+
= help: `CARGO_SOMETHING_TOTALLY_UNKNOWN` may not be available for the current Cargo target
5556
= help: Cargo sets build script variables at run time. Use `std::env::var("CARGO_SOMETHING_TOTALLY_UNKNOWN")` instead
5657

57-
error: aborting due to 7 previous errors
58+
error: environment variable `CARGO_BIN_NAME` not defined at compile time
59+
--> $DIR/env-cargo-var-typo-issue-148439.rs:54:13
60+
|
61+
LL | let _ = env!("CARGO_BIN_NAME");
62+
| ^^^^^^^^^^^^^^^^^^^^^^
63+
|
64+
= help: `CARGO_BIN_NAME` may not be available for the current Cargo target
65+
= help: Cargo sets build script variables at run time. Use `std::env::var("CARGO_BIN_NAME")` instead
66+
67+
error: aborting due to 8 previous errors
5868

tests/ui/env-macro/env-not-defined-default.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: environment variable `CARGO__HOPEFULLY_NOT_DEFINED__` not defined at comp
44
LL | env!("CARGO__HOPEFULLY_NOT_DEFINED__");
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7+
= help: `CARGO__HOPEFULLY_NOT_DEFINED__` may not be available for the current Cargo target
78
= help: Cargo sets build script variables at run time. Use `std::env::var("CARGO__HOPEFULLY_NOT_DEFINED__")` instead
89

910
error: aborting due to 1 previous error

0 commit comments

Comments
 (0)