Skip to content

Commit

Permalink
Revert "AstGen: preserve inferred ptr result loc for breaks"
Browse files Browse the repository at this point in the history
This reverts commit 8bf3e1f, which
introduced miscompilations for peer expressions any time they needed
coercions to runtime types.

I opened #11957 as a proposal to accomplish the goal of the reverted
commit.

Closes #11898
  • Loading branch information
andrewrk committed Jun 29, 2022
1 parent a058696 commit 7fb64e2
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10007,7 +10007,7 @@ const GenZir = struct {
.inferred_ptr => |ptr| {
gz.rl_ty_inst = .none;
gz.rl_ptr = ptr;
gz.break_result_loc = parent_rl;
gz.break_result_loc = .{ .block_ptr = gz };
},

.block_ptr => |parent_block_scope| {
Expand Down
22 changes: 0 additions & 22 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3079,28 +3079,6 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com

if (var_is_mut) {
try sema.validateVarType(block, ty_src, final_elem_ty, false);

// The value might have been bitcasted into a comptime only
// pointer type such as `*@Type(.EnumLiteral)` so we must now
// update all the stores to not give backends invalid AIR.

var air_tags = sema.air_instructions.items(.tag);
var air_data = sema.air_instructions.items(.data);
var peer_inst_index: usize = 0;
var i = ptr_inst;
while (i < air_tags.len and peer_inst_index < peer_inst_list.len) : (i += 1) {
if (air_tags[i] != .store) continue;
if (air_data[i].bin_op.rhs == peer_inst_list[peer_inst_index]) {
peer_inst_index += 1;
_ = (try sema.resolveMaybeUndefVal(block, .unneeded, air_data[i].bin_op.rhs)) orelse continue;
const coerced_val = try sema.coerce(block, final_elem_ty, air_data[i].bin_op.rhs, .unneeded);
air_tags = sema.air_instructions.items(.tag);
air_data = sema.air_instructions.items(.data);

air_data[i].bin_op.lhs = ptr;
air_data[i].bin_op.rhs = coerced_val;
}
}
} else ct: {
// Detect if the value is comptime known. In such case, the
// last 3 AIR instructions of the block will look like this:
Expand Down
12 changes: 0 additions & 12 deletions test/behavior/basic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -928,18 +928,6 @@ test "try in labeled block doesn't cast to wrong type" {
_ = s;
}

test "comptime int in switch in catch is casted to correct inferred type" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;

var a: error{ A, B }!u64 = 0;
var b = a catch |err| switch (err) {
error.A => 0,
else => unreachable,
};
_ = b;
}

test "vector initialized with array init syntax has proper type" {
comptime {
const actual = -@Vector(4, i32){ 1, 2, 3, 4 };
Expand Down
2 changes: 2 additions & 0 deletions test/behavior/cast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ test "peer type resolution: error union after non-error" {
}

test "peer cast *[0]T to E![]const T" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
Expand All @@ -826,6 +827,7 @@ test "peer cast *[0]T to E![]const T" {
}

test "peer cast *[0]T to []const T" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
Expand Down
2 changes: 1 addition & 1 deletion test/behavior/error.zig
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ test "simple else prong allowed even when all errors handled" {
}
};
var value = S.foo() catch |err| switch (err) {
error.Foo => 255,
error.Foo => @as(u8, 255),
else => |e| return e,
};
try expect(value == 255);
Expand Down
17 changes: 17 additions & 0 deletions test/behavior/if.zig
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,20 @@ test "if prongs cast to expected type instead of peer type resolution" {
try S.doTheTest(false);
comptime try S.doTheTest(false);
}

test "if peer expressions inferred optional type" {
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;

var self: []const u8 = "abcdef";
var index: usize = 0;
var left_index = (index << 1) + 1;
var right_index = left_index + 1;
var left = if (left_index < self.len) self[left_index] else null;
var right = if (right_index < self.len) self[right_index] else null;
try expect(left_index < self.len);
try expect(right_index < self.len);
try expect(left.? == 98);
try expect(right.? == 99);
}

0 comments on commit 7fb64e2

Please sign in to comment.