| ... | @@ -2881,6 +2881,28 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com | ... | @@ -2881,6 +2881,28 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com |
| 2881 | | 2881 | |
| 2882 | if (var_is_mut) { | 2882 | if (var_is_mut) { |
| 2883 | try sema.validateVarType(block, ty_src, final_elem_ty, false); | 2883 | try sema.validateVarType(block, ty_src, final_elem_ty, false); |
| | 2884 | |
| | 2885 | // The value might have been bitcasted into a comptime only |
| | 2886 | // pointer type such as `*@Type(.EnumLiteral)` so we must now |
| | 2887 | // update all the stores to not give backends invalid AIR. |
| | 2888 | |
| | 2889 | var air_tags = sema.air_instructions.items(.tag); |
| | 2890 | var air_data = sema.air_instructions.items(.data); |
| | 2891 | var peer_inst_index: usize = 0; |
| | 2892 | var i = ptr_inst; |
| | 2893 | while (i < air_tags.len and peer_inst_index < peer_inst_list.len) : (i += 1) { |
| | 2894 | if (air_tags[i] != .store) continue; |
| | 2895 | if (air_data[i].bin_op.rhs == peer_inst_list[peer_inst_index]) { |
| | 2896 | peer_inst_index += 1; |
| | 2897 | _ = (try sema.resolveMaybeUndefVal(block, .unneeded, air_data[i].bin_op.rhs)) orelse continue; |
| | 2898 | const coerced_val = try sema.coerce(block, final_elem_ty, air_data[i].bin_op.rhs, .unneeded); |
| | 2899 | air_tags = sema.air_instructions.items(.tag); |
| | 2900 | air_data = sema.air_instructions.items(.data); |
| | 2901 | |
| | 2902 | air_data[i].bin_op.lhs = ptr; |
| | 2903 | air_data[i].bin_op.rhs = coerced_val; |
| | 2904 | } |
| | 2905 | } |
| 2884 | } else ct: { | 2906 | } else ct: { |
| 2885 | // Detect if the value is comptime known. In such case, the | 2907 | // Detect if the value is comptime known. In such case, the |
| 2886 | // last 3 AIR instructions of the block will look like this: | 2908 | // last 3 AIR instructions of the block will look like this: |
| ... | @@ -5814,8 +5836,10 @@ fn zirArrayType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A | ... | @@ -5814,8 +5836,10 @@ fn zirArrayType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 5814 | defer tracy.end(); | 5836 | defer tracy.end(); |
| 5815 | | 5837 | |
| 5816 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; | 5838 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 5817 | const len = try sema.resolveInt(block, .unneeded, bin_inst.lhs, Type.usize); | 5839 | const len_src = sema.src; // TODO better source location |
| 5818 | const elem_type = try sema.resolveType(block, .unneeded, bin_inst.rhs); | 5840 | const elem_src = sema.src; // TODO better source location |
| | 5841 | const len = try sema.resolveInt(block, len_src, bin_inst.lhs, Type.usize); |
| | 5842 | const elem_type = try sema.resolveType(block, elem_src, bin_inst.rhs); |
| 5819 | const array_ty = try Type.array(sema.arena, len, null, elem_type, sema.mod); | 5843 | const array_ty = try Type.array(sema.arena, len, null, elem_type, sema.mod); |
| 5820 | | 5844 | |
| 5821 | return sema.addType(array_ty); | 5845 | return sema.addType(array_ty); |
| ... | @@ -7307,9 +7331,11 @@ fn zirElemVal(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air | ... | @@ -7307,9 +7331,11 @@ fn zirElemVal(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 7307 | defer tracy.end(); | 7331 | defer tracy.end(); |
| 7308 | | 7332 | |
| 7309 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; | 7333 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| | 7334 | const src = sema.src; // TODO better source location |
| | 7335 | const elem_index_src = sema.src; // TODO better source location |
| 7310 | const array = try sema.resolveInst(bin_inst.lhs); | 7336 | const array = try sema.resolveInst(bin_inst.lhs); |
| 7311 | const elem_index = try sema.resolveInst(bin_inst.rhs); | 7337 | const elem_index = try sema.resolveInst(bin_inst.rhs); |
| 7312 | return sema.elemVal(block, sema.src, array, elem_index, sema.src); | 7338 | return sema.elemVal(block, src, array, elem_index, elem_index_src); |
| 7313 | } | 7339 | } |
| 7314 | | 7340 | |
| 7315 | fn zirElemValNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { | 7341 | fn zirElemValNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| ... | @@ -18591,6 +18617,25 @@ fn elemValArray( | ... | @@ -18591,6 +18617,25 @@ fn elemValArray( |
| 18591 | } | 18617 | } |
| 18592 | } | 18618 | } |
| 18593 | | 18619 | |
| | 18620 | const valid_rt = try sema.validateRunTimeType(block, elem_index_src, elem_ty, false); |
| | 18621 | if (!valid_rt) { |
| | 18622 | const msg = msg: { |
| | 18623 | const msg = try sema.errMsg( |
| | 18624 | block, |
| | 18625 | elem_index_src, |
| | 18626 | "values of type '{}' must be comptime known, but index value is runtime known", |
| | 18627 | .{array_ty.fmt(sema.mod)}, |
| | 18628 | ); |
| | 18629 | errdefer msg.destroy(sema.gpa); |
| | 18630 | |
| | 18631 | const src_decl = sema.mod.declPtr(block.src_decl); |
| | 18632 | try sema.explainWhyTypeIsComptime(block, elem_index_src, msg, array_src.toSrcLoc(src_decl), array_ty); |
| | 18633 | |
| | 18634 | break :msg msg; |
| | 18635 | }; |
| | 18636 | return sema.failWithOwnedErrorMsg(block, msg); |
| | 18637 | } |
| | 18638 | |
| 18594 | const runtime_src = if (maybe_undef_array_val != null) elem_index_src else array_src; | 18639 | const runtime_src = if (maybe_undef_array_val != null) elem_index_src else array_src; |
| 18595 | try sema.requireRuntimeBlock(block, runtime_src); | 18640 | try sema.requireRuntimeBlock(block, runtime_src); |
| 18596 | if (block.wantSafety()) { | 18641 | if (block.wantSafety()) { |
| ... | @@ -18646,6 +18691,25 @@ fn elemPtrArray( | ... | @@ -18646,6 +18691,25 @@ fn elemPtrArray( |
| 18646 | } | 18691 | } |
| 18647 | } | 18692 | } |
| 18648 | | 18693 | |
| | 18694 | const valid_rt = try sema.validateRunTimeType(block, elem_index_src, array_ty.elemType2(), false); |
| | 18695 | if (!valid_rt) { |
| | 18696 | const msg = msg: { |
| | 18697 | const msg = try sema.errMsg( |
| | 18698 | block, |
| | 18699 | elem_index_src, |
| | 18700 | "values of type '{}' must be comptime known, but index value is runtime known", |
| | 18701 | .{array_ty.fmt(sema.mod)}, |
| | 18702 | ); |
| | 18703 | errdefer msg.destroy(sema.gpa); |
| | 18704 | |
| | 18705 | const src_decl = sema.mod.declPtr(block.src_decl); |
| | 18706 | try sema.explainWhyTypeIsComptime(block, elem_index_src, msg, array_ptr_src.toSrcLoc(src_decl), array_ty); |
| | 18707 | |
| | 18708 | break :msg msg; |
| | 18709 | }; |
| | 18710 | return sema.failWithOwnedErrorMsg(block, msg); |
| | 18711 | } |
| | 18712 | |
| 18649 | const runtime_src = if (maybe_undef_array_ptr_val != null) elem_index_src else array_ptr_src; | 18713 | const runtime_src = if (maybe_undef_array_ptr_val != null) elem_index_src else array_ptr_src; |
| 18650 | try sema.requireRuntimeBlock(block, runtime_src); | 18714 | try sema.requireRuntimeBlock(block, runtime_src); |
| 18651 | if (block.wantSafety()) { | 18715 | if (block.wantSafety()) { |