-
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
Ignore type of projections for upvar capturing #89648
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Regression test for #89606. Used to ICE. | ||
// | ||
// check-pass | ||
// revisions: twenty_eighteen twenty_twentyone | ||
// [twenty_eighteen]compile-flags: --edition 2018 | ||
// [twenty_twentyone]compile-flags: --edition 2021 | ||
|
||
struct S<'a>(Option<&'a mut i32>); | ||
|
||
fn by_ref(s: &mut S<'_>) { | ||
(|| { | ||
let S(_o) = s; | ||
s.0 = None; | ||
})(); | ||
} | ||
|
||
fn by_value(s: S<'_>) { | ||
(|| { | ||
let S(ref _o) = s; | ||
let _g = s.0; | ||
})(); | ||
} | ||
|
||
struct V<'a>((Option<&'a mut i32>,)); | ||
|
||
fn nested(v: &mut V<'_>) { | ||
(|| { | ||
let V((_o,)) = v; | ||
v.0 = (None, ); | ||
})(); | ||
} | ||
|
||
fn main() { | ||
let mut s = S(None); | ||
by_ref(&mut s); | ||
by_value(s); | ||
|
||
let mut v = V((None, )); | ||
nested(&mut v); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally we should refactor this to return something like
PlaceAncestryRelation::Same
and handle it explicity in the min capture code.I have a feeling that this would cause issues with borrow checker error reporting a bit.
Now the code thinks that Place1 is ancestor of Place2 and therefore Place1 is the reason it captures the path
t.0
but we need to capture via a mut borrow because of Place 2.Now in the borrow conflict it would say something like t.0 is captured mutably because of use at Place2 and t.0 is captured because of use at Place1. i.e. it will try reason for 2 different spans present in CaptureInfo which can be confusing for the user.
What it really should do is just point at Place2 saying that this is the reason we captured this via mut borrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works fine, because the lifetime is the same so no multiple occurrence of the same place will happen. I tried this example and it does show what you suggest:
gives
Fixing this would require us to keep the order of use, which currently is lost due to use of HashMap.
Given that this diagnostics confusion only happens for minority of cases (where we currently ICE), I think the best way forward is to just merge and backport as it is, and open an issue to track this case. I can author a follow-up PR later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use an IndexMap which maintains the order of within the closure, the reason we are seeing two lines is because ancestor code thinks that there is an actual diference in the paths being used and CaptureInfo has a different
path_expr_id
andcapture_kind_expr_id
.rust/compiler/rustc_typeck/src/check/upvar.rs
Lines 2132 to 2139 in d7f8a06
I'm fine with there being a second PR that fixed the diagnostics but I don't know if we should backport an incomplete fix. @nikomatsakis what are your thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indexed map doesn't help Immutable(lifetime A), Mutable(lifetime B), Mutable (lifetime A) case, and diagnostics would point to the 3rd use instead of the 2nd, because the place with lifetime A is inserted first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem you mentioned is visible today with unions:
reason is the same, we relied on indexed map and disregard the actual order.
So I think I would prefer to address these together in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have implemented your suggestion of
PlaceAncestryRelation::SamePlace
. By handling it like descendant instead of ancestor it will address the particular case you talked about, but it wouldn't solve the more generic case that I mentioned (I have started working on that already, but it seems too much change to backport).