From cd32b11eb8d33225a1a2bc3bc9e0755936a39ade Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 8 Mar 2022 11:53:43 -0800 Subject: [PATCH] stage2: inferred alloc can use bitcast ty The comment notes that we can't because of constness, but the recent work by @Vexu lets us use the bitcast ty cause constness comes later. The following tests pass from this: ``` test "A" { const ary = [_:0]u8{42}; const ptr: [*:0]const u8 = &ary; try expect(ptr[1] == 0); } test "B" { comptime { const ary = [_:0]u8{42}; const ptr: [*:0]const u8 = &ary; try expect(ptr[1] == 0); } } ``` --- src/Sema.zig | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 249e3be34d..a9de064e85 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -2623,10 +2623,9 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com // block so that codegen does not see it. block.instructions.shrinkRetainingCapacity(block.instructions.items.len - 3); sema.air_values.items[value_index] = try Value.Tag.decl_ref.create(sema.arena, new_decl); - // Would be nice if we could just assign `bitcast_ty_ref` to - // `air_datas[ptr_inst].ty_pl.ty`, wouldn't it? Alas, that is almost correct, - // except that the pointer is mutable and we need to make it constant here. - air_datas[ptr_inst].ty_pl.ty = try sema.addType(final_ptr_ty); + // if bitcast ty ref needs to be made const, make_ptr_const + // ZIR handles it later, so we can just use the ty ref here. + air_datas[ptr_inst].ty_pl.ty = air_datas[bitcast_inst].ty_op.ty; return; }