forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
let-else: add tests for moved expressions, copy out of non-copy
- Loading branch information
1 parent
34a9819
commit 61bcd8d
Showing
2 changed files
with
62 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// run-pass | ||
// | ||
// This is derived from a change to compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs, in | ||
// preparation for adopting let-else within the compiler (thanks @est31): | ||
// | ||
// ``` | ||
// - let place = if let mir::VarDebugInfoContents::Place(p) = var.value { p } else { continue }; | ||
// + let mir::VarDebugInfoContents::Place(place) = var.value else { continue }; | ||
// ``` | ||
// | ||
// The move was due to mir::Place being Copy, but mir::VarDebugInfoContents not being Copy. | ||
|
||
#![feature(let_else)] | ||
|
||
#[derive(Copy, Clone)] | ||
struct Copyable; | ||
|
||
enum NonCopy { | ||
Thing(Copyable), | ||
#[allow(unused)] | ||
Other, | ||
} | ||
|
||
struct Wrapper { | ||
field: NonCopy, | ||
} | ||
|
||
fn let_else() { | ||
let vec = vec![Wrapper { field: NonCopy::Thing(Copyable) }]; | ||
for item in &vec { | ||
let NonCopy::Thing(_copyable) = item.field else { continue }; | ||
} | ||
} | ||
|
||
fn if_let() { | ||
let vec = vec![Wrapper { field: NonCopy::Thing(Copyable) }]; | ||
for item in &vec { | ||
let _copyable = if let NonCopy::Thing(copyable) = item.field { copyable } else { continue }; | ||
} | ||
} | ||
|
||
fn main() { | ||
let_else(); | ||
if_let(); | ||
} |
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 @@ | ||
// run-pass | ||
// issue #89688 | ||
|
||
#![feature(let_else)] | ||
|
||
fn example_let_else(value: Option<String>) { | ||
let Some(inner) = value else { | ||
println!("other: {:?}", value); // OK | ||
return; | ||
}; | ||
println!("inner: {}", inner); | ||
} | ||
|
||
fn main() { | ||
example_let_else(Some("foo".into())); | ||
example_let_else(None); | ||
} |