diff options
| -rw-r--r-- | source/slang/slang-parser.cpp | 26 | ||||
| -rw-r--r-- | tests/bugs/parens-cast-issue.slang | 21 | ||||
| -rw-r--r-- | tests/bugs/parens-cast-issue.slang.expected.txt | 4 |
3 files changed, 44 insertions, 7 deletions
diff --git a/source/slang/slang-parser.cpp b/source/slang/slang-parser.cpp index fdc5c19b5..d606698a1 100644 --- a/source/slang/slang-parser.cpp +++ b/source/slang/slang-parser.cpp @@ -4362,7 +4362,7 @@ namespace Slang nullptr, // no semantics visitor available yet name, parser->currentScope); - if(!lookupResult.isValid() || lookupResult.isOverloaded()) + if (!lookupResult.isValid() || lookupResult.isOverloaded()) return false; return _isType(lookupResult.item.declRef.getDecl()); @@ -4370,7 +4370,7 @@ namespace Slang static bool peekTypeName(Parser* parser) { - if(!parser->LookAheadToken(TokenType::Identifier)) + if (!parser->LookAheadToken(TokenType::Identifier)) return false; auto name = parser->tokenReader.peekToken().getName(); @@ -5597,14 +5597,26 @@ namespace Slang case TokenType::LParent: { Token openParen = parser->ReadToken(TokenType::LParent); - Expr* typeExpr = nullptr; - if (peekTypeName(parser) && parser->LookAheadToken(TokenType::RParent)) + + // Only handles cases of `(type)`, where type is a single identifier, + // and at this point the type is known + if (peekTypeName(parser) && parser->LookAheadToken(TokenType::RParent, 1)) { - TypeCastExpr* tcexpr = parser->astBuilder->create<ExplicitCastExpr>(); - parser->FillPosition(tcexpr); - tcexpr->functionExpr = typeExpr; + // Get the identifier for the type + const Token typeToken = advanceToken(parser); + // Consume the closing `)` parser->ReadToken(TokenType::RParent); + auto varExpr = parser->astBuilder->create<VarExpr>(); + varExpr->scope = parser->currentScope; + varExpr->loc = typeToken.loc; + varExpr->name = typeToken.getName(); + + TypeCastExpr* tcexpr = parser->astBuilder->create<ExplicitCastExpr>(); + tcexpr->loc = openParen.loc; + + tcexpr->functionExpr = varExpr; + auto arg = parsePrefixExpr(parser); tcexpr->arguments.add(arg); diff --git a/tests/bugs/parens-cast-issue.slang b/tests/bugs/parens-cast-issue.slang new file mode 100644 index 000000000..2cfe603c8 --- /dev/null +++ b/tests/bugs/parens-cast-issue.slang @@ -0,0 +1,21 @@ +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj +//TEST(compute,vulkan):COMPARE_COMPUTE_EX:-vk -slang -compute -shaderobj + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name outputBuffer +RWStructuredBuffer<int> outputBuffer; + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + uint tid = dispatchThreadID.x; + int idx = (int)tid; + + float q = (idx * 0.5); + + int x = (uint)(int)q; + + // This combination of casts and parenthesis used to cause issues. + int z = ((uint)(int)q); + + outputBuffer[tid] = z + x; +}
\ No newline at end of file diff --git a/tests/bugs/parens-cast-issue.slang.expected.txt b/tests/bugs/parens-cast-issue.slang.expected.txt new file mode 100644 index 000000000..3333a969d --- /dev/null +++ b/tests/bugs/parens-cast-issue.slang.expected.txt @@ -0,0 +1,4 @@ +0 +0 +2 +2 |
