Probable bug with the output of option_env!("NON_UNICODE_ENV_VAR")
#122669
Labels
A-macros
Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
C-bug
Category: This is a bug.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
T-libs-api
Relevant to the library API team, which will review and decide on the PR/issue.
I tried this code:
compiler with
NON_UNICODE_ENV_VAR=$'\xFF' rustc code.rs
(using Bash shell syntax, any non-Unicode environment variable value forNON_UNICODE_ENV_VAR
will work).I expected to see this happen: a
Some
value, as the documentation currently states "If the named environment variable is present at compile time, this will expand into an expression of typeOption<&'static str>
whose value isSome
of the value of the environment variable".Instead, this happened: The program outputted:
Currently, the documentation doesn't consider the possibility of a non-Unicode environment variable value (as only Unicode values can be expressed as a &'static str), and the implementation emits
None
, the same as for a non-existent environment variable.There are three possibilities that I can think of:
&'static str
, and the documentation should also be updated to reflect this. Note that this would be a breaking change to the compiler itself, but would be allowed as it would be a bugfix.Personally, I believe 1 to be the case, as the current behaviour is unexpected and leads to code patterns like
let was_defined_at_compile_time = option_env!("VAR").is_some()
silently returning incorrect results. There doesn't seem to be a "correct" value to return, asNone
is already taken by "does not exist" andSome(&str)
cannot represent a non-Unicode value. It seems unlikely anyone is relying on the current behaviour given Rust's very UTF-8 everywhere design, the fact the same result can be achieved much more easily by just not setting the environment variable, and the platform-specific nature of what non-Unicode values can exist (Windows UTF-16 unpaired surrogates vs. Unix invalid UTF-8 bytes). It therefore seems to be more likely to occur when someone has mistyped something in their shell or due to data corruption, which the current output would make rather confusing to track down (as it would seem to say the variable was missing, rather than set incorrectly).Meta
rustc --version --verbose
:I believe the decision of what to do is down to libs-api, so
@rustbot label +T-libs-api
The text was updated successfully, but these errors were encountered: