diff --git a/src/Sema.zig b/src/Sema.zig index 109b6f2a22ea..eb228c824ede 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -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); } diff --git a/test/behavior/pointers.zig b/test/behavior/pointers.zig index 91d398e84dbb..b0fb0f3a4239 100644 --- a/test/behavior/pointers.zig +++ b/test/behavior/pointers.zig @@ -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)); +}