From 1487a1f98d8916985b243f4a0113d216c42e4ba3 Mon Sep 17 00:00:00 2001 From: Jay Kwak <82421531+jkwak-work@users.noreply.github.com> Date: Wed, 30 Oct 2024 22:58:07 -0700 Subject: Constant-fold for the type-casting in switch-case labels (#5436) --- source/slang/slang-check-expr.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'source') diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp index 076e310c0..8c170698b 100644 --- a/source/slang/slang-check-expr.cpp +++ b/source/slang/slang-check-expr.cpp @@ -2068,7 +2068,20 @@ IntVal* SemanticsVisitor::tryConstantFoldExpr( return nullptr; if (!isValidCompileTimeConstantType(substType)) return nullptr; - auto val = tryConstantFoldExpr(typeCastOperand, kind, circularityInfo); + + IntVal* val = tryConstantFoldExpr(typeCastOperand, kind, circularityInfo); + if (!val) + { + if (auto floatLitExpr = typeCastOperand.as()) + { + // When explicitly casting from float type to integer type, let's fold it as + // an integer value. + const IntegerLiteralValue value = + IntegerLiteralValue(floatLitExpr.getExpr()->value); + val = m_astBuilder->getIntVal(substType, value); + } + } + if (val) { if (!expr.getExpr()->type) -- cgit v1.2.3