Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions compiler/rustc_builtin_macros/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,16 @@ pub fn expand_env<'cx>(
Some(exprs) => exprs.into_iter(),
};

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

let custom_msg = match exprs.next() {
None => None,
Some(second) => match expr_to_string(cx, second, "expected string literal") {
None => return DummyResult::any(sp),
Some((s, _style)) => Some(s),
Some((s, _)) => Some(s),
},
};

Expand All @@ -80,10 +81,15 @@ pub fn expand_env<'cx>(
cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value));
let e = match value {
None => {
// Use the string literal in the code in the diagnostic to avoid confusing diagnostics,
// e.g. when the literal contains escape sequences.
let ast::ExprKind::Lit(ast::token::Lit { kind: ast::token::LitKind::Str, symbol: original_var, ..}) = &var_expr.kind else {
unreachable!("`expr_to_string` ensures this is a string lit")
};
cx.emit_err(errors::EnvNotDefined {
span: sp,
msg: custom_msg,
var,
var: *original_var,
help: custom_msg.is_none().then(|| help_for_missing_env_var(var.as_str())),
});
return DummyResult::any(sp);
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/extenv/extenv-escaped-var.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
env!("\t"); //~ERROR environment variable `\t` not defined at compile time
}
11 changes: 11 additions & 0 deletions tests/ui/extenv/extenv-escaped-var.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: environment variable `\t` not defined at compile time
--> $DIR/extenv-escaped-var.rs:2:5
|
LL | env!("\t");
| ^^^^^^^^^^
|
= help: use `std::env::var("\t")` to read the variable at run time
= note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

12 changes: 6 additions & 6 deletions tests/ui/extenv/issue-110547.stderr
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
error: environment variable ` ` not defined at compile time
error: environment variable `\t` not defined at compile time
--> $DIR/issue-110547.rs:4:5
|
LL | env!{"\t"};
| ^^^^^^^^^^
|
= help: use `std::env::var(" ")` to read the variable at run time
= help: use `std::env::var("\t")` to read the variable at run time
= note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)

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

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

error: aborting due to 3 previous errors
Expand Down