diff options
| author | bors <bors@rust-lang.org> | 2024-04-01 05:18:51 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-04-01 05:18:51 +0000 |
| commit | 7f84ede33d60599a4a97697cdd59e8bf96c85677 (patch) | |
| tree | 0b7d1012b86dd60e6a76d3c19387097a4ebe993a /library/core/src | |
| parent | defef8658e8f740cc8d2818b5b96441071e9a7a6 (diff) | |
| parent | 0bbaa2505be6d7e6483a0dcbe585c96684e73da5 (diff) | |
| download | rust-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 'library/core/src')
| -rw-r--r-- | library/core/src/macros/mod.rs | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index 574a357b44a..6da05a1ca86 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -1053,7 +1053,8 @@ pub(crate) mod builtin { /// /// If the environment variable is not defined, then a compilation error /// will be emitted. To not emit a compile error, use the [`option_env!`] - /// macro instead. + /// macro instead. A compilation error will also be emitted if the + /// environment variable is not a vaild Unicode string. /// /// # Examples /// |
