From 307315a7305e76529837fd1cdb677f534d5f539b Mon Sep 17 00:00:00 2001 From: Yong He Date: Sun, 20 Oct 2024 09:28:13 -0700 Subject: Properly check switch case. (#5341) --- source/slang/slang-check-stmt.cpp | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'source/slang/slang-check-stmt.cpp') diff --git a/source/slang/slang-check-stmt.cpp b/source/slang/slang-check-stmt.cpp index ae817f867..8b0e0b284 100644 --- a/source/slang/slang-check-stmt.cpp +++ b/source/slang/slang-check-stmt.cpp @@ -294,23 +294,21 @@ namespace Slang void SemanticsStmtVisitor::visitCaseStmt(CaseStmt* stmt) { - auto expr = CheckExpr(stmt->expr); - - // 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) { getSink()->diagnose(stmt, Diagnostics::caseOutsideSwitch); + return; } - else - { - // TODO: need to do some basic matching to ensure the type - // for the `case` is consistent with the type for the `switch`... - } + + // Check that the type for the `case` is consistent with the type for the `switch`. + auto expr = CheckExpr(stmt->expr); + expr = coerce(CoercionSite::Argument, switchStmt->condition->type, expr); + + // 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 = checkConstantIntVal(expr); stmt->expr = expr; stmt->exprVal = exprVal; -- cgit v1.2.3