-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
RFC: Pattern Guards with Bind-By-Move #107
RFC: Pattern Guards with Bind-By-Move #107
Conversation
zwarich
commented
Jun 6, 2014
•
edited by Centril
Loading
edited by Centril
- Rendered view
- Tracking issue
- Stabilization PR
What is the validity of these: // A
match x {
A(v) if { drop(v); true } => 1,
_ => 2
}
// B
match x {
A(v) if { drop(v); true } => 1,
A(v) => v.len()
}
// C
match x {
A(v) if true => v,
A(v) => v
}
// D
enum Foo { X(Box<int>), Y }
match some_foo {
X(b) if { drop(b); true } => (),
Y => {}
_ => {}
} I assume that A, C and D are valid, but B is not. (I ask these since all your examples have only one non- |
@huonw Is that based on the assumption that a value is not moved, when it is not used in the match arm? That is not currently the case. I would expect only C to be valid, since all others move into the match arm and inside the pattern guard, equivalently to the rejected example in the RFC. |
@huonw No moves of a bound-by-move value would be permitted in the guard, because there is a move between the guard and the expression, and any move in the guard would result in a double move. The only example that would be valid is C. Maybe I'll update the examples in the RFC to mention this. |
+1, this would be very useful to me in the HTML parser, e.g.
|
This has definite interaction with the plans to remove zero flags. |
@kmcallister, your exact example would also need nonlexical borrow scopes to be accepted. With the current system, the borrow for |
@nikomatsakis, what is the interaction with removing zero flags here? Removing zero flags will require checking that each control-flow merge point has the same moved paths. Since there is no new move here (just clarification about when it occurs), I don't see how it could complicate anything. |
In the Rust meeting it was decided that this should go in behind a feature gate. I arbitrarily chose the `bind_by_move_pattern_guards` name.
This was discussed at a recent meeting and it was concluded that we would like to merge this with the condition that a feature gate was mentioned in the RFC. The RFC has been updated, so I'm merging. |
Now that this RFC is merged, the above "rendered view" link is broken. The text of this RFC can now be found here: https://github.com/rust-lang/rfcs/blob/master/text/0107-pattern-guards-with-bind-by-move.md |
Typo in the docs
Any ideas/progress on that? I think this is perfectly valid code: fn f(x: Option<String>) {
match x {
Some(a) if &a == "Hello" => println!("x is Hello"),
Some(b) => println!("x is not Hello but {}", b),
None => println!("none"),
}
} But borrowcheker currently won't accept it The thumb rule here is if we only use matched values by-reference they could be logically moved back to |
This works just fine in 1.39, which is currently in Beta. |
Wow, guys, you're so quick. I wonder if I ask about GAT and they appear in 2 minutes thereafter... 😄 |