authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-19 16:49:34-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-21 21:38:41-04:00
log5e1db5e47800a8e9bb1d8d11f07b0023bd8e49ef
tree86ef8aa2fe9c9b839dfbe8df83e453baee5a17d2
parent7bab406c790566781406a7968be22961ed7c305d

Sema: migrate zirResolveInferredAlloc to new anon decl mechanism

This required a bug fix in zirMakePtrConst.

1 files changed, 29 insertions(+), 16 deletions(-)

src/Sema.zig+29-16
......@@ -3672,10 +3672,18 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
36723672 implicit_ct: {
36733673 const ptr_val = try sema.resolveMaybeUndefVal(alloc) orelse break :implicit_ct;
36743674 if (!ptr_val.isComptimeMutablePtr(mod)) {
3675 // It could still be a constant pointer to a decl
3676 const decl_index = ptr_val.pointerDecl(mod) orelse break :implicit_ct;
3677 const decl_val = mod.declPtr(decl_index).val.toIntern();
3678 if (mod.intern_pool.isRuntimeValue(decl_val)) break :implicit_ct;
3675 // It could still be a constant pointer to a decl.
3676 switch (mod.intern_pool.indexToKey(ptr_val.toIntern()).ptr.addr) {
3677 .anon_decl => |anon_decl| {
3678 if (mod.intern_pool.isVariable(anon_decl.val))
3679 break :implicit_ct;
3680 },
3681 else => {
3682 const decl_index = ptr_val.pointerDecl(mod) orelse break :implicit_ct;
3683 const decl_val = mod.declPtr(decl_index).val.toIntern();
3684 if (mod.intern_pool.isRuntimeValue(decl_val)) break :implicit_ct;
3685 },
3686 }
36793687 }
36803688 return sema.makePtrConst(block, alloc);
36813689 }
......@@ -3915,17 +3923,19 @@ fn finishResolveComptimeKnownAllocValue(sema: *Sema, result_val: InternPool.Inde
39153923 return result_val;
39163924}
39173925
3926fn makePtrTyConst(sema: *Sema, ptr_ty: Type) CompileError!Type {
3927 var ptr_info = ptr_ty.ptrInfo(sema.mod);
3928 ptr_info.flags.is_const = true;
3929 return sema.ptrType(ptr_info);
3930}
3931
39183932fn makePtrConst(sema: *Sema, block: *Block, alloc: Air.Inst.Ref) CompileError!Air.Inst.Ref {
3919 const mod = sema.mod;
39203933 const alloc_ty = sema.typeOf(alloc);
3921
3922 var ptr_info = alloc_ty.ptrInfo(mod);
3923 ptr_info.flags.is_const = true;
3924 const const_ptr_ty = try sema.ptrType(ptr_info);
3934 const const_ptr_ty = try sema.makePtrTyConst(alloc_ty);
39253935
39263936 // Detect if a comptime value simply needs to have its type changed.
39273937 if (try sema.resolveMaybeUndefVal(alloc)) |val| {
3928 return Air.internedToRef((try mod.getCoerced(val, const_ptr_ty)).toIntern());
3938 return Air.internedToRef((try sema.mod.getCoerced(val, const_ptr_ty)).toIntern());
39293939 }
39303940
39313941 return block.addBitCast(const_ptr_ty, alloc);
......@@ -4039,6 +4049,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
40394049 defer tracy.end();
40404050
40414051 const mod = sema.mod;
4052 const gpa = sema.gpa;
40424053 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
40434054 const src = inst_data.src();
40444055 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = inst_data.src_node };
......@@ -4104,11 +4115,14 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
41044115 if (!ia1.is_const) {
41054116 try sema.validateVarType(block, ty_src, final_elem_ty, false);
41064117 } else if (try sema.resolveComptimeKnownAllocValue(block, ptr, final_ptr_ty)) |val| {
4107 var anon_decl = try block.startAnonDecl();
4108 defer anon_decl.deinit();
4109 const new_decl_index = try anon_decl.finish(final_elem_ty, val.toValue(), ia1.alignment);
4110 const new_mut_ptr = Air.refToInterned(try sema.analyzeDeclRef(new_decl_index)).?.toValue();
4111 const new_const_ptr = (try mod.getCoerced(new_mut_ptr, final_ptr_ty)).toIntern();
4118 const const_ptr_ty = (try sema.makePtrTyConst(final_ptr_ty)).toIntern();
4119 const new_const_ptr = try mod.intern(.{ .ptr = .{
4120 .ty = const_ptr_ty,
4121 .addr = .{ .anon_decl = .{
4122 .val = val,
4123 .orig_ty = const_ptr_ty,
4124 } },
4125 } });
41124126
41134127 // Remap the ZIR oeprand to the resolved pointer value
41144128 sema.inst_map.putAssumeCapacity(Zir.refToIndex(inst_data.operand).?, Air.internedToRef(new_const_ptr));
......@@ -4131,7 +4145,6 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
41314145
41324146 // Now we need to go back over all the store instructions, and do the logic as if
41334147 // the new result ptr type was available.
4134 const gpa = sema.gpa;
41354148
41364149 for (ia2.prongs.items) |placeholder_inst| {
41374150 var replacement_block = block.makeSubBlock();