authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-14 18:10:54-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-14 18:11:46-07:00
log04572f6e341e6ff19877d1ae3b79e3baa653e652
tree07b91717329efb36bd8c8bd266b047fff60692ff
parentd7711ec9532d4c38bf1911f5c0ed2c813705ae15

Sema: fix coerceResultPtr

It did not handle properly when the dummy operand was a comptime_int; it was crashing in coerce because comptime_int is supposed to be comptime-known. So when calling coerceResultPtr, we pass the actual operand, not a dummy operand, which means it will have the proper comptime value when necessary.

3 files changed, 39 insertions(+), 24 deletions(-)

src/Sema.zig+22-20
......@@ -2030,7 +2030,8 @@ fn zirCoerceResultPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
20302030 defer trash_block.instructions.deinit(sema.gpa);
20312031
20322032 const dummy_ptr = try trash_block.addTy(.alloc, sema.typeOf(ptr));
2033 return coerceResultPtr(sema, block, src, ptr, dummy_ptr, pointee_ty, &trash_block);
2033 const dummy_operand = try trash_block.addBitCast(pointee_ty, .void_value);
2034 return coerceResultPtr(sema, block, src, ptr, dummy_ptr, dummy_operand, &trash_block);
20342035}
20352036
20362037fn coerceResultPtr(
......@@ -2039,13 +2040,14 @@ fn coerceResultPtr(
20392040 src: LazySrcLoc,
20402041 ptr: Air.Inst.Ref,
20412042 dummy_ptr: Air.Inst.Ref,
2042 pointee_ty: Type,
2043 dummy_operand: Air.Inst.Ref,
20432044 trash_block: *Block,
20442045) CompileError!Air.Inst.Ref {
20452046 const target = sema.mod.getTarget();
20462047 const addr_space = target_util.defaultAddressSpace(target, .local);
2048 const pointee_ty = sema.typeOf(dummy_operand);
2049 const prev_trash_len = trash_block.instructions.items.len;
20472050
2048 const dummy_operand = try trash_block.addBitCast(pointee_ty, .void_value);
20492051 try sema.storePtr2(trash_block, src, dummy_ptr, src, dummy_operand, src, .bitcast);
20502052
20512053 {
......@@ -2078,21 +2080,24 @@ fn coerceResultPtr(
20782080 while (true) {
20792081 const air_tags = sema.air_instructions.items(.tag);
20802082 const air_datas = sema.air_instructions.items(.data);
2083
2084 if (trash_block.instructions.items.len == prev_trash_len) {
2085 if (try sema.resolveDefinedValue(block, src, new_ptr)) |ptr_val| {
2086 return sema.addConstant(ptr_ty, ptr_val);
2087 }
2088 if (pointee_ty.eql(Type.@"null", sema.mod)) {
2089 const opt_ty = sema.typeOf(new_ptr).childType();
2090 const null_inst = try sema.addConstant(opt_ty, Value.@"null");
2091 _ = try block.addBinOp(.store, new_ptr, null_inst);
2092 return Air.Inst.Ref.void_value;
2093 }
2094 return sema.bitCast(block, ptr_ty, new_ptr, src);
2095 }
2096
20812097 const trash_inst = trash_block.instructions.pop();
2098
20822099 switch (air_tags[trash_inst]) {
20832100 .bitcast => {
2084 if (Air.indexToRef(trash_inst) == dummy_operand) {
2085 if (try sema.resolveDefinedValue(block, src, new_ptr)) |ptr_val| {
2086 return sema.addConstant(ptr_ty, ptr_val);
2087 }
2088 if (pointee_ty.eql(Type.@"null", sema.mod)) {
2089 const opt_ty = sema.typeOf(new_ptr).childType();
2090 const null_inst = try sema.addConstant(opt_ty, Value.@"null");
2091 _ = try block.addBinOp(.store, new_ptr, null_inst);
2092 return Air.Inst.Ref.void_value;
2093 }
2094 return sema.bitCast(block, ptr_ty, new_ptr, src);
2095 }
20962101 const ty_op = air_datas[trash_inst].ty_op;
20972102 const operand_ty = sema.typeOf(ty_op.operand);
20982103 const ptr_operand_ty = try Type.ptr(sema.arena, sema.mod, .{
......@@ -3309,8 +3314,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
33093314 defer bitcast_block.instructions.deinit(gpa);
33103315
33113316 trash_block.instructions.shrinkRetainingCapacity(empty_trash_count);
3312 const pointee_ty = sema.typeOf(peer_inst_list[i]);
3313 const sub_ptr = try coerceResultPtr(sema, &bitcast_block, src, ptr, dummy_ptr, pointee_ty, &trash_block);
3317 const sub_ptr = try coerceResultPtr(sema, &bitcast_block, src, ptr, dummy_ptr, peer_inst_list[i], &trash_block);
33143318
33153319 assert(bitcast_block.instructions.items.len > 0);
33163320 // If only one instruction is produced then we can replace the bitcast
......@@ -4190,7 +4194,7 @@ fn storeToInferredAlloc(
41904194 .stored_inst = operand,
41914195 .placeholder = Air.refToIndex(bitcasted_ptr).?,
41924196 });
4193 return sema.storePtr(block, src, bitcasted_ptr, operand);
4197 return sema.storePtr2(block, src, bitcasted_ptr, src, operand, src, .bitcast);
41944198}
41954199
41964200fn storeToInferredAllocComptime(
......@@ -21707,8 +21711,6 @@ fn storePtr2(
2170721711 if ((try sema.typeHasOnePossibleValue(block, src, elem_ty)) != null)
2170821712 return;
2170921713
21710 // TODO handle if the element type requires comptime
21711
2171221714 if (air_tag == .bitcast) {
2171321715 // `air_tag == .bitcast` is used as a special case for `zirCoerceResultPtr`
2171421716 // to avoid calling `requireRuntimeBlock` for the dummy block.
test/behavior/array.zig+1-4
......@@ -570,10 +570,7 @@ test "type coercion of pointer to anon struct literal to pointer to array" {
570570}
571571
572572test "array with comptime only element type" {
573 const a = [_]type{
574 u32,
575 i32,
576 };
573 const a = [_]type{ u32, i32 };
577574 try testing.expect(a[0] == u32);
578575 try testing.expect(a[1] == i32);
579576}
test/behavior/if.zig+16
......@@ -140,3 +140,19 @@ test "if-else expression with runtime condition result location is inferred opti
140140 const e = if (d) A{ .b = 15, .c = 30 } else null;
141141 try expect(e != null);
142142}
143
144test "result location with inferred type ends up being pointer to comptime_int" {
145 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
146 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
147 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
148 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
149 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
150
151 var a: ?u32 = 1234;
152 var b: u32 = 2000;
153 var c = if (a) |d| blk: {
154 if (d < b) break :blk @as(u32, 1);
155 break :blk 0;
156 } else @as(u32, 0);
157 try expect(c == 1);
158}