AstGen: do not forward result pointers through @as

The `coerce_result_ptr` instruction is highly problematic and leads to
unintentional memory reinterpretation in some cases. It is more correct
to simply not forward result pointers through this builtin.

`coerce_result_ptr` is still used for struct and array initializations,
where it can still cause issues. Eliminating this usage will be a future
change.

Resolves: #16991
This commit is contained in:
mlugg
2023-09-15 01:12:03 +01:00
committed by Andrew Kelley
parent 8592c5cdac
commit cba7e8a4e9
3 changed files with 24 additions and 35 deletions

View File

@@ -2502,3 +2502,21 @@ test "numeric coercions with undefined" {
to = 42.0;
try expectEqual(@as(f32, 42.0), to);
}
test "@as does not corrupt values with incompatible representations" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const x: f32 = @as(f16, blk: {
if (false) {
// Trick the compiler into trying to use a result pointer if it can!
break :blk .{undefined};
}
break :blk 1.23;
});
try std.testing.expectApproxEqAbs(@as(f32, 1.23), x, 0.001);
}