]> git.lizzy.rs Git - rust.git/blob - src/test/mir-opt/simplify-arm.rs
Rollup merge of #100247 - cjgillot:verify-dyn-trait-alias-defaults, r=lcnr
[rust.git] / src / test / mir-opt / simplify-arm.rs
1 // compile-flags: -Z mir-opt-level=3 -Zunsound-mir-opts
2 // EMIT_MIR simplify_arm.id.SimplifyArmIdentity.diff
3 // EMIT_MIR simplify_arm.id.SimplifyBranchSame.diff
4 // EMIT_MIR simplify_arm.id_result.SimplifyArmIdentity.diff
5 // EMIT_MIR simplify_arm.id_result.SimplifyBranchSame.diff
6 // EMIT_MIR simplify_arm.id_try.SimplifyArmIdentity.diff
7 // EMIT_MIR simplify_arm.id_try.SimplifyBranchSame.diff
8
9 fn id(o: Option<u8>) -> Option<u8> {
10     match o {
11         Some(v) => Some(v),
12         None => None,
13     }
14 }
15
16 fn id_result(r: Result<u8, i32>) -> Result<u8, i32> {
17     match r {
18         Ok(x) => Ok(x),
19         Err(y) => Err(y),
20     }
21 }
22
23 fn into_result<T, E>(r: Result<T, E>) -> Result<T, E> {
24     r
25 }
26
27 fn from_error<T, E>(e: E) -> Result<T, E> {
28     Err(e)
29 }
30
31 // This was written to the `?` from `try_trait`, but `try_trait_v2` uses a different structure,
32 // so the relevant desugar is copied inline in order to keep the test testing the same thing.
33 // FIXME(#85133): while this might be useful for `r#try!`, it would be nice to have a MIR
34 // optimization that picks up the `?` desugaring, as `SimplifyArmIdentity` does not.
35 fn id_try(r: Result<u8, i32>) -> Result<u8, i32> {
36     let x = match into_result(r) {
37         Err(e) => return from_error(From::from(e)),
38         Ok(v) => v,
39     };
40     Ok(x)
41 }
42
43 fn main() {
44     id(None);
45     id_result(Ok(4));
46     id_try(Ok(4));
47 }