Repository navigation
reject Reborrow impls on types with &'static mut fields - #165006
paradoxicalguy wants to merge 1 commit into
Conversation
|
r? @fee1-dead rustbot has assigned @fee1-dead. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
|
oops |
566ba0a to
48aa1c8
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
What about: struct Thing<'a: 'static> {
field: &'a mut u8,
} |
48aa1c8 to
a04e708
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
the check now looks at the declared field type rather than the substituted one, so |
|
It gives a borrowcheck error when you actually try to use the |
|
:O, yes pushed: the impl is now rejected for |
a04e708 to
4f03448
Compare
|
In general, I don't think we should keep adding ad-hoc checks here without a solid plan for the end-state. This is an unstable feature, it's fine if there are rough edges. |
|
yeah i think i went nuts; been extending the check around individual cases without establishing the general rule first. +would you prefer keeping this pr scoped to the original literal i'd rather get the invariant right than keep extending an incomplete region-bound scan |
25f3d21 to
80e4c80
Compare
|
pushed a simpler version of the fix. it now only checks for literal i also tested the all 125 reborrow UI tests pass, and tidy is clean. keeping this scoped to the original ICE for now rather than trying to handle every lifetime bound here. |
|
cc @fee1-dead |
This comment has been minimized.
This comment has been minimized.
|
np, i also have to solve merge conflicts :D |
80e4c80 to
d5e7c20
Compare
|
|
||
| fn main() { | ||
| let mut x = 0; | ||
| let thing = Thing { field: &mut x }; |
There was a problem hiding this comment.
question: After the merge of #163284 I'm wondering if this would get the same \x` does not live long enougherror without the main changes of this PR implemented? I believe we should already have tests showing that eg.Thing<'a>(PhantomData<&'a ()>, &'static mut u8)can implement Reborrow and it will even work if the end result is that all the Reborrows are always equal to just Moves and we start with a&'static mut u8` in the first place.
There was a problem hiding this comment.
i tested this against an unpatched compiler including #163284, and the ICE still reproduces.
even with a valid &'static mut u8 from Box::leak, using the Reborrow impl ICEs in gather_reborrows. removing the explicit bug!() just hits another panic at target_region.as_var(), since the field's region is still 'static.
since Reborrow performs a bitwise copy and relies on borrowck to enforce exclusivity, simply skipping that borrow doesn't seem safe.
i've updated the regression test and all 126 reborrow UI tests pass. would keeping the narrow coherence rejection for now make sense?
There was a problem hiding this comment.
Hmmm.... From a DX perspective the 'static check is exactly correct ... but I also feel like the ICE shouldn't be happening in the first place. I think the bug! in borrow_set.rs is just not valid anymore; it's technically okay to implement Reborrow on a type with &'static mut T, it's just very likely not going to borrowck. That being said, it's also a very bad idea to even try that so the improved wf-check is good.
But I'd like to see the bug! removed and the target_region.as_var() handled wherever it appears in: when we reborrow a struct Source(&'static mut ()) (or something like that) the end result should be that we effectively borrow the source.0 for 'static, making it inaccessible forevermore.
There was a problem hiding this comment.
updated borrowck to use the universal 'static region vid for static reborrows, removed the bug!() and handled both as_var() sites. also fixed a subsequent diagnostic ICE when trying to blame the static region for itself.
tested with the coherence rejection temporarily disabled: a single reborrow succeeds, repeated reborrows and subsequent field mutation are rejected.
kept the wf-check and added generic and nested regression tests. all 128 reborrow tests and the full ui suite passes
d5e7c20 to
5b0519f
Compare
|
@aapoalas reminder :) |
5b0519f to
0a22de1
Compare
|
@rustbot ready |
This comment has been minimized.
This comment has been minimized.
0a22de1 to
829d2fd
Compare
|
rebased onto current main and resolved the conflict (#166171) |
There was a problem hiding this comment.
This looks good to me now - I'd like a second set of eyes on the BorrowSet part specifically so
r? @oli-obk
if you have the time to take a look :)
View all comments
View all comments
fixes #164933
the
Reborrowimpl check accepted every&mutfield, so a type with a&'static mut Tfield passed coherence and then hit abug!in borrowck. this makes&'static mutfall through to theCopycheck, so the impl is rejected with a normal error instead of an ice.adds a ui test using the repro from the issue.
edit:
also updates
invalid-coerce-shared-static-mut-field: itsReborrowimpl is now rejected at the impl, so the test gains an E0277.