Repository navigation
Conversation
2053636 to
6579371
Compare
|
@bors try |
This comment has been minimized.
This comment has been minimized.
put fake reads for guards' fake borrows on the guard's failure path
|
I'm guessing a types fcp would make the most sense since this is kind of a borrow-checking change? r? @oli-obk maybe since you're familiar with both MIR building and T-types procedure, but feel free to reassign ^^ |
|
|
|
Some changes occurred in match lowering cc @Nadrieril |
This comment has been minimized.
This comment has been minimized.
6579371 to
80441e9
Compare
|
Some changes occurred in coverage tests. cc @Zalathar |
|
@craterbot check |
|
👌 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
just noticed oli is on vacation; oops! @rustbot reroll |
|
You will also need to bless the coverage-run tests with (You can also I'm not sure whether the resulting changes to coverage output are a problem or not, but my hope is that #161517 will make them vanish anyway. |
80441e9 to
e20b342
Compare
|
the coverage-run tests should be blessed now. I'm not familiar with how coverage uses spans (or how to read coverage tests), so if there is a problem I'll need some guidance. though I imagine #161517 should help if it means coverage won't scrape spans from fake reads anymore |
e20b342 to
3990980
Compare
3990980 to
38e7e26
Compare
|
updated the PR description and added some more tests to match. since I'm now framing this as a question of how much we want to enforce the abstraction of not being able to mutate fake-borrowed places in match guards, it feels kind of like it may also be a lang issue. even if the final decision is types, I imagine lang will want to see this, so I'll lang-nominate it. see the PR description for a summary of what's changing and see my above comment at #161581 (comment) for my opinions. |
|
on the technical side of things, maybe r? @matthewjasper or @Nadrieril |
|
another consideration for being more strict about not mutating fake-borrowed places in guards: we also use fake borrows for soundness in indexing expressions. if the argument to forbid it is consistency with our abstractions, maybe we should do the same with indexing? for example: fn errors() {
let mut x: &[&[u32]] = &[&[0]];
let y: &[&[u32]] = &[];
x[0][{ x = y; 0 }]; // for soundness, this is forbidden
//~^ ERROR: cannot assign `x` in indexing expression
}
fn works() {
let mut x: &[&[u32]] = &[&[0]];
let y: &[&[u32]] = &[];
x[0][{ x = y; return; 0 }]; // this is fine
}
fn unsound() {
let mut x: &[&[&[u32]]] = &[&[&[0]]];
let y: &[&[&[u32]]] = &[];
x[0][{ x = y; 0 }][{ return; 0 }]; // UB when reading `y[0]`'s length for the bounds check
}I'll have to file an issue for that last one. (edit: filed as #161852) |
This comment has been minimized.
This comment has been minimized.
|
Following up on yesterday's lang team meeting, I've opened #162242 as a more restrictive alternative to this. Lang was interested to see a version that consistently prohibits mutating fake-borrowed places without regard to CFG shape. |
|
Issuing a correction: we do need fake reads of fake borrows on guard success. We borrow by-value-bound places before guards and fake-read those borrows on guard success, so I'd been assuming that that would be sufficient to ensure we can create by-value bindings. It's not: fn main() {
let mut x: Option<&u64> = Some(&7);
match x {
// assignment is a shallow access, so it doesn't invalidate the ref-within-guard. uh oh.
Some(&y) if { x = None; true } || return => println!("{y}"),
_ => println!("_"),
}
}.. though this does leave me a bit confused about what the fake reads of ref-within-guard bindings are for, if not that. I've updated this PR to put fake reads of fake borrows on both success and failure. Though I'm leaning towards preferring #162242 over this PR as it is right now. The only advantage I can see this PR still having in its current state is implementation simplicity. For the more precise mental model, I'd want to only put fake reads on guard success of the fake borrows that need to hold to make that arm's by-value bindings. |
38e7e26 to
544335a
Compare
This comment has been minimized.
This comment has been minimized.
|
Given that @nikomatsakis is now persuaded, I'm planning to propose FCP merge on this. @dianne, do you want to do the variation you mentioned above?
Or what you mentioned in #162242 (comment)?
Also, @rust-lang/types, do you want to be on this FCP (I'm not sure whether you'd be interested here or not)? |
|
@traviscross For now, I'm inclined towards what I mentioned in #162242 (comment) (i.e. what this PR currently implements). Although I still think it makes more sense conceptually for guard success to only require that the arm's by-value bindings can be created, I don't have a use-case to motivate that change from a compiler complexity perspective. These can be thought of separately too: this PR strengthens the restrictions put on guard failure1, and a hypothetical future change could weaken the restrictions on guard success2. Though if it'd be easier to FCP both at once and there's a preference for loosening the fake reads on guard success, I can also make that change. Footnotes |
|
For my part, I'm persuaded by the conceptual argument you made in favor of the more precise check and by the fact it creates parity between forms that logically should be treated in the same way. As you say, it makes more sense conceptually. It is probably mostly a pedagogical matter. But those matter too: it's valuable to have a clean and clear language definition that we can write down in the Reference. While we could, of course, loosen this later, the cost of the team loading this into cache to review is nontrivial, so if the complexity of the more precise check is manageable, doing it now would indeed be my inclination. |
|
I'll experiment with it! I should have something ready before next week's meeting. |
544335a to
82ad3be
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. |
|
I've rebased this onto the shared test suite at #166092 |
View all comments
This PR is based on #166092. Its first commit is that test suite.
Fixes #161578.
Under this PR, it's now forbidden to mutate fake-borrowed places when guard failure is reachable, even if guard success is unreachable:
This was already disallowed when success was reachable. This keeps the fake reads on guard success, even when they're not strictly required for soundness:
It's still allowed to mutate fake-borrowed places when both success and failure are unreachable:
Though they break the abstraction that
xcan't be mutated in the guard, I believe the allowed cases are sound. The guard lacking a failure path afterxis mutated means we can't continue matching after that, and lacking a success path means we can't create by-value bindings.