-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Compiler accepts return mut ref to local var on no longer valid stackframe #46557
Comments
This program is rejected by rustc 1.20.0, but is accepted by rustc 1.21.0. Bisecting now. |
@mauricioc This is due to #38865. It wasn't a feature in 1.20. 1.20 also doesn't compile the immutable (safe) variant: fn gimme_static() -> &'static u32 {
let ref x = 1234543;
x
} |
An alternative way to define gimme_static_mut:
Gives the expected compiler error:
Looks like an oversight with the |
Fixed by MIR borrowck. |
Which isn't enabled in Rust 1.23 beta, so we still need to fix this in some other fashion for the next release, correct? |
I see that Niko has commented though I don't see a priority assigned yet, so nominating for completeness. |
This issue continues to ship in stable Rust 1.23 |
My expectation is that this will be fixed (along with a number of other bugs) when we transition to the MIR-based borrow checker (and NLL). I guess it's worth discussing whether we feel we ought to try to fix faster than that. |
Well before we can discuss whether we ought to get a fix in "faster", we'd need to have an official estimate of when MIR borrowck/NLL is expected to land in stable. :) I've gotten the impression that people have been deliberately noncommittal towards a timeframe up til now, but is it safe to assume that the answer is "hopefully stable sometime in 2018"? |
@bstrie definitely -- first half of 2018 is my general plan, hopefully towards the beginning of that. |
triage: P-medium Plan is to fix this as part of the transition to NLL. I'll be adding a test case (marked with |
Closes rust-lang#47366 Adapt the sample code from the issues into mir-borrowck/nll test cases.
Add NLL tests for rust-lang#46557 and rust-lang#38899 This adapts the sample code from the two issues into test code. Closes rust-lang#46557 Closes rust-lang#38899 r? @nikomatsakis
Should this be close while |
I think it's reasonable to say this is a P-medium issue until the fix is in master and enabled by default (I'm open to disagreement though!). |
Well, it was intentional to close the issues now that there is a test, so as to keep the issue tracked (and in particular the WG-compielr-nll) more oriented at "actionable" issues. In other words, this is basically -- at this point -- a dup of the NLL tracking issue. I'm still inclined to close, I don't know what value there is from keeping it open. But I too am willing to give away if others feel really strongly. We could perhaps compromise by removing WG-compiler-nll or something, so that I don't have to keep looking at the same issues over and over. But it's kind of annoying. |
More specifically, the idea was to mark this as a "duplicate" of #47366, which admittedly wasn't quite clear. |
Er, perhaps that was premature. |
Decided to just remove WG-compiler-nll for such issues, so we can keep them open but they can stop annoying me. |
I took a peek at this. The problem seems to be that we decide that the expression is promotable — which is true, but we ought not to be promoting a |
Ah, I suspect I know the problem. Note that this variant is properly rejected: fn gimme_static_mut() -> &'static mut u32 {
match 123443 {
ref mut x => x,
}
}
fn main() {
let x = gimme_static_mut();
let y = gimme_static_mut();
*y = 42;
let a = *x;
println!("a: {}", a);
} I think the error is that we specifically mark things that are rust/src/librustc_passes/rvalue_promotion.rs Lines 227 to 239 in 577a5b2
but we don't do anything similar for cc @eddyb — sound plausible? |
I imagine then that we have to add some similar code here: rust/src/librustc_passes/rvalue_promotion.rs Lines 204 to 206 in 577a5b2
basically, checking if the variable being assigned has a |
Here is a bonus bug. This compiles: fn gimme_static_mut() -> &'static mut u32 {
match (123443,) {
(ref mut x,) => x,
}
} |
Fix in #51274 |
since this was fixed for AST borrow-check, it no longer should have the NLL-fixed-by-NLL label. |
Spotted on reddit: https://www.reddit.com/r/rust/comments/7i64sy/safe_function_returns_static_mut/
credit to jDomantas
I tried this code: playground
This is accepted by the compiler and prints 42.
I expected to see this happen: The program should be rejected by the compiler with a lifetime error
Instead this happened: The program was accepted and miscompiled badly
The assembly code clearly demonstrates the problem, a local variable is created for the literal, its address is taken and returned:
Meta
Latest version on playground
The text was updated successfully, but these errors were encountered: