Skip to content

Commit 945d8dd

Browse files
authored
Fix parsing logic of for loops' initial statement. (#5813)
1 parent ebfbe58 commit 945d8dd

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

source/slang/slang-parser.cpp

+12-9
Original file line numberDiff line numberDiff line change
@@ -5953,22 +5953,25 @@ ForStmt* Parser::ParseForStatement()
59535953
FillPosition(stmt);
59545954
ReadToken("for");
59555955
ReadToken(TokenType::LParent);
5956-
auto modifiers = ParseModifiers(this);
5957-
if (peekTypeName(this) || !modifiers.isEmpty())
5958-
{
5959-
stmt->initialStatement = parseVarDeclrStatement(modifiers);
5960-
}
5961-
else
5956+
if (!LookAheadToken(TokenType::Semicolon))
59625957
{
5963-
if (!LookAheadToken(TokenType::Semicolon))
5958+
stmt->initialStatement = ParseStatement();
5959+
if (as<DeclStmt>(stmt->initialStatement) || as<ExpressionStmt>(stmt->initialStatement))
59645960
{
5965-
stmt->initialStatement = ParseExpressionStatement();
5961+
// These are the only allowed form of initial statements of a for loop.
59665962
}
59675963
else
59685964
{
5969-
ReadToken(TokenType::Semicolon);
5965+
sink->diagnose(
5966+
stmt->initialStatement->loc,
5967+
Diagnostics::unexpectedTokenExpectedTokenType,
5968+
"expression");
59705969
}
59715970
}
5971+
else
5972+
{
5973+
ReadToken(TokenType::Semicolon);
5974+
}
59725975
if (!LookAheadToken(TokenType::Semicolon))
59735976
stmt->predicateExpression = ParseExpression();
59745977
ReadToken(TokenType::Semicolon);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//TEST_IGNORE_FILE:
2+
3+
typealias i32 = int32_t;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK): -output-using-type
2+
3+
// Regression test for bug https://github.com/shader-slang/slang/issues/5808
4+
5+
// Using a type defined from a different module
6+
// in a for loop should work.
7+
8+
import lib;
9+
10+
//TEST_INPUT:set output = out ubuffer(data=[0 0 0 0], stride=4)
11+
RWStructuredBuffer<i32> output;
12+
13+
[numthreads(1,1,1)]
14+
void computeMain()
15+
{
16+
// CHECK: 0
17+
// CHECK: 1
18+
// CHECK: 2
19+
// CHECK: 3
20+
for (i32 i = 0; i < 4; i++)
21+
{
22+
output[i] = i;
23+
}
24+
}

0 commit comments

Comments
 (0)