From a32156ef52f43b8503b2c77f2f1d51220ab9bdea Mon Sep 17 00:00:00 2001 From: Anders Leino Date: Thu, 5 Sep 2024 11:59:31 +0300 Subject: [PATCH] Coerce 'case' expressions to the same type as 'condition' 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 #4921. --- source/slang/slang-check-stmt.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/source/slang/slang-check-stmt.cpp b/source/slang/slang-check-stmt.cpp index ae817f8673..71ee421911 100644 --- a/source/slang/slang-check-stmt.cpp +++ b/source/slang/slang-check-stmt.cpp @@ -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(); if (!switchStmt) @@ -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;