-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Improve upvar analysis for deref of child capture #138517
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
927a184
to
ae4a479
Compare
This was clearly (to me) always intended to work and imo doesn't need an additional FCP. It's just a bugfix for behaviour that was approved along with the original feature. cc @rust-lang/types for being aware of "code that didn't compile before now compiles". @bors r+ |
bors
added a commit
to rust-lang-ci/rust
that referenced
this pull request
Mar 17, 2025
Rollup of 9 pull requests Successful merges: - rust-lang#136355 (Add `*_value` methods to proc_macro lib) - rust-lang#137621 (Add std support to cygwin target) - rust-lang#137793 (Stablize anonymous pipe) - rust-lang#138341 (std: Mention clone-on-write mutation in Arc<T>) - rust-lang#138517 (Improve upvar analysis for deref of child capture) - rust-lang#138584 (Update Rust Foundation links in Readme) - rust-lang#138586 (Document `#![register_tool]`) - rust-lang#138590 (Flatten and simplify some control flow 🫓) - rust-lang#138592 (update change entry for rust-lang#137147) r? `@ghost` `@rustbot` modify labels: rollup
rust-timer
added a commit
to rust-lang-ci/rust
that referenced
this pull request
Mar 17, 2025
Rollup merge of rust-lang#138517 - compiler-errors:better-child-capture, r=oli-obk Improve upvar analysis for deref of child capture Two fixes to the heuristic I implemented in rust-lang#123660. As I noted in the code: > Luckily, if this function is not correct, then the program is not unsound, since we still borrowck and validate the choices made from this function -- the only side-effect is that the user may receive unnecessary borrowck errors. This indeed fixes unnecessary borrowck errors. r? oli-obk --- The heuristic is only valid if we deref a `&T`, not a `&mut T` or `Box<T>`, so make sure to check the type. This fixes: ```rust struct Foo { precise: i32 } fn mut_ref_inside_mut(f: &mut Foo) { let x: impl AsyncFn() = async move || { let y = &f.precise; }; } ``` Since the capture from `f` to `&f.precise` needs to be treated as a lending borrow from the parent coroutine-closure to the child coroutine. --- The heuristic is also valid if *any* deref projection in the child capture's projections is a `&T`, but we were only looking at the last one. This ensures that this function is considered not to be lending: ```rust struct Foo { precise: i32 } fn ref_inside_mut(f: &mut &Foo) { let x: impl Fn() -> _ = async move || { let y = &f.precise; }; } ``` (Specifically, checking that `impl Fn() -> _` is satisfied is exercising that the coroutine is not considered to be lending.)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
S-waiting-on-bors
Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Two fixes to the heuristic I implemented in #123660. As I noted in the code:
This indeed fixes unnecessary borrowck errors.
r? oli-obk
The heuristic is only valid if we deref a
&T
, not a&mut T
orBox<T>
, so make sure to check the type. This fixes:Since the capture from
f
to&f.precise
needs to be treated as a lending borrow from the parent coroutine-closure to the child coroutine.The heuristic is also valid if any deref projection in the child capture's projections is a
&T
, but we were only looking at the last one. This ensures that this function is considered not to be lending:(Specifically, checking that
impl Fn() -> _
is satisfied is exercising that the coroutine is not considered to be lending.)