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 {...@@ -252,7 +252,6 @@ pub const Block = struct {
252 // TODO is_comptime and comptime_reason should probably be merged together.252 // TODO is_comptime and comptime_reason should probably be merged together.
253 is_comptime: bool,253 is_comptime: bool,
254 is_typeof: bool = false,254 is_typeof: bool = false,
255 is_coerce_result_ptr: bool = false,
256255
257 /// Keep track of the active error return trace index around blocks so that we can correctly256 /// Keep track of the active error return trace index around blocks so that we can correctly
258 /// pop the error trace upon block exit.257 /// pop the error trace upon block exit.
...@@ -2469,7 +2468,6 @@ fn zirCoerceResultPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE...@@ -2469,7 +2468,6 @@ fn zirCoerceResultPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
2469 // kind of transformations to make on the result pointer.2468 // kind of transformations to make on the result pointer.
2470 var trash_block = block.makeSubBlock();2469 var trash_block = block.makeSubBlock();
2471 trash_block.is_comptime = false;2470 trash_block.is_comptime = false;
2472 trash_block.is_coerce_result_ptr = true;
2473 defer trash_block.instructions.deinit(sema.gpa);2471 defer trash_block.instructions.deinit(sema.gpa);
24742472
2475 const dummy_ptr = try trash_block.addTy(.alloc, sema.typeOf(ptr));2473 const dummy_ptr = try trash_block.addTy(.alloc, sema.typeOf(ptr));
...@@ -2579,6 +2577,9 @@ fn coerceResultPtr(...@@ -2579,6 +2577,9 @@ fn coerceResultPtr(
2579 .array_to_slice => {2577 .array_to_slice => {
2580 return sema.fail(block, src, "TODO coerce_result_ptr array_to_slice", .{});2578 return sema.fail(block, src, "TODO coerce_result_ptr array_to_slice", .{});
2581 },2579 },
2580 .get_union_tag => {
2581 return sema.fail(block, src, "TODO coerce_result_ptr get_union_tag", .{});
2582 },
2582 else => {2583 else => {
2583 if (std.debug.runtime_safety) {2584 if (std.debug.runtime_safety) {
2584 std.debug.panic("unexpected AIR tag for coerce_result_ptr: {}", .{2585 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...@@ -3563,6 +3564,13 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
3563 ));3564 ));
3564 }3565 }
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;
3566 ptr_info.mutable = false;3574 ptr_info.mutable = false;
3567 const const_ptr_ty = try Type.ptr(sema.arena, sema.mod, ptr_info);3575 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...@@ -3831,7 +3839,6 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
38313839
3832 var trash_block = block.makeSubBlock();3840 var trash_block = block.makeSubBlock();
3833 trash_block.is_comptime = false;3841 trash_block.is_comptime = false;
3834 trash_block.is_coerce_result_ptr = true;
3835 defer trash_block.instructions.deinit(gpa);3842 defer trash_block.instructions.deinit(gpa);
38363843
3837 const mut_final_ptr_ty = try Type.ptr(sema.arena, sema.mod, .{3844 const mut_final_ptr_ty = try Type.ptr(sema.arena, sema.mod, .{
...@@ -17773,7 +17780,7 @@ fn zirStructInit(...@@ -17773,7 +17780,7 @@ fn zirStructInit(
17773 try sema.storePtr(block, src, field_ptr, init_inst);17780 try sema.storePtr(block, src, field_ptr, init_inst);
17774 const new_tag = try sema.addConstant(resolved_ty.unionTagTypeHypothetical(), tag_val);17781 const new_tag = try sema.addConstant(resolved_ty.unionTagTypeHypothetical(), tag_val);
17775 _ = try block.addBinOp(.set_union_tag, alloc, new_tag);17782 _ = try block.addBinOp(.set_union_tag, alloc, new_tag);
17776 return alloc;17783 return sema.makePtrConst(block, alloc);
17777 }17784 }
1777817785
17779 try sema.requireRuntimeBlock(block, src, null);17786 try sema.requireRuntimeBlock(block, src, null);
...@@ -17900,7 +17907,7 @@ fn finishStructInit(...@@ -17900,7 +17907,7 @@ fn finishStructInit(
17900 try sema.storePtr(block, dest_src, field_ptr, field_init);17907 try sema.storePtr(block, dest_src, field_ptr, field_init);
17901 }17908 }
1790217909
17903 return alloc;17910 return sema.makePtrConst(block, alloc);
17904 }17911 }
1790517912
17906 try sema.requireRuntimeBlock(block, dest_src, null);17913 try sema.requireRuntimeBlock(block, dest_src, null);
...@@ -18017,7 +18024,7 @@ fn zirStructInitAnon(...@@ -18017,7 +18024,7 @@ fn zirStructInitAnon(
18017 }18024 }
18018 }18025 }
1801918026
18020 return alloc;18027 return sema.makePtrConst(block, alloc);
18021 }18028 }
1802218029
18023 const element_refs = try sema.arena.alloc(Air.Inst.Ref, types.len);18030 const element_refs = try sema.arena.alloc(Air.Inst.Ref, types.len);
...@@ -18120,7 +18127,7 @@ fn zirArrayInit(...@@ -18120,7 +18127,7 @@ fn zirArrayInit(
18120 const elem_ptr = try block.addPtrElemPtrTypeRef(alloc, index, elem_ptr_ty_ref);18127 const elem_ptr = try block.addPtrElemPtrTypeRef(alloc, index, elem_ptr_ty_ref);
18121 _ = try block.addBinOp(.store, elem_ptr, arg);18128 _ = try block.addBinOp(.store, elem_ptr, arg);
18122 }18129 }
18123 return alloc;18130 return sema.makePtrConst(block, alloc);
18124 }18131 }
1812518132
18126 const elem_ptr_ty = try Type.ptr(sema.arena, sema.mod, .{18133 const elem_ptr_ty = try Type.ptr(sema.arena, sema.mod, .{
...@@ -18135,7 +18142,7 @@ fn zirArrayInit(...@@ -18135,7 +18142,7 @@ fn zirArrayInit(
18135 const elem_ptr = try block.addPtrElemPtrTypeRef(alloc, index, elem_ptr_ty_ref);18142 const elem_ptr = try block.addPtrElemPtrTypeRef(alloc, index, elem_ptr_ty_ref);
18136 _ = try block.addBinOp(.store, elem_ptr, arg);18143 _ = try block.addBinOp(.store, elem_ptr, arg);
18137 }18144 }
18138 return alloc;18145 return sema.makePtrConst(block, alloc);
18139 }18146 }
1814018147
18141 return block.addAggregateInit(array_ty, resolved_args);18148 return block.addAggregateInit(array_ty, resolved_args);
...@@ -18213,7 +18220,7 @@ fn zirArrayInitAnon(...@@ -18213,7 +18220,7 @@ fn zirArrayInitAnon(
18213 }18220 }
18214 }18221 }
1821518222
18216 return alloc;18223 return sema.makePtrConst(block, alloc);
18217 }18224 }
1821818225
18219 const element_refs = try sema.arena.alloc(Air.Inst.Ref, operands.len);18226 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" {...@@ -1145,3 +1145,10 @@ test "arrays and vectors with big integers" {
1145 try expect(b[0] == comptime std.math.maxInt(Int));1145 try expect(b[0] == comptime std.math.maxInt(Int));
1146 }1146 }
1147}1147}
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}