-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Garbage value when accessing a reference into a field/element of a const value. #49955
Comments
It appears that (AST) borrowck thinks that the constant will get promoted to a static, but it isn't. NLL and pre-1.20 borrowck correctly error with a "temporary value does not live long enough" error. |
cc @eddyb (const promotion) |
Uhhh this should be promoted. AFAIK, EDIT: looks like we generate cc @rust-lang/compiler |
After some discussion with @eddyb, we realized this is kind of a more general bug than originally thought. There are also some other troublesome examples: https://play.rust-lang.org/?gist=5d024782ad35c5e58e000ecea7158cfd&version=stable&mode=debug Therefore, bumping to P-high. We're gonna try to fix by improving the constant promoter and how it handles projections. |
This is much worse (I've updated the issue description), and it happens on all projections, e.g.: fn get_element() -> &'static u32 { &(0,).0 } I think I messed up: instead of promoting only |
Miri "catches" this kind of UB with an ICE if you try it inside a const fn (see #50529) |
rustc_mir: allow promotion of promotable temps indexed at runtime. Fixes #49955. r? @nikomatsakis
All of the below code seems to be neccesary to reproduce the bug; If
ALL_THE_NUMS
is accessed directly within main, the program works as expected.Example:
I expected to see this happen:
Num is 1
as the output, or a compiler error if I'm doing something wrong here (am I?)Instead, this happened:
Num is 32766
or some other garbage value is outputMeta
rustc --version --verbose
:Also tested on master-branch version: (same result)
The text was updated successfully, but these errors were encountered: