Skip to content
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

Fix explicit deref problems in closure capture #14576

Merged
merged 1 commit into from
Apr 14, 2023
Merged

Conversation

HKalbasi
Copy link
Member

fix the need-mut part of #14562

Perhaps surprisingly, it wasn't unique immutable borrow. The code still doesn't emit any of them, and I think those won't happen in edition 2021 (which is currently the only thing implemented), since we always capture &mut *x instead of &mut x. But I'm not very sure about it.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 14, 2023
@HKalbasi
Copy link
Member Author

I'm going to merge this since it is a problem anyway.

If someone find a case that we need unique immutable borrow in edition 2021, it would be highly appreciated.

@bors r+

@bors
Copy link
Contributor

bors commented Apr 14, 2023

📌 Commit 7cb4318 has been approved by HKalbasi

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Apr 14, 2023

⌛ Testing commit 7cb4318 with merge b218009...

@bors
Copy link
Contributor

bors commented Apr 14, 2023

☀️ Test successful - checks-actions
Approved by: HKalbasi
Pushing b218009 to master...

@bors bors merged commit b218009 into rust-lang:master Apr 14, 2023
@Veykril
Copy link
Member

Veykril commented Apr 14, 2023

&mut *x is not a capture, you can only capture by value or by reference. This is very much a by unique immutable reference capture here, since x is not marked mut a by mutable reference capture is incorrect as is outlined in the rust reference. The edition is not relevant here either. Though it might not be significant for our purposes whether this is modeled as such or not.

@HKalbasi
Copy link
Member Author

Since edition 2021, each place can be captured, so a closure can capture x, ***x, x.foo, ... each with 4 capture mode (by ref, by mut, by shallow and by unique) but in older editions only x can be captured with those capture modes.

For example in this test

size_and_align_expr! {
    minicore: copy, deref_mut;
    stmts: [
        struct X(i32, i64);
        let x: &mut X = &mut X(2, 6);
    ]
    || {
        (*x).0 = x.1 as i64;
    }
}

In edition 2021, captures are (*x).0 by mutable borrow and (*x).1, by shared borrow, but in older editions the only capture is x by unique immutable borrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants