From 0b46715bff7f047fefed303ae149d3de8eb6be4f Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 21 Feb 2024 13:59:30 -0800 Subject: Fix parsing of literals in stdlib. (#3610) * Fix parsing of literals in stdlib. * Fix double lit limits. --- source/slang/core.meta.slang | 4 ++-- source/slang/slang-ast-expr.h | 1 + source/slang/slang-check-expr.cpp | 4 ++-- source/slang/slang-parser.cpp | 9 ++------- tests/bugs/gh-3589.slang | 18 ++++++++++++++++++ 5 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 tests/bugs/gh-3589.slang diff --git a/source/slang/core.meta.slang b/source/slang/core.meta.slang index 1e1ef061e..b02a44c5c 100644 --- a/source/slang/core.meta.slang +++ b/source/slang/core.meta.slang @@ -997,8 +997,8 @@ extension float : IRangedValue extension double : IRangedValue { - static const double maxValue = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0; - static const double minValue = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0; + static const double maxValue = bit_cast(0x7fefffffffffffffULL); + static const double minValue = bit_cast(0xffefffffffffffffULL); } extension int : IRangedValue diff --git a/source/slang/slang-ast-expr.h b/source/slang/slang-ast-expr.h index 9f2cd22a9..9a7993937 100644 --- a/source/slang/slang-ast-expr.h +++ b/source/slang/slang-ast-expr.h @@ -79,6 +79,7 @@ class LiteralExpr : public Expr // The token that was used to express the literal. This can be // used to get the raw text of the literal, including any suffix. Token token; + BaseType suffixType = BaseType::Void; }; class IntegerLiteralExpr : public LiteralExpr diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp index e9de74d8e..811e4b395 100644 --- a/source/slang/slang-check-expr.cpp +++ b/source/slang/slang-check-expr.cpp @@ -1384,7 +1384,7 @@ namespace Slang // if(!expr->type.type) { - expr->type = m_astBuilder->getIntType(); + expr->type = m_astBuilder->getBuiltinType(expr->suffixType); } return expr; } @@ -1393,7 +1393,7 @@ namespace Slang { if(!expr->type.type) { - expr->type = m_astBuilder->getFloatType(); + expr->type = m_astBuilder->getBuiltinType(expr->suffixType); } return expr; } diff --git a/source/slang/slang-parser.cpp b/source/slang/slang-parser.cpp index ad4e65bf2..f2aff32ea 100644 --- a/source/slang/slang-parser.cpp +++ b/source/slang/slang-parser.cpp @@ -6590,11 +6590,9 @@ namespace Slang value = _fixIntegerLiteral(suffixBaseType, value, &token, parser->sink); - ASTBuilder* astBuilder = parser->astBuilder; - Type* suffixType = (suffixBaseType == BaseType::Void) ? astBuilder->getErrorType() : astBuilder->getBuiltinType(suffixBaseType); constExpr->value = value; - constExpr->type = QualType(suffixType); + constExpr->suffixType = suffixBaseType; return constExpr; } @@ -6704,12 +6702,9 @@ namespace Slang } } - ASTBuilder* astBuilder = parser->astBuilder; - - Type* suffixType = (suffixBaseType == BaseType::Void) ? astBuilder->getErrorType() : astBuilder->getBuiltinType(suffixBaseType); constExpr->value = fixedValue; - constExpr->type = QualType(suffixType); + constExpr->suffixType = suffixBaseType; return constExpr; } diff --git a/tests/bugs/gh-3589.slang b/tests/bugs/gh-3589.slang new file mode 100644 index 000000000..58bb74f37 --- /dev/null +++ b/tests/bugs/gh-3589.slang @@ -0,0 +1,18 @@ +//TEST:SIMPLE(filecheck=CHECK): -target hlsl -entry main -profile cs_6_0 +RWStructuredBuffer outputBuffer; +RWStructuredBuffer outputBuffer2; + +void main(uint id: SV_DispatchThreadID) +{ + let i = int64_t.maxValue; + let m = int64_t.minValue; + // CHECK: 9223372036854775807LL + // CHECK: -9223372036854775808LL + outputBuffer[0] = i; + outputBuffer[1] = m; + + // CHECK: -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0 + outputBuffer2[0] = double.minValue; + // CHECK: 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0 + outputBuffer2[1] = double.maxValue; +} \ No newline at end of file -- cgit v1.2.3