From cba7e8a4e95aa2a2031d0fbaa8247de37e61fd78 Mon Sep 17 00:00:00 2001 From: mlugg Date: Fri, 15 Sep 2023 01:12:03 +0100 Subject: 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 --- test/behavior/cast.zig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'test/behavior') diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig index 334259661d..34d18f6717 100644 --- a/test/behavior/cast.zig +++ b/test/behavior/cast.zig @@ -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); +} -- cgit v1.2.3