stage2: add error note for comparing booleans with '||'

This commit is contained in:
Jacob G-W
2021-07-05 20:17:51 -04:00
committed by Veikka Tuominen
parent d089b3df7a
commit 6c11e9bb07
2 changed files with 22 additions and 2 deletions

View File

@@ -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)

View File

@@ -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);