-
Notifications
You must be signed in to change notification settings - Fork 13k
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
NLL migrate mode erroneously downgrades error with nested closures, yielding use-after-free #58776
Comments
In stable (clean build): Standard Error Compiling playground v0.0.1 (/playground)
warning[E0506]: cannot assign to `greeting` because it is borrowed
--> src/main.rs:28:5
|
26 | let res = scope(|s| s.spawn(|| &greeting));
| --- -------- borrow occurs due to use in closure
| |
| borrow of `greeting` occurs here
27 |
28 | greeting = "DEALLOCATED".to_string();
| ^^^^^^^^ assignment to borrowed `greeting` occurs here
...
31 | println!("thread result: {:?}", res);
| --- borrow later used here
|
= warning: This error has been downgraded to a warning for backwards compatibility with previous releases.
It represents potential unsoundness in your code.
This warning will become a hard error in the future.
warning[E0505]: cannot move out of `greeting` because it is borrowed
--> src/main.rs:29:10
|
26 | let res = scope(|s| s.spawn(|| &greeting));
| --- -------- borrow occurs due to use in closure
| |
| borrow of `greeting` occurs here
...
29 | drop(greeting);
| ^^^^^^^^ move out of `greeting` occurs here
30 |
31 | println!("thread result: {:?}", res);
| --- borrow later used here
|
= warning: This error has been downgraded to a warning for backwards compatibility with previous releases.
It represents potential unsoundness in your code.
This warning will become a hard error in the future.
Finished release [optimized] target(s) in 1.13s
Running `target/release/playground` Standard Output
|
So the playground doesn't show this if you run the code, but we are emitting warnings. Looks like the issue is with the migrate mode. |
So the solution would be to make it an hard error now? ;) |
cc #55492 |
triage: P-high. Leaving assigned to @matthewjasper until they complain. :) |
As @lqd pointed out on Zulip, the code actually gets rejected properly when adding |
Yeah, this is why Felix changed the title. This problem is not in MIR borrowck but in the "migrate mode" itself: the piece of code responsible for checking if an NLL error would have been missed by AST borrowck, and downgrading it to a warning (to make the AST borrowck bug fixes not break existing code during a migration period). Here, the 2 borrowck's errors are different enough that migrate mode misses that they are related and shouldn't downgrade them. |
…nkfelix Make migrate mode work at item level granularity Migrate mode now works entirely at the item level rather than the body level, ensuring that we don't lose any errors in contained closures. Closes rust-lang#58776 r? @pnkfelix
…nkfelix Make migrate mode work at item level granularity Migrate mode now works entirely at the item level rather than the body level, ensuring that we don't lose any errors in contained closures. Closes rust-lang#58776 r? @pnkfelix
…nkfelix Make migrate mode work at item level granularity Migrate mode now works entirely at the item level rather than the body level, ensuring that we don't lose any errors in contained closures. Closes rust-lang#58776 r? @pnkfelix
…nkfelix Make migrate mode work at item level granularity Migrate mode now works entirely at the item level rather than the body level, ensuring that we don't lose any errors in contained closures. Closes rust-lang#58776 r? @pnkfelix
…nkfelix Make migrate mode work at item level granularity Migrate mode now works entirely at the item level rather than the body level, ensuring that we don't lose any errors in contained closures. Closes rust-lang#58776 r? @pnkfelix
The following code compiles with the 2018 edition on stable, beta and nightly, and produces this error:
Code:
And indeed there's a use-after-free here.
Interestingly, in the 2015 edition this is correctly detected as incorrect, though the error is not quite what I would have expected:
I would have expected a complaint at the re-assignment of
greeting
.(I found this while modifying an example by @stjepang and failing to reproduce some code that gets a migration warning on 2018...)
The text was updated successfully, but these errors were encountered: