2021-03-04 07:35:11 -06:00
|
|
|
// compile-flags: -Z mir-opt-level=3 -Zunsound-mir-opts
|
2020-07-27 14:22:43 -05:00
|
|
|
// EMIT_MIR simplify_arm.id.SimplifyArmIdentity.diff
|
|
|
|
// EMIT_MIR simplify_arm.id.SimplifyBranchSame.diff
|
|
|
|
// EMIT_MIR simplify_arm.id_result.SimplifyArmIdentity.diff
|
|
|
|
// EMIT_MIR simplify_arm.id_result.SimplifyBranchSame.diff
|
|
|
|
// EMIT_MIR simplify_arm.id_try.SimplifyArmIdentity.diff
|
|
|
|
// EMIT_MIR simplify_arm.id_try.SimplifyBranchSame.diff
|
Modify SimplifyArmIdentity so it can trigger on mir-opt-level=1
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.
2020-05-11 19:13:15 -05:00
|
|
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 00:02:48 -05:00
|
|
|
fn into_result<T, E>(r: Result<T, E>) -> Result<T, E> {
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_error<T, E>(e: E) -> Result<T, E> {
|
|
|
|
Err(e)
|
|
|
|
}
|
|
|
|
|
2021-05-10 00:05:02 -05:00
|
|
|
// This was written to the `?` from `try_trait`, but `try_trait_v2` uses a different structure,
|
|
|
|
// so the relevant desugar is copied inline in order to keep the test testing the same thing.
|
2021-05-18 13:29:34 -05:00
|
|
|
// FIXME(#85133): while this might be useful for `r#try!`, it would be nice to have a MIR optimization
|
2021-05-10 00:05:02 -05:00
|
|
|
// that picks up the `?` desugaring, as `SimplifyArmIdentity` does not. See #85133
|
Modify SimplifyArmIdentity so it can trigger on mir-opt-level=1
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.
2020-05-11 19:13:15 -05:00
|
|
|
fn id_try(r: Result<u8, i32>) -> Result<u8, i32> {
|
2021-04-26 00:02:48 -05:00
|
|
|
let x = match into_result(r) {
|
|
|
|
Err(e) => return from_error(From::from(e)),
|
|
|
|
Ok(v) => v,
|
|
|
|
};
|
Modify SimplifyArmIdentity so it can trigger on mir-opt-level=1
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.
2020-05-11 19:13:15 -05:00
|
|
|
Ok(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
id(None);
|
|
|
|
id_result(Ok(4));
|
|
|
|
id_try(Ok(4));
|
|
|
|
}
|