| author | |
| committer | |
| log | e218b7ea0c2a907c5728006b38e7ca492e19ccb6 |
| tree | 636e5a1fc5fbe220cf29060c657b8719de78764f |
| parent | 0e118ed0aca3d852d2499fa37d04adef62b03ead |
3 files changed, 31 insertions(+), 0 deletions(-)
src/Sema.zig+9| ... | ... | @@ -17272,6 +17272,15 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 17272 | 17272 | else |
| 17273 | 17273 | operand; |
| 17274 | 17274 | |
| 17275 | if (try sema.resolveMaybeUndefVal(block, operand_src, operand)) |operand_val| { | |
| 17276 | if (!dest_ty.ptrAllowsZero() and operand_val.isUndef()) { | |
| 17277 | return sema.failWithUseOfUndef(block, operand_src); | |
| 17278 | } | |
| 17279 | if (!dest_ty.ptrAllowsZero() and operand_val.isNull()) { | |
| 17280 | return sema.fail(block, operand_src, "null pointer casted to type {}", .{dest_ty.fmt(sema.mod)}); | |
| 17281 | } | |
| 17282 | } | |
| 17283 | ||
| 17275 | 17284 | const dest_elem_ty = dest_ty.elemType2(); |
| 17276 | 17285 | try sema.resolveTypeLayout(block, dest_ty_src, dest_elem_ty); |
| 17277 | 17286 | const dest_align = dest_ty.ptrAlignment(target); |
test/cases/compile_errors/compile_time_null_ptr_cast.zig created+11| ... | ... | @@ -0,0 +1,11 @@ |
| 1 | comptime { | |
| 2 | var opt_ptr: ?*i32 = null; | |
| 3 | const ptr = @ptrCast(*i32, opt_ptr); | |
| 4 | _ = ptr; | |
| 5 | } | |
| 6 | ||
| 7 | // error | |
| 8 | // backend=llvm | |
| 9 | // target=native | |
| 10 | // | |
| 11 | // :3:32: error: null pointer casted to type *i32 |
test/cases/compile_errors/compile_time_undef_ptr_cast.zig created+11| ... | ... | @@ -0,0 +1,11 @@ |
| 1 | comptime { | |
| 2 | var undef_ptr: *i32 = undefined; | |
| 3 | const ptr = @ptrCast(*i32, undef_ptr); | |
| 4 | _ = ptr; | |
| 5 | } | |
| 6 | ||
| 7 | // error | |
| 8 | // backend=llvm | |
| 9 | // target=native | |
| 10 | // | |
| 11 | // :3:32: error: use of undefined value here causes undefined behavior |