Skip to content

Commit

Permalink
Allow unpacked unions in equality and conditional expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
MikePopoloski committed Oct 5, 2024
1 parent 6843b58 commit 903ed2a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
14 changes: 12 additions & 2 deletions source/ast/expressions/OperatorExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ Expression& BinaryExpression::fromComponents(Expression& lhs, Expression& rhs, B
contextDetermined(context, result->right_, result, compilation.getStringType(),
opRange);
}
else if (lt->isAggregate() && lt->isEquivalent(*rt) && !lt->isUnpackedUnion()) {
else if (lt->isAggregate() && lt->isEquivalent(*rt)) {
good = !isWildcard;
result->type = singleBitType(compilation, lt, rt);
}
Expand Down Expand Up @@ -1342,7 +1342,7 @@ Expression& ConditionalExpression::fromSyntax(Compilation& comp,
else
good = false;
}
else if (lt->isEquivalent(*rt) && !lt->isUnpackedUnion()) {
else if (lt->isEquivalent(*rt)) {
result->type = lt;
}
else if (left.isImplicitString() && right.isImplicitString()) {
Expand Down Expand Up @@ -2615,6 +2615,16 @@ ConstantValue Expression::evalBinaryOperator(BinaryOperator op, const ConstantVa
SLANG_UNREACHABLE;
}
}
else if (cvl.isUnion() && cvr.isUnion()) {
switch (op) {
OP(Equality, SVInt(cvl == cvr));
OP(Inequality, SVInt(cvl != cvr));
OP(CaseEquality, SVInt(cvl == cvr));
OP(CaseInequality, SVInt(cvl != cvr));
default:
SLANG_UNREACHABLE;
}
}

#undef OP
SLANG_UNREACHABLE;
Expand Down
3 changes: 3 additions & 0 deletions tests/unittests/ast/EvalTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,9 @@ union {
session.eval("baz.c = 123;");
CHECK(session.eval("baz.a.s1").integer() == 123);

CHECK(session.eval("foo == bar").integer() == 1);
CHECK(session.eval("1 ? foo : bar").toString() == "(0) [3,4,42]");

NO_SESSION_ERRORS;
}

Expand Down
11 changes: 4 additions & 7 deletions tests/unittests/ast/ExpressionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,12 @@ TEST_CASE("Expression types") {

// Unpacked unions
declare("union { int i; real r; } uu1, uu2;");
CHECK(typeof("uu1 == uu2") == "<error>");
CHECK(typeof("uu1 !== uu2") == "<error>");
CHECK(typeof("1 ? uu1 : uu2") == "<error>");
CHECK(typeof("uu1 == uu2") == "bit");
CHECK(typeof("uu1 !== uu2") == "bit");
CHECK(typeof("1 ? uu1 : uu2") == "union{int i;real r;}u$3");

auto diags = filterWarnings(compilation.getAllDiagnostics());
REQUIRE(diags.size() == 11);
REQUIRE(diags.size() == 8);
CHECK(diags[0].code == diag::BadUnaryExpression);
CHECK(diags[1].code == diag::BadBinaryExpression);
CHECK(diags[2].code == diag::BadBinaryExpression);
Expand All @@ -350,9 +350,6 @@ TEST_CASE("Expression types") {
CHECK(diags[5].code == diag::BadBinaryExpression);
CHECK(diags[6].code == diag::BadConditionalExpression);
CHECK(diags[7].code == diag::NotBooleanConvertible);
CHECK(diags[8].code == diag::BadBinaryExpression);
CHECK(diags[9].code == diag::BadBinaryExpression);
CHECK(diags[10].code == diag::BadConditionalExpression);
}

TEST_CASE("Expression - bad name references") {
Expand Down

0 comments on commit 903ed2a

Please sign in to comment.