| ... | ... | @@ -2854,7 +2854,15 @@ fn zirValidateArrayInit( |
| 2854 | 2854 | |
| 2855 | 2855 | if (is_comptime or block.is_comptime) { |
| 2856 | 2856 | // In this case the comptime machinery will have evaluated the store instructions |
| 2857 | | // at comptime and we have nothing to do here. |
| 2857 | // at comptime so we have almost nothing to do here. However, in case of a |
| 2858 | // sentinel-terminated array, the sentinel will not have been populated by |
| 2859 | // any ZIR instructions at comptime; we need to do that here. |
| 2860 | if (array_ty.sentinel()) |sentinel_val| { |
| 2861 | const array_len_ref = try sema.addIntUnsigned(Type.usize, array_len); |
| 2862 | const sentinel_ptr = try sema.elemPtrArray(block, init_src, array_ptr, array_len_ref, init_src); |
| 2863 | const sentinel = try sema.addConstant(array_ty.childType(), sentinel_val); |
| 2864 | try sema.storePtr2(block, init_src, sentinel_ptr, init_src, sentinel, init_src, .store); |
| 2865 | } |
| 2858 | 2866 | return; |
| 2859 | 2867 | } |
| 2860 | 2868 | |
| ... | ... | @@ -2863,7 +2871,7 @@ fn zirValidateArrayInit( |
| 2863 | 2871 | |
| 2864 | 2872 | // Collect the comptime element values in case the array literal ends up |
| 2865 | 2873 | // being comptime-known. |
| 2866 | | const element_vals = try sema.arena.alloc(Value, instrs.len); |
| 2874 | const element_vals = try sema.arena.alloc(Value, array_ty.arrayLenIncludingSentinel()); |
| 2867 | 2875 | const opt_opv = try sema.typeHasOnePossibleValue(block, init_src, array_ty); |
| 2868 | 2876 | const air_tags = sema.air_instructions.items(.tag); |
| 2869 | 2877 | const air_datas = sema.air_instructions.items(.data); |
| ... | ... | @@ -2920,6 +2928,10 @@ fn zirValidateArrayInit( |
| 2920 | 2928 | if (array_is_comptime) { |
| 2921 | 2929 | // Our task is to delete all the `elem_ptr` and `store` instructions, and insert |
| 2922 | 2930 | // instead a single `store` to the array_ptr with a comptime struct value. |
| 2931 | // Also to populate the sentinel value, if any. |
| 2932 | if (array_ty.sentinel()) |sentinel_val| { |
| 2933 | element_vals[instrs.len] = sentinel_val; |
| 2934 | } |
| 2923 | 2935 | |
| 2924 | 2936 | block.instructions.shrinkRetainingCapacity(first_block_index); |
| 2925 | 2937 | |
| ... | ... | @@ -13436,6 +13448,12 @@ fn elemValArray( |
| 13436 | 13448 | const maybe_index_val = try sema.resolveDefinedValue(block, elem_index_src, elem_index); |
| 13437 | 13449 | if (maybe_index_val) |index_val| { |
| 13438 | 13450 | const index = @intCast(usize, index_val.toUnsignedInt()); |
| 13451 | const len = array_ty.arrayLenIncludingSentinel(); |
| 13452 | if (index >= len) { |
| 13453 | return sema.fail(block, elem_index_src, "index {d} outside array of length {d}", .{ |
| 13454 | index, len, |
| 13455 | }); |
| 13456 | } |
| 13439 | 13457 | const elem_val = try array_val.elemValue(sema.arena, index); |
| 13440 | 13458 | return sema.addConstant(elem_ty, elem_val); |
| 13441 | 13459 | } |