-
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
Unreachable code is not borrow-checked #91377
Comments
I'm not sure if this is really a regression since I'm pretty sure it's expected behavior. Borrow-checking is now a flow-based analysis, so unreachable code can't be borrow-checked I think. You can also use uninitialized variables in unreachable code, since initialization-checking is also a flow-based analysis (playground): pub fn foo() -> i32 {
loop {}
let x;
x
} cc #79735, a similar issue but about the unsafety-checker |
Is this closely related to the borrowck code? I notice that whether this is accepted also depends on whether the edition+rustc version uses NLL borrowck or not. |
I think this is expected behavior, per nikomatsakis. I'm sorry I don't have a more canonical source. It does seem to be implied by the NLL RFC, but that requires a fair bit of reading between the lines. |
Hmm. Actually, in terms of initialization-checking the code in my original post, I notice that it is either
And 2 does seem like the less surprising option. |
Hi all! When we were implementing NLL, we couldn't figure out what to do about errors in unreachable code, since it's not clear what things are initialized, which borrows ought to be in scope, etc, and so we decided to accept programs like this (for better or worse). Going to close this as "working as designed" -- if we were going to make changes here, I expect what we could do is talk about some kind of lint, to avoid breaking working code, but it doesn't seem like a high priority. |
I tried this code:
I expected this to fail to compile, since I create one immutable and two mutable borrows of an immutable variable that all have to be alive at once.
It's not unsound to accept this, since none of it is even codegened, but unreachable code is otherwise held to the same standards as reachable code (must type-check, etc.).
Instead, this code is accepted with only warnings:
On Rust 1.35 and before, this code was rejected in the 2015 edition (which used the old lexical borrowck).
@rustbot label regression-from-stable-to-stable
The text was updated successfully, but these errors were encountered: