Sema: use correct value when @ptrCast operand is comptime known

Closes #13034
This commit is contained in:
Veikka Tuominen 2022-10-03 14:12:08 +03:00
parent 40578656e8
commit ba4aa12098
2 changed files with 12 additions and 1 deletions

View File

@ -18352,7 +18352,7 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
return sema.failWithOwnedErrorMsg(msg);
}
if (try sema.resolveMaybeUndefVal(block, operand_src, operand)) |operand_val| {
if (try sema.resolveMaybeUndefVal(block, operand_src, ptr)) |operand_val| {
if (!dest_ty.ptrAllowsZero() and operand_val.isUndef()) {
return sema.failWithUseOfUndef(block, operand_src);
}

View File

@ -483,3 +483,14 @@ test "pointer to constant decl preserves alignment" {
const alignment = @typeInfo(@TypeOf(&S.aligned)).Pointer.alignment;
try std.testing.expect(alignment == 8);
}
test "ptrCast comptime known slice to C pointer" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
const s: [:0]const u8 = "foo";
var p = @ptrCast([*c]const u8, s);
try std.testing.expectEqualStrings(s, std.mem.sliceTo(p, 0));
}