authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-06-03 16:10:18+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-06-03 20:21:20+03:00
log2b93546b39a85e9316c2bf12f336c357577114fa
tree9a7d6337843e025f460cc5572e92839730bee39d
parent4e1aa5d54377aa2b8d1395d666efb6eb935b7917

Sema: fix initialization of array with comptime only elem type


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

src/Sema.zig+11-9
...@@ -3598,7 +3598,7 @@ fn zirValidateArrayInit(...@@ -3598,7 +3598,7 @@ fn zirValidateArrayInit(
3598 // any ZIR instructions at comptime; we need to do that here.3598 // any ZIR instructions at comptime; we need to do that here.
3599 if (array_ty.sentinel()) |sentinel_val| {3599 if (array_ty.sentinel()) |sentinel_val| {
3600 const array_len_ref = try sema.addIntUnsigned(Type.usize, array_len);3600 const array_len_ref = try sema.addIntUnsigned(Type.usize, array_len);
3601 const sentinel_ptr = try sema.elemPtrArray(block, init_src, array_ptr, init_src, array_len_ref);3601 const sentinel_ptr = try sema.elemPtrArray(block, init_src, array_ptr, init_src, array_len_ref, true);
3602 const sentinel = try sema.addConstant(array_ty.childType(), sentinel_val);3602 const sentinel = try sema.addConstant(array_ty.childType(), sentinel_val);
3603 try sema.storePtr2(block, init_src, sentinel_ptr, init_src, sentinel, init_src, .store);3603 try sema.storePtr2(block, init_src, sentinel_ptr, init_src, sentinel, init_src, .store);
3604 }3604 }
...@@ -7540,7 +7540,7 @@ fn zirElemPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -7540,7 +7540,7 @@ fn zirElemPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
7540 const bin_inst = sema.code.instructions.items(.data)[inst].bin;7540 const bin_inst = sema.code.instructions.items(.data)[inst].bin;
7541 const array_ptr = try sema.resolveInst(bin_inst.lhs);7541 const array_ptr = try sema.resolveInst(bin_inst.lhs);
7542 const elem_index = try sema.resolveInst(bin_inst.rhs);7542 const elem_index = try sema.resolveInst(bin_inst.rhs);
7543 return sema.elemPtr(block, sema.src, array_ptr, elem_index, sema.src);7543 return sema.elemPtr(block, sema.src, array_ptr, elem_index, sema.src, false);
7544}7544}
75457545
7546fn zirElemPtrNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {7546fn zirElemPtrNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
...@@ -7553,7 +7553,7 @@ fn zirElemPtrNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError...@@ -7553,7 +7553,7 @@ fn zirElemPtrNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
7553 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;7553 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
7554 const array_ptr = try sema.resolveInst(extra.lhs);7554 const array_ptr = try sema.resolveInst(extra.lhs);
7555 const elem_index = try sema.resolveInst(extra.rhs);7555 const elem_index = try sema.resolveInst(extra.rhs);
7556 return sema.elemPtr(block, src, array_ptr, elem_index, elem_index_src);7556 return sema.elemPtr(block, src, array_ptr, elem_index, elem_index_src, false);
7557}7557}
75587558
7559fn zirElemPtrImm(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {7559fn zirElemPtrImm(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
...@@ -7565,7 +7565,7 @@ fn zirElemPtrImm(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!...@@ -7565,7 +7565,7 @@ fn zirElemPtrImm(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
7565 const extra = sema.code.extraData(Zir.Inst.ElemPtrImm, inst_data.payload_index).data;7565 const extra = sema.code.extraData(Zir.Inst.ElemPtrImm, inst_data.payload_index).data;
7566 const array_ptr = try sema.resolveInst(extra.ptr);7566 const array_ptr = try sema.resolveInst(extra.ptr);
7567 const elem_index = try sema.addIntUnsigned(Type.usize, extra.index);7567 const elem_index = try sema.addIntUnsigned(Type.usize, extra.index);
7568 return sema.elemPtr(block, src, array_ptr, elem_index, src);7568 return sema.elemPtr(block, src, array_ptr, elem_index, src, true);
7569}7569}
75707570
7571fn zirSliceStart(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {7571fn zirSliceStart(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
...@@ -18724,6 +18724,7 @@ fn elemPtr(...@@ -18724,6 +18724,7 @@ fn elemPtr(
18724 indexable_ptr: Air.Inst.Ref,18724 indexable_ptr: Air.Inst.Ref,
18725 elem_index: Air.Inst.Ref,18725 elem_index: Air.Inst.Ref,
18726 elem_index_src: LazySrcLoc,18726 elem_index_src: LazySrcLoc,
18727 init: bool,
18727) CompileError!Air.Inst.Ref {18728) CompileError!Air.Inst.Ref {
18728 const indexable_ptr_src = src; // TODO better source location18729 const indexable_ptr_src = src; // TODO better source location
18729 const indexable_ptr_ty = sema.typeOf(indexable_ptr);18730 const indexable_ptr_ty = sema.typeOf(indexable_ptr);
...@@ -18760,11 +18761,11 @@ fn elemPtr(...@@ -18760,11 +18761,11 @@ fn elemPtr(
18760 },18761 },
18761 .One => {18762 .One => {
18762 assert(indexable_ty.childType().zigTypeTag() == .Array); // Guaranteed by isIndexable18763 assert(indexable_ty.childType().zigTypeTag() == .Array); // Guaranteed by isIndexable
18763 return sema.elemPtrArray(block, indexable_ptr_src, indexable, elem_index_src, elem_index);18764 return sema.elemPtrArray(block, indexable_ptr_src, indexable, elem_index_src, elem_index, init);
18764 },18765 },
18765 }18766 }
18766 },18767 },
18767 .Array, .Vector => return sema.elemPtrArray(block, indexable_ptr_src, indexable_ptr, elem_index_src, elem_index),18768 .Array, .Vector => return sema.elemPtrArray(block, indexable_ptr_src, indexable_ptr, elem_index_src, elem_index, init),
18768 .Struct => {18769 .Struct => {
18769 // Tuple field access.18770 // Tuple field access.
18770 const index_val = try sema.resolveConstValue(block, elem_index_src, elem_index);18771 const index_val = try sema.resolveConstValue(block, elem_index_src, elem_index);
...@@ -18818,7 +18819,7 @@ fn elemVal(...@@ -18818,7 +18819,7 @@ fn elemVal(
18818 },18819 },
18819 .One => {18820 .One => {
18820 assert(indexable_ty.childType().zigTypeTag() == .Array); // Guaranteed by isIndexable18821 assert(indexable_ty.childType().zigTypeTag() == .Array); // Guaranteed by isIndexable
18821 const elem_ptr = try sema.elemPtr(block, indexable_src, indexable, elem_index, elem_index_src);18822 const elem_ptr = try sema.elemPtr(block, indexable_src, indexable, elem_index, elem_index_src, false);
18822 return sema.analyzeLoad(block, indexable_src, elem_ptr, elem_index_src);18823 return sema.analyzeLoad(block, indexable_src, elem_ptr, elem_index_src);
18823 },18824 },
18824 },18825 },
...@@ -18999,6 +19000,7 @@ fn elemPtrArray(...@@ -18999,6 +19000,7 @@ fn elemPtrArray(
18999 array_ptr: Air.Inst.Ref,19000 array_ptr: Air.Inst.Ref,
19000 elem_index_src: LazySrcLoc,19001 elem_index_src: LazySrcLoc,
19001 elem_index: Air.Inst.Ref,19002 elem_index: Air.Inst.Ref,
19003 init: bool,
19002) CompileError!Air.Inst.Ref {19004) CompileError!Air.Inst.Ref {
19003 const target = sema.mod.getTarget();19005 const target = sema.mod.getTarget();
19004 const array_ptr_ty = sema.typeOf(array_ptr);19006 const array_ptr_ty = sema.typeOf(array_ptr);
...@@ -19035,7 +19037,7 @@ fn elemPtrArray(...@@ -19035,7 +19037,7 @@ fn elemPtrArray(
19035 }19037 }
1903619038
19037 const valid_rt = try sema.validateRunTimeType(block, elem_index_src, array_ty.elemType2(), false);19039 const valid_rt = try sema.validateRunTimeType(block, elem_index_src, array_ty.elemType2(), false);
19038 if (!valid_rt) {19040 if (!valid_rt and !init) {
19039 const msg = msg: {19041 const msg = msg: {
19040 const msg = try sema.errMsg(19042 const msg = try sema.errMsg(
19041 block,19043 block,
...@@ -20138,7 +20140,7 @@ fn storePtr2(...@@ -20138,7 +20140,7 @@ fn storePtr2(
20138 const elem_src = operand_src; // TODO better source location20140 const elem_src = operand_src; // TODO better source location
20139 const elem = try tupleField(sema, block, operand_src, uncasted_operand, elem_src, i);20141 const elem = try tupleField(sema, block, operand_src, uncasted_operand, elem_src, i);
20140 const elem_index = try sema.addIntUnsigned(Type.usize, i);20142 const elem_index = try sema.addIntUnsigned(Type.usize, i);
20141 const elem_ptr = try sema.elemPtr(block, ptr_src, ptr, elem_index, elem_src);20143 const elem_ptr = try sema.elemPtr(block, ptr_src, ptr, elem_index, elem_src, false);
20142 try sema.storePtr2(block, src, elem_ptr, elem_src, elem, elem_src, .store);20144 try sema.storePtr2(block, src, elem_ptr, elem_src, elem, elem_src, .store);
20143 }20145 }
20144 return;20146 return;
test/behavior/array.zig+9
...@@ -573,3 +573,12 @@ test "type coercion of pointer to anon struct literal to pointer to array" {...@@ -573,3 +573,12 @@ test "type coercion of pointer to anon struct literal to pointer to array" {
573 try S.doTheTest();573 try S.doTheTest();
574 comptime try S.doTheTest();574 comptime try S.doTheTest();
575}575}
576
577test "array with comptime only element type" {
578 const a = [_]type{
579 u32,
580 i32,
581 };
582 try testing.expect(a[0] == u32);
583 try testing.expect(a[1] == i32);
584}