From 2e4a59a333661ddc0bb3a6e7b94bf0d80efeaddb 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 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/source/slang/slang-check-stmt.cpp b/source/slang/slang-check-stmt.cpp index ae817f8673..3d161518fb 100644 --- a/source/slang/slang-check-stmt.cpp +++ b/source/slang/slang-check-stmt.cpp @@ -308,8 +308,11 @@ 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); } stmt->expr = expr;