Skip to content

Commit 7ec2261

Browse files
mitchellhandrewrk
authored andcommitted
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.
1 parent 804b82b commit 7ec2261

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

test/behavior/bugs/6456.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test "issue 6456" {
1919
.alignment = 0,
2020
.name = name,
2121
.field_type = usize,
22-
.default_value = @as(?usize, null),
22+
.default_value = &@as(?usize, null),
2323
.is_comptime = false,
2424
}};
2525
}

test/stage2/llvm.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,4 +422,17 @@ pub fn addCases(ctx: *TestContext) !void {
422422
\\}
423423
, "");
424424
}
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+
}
425438
}

0 commit comments

Comments
 (0)