2023-10-16 17:36:39 +00:00
|
|
|
// skip-filecheck
|
2021-03-04 13:35:11 +00:00
|
|
|
//@ compile-flags: -Z mir-opt-level=3 -Zunsound-mir-opts
|
2020-07-27 19:22:43 +00: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-12 00:13:15 +00:00
|
|
|
|
2023-04-15 23:11:42 +00:00
|
|
|
//@ ignore-test This pass is broken since deaggregation changed
|
2022-08-21 04:47:53 +00:00
|
|
|
|
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-12 00:13:15 +00: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 05:02:48 +00: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 05:05:02 +00: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 18:48:00 +00:00
|
|
|
// FIXME(#85133): while this might be useful for `r#try!`, it would be nice to have a MIR
|
|
|
|
// optimization that picks up the `?` desugaring, as `SimplifyArmIdentity` does not.
|
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-12 00:13:15 +00:00
|
|
|
fn id_try(r: Result<u8, i32>) -> Result<u8, i32> {
|
2021-04-26 05:02:48 +00: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-12 00:13:15 +00:00
|
|
|
Ok(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
id(None);
|
|
|
|
id_result(Ok(4));
|
|
|
|
id_try(Ok(4));
|
|
|
|
}
|