about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-04-01 05:18:51 +0000
committerbors <bors@rust-lang.org>2024-04-01 05:18:51 +0000
commit7f84ede33d60599a4a97697cdd59e8bf96c85677 (patch)
tree0b7d1012b86dd60e6a76d3c19387097a4ebe993a /tests
parentdefef8658e8f740cc8d2818b5b96441071e9a7a6 (diff)
parent0bbaa2505be6d7e6483a0dcbe585c96684e73da5 (diff)
downloadrust-7f84ede33d60599a4a97697cdd59e8bf96c85677.tar.gz
rust-7f84ede33d60599a4a97697cdd59e8bf96c85677.zip
Auto merge of #122663 - beetrees:non-unicode-env-error, r=TaKO8Ki
Fix error message for `env!` when env var is not valid Unicode

Currently (without this PR) the `env!` macro emits an ```environment variable `name` not defined at compile time``` error when the environment variable is defined, but not a valid Unicode string. This PR introduces a separate more accurate error message, and a test to verify this behaviour.

For reference, before this PR, the new test would have outputted:
```
error: environment variable `NON_UNICODE_VAR` not defined at compile time
 --> non_unicode_env.rs:2:13
  |
2 |     let _ = env!("NON_UNICODE_VAR");
  |             ^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: use `std::env::var("NON_UNICODE_VAR")` 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 1 previous error
```

whereas with this PR, the test ouputs:
```
error: environment variable `NON_UNICODE_VAR` is not a valid Unicode string
 --> non_unicode_env.rs:2:13
  |
2 |     let _ = env!("NON_UNICODE_VAR");
  |             ^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error
```
Diffstat (limited to 'tests')
-rw-r--r--tests/run-make/non-unicode-env/non_unicode_env.rs3
-rw-r--r--tests/run-make/non-unicode-env/non_unicode_env.stderr10
-rw-r--r--tests/run-make/non-unicode-env/rmake.rs14
3 files changed, 27 insertions, 0 deletions
diff --git a/tests/run-make/non-unicode-env/non_unicode_env.rs b/tests/run-make/non-unicode-env/non_unicode_env.rs
new file mode 100644
index 00000000000..865fc937365
--- /dev/null
+++ b/tests/run-make/non-unicode-env/non_unicode_env.rs
@@ -0,0 +1,3 @@
+fn main() {
+    let _ = env!("NON_UNICODE_VAR");
+}
diff --git a/tests/run-make/non-unicode-env/non_unicode_env.stderr b/tests/run-make/non-unicode-env/non_unicode_env.stderr
new file mode 100644
index 00000000000..c4dcd7b2eb7
--- /dev/null
+++ b/tests/run-make/non-unicode-env/non_unicode_env.stderr
@@ -0,0 +1,10 @@
+error: environment variable `NON_UNICODE_VAR` is not a valid Unicode string
+ --> non_unicode_env.rs:2:13
+  |
+2 |     let _ = env!("NON_UNICODE_VAR");
+  |             ^^^^^^^^^^^^^^^^^^^^^^^
+  |
+  = note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to 1 previous error
+
diff --git a/tests/run-make/non-unicode-env/rmake.rs b/tests/run-make/non-unicode-env/rmake.rs
new file mode 100644
index 00000000000..ba4aa1609b5
--- /dev/null
+++ b/tests/run-make/non-unicode-env/rmake.rs
@@ -0,0 +1,14 @@
+extern crate run_make_support;
+
+use run_make_support::rustc;
+
+fn main() {
+    #[cfg(unix)]
+    let non_unicode: &std::ffi::OsStr = std::os::unix::ffi::OsStrExt::from_bytes(&[0xFF]);
+    #[cfg(windows)]
+    let non_unicode: std::ffi::OsString = std::os::windows::ffi::OsStringExt::from_wide(&[0xD800]);
+    let output = rustc().input("non_unicode_env.rs").env("NON_UNICODE_VAR", non_unicode).run_fail();
+    let actual = std::str::from_utf8(&output.stderr).unwrap();
+    let expected = std::fs::read_to_string("non_unicode_env.stderr").unwrap();
+    assert_eq!(actual, expected);
+}