forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Give temporaries in if let guards correct scopes
- Make temporaries in if-let guards be the same variable in MIR when the guard is duplicated due to or-patterns. - Change the "destruction scope" for match arms to be the arm scope rather than the arm body scope. - Add tests.
- Loading branch information
1 parent
68d684c
commit d437a11
Showing
10 changed files
with
178 additions
and
10 deletions.
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
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,59 @@ | ||
// check drop order of temporaries create in match guards. | ||
// For normal guards all temporaries are dropped before the body of the arm. | ||
// For let guards temporaries live until the end of the arm. | ||
|
||
// run-pass | ||
|
||
#![feature(if_let_guard)] | ||
#![allow(irrefutable_let_patterns)] | ||
|
||
use std::sync::Mutex; | ||
|
||
static A: Mutex<Vec<i32>> = Mutex::new(Vec::new()); | ||
|
||
struct D(i32); | ||
|
||
fn make_d(x: i32) -> D { | ||
A.lock().unwrap().push(x); | ||
D(x) | ||
} | ||
|
||
impl Drop for D { | ||
fn drop(&mut self) { | ||
A.lock().unwrap().push(!self.0); | ||
} | ||
} | ||
|
||
fn if_guard(num: i32) { | ||
let _d = make_d(1); | ||
match num { | ||
1 | 2 if make_d(2).0 == 2 => { | ||
make_d(3); | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
fn if_let_guard(num: i32) { | ||
let _d = make_d(1); | ||
match num { | ||
1 | 2 if let D(ref _x) = make_d(2) => { | ||
make_d(3); | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
fn main() { | ||
if_guard(1); | ||
if_guard(2); | ||
if_let_guard(1); | ||
if_let_guard(2); | ||
let expected = [ | ||
1, 2, !2, 3, !3, !1, | ||
1, 2, !2, 3, !3, !1, | ||
1, 2, 3, !3, !2, !1, | ||
1, 2, 3, !3, !2, !1, | ||
]; | ||
assert_eq!(*A.lock().unwrap(), expected); | ||
} |
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,19 @@ | ||
// check-pass | ||
|
||
#![feature(if_let_guard)] | ||
|
||
fn split_last(_: &()) -> Option<(&i32, &i32)> { | ||
None | ||
} | ||
|
||
fn assign_twice() { | ||
loop { | ||
match () { | ||
#[allow(irrefutable_let_patterns)] | ||
() if let _ = split_last(&()) => {} | ||
_ => {} | ||
} | ||
} | ||
} | ||
|
||
fn main() {} |
32 changes: 32 additions & 0 deletions
32
tests/ui/rfcs/rfc-2294-if-let-guard/scoping-consistency-async.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,32 @@ | ||
// Check that temporaries in if-let guards are correctly scoped. | ||
// Regression test for #116079. | ||
|
||
// build-pass | ||
// edition:2018 | ||
// -Zvalidate-mir | ||
|
||
#![feature(if_let_guard)] | ||
|
||
static mut A: [i32; 5] = [1, 2, 3, 4, 5]; | ||
|
||
async fn fun() { | ||
unsafe { | ||
match A { | ||
_ => (), | ||
i if let Some(1) = async { Some(1) }.await => (), | ||
_ => (), | ||
} | ||
} | ||
} | ||
|
||
async fn funner() { | ||
unsafe { | ||
match A { | ||
_ => (), | ||
_ | _ if let Some(1) = async { Some(1) }.await => (), | ||
_ => (), | ||
} | ||
} | ||
} | ||
|
||
fn main() {} |
24 changes: 24 additions & 0 deletions
24
tests/ui/rfcs/rfc-2294-if-let-guard/scoping-consistency.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,24 @@ | ||
// Check that temporaries in if-let guards are correctly scoped. | ||
|
||
// build-pass | ||
// -Zvalidate-mir | ||
|
||
#![feature(if_let_guard)] | ||
|
||
fn fun() { | ||
match 0 { | ||
_ => (), | ||
_ if let Some(s) = std::convert::identity(&Some(String::new())) => {} | ||
_ => (), | ||
} | ||
} | ||
|
||
fn funner() { | ||
match 0 { | ||
_ => (), | ||
_ | _ if let Some(s) = std::convert::identity(&Some(String::new())) => {} | ||
_ => (), | ||
} | ||
} | ||
|
||
fn main() {} |
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