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...@@ -3672,10 +3672,18 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
3672 implicit_ct: {3672 implicit_ct: {
3673 const ptr_val = try sema.resolveMaybeUndefVal(alloc) orelse break :implicit_ct;3673 const ptr_val = try sema.resolveMaybeUndefVal(alloc) orelse break :implicit_ct;
3674 if (!ptr_val.isComptimeMutablePtr(mod)) {3674 if (!ptr_val.isComptimeMutablePtr(mod)) {
3675 // It could still be a constant pointer to a decl3675 // It could still be a constant pointer to a decl.
3676 const decl_index = ptr_val.pointerDecl(mod) orelse break :implicit_ct;3676 switch (mod.intern_pool.indexToKey(ptr_val.toIntern()).ptr.addr) {
3677 const decl_val = mod.declPtr(decl_index).val.toIntern();3677 .anon_decl => |anon_decl| {
3678 if (mod.intern_pool.isRuntimeValue(decl_val)) break :implicit_ct;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 }
3679 }3687 }
3680 return sema.makePtrConst(block, alloc);3688 return sema.makePtrConst(block, alloc);
3681 }3689 }
...@@ -3915,17 +3923,19 @@ fn finishResolveComptimeKnownAllocValue(sema: *Sema, result_val: InternPool.Inde...@@ -3915,17 +3923,19 @@ fn finishResolveComptimeKnownAllocValue(sema: *Sema, result_val: InternPool.Inde
3915 return result_val;3923 return result_val;
3916}3924}
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
3918fn makePtrConst(sema: *Sema, block: *Block, alloc: Air.Inst.Ref) CompileError!Air.Inst.Ref {3932fn makePtrConst(sema: *Sema, block: *Block, alloc: Air.Inst.Ref) CompileError!Air.Inst.Ref {
3919 const mod = sema.mod;
3920 const alloc_ty = sema.typeOf(alloc);3933 const alloc_ty = sema.typeOf(alloc);
39213934 const const_ptr_ty = try sema.makePtrTyConst(alloc_ty);
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);
39253935
3926 // Detect if a comptime value simply needs to have its type changed.3936 // Detect if a comptime value simply needs to have its type changed.
3927 if (try sema.resolveMaybeUndefVal(alloc)) |val| {3937 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());
3929 }3939 }
39303940
3931 return block.addBitCast(const_ptr_ty, alloc);3941 return block.addBitCast(const_ptr_ty, alloc);
...@@ -4039,6 +4049,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com...@@ -4039,6 +4049,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
4039 defer tracy.end();4049 defer tracy.end();
40404050
4041 const mod = sema.mod;4051 const mod = sema.mod;
4052 const gpa = sema.gpa;
4042 const inst_data = sema.code.instructions.items(.data)[inst].un_node;4053 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
4043 const src = inst_data.src();4054 const src = inst_data.src();
4044 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = inst_data.src_node };4055 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...@@ -4104,11 +4115,14 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
4104 if (!ia1.is_const) {4115 if (!ia1.is_const) {
4105 try sema.validateVarType(block, ty_src, final_elem_ty, false);4116 try sema.validateVarType(block, ty_src, final_elem_ty, false);
4106 } else if (try sema.resolveComptimeKnownAllocValue(block, ptr, final_ptr_ty)) |val| {4117 } else if (try sema.resolveComptimeKnownAllocValue(block, ptr, final_ptr_ty)) |val| {
4107 var anon_decl = try block.startAnonDecl();4118 const const_ptr_ty = (try sema.makePtrTyConst(final_ptr_ty)).toIntern();
4108 defer anon_decl.deinit();4119 const new_const_ptr = try mod.intern(.{ .ptr = .{
4109 const new_decl_index = try anon_decl.finish(final_elem_ty, val.toValue(), ia1.alignment);4120 .ty = const_ptr_ty,
4110 const new_mut_ptr = Air.refToInterned(try sema.analyzeDeclRef(new_decl_index)).?.toValue();4121 .addr = .{ .anon_decl = .{
4111 const new_const_ptr = (try mod.getCoerced(new_mut_ptr, final_ptr_ty)).toIntern();4122 .val = val,
4123 .orig_ty = const_ptr_ty,
4124 } },
4125 } });
41124126
4113 // Remap the ZIR oeprand to the resolved pointer value4127 // Remap the ZIR oeprand to the resolved pointer value
4114 sema.inst_map.putAssumeCapacity(Zir.refToIndex(inst_data.operand).?, Air.internedToRef(new_const_ptr));4128 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...@@ -4131,7 +4145,6 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
41314145
4132 // Now we need to go back over all the store instructions, and do the logic as if4146 // Now we need to go back over all the store instructions, and do the logic as if
4133 // the new result ptr type was available.4147 // the new result ptr type was available.
4134 const gpa = sema.gpa;
41354148
4136 for (ia2.prongs.items) |placeholder_inst| {4149 for (ia2.prongs.items) |placeholder_inst| {
4137 var replacement_block = block.makeSubBlock();4150 var replacement_block = block.makeSubBlock();