Skip to content

Commit 2555f3b

Browse files
committed
Better diagnostics for env! where variable contains escape
1 parent 4d941cd commit 2555f3b

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

compiler/rustc_builtin_macros/src/env.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,16 @@ pub fn expand_env<'cx>(
6363
Some(exprs) => exprs.into_iter(),
6464
};
6565

66-
let Some((var, _style)) = expr_to_string(cx, exprs.next().unwrap(), "expected string literal") else {
66+
let var_expr = exprs.next().unwrap();
67+
let Some((var, _)) = expr_to_string(cx, var_expr.clone(), "expected string literal") else {
6768
return DummyResult::any(sp);
6869
};
6970

7071
let custom_msg = match exprs.next() {
7172
None => None,
7273
Some(second) => match expr_to_string(cx, second, "expected string literal") {
7374
None => return DummyResult::any(sp),
74-
Some((s, _style)) => Some(s),
75+
Some((s, _)) => Some(s),
7576
},
7677
};
7778

@@ -80,10 +81,15 @@ pub fn expand_env<'cx>(
8081
cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value));
8182
let e = match value {
8283
None => {
84+
// Use the string literal in the code in the diagnostic to avoid confusing diagnostics,
85+
// e.g. when the literal contains escape sequences.
86+
let ast::ExprKind::Lit(ast::token::Lit { kind: ast::token::LitKind::Str, symbol: original_var, ..}) = &var_expr.kind else {
87+
unreachable!("`expr_to_string` ensures this is a string lit")
88+
};
8389
cx.emit_err(errors::EnvNotDefined {
8490
span: sp,
8591
msg: custom_msg,
86-
var,
92+
var: *original_var,
8793
help: custom_msg.is_none().then(|| help_for_missing_env_var(var.as_str())),
8894
});
8995
return DummyResult::any(sp);

tests/ui/extenv/extenv-escaped-var.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
env!("\t"); //~ERROR environment variable `\t` not defined at compile time
3+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: environment variable `\t` not defined at compile time
2+
--> $DIR/extenv-escaped-var.rs:2:5
3+
|
4+
LL | env!("\t");
5+
| ^^^^^^^^^^
6+
|
7+
= help: use `std::env::var("\t")` to read the variable at run time
8+
= note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)
9+
10+
error: aborting due to previous error
11+

tests/ui/extenv/issue-110547.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
error: environment variable ` ` not defined at compile time
1+
error: environment variable `\t` not defined at compile time
22
--> $DIR/issue-110547.rs:4:5
33
|
44
LL | env!{"\t"};
55
| ^^^^^^^^^^
66
|
7-
= help: use `std::env::var(" ")` to read the variable at run time
7+
= help: use `std::env::var("\t")` to read the variable at run time
88
= note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)
99

10-
error: environment variable ` ` not defined at compile time
10+
error: environment variable `\t` not defined at compile time
1111
--> $DIR/issue-110547.rs:5:5
1212
|
1313
LL | env!("\t");
1414
| ^^^^^^^^^^
1515
|
16-
= help: use `std::env::var(" ")` to read the variable at run time
16+
= help: use `std::env::var("\t")` to read the variable at run time
1717
= note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)
1818

19-
error: environment variable `` not defined at compile time
19+
error: environment variable `\u{2069}` not defined at compile time
2020
--> $DIR/issue-110547.rs:6:5
2121
|
2222
LL | env!("\u{2069}");
2323
| ^^^^^^^^^^^^^^^^
2424
|
25-
= help: use `std::env::var("")` to read the variable at run time
25+
= help: use `std::env::var("\u{2069}")` to read the variable at run time
2626
= note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)
2727

2828
error: aborting due to 3 previous errors

0 commit comments

Comments
 (0)