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
21382138 .alloc_inferred_mut,
21392139 .alloc_inferred_comptime,
21402140 .alloc_inferred_comptime_mut,
2141 .make_ptr_const,
21412142 .array_cat,
21422143 .array_mul,
21432144 .array_type,
......@@ -2754,12 +2755,13 @@ fn varDecl(
27542755 if (resolve_inferred_alloc != .none) {
27552756 _ = try gz.addUnNode(.resolve_inferred_alloc, resolve_inferred_alloc, node);
27562757 }
2758 const const_ptr = try gz.addUnNode(.make_ptr_const, init_scope.rl_ptr, node);
27572759 const sub_scope = try block_arena.create(Scope.LocalPtr);
27582760 sub_scope.* = .{
27592761 .parent = scope,
27602762 .gen_zir = gz,
27612763 .name = ident_name,
2762 .ptr = init_scope.rl_ptr,
2764 .ptr = const_ptr,
27632765 .token_src = name_token,
27642766 .maybe_comptime = true,
27652767 .id_cat = .@"local constant",
src/Sema.zig+16
......@@ -607,6 +607,7 @@ fn analyzeBodyInner(
607607 .alloc_inferred_comptime_mut => try sema.zirAllocInferredComptime(inst, Type.initTag(.inferred_alloc_mut)),
608608 .alloc_mut => try sema.zirAllocMut(block, inst),
609609 .alloc_comptime_mut => try sema.zirAllocComptime(block, inst),
610 .make_ptr_const => try sema.zirMakePtrConst(block, inst),
610611 .anyframe_type => try sema.zirAnyframeType(block, inst),
611612 .array_cat => try sema.zirArrayCat(block, inst),
612613 .array_mul => try sema.zirArrayMul(block, inst),
......@@ -2409,6 +2410,21 @@ fn zirAllocComptime(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
24092410 return sema.analyzeComptimeAlloc(block, var_ty, 0, ty_src);
24102411}
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
24122428fn zirAllocInferredComptime(
24132429 sema: *Sema,
24142430 inst: Zir.Inst.Index,
src/Zir.zig+6
......@@ -954,6 +954,10 @@ pub const Inst = struct {
954954 /// is the allocation that needs to have its type inferred.
955955 /// Uses the `un_node` field. The AST node is the var decl.
956956 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
958962 /// Implements `resume` syntax. Uses `un_node` field.
959963 @"resume",
......@@ -993,6 +997,7 @@ pub const Inst = struct {
993997 .alloc_inferred_mut,
994998 .alloc_inferred_comptime,
995999 .alloc_inferred_comptime_mut,
1000 .make_ptr_const,
9961001 .array_cat,
9971002 .array_mul,
9981003 .array_type,
......@@ -1496,6 +1501,7 @@ pub const Inst = struct {
14961501 .alloc_inferred_comptime = .node,
14971502 .alloc_inferred_comptime_mut = .node,
14981503 .resolve_inferred_alloc = .un_node,
1504 .make_ptr_const = .un_node,
14991505
15001506 .@"resume" = .un_node,
15011507 .@"await" = .un_node,
src/print_zir.zig+1
......@@ -238,6 +238,7 @@ const Writer = struct {
238238 .field_base_ptr,
239239 .validate_array_init_ty,
240240 .validate_struct_init_ty,
241 .make_ptr_const,
241242 => try self.writeUnNode(stream, inst),
242243
243244 .ref,
test/behavior/bugs/5474.zig-2
......@@ -54,8 +54,6 @@ test "pointer-to-array constness for zero-size elements, var" {
5454}
5555
5656test "pointer-to-array constness for zero-size elements, const" {
57 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
58
5957 try constant();
6058 comptime try constant();
6159}