diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2023-09-15 01:12:03 +0100 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-09-15 01:05:02 -0700 |
| commit | cba7e8a4e95aa2a2031d0fbaa8247de37e61fd78 (patch) | |
| tree | f6e7c7139d90aa41ace1498475d6a56f4c40956f /test | |
| parent | 8592c5cdac41e4e04034e4f9a0fd8cb51e8c4257 (diff) | |
| download | zig-cba7e8a4e95aa2a2031d0fbaa8247de37e61fd78.tar.gz zig-cba7e8a4e95aa2a2031d0fbaa8247de37e61fd78.zip | |
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
Diffstat (limited to 'test')
| -rw-r--r-- | test/behavior/cast.zig | 18 |
1 files changed, 18 insertions, 0 deletions
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); +} |
