aboutsummaryrefslogtreecommitdiff
path: root/src/Sema.zig
diff options
context:
space:
mode:
authorInKryption <inkryption07@gmail.com>2022-08-10 17:09:27 +0200
committerAndrew Kelley <andrew@ziglang.org>2022-08-10 16:39:46 -0400
commite218b7ea0c2a907c5728006b38e7ca492e19ccb6 (patch)
tree636e5a1fc5fbe220cf29060c657b8719de78764f /src/Sema.zig
parent0e118ed0aca3d852d2499fa37d04adef62b03ead (diff)
downloadzig-e218b7ea0c2a907c5728006b38e7ca492e19ccb6.tar.gz
zig-e218b7ea0c2a907c5728006b38e7ca492e19ccb6.zip
stage2: add compile error for invalid null/undefined pointer cast
Diffstat (limited to 'src/Sema.zig')
-rw-r--r--src/Sema.zig9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index 4fefff7c26..fe19de39dd 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -17272,6 +17272,15 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
else
operand;
+ if (try sema.resolveMaybeUndefVal(block, operand_src, operand)) |operand_val| {
+ if (!dest_ty.ptrAllowsZero() and operand_val.isUndef()) {
+ return sema.failWithUseOfUndef(block, operand_src);
+ }
+ if (!dest_ty.ptrAllowsZero() and operand_val.isNull()) {
+ return sema.fail(block, operand_src, "null pointer casted to type {}", .{dest_ty.fmt(sema.mod)});
+ }
+ }
+
const dest_elem_ty = dest_ty.elemType2();
try sema.resolveTypeLayout(block, dest_ty_src, dest_elem_ty);
const dest_align = dest_ty.ptrAlignment(target);