Skip to content

Commit

Permalink
Parser: boolCast boolean results even if not evaluated
Browse files Browse the repository at this point in the history
Even if the expression is not evaluated, we need the result to be
an integral type

Fixes #686
  • Loading branch information
ehaas authored and Vexu committed Apr 19, 2024
1 parent 94c3ee9 commit f60a2de
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/aro/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6447,6 +6447,8 @@ fn landExpr(p: *Parser) Error!Result {
if (try lhs.adjustTypes(tok, &rhs, p, .boolean_logic)) {
const res = lhs.val.toBool(p.comp) and rhs.val.toBool(p.comp);
lhs.val = Value.fromBool(res);
} else {
lhs.val.boolCast(p.comp);
}
try lhs.boolRes(p, .bool_and_expr, rhs);
}
Expand Down Expand Up @@ -6516,6 +6518,8 @@ fn eqExpr(p: *Parser) Error!Result {
const op: std.math.CompareOperator = if (tag == .equal_expr) .eq else .neq;
const res = lhs.val.compare(op, rhs.val, p.comp);
lhs.val = Value.fromBool(res);
} else {
lhs.val.boolCast(p.comp);
}
try lhs.boolRes(p, tag, rhs);
}
Expand Down Expand Up @@ -6545,6 +6549,8 @@ fn compExpr(p: *Parser) Error!Result {
};
const res = lhs.val.compare(op, rhs.val, p.comp);
lhs.val = Value.fromBool(res);
} else {
lhs.val.boolCast(p.comp);
}
try lhs.boolRes(p, tag, rhs);
}
Expand Down
3 changes: 3 additions & 0 deletions test/cases/binary expressions.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ _Static_assert((-1 ^ 0) == -1, "");
_Static_assert((-1 ^ -1) == 0, "");
_Static_assert((-2 ^ 2) == -4, "");

_Static_assert(2.0||(2.0 == 2.0), "");
_Static_assert(2.0||(3.0 > 2.0), "");
_Static_assert(2.0||(2.0 && 2.0), "");

#define EXPECTED_ERRORS "binary expressions.c:3:7: error: invalid operands to binary expression ('long' and 'float')" \
"binary expressions.c:6:13: error: invalid operands to binary expression ('char' and 'int *')" \
Expand Down

0 comments on commit f60a2de

Please sign in to comment.