summaryrefslogtreecommitdiff
path: root/source/slang/slang-check-stmt.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2024-10-20 09:28:13 -0700
committerGitHub <noreply@github.com>2024-10-20 09:28:13 -0700
commit307315a7305e76529837fd1cdb677f534d5f539b (patch)
treeba39e96ba2e9b3d62d1213aab2f1cc54febe451a /source/slang/slang-check-stmt.cpp
parent9936178dd3efb026bfa142512a2bf061d7a75ab5 (diff)
Properly check switch case. (#5341)
Diffstat (limited to 'source/slang/slang-check-stmt.cpp')
-rw-r--r--source/slang/slang-check-stmt.cpp22
1 files changed, 10 insertions, 12 deletions
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<SwitchStmt>();
-
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;