-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Tracking issue for Pattern Guards with Bind-By-Move #15287
Comments
This comment has been minimized.
This comment has been minimized.
I keep running into this (or rather, the simpler #14252) in places where I now end up with an extra level of indentation: match x {
Foo(v) => if do_a {
// ...
} else if do_b {
// ...
} else if do_c {
// ...
} else {
// ...
}
} when it could be match x {
Foo(v) if do_a => {
// ...
},
Foo(v) if do_b => {
// ...
},
Foo(v) if do_c => {
// ...
},
Foo(v) => {
// ...
}
} |
implementation of issue rust-lang#15287
Actually perhaps the right thing for me to do would be to put in the |
…ds, r=nikomatsakis Add feature to enable bind by move pattern guards Implement #15287 as described on #15287 (comment)
Now that PR #54034 has landed, I'm unassigning myself. (But, if I understand correctly, the issue should stay open until |
(I guess it would be good to remove the |
We discussed this in the language team meeting pre-triage (but not the proper language team meeting; cc @rust-lang/lang). Of note, with respect to #15287 (comment), is that NLL has been stabilized on both editions. Therefore, the participants at the pre-triage (e.g. me, @nikomatsakis, and @pnkfelix) found that we should have "clearance to prep for a stabilization report". This would entail writing up a report (example: #57175 (comment)) of:
@matthewjasper Since you've been working on things related to this, do you think you could survey the implementation and tests and check whether improvements need to be made in either and then possibly write up a report? |
MotivationThe naive lowering of a bind-by-move pattern with a guard in a
The error explanation provides a more detailed explanation of the problem. The checks for
These are now handled by careful lowering to MIR so that the MIR borrow checker will check them for soundness. As these errors are unnecessary and occasionally come up, this feature removes them. ExplanationThe feature gate currently disables the checks for the above errors. Bind by move guardsWe cannot handle these in the simplest way for the reasons given above. A by value binding in a pattern with a guard is lowered as follows:
For example:
Checking for mutation
To handle the cases in
The borrow lasts from the start of the guard to just after the guard is evaluated successfully. As such if a guard unconditionally diverges, then the borrow will not be active in the guard. A shallow borrow only affects a place and its parents. A shallow borrow of TestsAs a general observation, most of the tests are using Bind by move guardsThe tests for this feature are here: https://github.com/rust-lang/rust/tree/master/src/test/ui/rfc-0107-bind-by-move-pattern-guards Of particular note is https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-0107-bind-by-move-pattern-guards/rfc-reject-double-move-across-arms.rs. This test compiles in migrate mode, albeit with a scary warning, and will double free a Mutation in guards
Work before stabilization
Related workThe following errors are also apparently unnecessary restrictions on patterns. There should probably be a check for why these are errors and whether they can be removed. |
@matthewjasper Thanks for the report!
So we would need to adjust some tests then to make sure that stable behavior is exercised.
Let's not pick the soundness hole solution... ;)
So this would mean that we don't introduce new unsound behavior on stable, right?
Given that we've transitioned into NLL on both 2018 & 2015 now, what is your guesstimate as to when we can do full NLL stabilization?
Let's follow up with fresh issues? |
Correct
This year. (1.39 or 1.40) |
Cool;
@nikomatsakis @pnkfelix Interested to hear what you think about this. My hunch is to say that we should do it assuming we have the tests to show that it works. |
We discussed in the lang team meeting -- treating the existing errors as "AST borrowck errors" for purpose of migration mode seems like a good solution, and we are generally in favor of moving forward here once that is done plus test cases are finished. |
If anyone wants to work on this then some steps to get started are
|
…ewjasper Make `#![feature(bind_by_move_pattern_guards)]` sound without `#[feature(nll)]` Implements rust-lang#15287 (comment). Fixes rust-lang#31287 Fixes rust-lang#27282 r? @matthewjasper
…ewjasper Make `#![feature(bind_by_move_pattern_guards)]` sound without `#[feature(nll)]` Implements rust-lang#15287 (comment). Fixes rust-lang#31287 Fixes rust-lang#27282 r? @matthewjasper
…ewjasper Make `#![feature(bind_by_move_pattern_guards)]` sound without `#[feature(nll)]` Implements rust-lang#15287 (comment) making `#![feature(bind_by_move_pattern_guards)]]` sound without also having `#![feature(nll)]`. The logic here is that if we see a `match` guard, we will refuse to downgrade NLL errors to warnings. This is in preparation for hopefully stabilizing the former feature in rust-lang#63118. As fall out from the implementation we also: Fixes rust-lang#31287 Fixes rust-lang#27282 r? @matthewjasper
Make `#![feature(bind_by_move_pattern_guards)]` sound without `#[feature(nll)]` Implements #15287 (comment) making `#![feature(bind_by_move_pattern_guards)]]` sound without also having `#![feature(nll)]`. The logic here is that if we see a `match` guard, we will refuse to downgrade NLL errors to warnings. This is in preparation for hopefully stabilizing the former feature in #63118. As fall out from the implementation we also: Fixes #31287 Fixes #27282 r? @matthewjasper
Plan in #15287 (comment) was implemented in #63059. |
…cola internal: remove `crate` visibility modifier This PR removes `crate` as a now-unaccepted experimental visibility modifier from our parser. This feature has been [unaccepted] and [removed] from rustc more than a year ago, so I don't think this removal affects anyone. [unaccepted]: rust-lang#53120 (comment) [removed]: rust-lang#97239
Tracking issue for "Pattern Guards with Bind-By-Move" (rust-lang/rfcs#107)
Steps:
The text was updated successfully, but these errors were encountered: