| ... | @@ -167,3 +167,36 @@ test "if-@as-if chain" { | ... | @@ -167,3 +167,36 @@ test "if-@as-if chain" { |
| 167 | | 167 | |
| 168 | try expect(num_frames == 4); | 168 | try expect(num_frames == 4); |
| 169 | } | 169 | } |
| | 170 | |
| | 171 | fn returnTrue() bool { |
| | 172 | return true; |
| | 173 | } |
| | 174 | |
| | 175 | test "if value shouldn't be load-elided if used later (structs)" { |
| | 176 | const Foo = struct { x: i32 }; |
| | 177 | |
| | 178 | var a = Foo{ .x = 1 }; |
| | 179 | var b = Foo{ .x = 1 }; |
| | 180 | |
| | 181 | const c = if (@call(.never_inline, returnTrue, .{})) a else b; |
| | 182 | // The second variable is superfluous with the current |
| | 183 | // state of codegen optimizations, but in future |
| | 184 | // "if (smthg) a else a" may be optimized simply into "a". |
| | 185 | |
| | 186 | a.x = 2; |
| | 187 | b.x = 3; |
| | 188 | |
| | 189 | try std.testing.expectEqual(c.x, 1); |
| | 190 | } |
| | 191 | |
| | 192 | test "if value shouldn't be load-elided if used later (optionals)" { |
| | 193 | var a: ?i32 = 1; |
| | 194 | var b: ?i32 = 1; |
| | 195 | |
| | 196 | const c = if (@call(.never_inline, returnTrue, .{})) a else b; |
| | 197 | |
| | 198 | a = 2; |
| | 199 | b = 3; |
| | 200 | |
| | 201 | try std.testing.expectEqual(c, 1); |
| | 202 | } |