I also added test cases to make sure the optimization can fire on all of
these cases:
```rust
fn case_1(o: Option<u8>) -> Option<u8> {
match o {
Some(u) => Some(u),
None => None,
}
}
fn case2(r: Result<u8, i32>) -> Result<u8, i32> {
match r {
Ok(u) => Ok(u),
Err(i) => Err(i),
}
}
fn case3(r: Result<u8, i32>) -> Result<u8, i32> {
let u = r?;
Ok(u)
}
```
Without MIR inlining, this still does not completely optimize away the
`?` operator because the `Try::into_result()`, `From::from()` and
`Try::from_error()` calls still exist. This does move us a bit closer to
that goal though because:
- We can now run the pass on mir-opt-level=1
- We no longer depend on the copy propagation pass running which is
unlikely to stabilize anytime soon.
32 lines
719 B
Rust
32 lines
719 B
Rust
// compile-flags: -Z mir-opt-level=1
|
|
// EMIT_MIR rustc.id.SimplifyArmIdentity.diff
|
|
// EMIT_MIR rustc.id.SimplifyBranchSame.diff
|
|
// EMIT_MIR rustc.id_result.SimplifyArmIdentity.diff
|
|
// EMIT_MIR rustc.id_result.SimplifyBranchSame.diff
|
|
// EMIT_MIR rustc.id_try.SimplifyArmIdentity.diff
|
|
// EMIT_MIR rustc.id_try.SimplifyBranchSame.diff
|
|
|
|
fn id(o: Option<u8>) -> Option<u8> {
|
|
match o {
|
|
Some(v) => Some(v),
|
|
None => None,
|
|
}
|
|
}
|
|
|
|
fn id_result(r: Result<u8, i32>) -> Result<u8, i32> {
|
|
match r {
|
|
Ok(x) => Ok(x),
|
|
Err(y) => Err(y),
|
|
}
|
|
}
|
|
|
|
fn id_try(r: Result<u8, i32>) -> Result<u8, i32> {
|
|
let x = r?;
|
|
Ok(x)
|
|
}
|
|
|
|
fn main() {
|
|
id(None);
|
|
id_result(Ok(4));
|
|
id_try(Ok(4));
|
|
}
|