-
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
Add feature to enable bind by move pattern guards #54034
Merged
bors
merged 3 commits into
rust-lang:master
from
pnkfelix:issue-15287-bind-by-move-pattern-guards
Sep 18, 2018
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
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
11 changes: 11 additions & 0 deletions
11
src/test/ui/bind-by-move/bind-by-move-no-guards.nll.stderr
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,11 @@ | ||
error[E0008]: cannot bind by-move into a pattern guard | ||
--> $DIR/bind-by-move-no-guards.rs:8:14 | ||
| | ||
LL | Some(z) if z.recv().unwrap() => { panic!() }, | ||
| ^ moves value into pattern guard | ||
| | ||
= help: add #![feature(bind_by_move_pattern_guards)] to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0008`. |
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
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,24 @@ | ||
error[E0302]: cannot assign in a pattern guard | ||
--> $DIR/borrowck-mutate-in-guard.rs:20:25 | ||
| | ||
LL | Enum::A(_) if { x = Enum::B(false); false } => 1, | ||
| ^^^^^^^^^^^^^^^^^^ assignment in pattern guard | ||
|
||
error[E0301]: cannot mutably borrow in a pattern guard | ||
--> $DIR/borrowck-mutate-in-guard.rs:22:38 | ||
| | ||
LL | Enum::A(_) if { let y = &mut x; *y = Enum::B(false); false } => 1, | ||
| ^ borrowed mutably in pattern guard | ||
| | ||
= help: add #![feature(bind_by_move_pattern_guards)] to the crate attributes to enable | ||
|
||
error[E0302]: cannot assign in a pattern guard | ||
--> $DIR/borrowck-mutate-in-guard.rs:22:41 | ||
| | ||
LL | Enum::A(_) if { let y = &mut x; *y = Enum::B(false); false } => 1, | ||
| ^^^^^^^^^^^^^^^^^^^ assignment in pattern guard | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
Some errors occurred: E0301, E0302. | ||
For more information about an error, try `rustc --explain E0301`. |
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,11 @@ | ||
error[E0008]: cannot bind by-move into a pattern guard | ||
--> $DIR/E0008.rs:13:14 | ||
| | ||
LL | Some(s) if s.len() == 0 => {}, | ||
| ^ moves value into pattern guard | ||
| | ||
= help: add #![feature(bind_by_move_pattern_guards)] to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0008`. |
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,11 @@ | ||
error[E0301]: cannot mutably borrow in a pattern guard | ||
--> $DIR/E0301.rs:14:19 | ||
| | ||
LL | option if option.take().is_none() => {}, //~ ERROR E0301 | ||
| ^^^^^^ borrowed mutably in pattern guard | ||
| | ||
= help: add #![feature(bind_by_move_pattern_guards)] to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0301`. |
21 changes: 21 additions & 0 deletions
21
src/test/ui/rfc-0107-bind-by-move-pattern-guards/bind-by-move-no-guards.rs
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,21 @@ | ||
// Adaptation of existing ui test (from way back in | ||
// rust-lang/rust#2329), that starts passing with this feature in | ||
// place. | ||
|
||
// compile-pass | ||
|
||
#![feature(nll)] | ||
#![feature(bind_by_move_pattern_guards)] | ||
|
||
use std::sync::mpsc::channel; | ||
|
||
fn main() { | ||
let (tx, rx) = channel(); | ||
let x = Some(rx); | ||
tx.send(false); | ||
match x { | ||
Some(z) if z.recv().unwrap() => { panic!() }, | ||
Some(z) => { assert!(!z.recv().unwrap()); }, | ||
None => panic!() | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/test/ui/rfc-0107-bind-by-move-pattern-guards/feature-gate.gate_and_2015.stderr
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,9 @@ | ||
error[E0008]: cannot bind by-move into a pattern guard | ||
--> $DIR/feature-gate.rs:33:16 | ||
| | ||
LL | A { a: v } if *v == 42 => v, | ||
| ^ moves value into pattern guard | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0008`. |
10 changes: 10 additions & 0 deletions
10
src/test/ui/rfc-0107-bind-by-move-pattern-guards/feature-gate.gate_and_2018.stderr
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,10 @@ | ||
error: compilation successful | ||
--> $DIR/feature-gate.rs:42:1 | ||
| | ||
LL | / fn main() { | ||
LL | | foo(107) | ||
LL | | } | ||
| |_^ | ||
|
||
error: aborting due to previous error | ||
|
10 changes: 10 additions & 0 deletions
10
src/test/ui/rfc-0107-bind-by-move-pattern-guards/feature-gate.gate_and_feature_nll.stderr
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,10 @@ | ||
error: compilation successful | ||
--> $DIR/feature-gate.rs:42:1 | ||
| | ||
LL | / fn main() { | ||
LL | | foo(107) | ||
LL | | } | ||
| |_^ | ||
|
||
error: aborting due to previous error | ||
|
10 changes: 10 additions & 0 deletions
10
src/test/ui/rfc-0107-bind-by-move-pattern-guards/feature-gate.gate_and_znll.stderr
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,10 @@ | ||
error: compilation successful | ||
--> $DIR/feature-gate.rs:42:1 | ||
| | ||
LL | / fn main() { | ||
LL | | foo(107) | ||
LL | | } | ||
| |_^ | ||
|
||
error: aborting due to previous error | ||
|
9 changes: 9 additions & 0 deletions
9
src/test/ui/rfc-0107-bind-by-move-pattern-guards/feature-gate.no_gate.stderr
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,9 @@ | ||
error[E0008]: cannot bind by-move into a pattern guard | ||
--> $DIR/feature-gate.rs:33:16 | ||
| | ||
LL | A { a: v } if *v == 42 => v, | ||
| ^ moves value into pattern guard | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0008`. |
47 changes: 47 additions & 0 deletions
47
src/test/ui/rfc-0107-bind-by-move-pattern-guards/feature-gate.rs
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,47 @@ | ||
// Check that pattern-guards with move-bound variables is only allowed | ||
// with the appropriate set of feature gates. (Note that we require | ||
// the code to opt into MIR-borrowck in *some* way before the feature | ||
// will work; we use the revision system here to enumerate a number of | ||
// ways that opt-in could occur.) | ||
|
||
// gate-test-bind_by_move_pattern_guards | ||
|
||
// revisions: no_gate gate_and_2015 gate_and_2018 gate_and_znll gate_and_feature_nll | ||
|
||
// (We're already testing NLL behavior quite explicitly, no need for compare-mode=nll.) | ||
// ignore-compare-mode-nll | ||
|
||
#![feature(rustc_attrs)] | ||
|
||
#![cfg_attr(gate_and_2015, feature(bind_by_move_pattern_guards))] | ||
#![cfg_attr(gate_and_2018, feature(bind_by_move_pattern_guards))] | ||
#![cfg_attr(gate_and_znll, feature(bind_by_move_pattern_guards))] | ||
#![cfg_attr(gate_and_feature_nll, feature(bind_by_move_pattern_guards))] | ||
|
||
#![cfg_attr(gate_and_feature_nll, feature(nll))] | ||
|
||
//[gate_and_2015] edition:2015 | ||
//[gate_and_2018] edition:2018 | ||
//[gate_and_znll] compile-flags: -Z borrowck=mir | ||
|
||
struct A { a: Box<i32> } | ||
|
||
fn foo(n: i32) { | ||
let x = A { a: Box::new(n) }; | ||
let _y = match x { | ||
|
||
A { a: v } if *v == 42 => v, | ||
//[no_gate]~^ ERROR cannot bind by-move into a pattern guard | ||
//[gate_and_2015]~^^ ERROR cannot bind by-move into a pattern guard | ||
|
||
_ => Box::new(0) | ||
}; | ||
} | ||
|
||
#[rustc_error] | ||
fn main() { | ||
foo(107) | ||
} | ||
//[gate_and_2018]~^^^ ERROR compilation successful | ||
//[gate_and_znll]~^^^^ ERROR compilation successful | ||
//[gate_and_feature_nll]~^^^^^ ERROR compilation successful |
40 changes: 40 additions & 0 deletions
40
src/test/ui/rfc-0107-bind-by-move-pattern-guards/rfc-basic-examples.rs
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 @@ | ||
#![feature(nll)] | ||
#![feature(bind_by_move_pattern_guards)] | ||
|
||
// compile-pass | ||
|
||
struct A { a: Box<i32> } | ||
|
||
impl A { | ||
fn get(&self) -> i32 { *self.a } | ||
} | ||
|
||
fn foo(n: i32) { | ||
let x = A { a: Box::new(n) }; | ||
let y = match x { | ||
A { a: v } if *v == 42 => v, | ||
_ => Box::new(0), | ||
}; | ||
} | ||
|
||
fn bar(n: i32) { | ||
let x = A { a: Box::new(n) }; | ||
let y = match x { | ||
A { a: v } if x.get() == 42 => v, | ||
_ => Box::new(0), | ||
}; | ||
} | ||
|
||
fn baz(n: i32) { | ||
let x = A { a: Box::new(n) }; | ||
let y = match x { | ||
A { a: v } if *v.clone() == 42 => v, | ||
_ => Box::new(0), | ||
}; | ||
} | ||
|
||
fn main() { | ||
foo(107); | ||
bar(107); | ||
baz(107); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/test/ui/rfc-0107-bind-by-move-pattern-guards/rfc-reject-double-move-across-arms.rs
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,16 @@ | ||
#![feature(nll)] | ||
#![feature(bind_by_move_pattern_guards)] | ||
|
||
enum VecWrapper { A(Vec<i32>) } | ||
|
||
fn foo(x: VecWrapper) -> usize { | ||
match x { | ||
VecWrapper::A(v) if { drop(v); false } => 1, | ||
//~^ ERROR cannot move out of borrowed content | ||
VecWrapper::A(v) => v.len() | ||
} | ||
} | ||
|
||
fn main() { | ||
foo(VecWrapper::A(vec![107])); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/test/ui/rfc-0107-bind-by-move-pattern-guards/rfc-reject-double-move-across-arms.stderr
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,9 @@ | ||
error[E0507]: cannot move out of borrowed content | ||
--> $DIR/rfc-reject-double-move-across-arms.rs:8:36 | ||
| | ||
LL | VecWrapper::A(v) if { drop(v); false } => 1, | ||
| ^ cannot move out of borrowed content | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0507`. |
17 changes: 17 additions & 0 deletions
17
src/test/ui/rfc-0107-bind-by-move-pattern-guards/rfc-reject-double-move-in-first-arm.rs
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,17 @@ | ||
#![feature(nll)] | ||
#![feature(bind_by_move_pattern_guards)] | ||
|
||
struct A { a: Box<i32> } | ||
|
||
fn foo(n: i32) { | ||
let x = A { a: Box::new(n) }; | ||
let _y = match x { | ||
A { a: v } if { drop(v); true } => v, | ||
//~^ ERROR cannot move out of borrowed content | ||
_ => Box::new(0), | ||
}; | ||
} | ||
|
||
fn main() { | ||
foo(107); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/test/ui/rfc-0107-bind-by-move-pattern-guards/rfc-reject-double-move-in-first-arm.stderr
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,9 @@ | ||
error[E0507]: cannot move out of borrowed content | ||
--> $DIR/rfc-reject-double-move-in-first-arm.rs:9:30 | ||
| | ||
LL | A { a: v } if { drop(v); true } => v, | ||
| ^ cannot move out of borrowed content | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0507`. |
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.
Oh, I guess it would be nice (when on nightly and with MIR-borrowck enabled) if this issued a diagnostic suggesting the feature gate.
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.
(done, see commit c329897)
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.
Could also be useful to suggest enabling NLL and the feature if not? Perhaps that's a little convoluted though.
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 don't think that's necessary; the 2018 edition has NLL enabled anyway (under migrate mode), so I think that will be enough.