Skip to content

Commit

Permalink
stage2: add error note for comparing booleans with '||'
Browse files Browse the repository at this point in the history
  • Loading branch information
g-w1 authored and Vexu committed Jul 6, 2021
1 parent d089b3d commit 6c11e9b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Sema.zig
Expand Up @@ -2566,8 +2566,19 @@ fn zirMergeErrorSets(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Inn
const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
const lhs_ty = try sema.resolveType(block, lhs_src, extra.lhs);
const rhs_ty = try sema.resolveType(block, rhs_src, extra.rhs);
const lhs = try sema.resolveInst(extra.lhs);
const rhs = try sema.resolveInst(extra.rhs);
if (rhs.ty.zigTypeTag() == .Bool and lhs.ty.zigTypeTag() == .Bool) {
const msg = msg: {
const msg = try sema.mod.errMsg(&block.base, lhs_src, "expected error set type, found 'bool'", .{});
errdefer msg.destroy(sema.gpa);
try sema.mod.errNote(&block.base, src, msg, "'||' merges error sets; 'or' performs boolean OR", .{});
break :msg msg;
};
return sema.mod.failWithOwnedErrorMsg(&block.base, msg);
}
const rhs_ty = try sema.resolveAirAsType(block, rhs_src, rhs);
const lhs_ty = try sema.resolveAirAsType(block, lhs_src, lhs);
if (rhs_ty.zigTypeTag() != .ErrorSet)
return sema.mod.fail(&block.base, rhs_src, "expected error set type, found {}", .{rhs_ty});
if (lhs_ty.zigTypeTag() != .ErrorSet)
Expand Down
9 changes: 9 additions & 0 deletions test/cases.zig
Expand Up @@ -1481,6 +1481,15 @@ pub fn addCases(ctx: *TestContext) !void {
,
"",
);
case.addError(
\\pub fn main() void {
\\ const z = true || false;
\\ _ = z;
\\}
, &.{
":2:15: error: expected error set type, found 'bool'",
":2:20: note: '||' merges error sets; 'or' performs boolean OR",
});
}
{
var case = ctx.exe("inline assembly", linux_x64);
Expand Down

0 comments on commit 6c11e9b

Please sign in to comment.