-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Compile error when directly using &mut
in explicity const context
#140126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
&mut
in explicity const context directly&mut
in explicity const context
This seems to happen only with empty arrays. It is perfectly fine to have two &mut references pointing to the same place if the references refer only to a zero-sized type (such as empty arrays). Therefore, I don't think this can lead to UB. I'm guessing that this probably isn't intended though. |
It appears that constant promotion, for some reason, works on fn require_static<T>(_: &'static mut T) {}
fn works() {
require_static::<[i32; 0]>(&mut []);
}
fn fails() {
// error[E0716]: temporary value dropped while borrowed
require_static::<()>(&mut ());
} |
Relevant past discussion: #110288 (comment) |
From #103821 (comment):
|
From #76411:
And: rust/compiler/rustc_mir_transform/src/promote_consts.rs Lines 411 to 413 in 9bfa31f
Based on this, I believe that the Note that this also compiles fine. const ALSO_COMPILES: &mut [f32] = {
let x = &mut [];
x
}; |
I tried this code:
I expected to see this happen: Both constants compile or both fail to compile.
Instead, this happened:
BAD
failed to compile.I'm not sure which is the intended behavior or if it is UB. I don't know the rules around constants that hold mutable references.
This fails to compile in constants, statics and
const {}
blocks. This succeeds when using const functions to wrap the expressions, even in constants, statics, andconst {}
blocks.Additional examples: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=7b7a327957429e5706e520755d9cd467
See also an ICE related to this "ICE
error performing operation: fully_perform
" #140123Meta
rustc --version --verbose
:Backtrace
RUST_BACKTRACE=1 cargo +nightly build
The text was updated successfully, but these errors were encountered: