authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-05-09 15:30:14+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-05-11 12:23:57+03:00
log0a7f8c2e013f24aa6c94093400fe6377ab74b4e1
treee8e264154db69d9e3b3edc8908d871a74a2f664e
parent0ad692e76c2f09bb8dbac38208579cd76fcc5a07

Sema: return const pointers from ref inits

Closes #12189

2 files changed, 23 insertions(+), 9 deletions(-)

src/Sema.zig+16-9
......@@ -252,7 +252,6 @@ pub const Block = struct {
252252 // TODO is_comptime and comptime_reason should probably be merged together.
253253 is_comptime: bool,
254254 is_typeof: bool = false,
255 is_coerce_result_ptr: bool = false,
256255
257256 /// Keep track of the active error return trace index around blocks so that we can correctly
258257 /// pop the error trace upon block exit.
......@@ -2469,7 +2468,6 @@ fn zirCoerceResultPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
24692468 // kind of transformations to make on the result pointer.
24702469 var trash_block = block.makeSubBlock();
24712470 trash_block.is_comptime = false;
2472 trash_block.is_coerce_result_ptr = true;
24732471 defer trash_block.instructions.deinit(sema.gpa);
24742472
24752473 const dummy_ptr = try trash_block.addTy(.alloc, sema.typeOf(ptr));
......@@ -2579,6 +2577,9 @@ fn coerceResultPtr(
25792577 .array_to_slice => {
25802578 return sema.fail(block, src, "TODO coerce_result_ptr array_to_slice", .{});
25812579 },
2580 .get_union_tag => {
2581 return sema.fail(block, src, "TODO coerce_result_ptr get_union_tag", .{});
2582 },
25822583 else => {
25832584 if (std.debug.runtime_safety) {
25842585 std.debug.panic("unexpected AIR tag for coerce_result_ptr: {}", .{
......@@ -3563,6 +3564,13 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
35633564 ));
35643565 }
35653566
3567 return sema.makePtrConst(block, alloc);
3568}
3569
3570fn makePtrConst(sema: *Sema, block: *Block, alloc: Air.Inst.Ref) CompileError!Air.Inst.Ref {
3571 const alloc_ty = sema.typeOf(alloc);
3572
3573 var ptr_info = alloc_ty.ptrInfo().data;
35663574 ptr_info.mutable = false;
35673575 const const_ptr_ty = try Type.ptr(sema.arena, sema.mod, ptr_info);
35683576
......@@ -3831,7 +3839,6 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
38313839
38323840 var trash_block = block.makeSubBlock();
38333841 trash_block.is_comptime = false;
3834 trash_block.is_coerce_result_ptr = true;
38353842 defer trash_block.instructions.deinit(gpa);
38363843
38373844 const mut_final_ptr_ty = try Type.ptr(sema.arena, sema.mod, .{
......@@ -17773,7 +17780,7 @@ fn zirStructInit(
1777317780 try sema.storePtr(block, src, field_ptr, init_inst);
1777417781 const new_tag = try sema.addConstant(resolved_ty.unionTagTypeHypothetical(), tag_val);
1777517782 _ = try block.addBinOp(.set_union_tag, alloc, new_tag);
17776 return alloc;
17783 return sema.makePtrConst(block, alloc);
1777717784 }
1777817785
1777917786 try sema.requireRuntimeBlock(block, src, null);
......@@ -17900,7 +17907,7 @@ fn finishStructInit(
1790017907 try sema.storePtr(block, dest_src, field_ptr, field_init);
1790117908 }
1790217909
17903 return alloc;
17910 return sema.makePtrConst(block, alloc);
1790417911 }
1790517912
1790617913 try sema.requireRuntimeBlock(block, dest_src, null);
......@@ -18017,7 +18024,7 @@ fn zirStructInitAnon(
1801718024 }
1801818025 }
1801918026
18020 return alloc;
18027 return sema.makePtrConst(block, alloc);
1802118028 }
1802218029
1802318030 const element_refs = try sema.arena.alloc(Air.Inst.Ref, types.len);
......@@ -18120,7 +18127,7 @@ fn zirArrayInit(
1812018127 const elem_ptr = try block.addPtrElemPtrTypeRef(alloc, index, elem_ptr_ty_ref);
1812118128 _ = try block.addBinOp(.store, elem_ptr, arg);
1812218129 }
18123 return alloc;
18130 return sema.makePtrConst(block, alloc);
1812418131 }
1812518132
1812618133 const elem_ptr_ty = try Type.ptr(sema.arena, sema.mod, .{
......@@ -18135,7 +18142,7 @@ fn zirArrayInit(
1813518142 const elem_ptr = try block.addPtrElemPtrTypeRef(alloc, index, elem_ptr_ty_ref);
1813618143 _ = try block.addBinOp(.store, elem_ptr, arg);
1813718144 }
18138 return alloc;
18145 return sema.makePtrConst(block, alloc);
1813918146 }
1814018147
1814118148 return block.addAggregateInit(array_ty, resolved_args);
......@@ -18213,7 +18220,7 @@ fn zirArrayInitAnon(
1821318220 }
1821418221 }
1821518222
18216 return alloc;
18223 return sema.makePtrConst(block, alloc);
1821718224 }
1821818225
1821918226 const element_refs = try sema.arena.alloc(Air.Inst.Ref, operands.len);
test/behavior/basic.zig+7
......@@ -1145,3 +1145,10 @@ test "arrays and vectors with big integers" {
11451145 try expect(b[0] == comptime std.math.maxInt(Int));
11461146 }
11471147}
1148
1149test "pointer to struct literal with runtime field is constant" {
1150 const S = struct { data: usize };
1151 var runtime_zero: usize = 0;
1152 const ptr = &S{ .data = runtime_zero };
1153 try expect(@typeInfo(@TypeOf(ptr)).Pointer.is_const);
1154}