]>
Commit | Line | Data |
---|---|---|
dc9dc135 XL |
1 | // Test that constructors are considered to be const fns with the required feature. |
2 | ||
3 | // run-pass | |
4 | ||
5 | // revisions: min_const_fn const_fn | |
6 | ||
7 | #![cfg_attr(const_fn, feature(const_fn))] | |
8 | ||
dc9dc135 XL |
9 | // Ctor(..) is transformed to Ctor { 0: ... } in HAIR lowering, so directly |
10 | // calling constructors doesn't require them to be const. | |
11 | ||
12 | type ExternalType = std::panic::AssertUnwindSafe<(Option<i32>, Result<i32, bool>)>; | |
13 | ||
14 | const fn call_external_constructors_in_local_vars() -> ExternalType { | |
15 | let f = Some; | |
16 | let g = Err; | |
17 | let h = std::panic::AssertUnwindSafe; | |
18 | let x = f(5); | |
19 | let y = g(false); | |
20 | let z = h((x, y)); | |
21 | z | |
22 | } | |
23 | ||
24 | const CALL_EXTERNAL_CONSTRUCTORS_IN_LOCAL_VARS: ExternalType = { | |
25 | let f = Some; | |
26 | let g = Err; | |
27 | let h = std::panic::AssertUnwindSafe; | |
28 | let x = f(5); | |
29 | let y = g(false); | |
30 | let z = h((x, y)); | |
31 | z | |
32 | }; | |
33 | ||
34 | const fn call_external_constructors_in_temps() -> ExternalType { | |
35 | let x = { Some }(5); | |
36 | let y = (*&Err)(false); | |
37 | let z = [std::panic::AssertUnwindSafe][0]((x, y)); | |
38 | z | |
39 | } | |
40 | ||
41 | const CALL_EXTERNAL_CONSTRUCTORS_IN_TEMPS: ExternalType = { | |
42 | let x = { Some }(5); | |
43 | let y = (*&Err)(false); | |
44 | let z = [std::panic::AssertUnwindSafe][0]((x, y)); | |
45 | z | |
46 | }; | |
47 | ||
48 | #[derive(Debug, PartialEq)] | |
49 | enum LocalOption<T> { | |
50 | Some(T), | |
51 | _None, | |
52 | } | |
53 | ||
54 | #[derive(Debug, PartialEq)] | |
55 | enum LocalResult<T, E> { | |
56 | _Ok(T), | |
57 | Err(E), | |
58 | } | |
59 | ||
60 | #[derive(Debug, PartialEq)] | |
61 | struct LocalAssertUnwindSafe<T>(T); | |
62 | ||
63 | type LocalType = LocalAssertUnwindSafe<(LocalOption<i32>, LocalResult<i32, bool>)>; | |
64 | ||
65 | const fn call_local_constructors_in_local_vars() -> LocalType { | |
66 | let f = LocalOption::Some; | |
67 | let g = LocalResult::Err; | |
68 | let h = LocalAssertUnwindSafe; | |
69 | let x = f(5); | |
70 | let y = g(false); | |
71 | let z = h((x, y)); | |
72 | z | |
73 | } | |
74 | ||
75 | const CALL_LOCAL_CONSTRUCTORS_IN_LOCAL_VARS: LocalType = { | |
76 | let f = LocalOption::Some; | |
77 | let g = LocalResult::Err; | |
78 | let h = LocalAssertUnwindSafe; | |
79 | let x = f(5); | |
80 | let y = g(false); | |
81 | let z = h((x, y)); | |
82 | z | |
83 | }; | |
84 | ||
85 | const fn call_local_constructors_in_temps() -> LocalType { | |
86 | let x = { LocalOption::Some }(5); | |
87 | let y = (*&LocalResult::Err)(false); | |
88 | let z = [LocalAssertUnwindSafe][0]((x, y)); | |
89 | z | |
90 | } | |
91 | ||
92 | const CALL_LOCAL_CONSTRUCTORS_IN_TEMPS: LocalType = { | |
93 | let x = { LocalOption::Some }(5); | |
94 | let y = (*&LocalResult::Err)(false); | |
95 | let z = [LocalAssertUnwindSafe][0]((x, y)); | |
96 | z | |
97 | }; | |
98 | ||
99 | fn main() { | |
100 | assert_eq!( | |
101 | ( | |
102 | call_external_constructors_in_local_vars().0, | |
103 | call_external_constructors_in_temps().0, | |
104 | call_local_constructors_in_local_vars(), | |
105 | call_local_constructors_in_temps(), | |
106 | ), | |
107 | ( | |
108 | CALL_EXTERNAL_CONSTRUCTORS_IN_LOCAL_VARS.0, | |
109 | CALL_EXTERNAL_CONSTRUCTORS_IN_TEMPS.0, | |
110 | CALL_LOCAL_CONSTRUCTORS_IN_LOCAL_VARS, | |
111 | CALL_LOCAL_CONSTRUCTORS_IN_TEMPS, | |
112 | ) | |
113 | ); | |
114 | } |