Skip to content

Commit

Permalink
Coerce 'case' expressions to the same type as 'condition'
Browse files Browse the repository at this point in the history
Some WGSL compilers complain if e.g. case expression is signed while
condition expression is unsigned.
Insert logic to coerce the case expression to be of the same type as the condition
expression.

This addresses issue shader-slang#4921.
  • Loading branch information
aleino-nv committed Sep 6, 2024
1 parent 8662375 commit a32156e
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions source/slang/slang-check-stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ namespace Slang
// coerce to type being switch on, and ensure that value is a compile-time constant
// The Vals in the AST are pointer-unique, making them easy to check for duplicates
// by addeing them to a HashSet.
auto exprVal = tryConstantFoldExpr(expr, ConstantFoldingKind::CompileTime, nullptr);
auto switchStmt = FindOuterStmt<SwitchStmt>();

if (!switchStmt)
Expand All @@ -308,9 +307,13 @@ namespace Slang
}
else
{
// TODO: need to do some basic matching to ensure the type
// for the `case` is consistent with the type for the `switch`...
// Try to coerce the case expression to the same type as the condition
Expr* condition = switchStmt->condition;
expr = coerce(CoercionSite::General, condition->type, expr);
// Can't think of any cases where this would fail
SLANG_ASSERT(expr);
}
auto exprVal = tryConstantFoldExpr(expr, ConstantFoldingKind::CompileTime, nullptr);

stmt->expr = expr;
stmt->exprVal = exprVal;
Expand Down

0 comments on commit a32156e

Please sign in to comment.