about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-05-14 08:21:39 +0200
committerGitHub <noreply@github.com>2023-05-14 08:21:39 +0200
commit0b8f2bfe5f9f405162cfeaea9753c020885f2efb (patch)
tree47ae51b46941779a717d1888763121a8fdbf7acd
parentad6ab11234ae885913229f6de2c4465bdc0d76f3 (diff)
parent2555f3bbcf0c42e2405912ff93318107596f698b (diff)
downloadrust-0b8f2bfe5f9f405162cfeaea9753c020885f2efb.tar.gz
rust-0b8f2bfe5f9f405162cfeaea9753c020885f2efb.zip
Rollup merge of #111463 - clubby789:env-escaped-var, r=cjgillot
Better diagnostics for `env!` where variable contains escape

Fixes #110559
-rw-r--r--compiler/rustc_builtin_macros/src/env.rs12
-rw-r--r--tests/ui/extenv/extenv-escaped-var.rs3
-rw-r--r--tests/ui/extenv/extenv-escaped-var.stderr11
-rw-r--r--tests/ui/extenv/issue-110547.stderr12
4 files changed, 29 insertions, 9 deletions
diff --git a/compiler/rustc_builtin_macros/src/env.rs b/compiler/rustc_builtin_macros/src/env.rs
index 58c972738c4..8f64e332861 100644
--- a/compiler/rustc_builtin_macros/src/env.rs
+++ b/compiler/rustc_builtin_macros/src/env.rs
@@ -63,7 +63,8 @@ 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);
     };
 
@@ -71,7 +72,7 @@ pub fn expand_env<'cx>(
         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),
         },
     };
 
@@ -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);
diff --git a/tests/ui/extenv/extenv-escaped-var.rs b/tests/ui/extenv/extenv-escaped-var.rs
new file mode 100644
index 00000000000..d898feb78c6
--- /dev/null
+++ b/tests/ui/extenv/extenv-escaped-var.rs
@@ -0,0 +1,3 @@
+fn main() {
+    env!("\t"); //~ERROR environment variable `\t` not defined at compile time
+}
diff --git a/tests/ui/extenv/extenv-escaped-var.stderr b/tests/ui/extenv/extenv-escaped-var.stderr
new file mode 100644
index 00000000000..25e218c63f3
--- /dev/null
+++ b/tests/ui/extenv/extenv-escaped-var.stderr
@@ -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
+
diff --git a/tests/ui/extenv/issue-110547.stderr b/tests/ui/extenv/issue-110547.stderr
index 1219630d346..10589ec2f54 100644
--- a/tests/ui/extenv/issue-110547.stderr
+++ b/tests/ui/extenv/issue-110547.stderr
@@ -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