forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#88642 - c410-f3r:let_chains_2, r=matthewjasper
Formally implement let chains ## Let chains My longest and hardest contribution since rust-lang#64010. Thanks to `@Centril` for creating the RFC and special thanks to `@matthewjasper` for helping me since the beginning of this journey. In fact, `@matthewjasper` did much of the complicated MIR stuff so it's true to say that this feature wouldn't be possible without him. Thanks again `@matthewjasper!` With the changes proposed in this PR, it will be possible to chain let expressions along side local variable declarations or ordinary conditional expressions. In other words, do much of what the `if_chain` crate already does. ## Other considerations * `if let guard` and `let ... else` features need special care and should be handled in a following PR. * Irrefutable patterns are allowed within a let chain context * ~~Three Clippy lints were already converted to start dogfooding and help detect possible corner cases~~ cc rust-lang#53667
- Loading branch information
Showing
31 changed files
with
536 additions
and
340 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
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 was deleted.
Oops, something went wrong.
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,93 @@ | ||
// run-pass | ||
// needs-unwind | ||
// ignore-wasm32-bare compiled with panic=abort by default | ||
|
||
// See `mir_drop_order.rs` for more information | ||
|
||
#![feature(let_chains)] | ||
|
||
use std::cell::RefCell; | ||
use std::panic; | ||
|
||
pub struct DropLogger<'a, T> { | ||
extra: T, | ||
id: usize, | ||
log: &'a panic::AssertUnwindSafe<RefCell<Vec<usize>>> | ||
} | ||
|
||
impl<'a, T> Drop for DropLogger<'a, T> { | ||
fn drop(&mut self) { | ||
self.log.0.borrow_mut().push(self.id); | ||
} | ||
} | ||
|
||
struct InjectedFailure; | ||
|
||
#[allow(unreachable_code)] | ||
fn main() { | ||
let log = panic::AssertUnwindSafe(RefCell::new(vec![])); | ||
let d = |id, extra| DropLogger { extra, id: id, log: &log }; | ||
let get = || -> Vec<_> { | ||
let mut m = log.0.borrow_mut(); | ||
let n = m.drain(..); | ||
n.collect() | ||
}; | ||
|
||
{ | ||
let _x = ( | ||
d( | ||
0, | ||
d( | ||
1, | ||
if let Some(_) = d(2, Some(true)).extra && let DropLogger { .. } = d(3, None) { | ||
None | ||
} else { | ||
Some(true) | ||
} | ||
).extra | ||
), | ||
d(4, None), | ||
&d(5, None), | ||
d(6, None), | ||
if let DropLogger { .. } = d(7, None) && let DropLogger { .. } = d(8, None) { | ||
d(9, None) | ||
} | ||
else { | ||
// 10 is not constructed | ||
d(10, None) | ||
} | ||
); | ||
assert_eq!(get(), vec![3, 8, 7, 1, 2]); | ||
} | ||
assert_eq!(get(), vec![0, 4, 6, 9, 5]); | ||
|
||
let _ = std::panic::catch_unwind(|| { | ||
( | ||
d( | ||
11, | ||
d( | ||
12, | ||
if let Some(_) = d(13, Some(true)).extra | ||
&& let DropLogger { .. } = d(14, None) | ||
{ | ||
None | ||
} else { | ||
Some(true) | ||
} | ||
).extra | ||
), | ||
d(15, None), | ||
&d(16, None), | ||
d(17, None), | ||
if let DropLogger { .. } = d(18, None) && let DropLogger { .. } = d(19, None) { | ||
d(20, None) | ||
} | ||
else { | ||
// 10 is not constructed | ||
d(21, None) | ||
}, | ||
panic::panic_any(InjectedFailure) | ||
); | ||
}); | ||
assert_eq!(get(), vec![14, 19, 20, 17, 15, 11, 18, 16, 12, 13]); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.