authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-03-07 20:41:48+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-03-08 11:23:39+02:00
log8f037db88552b5171807d4a9f8be6e8942aab8a1
treed9985543d0373138ba2357529a139c3047a61511
parent1f4a097117a75b053d620658df4cb4e81c40fe1a

stage2: correct constness of allocs


5 files changed, 26 insertions(+), 3 deletions(-)

src/AstGen.zig+3-1
...@@ -2138,6 +2138,7 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner...@@ -2138,6 +2138,7 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
2138 .alloc_inferred_mut,2138 .alloc_inferred_mut,
2139 .alloc_inferred_comptime,2139 .alloc_inferred_comptime,
2140 .alloc_inferred_comptime_mut,2140 .alloc_inferred_comptime_mut,
2141 .make_ptr_const,
2141 .array_cat,2142 .array_cat,
2142 .array_mul,2143 .array_mul,
2143 .array_type,2144 .array_type,
...@@ -2754,12 +2755,13 @@ fn varDecl(...@@ -2754,12 +2755,13 @@ fn varDecl(
2754 if (resolve_inferred_alloc != .none) {2755 if (resolve_inferred_alloc != .none) {
2755 _ = try gz.addUnNode(.resolve_inferred_alloc, resolve_inferred_alloc, node);2756 _ = try gz.addUnNode(.resolve_inferred_alloc, resolve_inferred_alloc, node);
2756 }2757 }
2758 const const_ptr = try gz.addUnNode(.make_ptr_const, init_scope.rl_ptr, node);
2757 const sub_scope = try block_arena.create(Scope.LocalPtr);2759 const sub_scope = try block_arena.create(Scope.LocalPtr);
2758 sub_scope.* = .{2760 sub_scope.* = .{
2759 .parent = scope,2761 .parent = scope,
2760 .gen_zir = gz,2762 .gen_zir = gz,
2761 .name = ident_name,2763 .name = ident_name,
2762 .ptr = init_scope.rl_ptr,2764 .ptr = const_ptr,
2763 .token_src = name_token,2765 .token_src = name_token,
2764 .maybe_comptime = true,2766 .maybe_comptime = true,
2765 .id_cat = .@"local constant",2767 .id_cat = .@"local constant",
src/Sema.zig+16
...@@ -607,6 +607,7 @@ fn analyzeBodyInner(...@@ -607,6 +607,7 @@ fn analyzeBodyInner(
607 .alloc_inferred_comptime_mut => try sema.zirAllocInferredComptime(inst, Type.initTag(.inferred_alloc_mut)),607 .alloc_inferred_comptime_mut => try sema.zirAllocInferredComptime(inst, Type.initTag(.inferred_alloc_mut)),
608 .alloc_mut => try sema.zirAllocMut(block, inst),608 .alloc_mut => try sema.zirAllocMut(block, inst),
609 .alloc_comptime_mut => try sema.zirAllocComptime(block, inst),609 .alloc_comptime_mut => try sema.zirAllocComptime(block, inst),
610 .make_ptr_const => try sema.zirMakePtrConst(block, inst),
610 .anyframe_type => try sema.zirAnyframeType(block, inst),611 .anyframe_type => try sema.zirAnyframeType(block, inst),
611 .array_cat => try sema.zirArrayCat(block, inst),612 .array_cat => try sema.zirArrayCat(block, inst),
612 .array_mul => try sema.zirArrayMul(block, inst),613 .array_mul => try sema.zirArrayMul(block, inst),
...@@ -2409,6 +2410,21 @@ fn zirAllocComptime(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr...@@ -2409,6 +2410,21 @@ fn zirAllocComptime(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
2409 return sema.analyzeComptimeAlloc(block, var_ty, 0, ty_src);2410 return sema.analyzeComptimeAlloc(block, var_ty, 0, ty_src);
2410}2411}
24112412
2413fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
2414 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
2415 const ptr = sema.resolveInst(inst_data.operand);
2416 const ptr_ty = sema.typeOf(ptr);
2417 var ptr_info = ptr_ty.ptrInfo().data;
2418 ptr_info.mutable = false;
2419 const const_ptr_ty = try Type.ptr(sema.arena, sema.mod.getTarget(), ptr_info);
2420
2421 if (try sema.resolveMaybeUndefVal(block, inst_data.src(), ptr)) |val| {
2422 return sema.addConstant(const_ptr_ty, val);
2423 }
2424 try sema.requireRuntimeBlock(block, inst_data.src());
2425 return block.addBitCast(const_ptr_ty, ptr);
2426}
2427
2412fn zirAllocInferredComptime(2428fn zirAllocInferredComptime(
2413 sema: *Sema,2429 sema: *Sema,
2414 inst: Zir.Inst.Index,2430 inst: Zir.Inst.Index,
src/Zir.zig+6
...@@ -954,6 +954,10 @@ pub const Inst = struct {...@@ -954,6 +954,10 @@ pub const Inst = struct {
954 /// is the allocation that needs to have its type inferred.954 /// is the allocation that needs to have its type inferred.
955 /// Uses the `un_node` field. The AST node is the var decl.955 /// Uses the `un_node` field. The AST node is the var decl.
956 resolve_inferred_alloc,956 resolve_inferred_alloc,
957 /// Turns a pointer coming from an `alloc`, `alloc_inferred`, `alloc_inferred_comptime` or
958 /// `Extended.alloc` into a constant version of the same pointer.
959 /// Uses the `un_node` union field.
960 make_ptr_const,
957961
958 /// Implements `resume` syntax. Uses `un_node` field.962 /// Implements `resume` syntax. Uses `un_node` field.
959 @"resume",963 @"resume",
...@@ -993,6 +997,7 @@ pub const Inst = struct {...@@ -993,6 +997,7 @@ pub const Inst = struct {
993 .alloc_inferred_mut,997 .alloc_inferred_mut,
994 .alloc_inferred_comptime,998 .alloc_inferred_comptime,
995 .alloc_inferred_comptime_mut,999 .alloc_inferred_comptime_mut,
1000 .make_ptr_const,
996 .array_cat,1001 .array_cat,
997 .array_mul,1002 .array_mul,
998 .array_type,1003 .array_type,
...@@ -1496,6 +1501,7 @@ pub const Inst = struct {...@@ -1496,6 +1501,7 @@ pub const Inst = struct {
1496 .alloc_inferred_comptime = .node,1501 .alloc_inferred_comptime = .node,
1497 .alloc_inferred_comptime_mut = .node,1502 .alloc_inferred_comptime_mut = .node,
1498 .resolve_inferred_alloc = .un_node,1503 .resolve_inferred_alloc = .un_node,
1504 .make_ptr_const = .un_node,
14991505
1500 .@"resume" = .un_node,1506 .@"resume" = .un_node,
1501 .@"await" = .un_node,1507 .@"await" = .un_node,
src/print_zir.zig+1
...@@ -238,6 +238,7 @@ const Writer = struct {...@@ -238,6 +238,7 @@ const Writer = struct {
238 .field_base_ptr,238 .field_base_ptr,
239 .validate_array_init_ty,239 .validate_array_init_ty,
240 .validate_struct_init_ty,240 .validate_struct_init_ty,
241 .make_ptr_const,
241 => try self.writeUnNode(stream, inst),242 => try self.writeUnNode(stream, inst),
242243
243 .ref,244 .ref,
test/behavior/bugs/5474.zig-2
...@@ -54,8 +54,6 @@ test "pointer-to-array constness for zero-size elements, var" {...@@ -54,8 +54,6 @@ test "pointer-to-array constness for zero-size elements, var" {
54}54}
5555
56test "pointer-to-array constness for zero-size elements, const" {56test "pointer-to-array constness for zero-size elements, const" {
57 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
58
59 try constant();57 try constant();
60 comptime try constant();58 comptime try constant();
61}59}