Skip to content

Commit 5007f72

Browse files
committed
stage2: move C pointer allowzero error to AstGen
1 parent b5ac2b4 commit 5007f72

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/AstGen.zig

+4
Original file line numberDiff line numberDiff line change
@@ -3104,6 +3104,10 @@ fn ptrType(
31043104
node: Ast.Node.Index,
31053105
ptr_info: Ast.full.PtrType,
31063106
) InnerError!Zir.Inst.Ref {
3107+
if (ptr_info.size == .C and ptr_info.allowzero_token != null) {
3108+
return gz.astgen.failTok(ptr_info.allowzero_token.?, "C pointers always allow address zero", .{});
3109+
}
3110+
31073111
const elem_type = try typeExpr(gz, scope, ptr_info.ast.child_type);
31083112

31093113
const simple = ptr_info.ast.align_node == 0 and

src/Sema.zig

+3-5
Original file line numberDiff line numberDiff line change
@@ -13713,7 +13713,7 @@ fn zirPtrTypeSimple(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
1371313713
.@"volatile" = inst_data.is_volatile,
1371413714
.size = inst_data.size,
1371513715
});
13716-
try sema.validatePtrTy(block, elem_ty_src, ty, inst_data.is_allowzero);
13716+
try sema.validatePtrTy(block, elem_ty_src, ty);
1371713717
return sema.addType(ty);
1371813718
}
1371913719

@@ -13799,11 +13799,11 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1379913799
.@"volatile" = inst_data.flags.is_volatile,
1380013800
.size = inst_data.size,
1380113801
});
13802-
try sema.validatePtrTy(block, elem_ty_src, ty, inst_data.flags.is_allowzero);
13802+
try sema.validatePtrTy(block, elem_ty_src, ty);
1380313803
return sema.addType(ty);
1380413804
}
1380513805

13806-
fn validatePtrTy(sema: *Sema, block: *Block, elem_src: LazySrcLoc, ty: Type, explicit_allowzer: bool) CompileError!void {
13806+
fn validatePtrTy(sema: *Sema, block: *Block, elem_src: LazySrcLoc, ty: Type) CompileError!void {
1380713807
const ptr_info = ty.ptrInfo().data;
1380813808
const pointee_tag = ptr_info.pointee_type.zigTypeTag();
1380913809
if (pointee_tag == .NoReturn) {
@@ -13814,8 +13814,6 @@ fn validatePtrTy(sema: *Sema, block: *Block, elem_src: LazySrcLoc, ty: Type, exp
1381413814
// TODO check extern type
1381513815
if (pointee_tag == .Opaque) {
1381613816
return sema.fail(block, elem_src, "C pointers cannot point to opaque types", .{});
13817-
} else if (explicit_allowzer) {
13818-
return sema.fail(block, elem_src, "C pointers always allow address zero", .{});
1381913817
}
1382013818
}
1382113819
}

0 commit comments

Comments
 (0)