authorgravatar for mitchell.hashimoto@gmail.comMitchell Hashimoto <mitchell.hashimoto@gmail.com> 2022-03-12 15:01:43-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-12 22:03:01-05:00
log7ec2261dbf7a85f5c215f2dac22b80fca7de1e0f
treeb8faff85307df51c3a91cd6c46ec432ba9e175d0
parent804b82b6e82b938eaa736fc90442dd4fd552717e

stage2: add compiler test to ensure typed null doesn't coerce to any

In stage1, this behavior was allowed (by accident?) and also accidentally exercised by the behavior test changed in this commit. In discussion on Discord, Andrew decided this should not be allowed in stage2 since there is currently on real world reason to allow this strange edge case. I've added the compiler test to solidify that this behavior should NOT occur and updated the behavior test to the new valid semantics.

2 files changed, 14 insertions(+), 1 deletions(-)

test/behavior/bugs/6456.zig+1-1
...@@ -19,7 +19,7 @@ test "issue 6456" {...@@ -19,7 +19,7 @@ test "issue 6456" {
19 .alignment = 0,19 .alignment = 0,
20 .name = name,20 .name = name,
21 .field_type = usize,21 .field_type = usize,
22 .default_value = @as(?usize, null),22 .default_value = &@as(?usize, null),
23 .is_comptime = false,23 .is_comptime = false,
24 }};24 }};
25 }25 }
test/stage2/llvm.zig+13
...@@ -422,4 +422,17 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -422,4 +422,17 @@ pub fn addCases(ctx: *TestContext) !void {
422 \\}422 \\}
423 , "");423 , "");
424 }424 }
425
426 {
427 // This worked in stage1 and we expressly do not want this to work in stage2
428 var case = ctx.exeUsingLlvmBackend("any typed null to any typed optional", linux_x64);
429 case.addError(
430 \\pub export fn main() void {
431 \\ var a: ?*anyopaque = undefined;
432 \\ a = @as(?usize, null);
433 \\}
434 , &[_][]const u8{
435 ":3:21: error: expected pointer type, found '?usize'",
436 });
437 }
425}438}