]> git.lizzy.rs Git - rust.git/blob - tests/codegen/try_identity.rs
Rollup merge of #107731 - RalfJung:interpret-discriminant, r=cjgillot
[rust.git] / tests / codegen / try_identity.rs
1 // compile-flags: -C no-prepopulate-passes -O -Z mir-opt-level=3 -Zunsound-mir-opts
2
3 // Ensure that `x?` has no overhead on `Result<T, E>` due to identity `match`es in lowering.
4 // This requires inlining to trigger the MIR optimizations in `SimplifyArmIdentity`.
5
6 #![crate_type = "lib"]
7
8 type R = Result<u64, i32>;
9
10 // This was written to the `?` from `try_trait`, but `try_trait_v2` uses a different structure,
11 // so the relevant desugar is copied inline in order to keep the test testing the same thing.
12 // FIXME(#85133): while this might be useful for `r#try!`, it would be nice to have a MIR
13 // optimization that picks up the `?` desugaring, as `SimplifyArmIdentity` does not.
14 #[no_mangle]
15 pub fn try_identity(x: R) -> R {
16 // CHECK: start:
17 // FIXME(JakobDegen): Broken by deaggregation change CHECK-NOT\: br {{.*}}
18 // CHECK ret void
19     let y = match into_result(x) {
20         Err(e) => return from_error(From::from(e)),
21         Ok(v) => v,
22     };
23     Ok(y)
24 }
25
26 #[inline]
27 fn into_result<T, E>(r: Result<T, E>) -> Result<T, E> {
28     r
29 }
30
31 #[inline]
32 fn from_error<T, E>(e: E) -> Result<T, E> {
33     Err(e)
34 }