diff --git a/internal/shader/stmt.go b/internal/shader/stmt.go index 8c16f4cce549..df7770c6f875 100644 --- a/internal/shader/stmt.go +++ b/internal/shader/stmt.go @@ -214,7 +214,7 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP } // Create a new pseudo block for the initial statement, so that the counter variable belongs to the - // new pseudo block for each for-loop. Without this, the samely named counter variables in different + // new pseudo block for each for-loop. Without this, the same-named counter variables in different // for-loops confuses the parser. pseudoBlock, ok := cs.parseBlock(block, fname, []ast.Stmt{stmt.Init}, inParams, outParams, returnType, false) if !ok { @@ -240,6 +240,11 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP return nil, false } + if len(pseudoBlock.vars) != 1 { + cs.addError(stmt.Pos(), msg) + return nil, false + } + vartype := pseudoBlock.vars[0].typ init := ss[0].Exprs[1].Const diff --git a/internal/shader/syntax_test.go b/internal/shader/syntax_test.go index ae8fe733c33c..e9d3475752f3 100644 --- a/internal/shader/syntax_test.go +++ b/internal/shader/syntax_test.go @@ -3521,3 +3521,25 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { } } } + +// Issue #2680 +func TestForWithLocalVariable(t *testing.T) { + if _, err := compileToIR([]byte(`package main + +func foo() { + i := 0 + for i = 0; i < 1; i++ { + } +}`)); err == nil { + t.Error("compileToIR must return an error but did not") + } + if _, err := compileToIR([]byte(`package main + +func foo() { + for i, j := 0, 0; i < 1; i++ { + _ = j + } +}`)); err == nil { + t.Error("compileToIR must return an error but did not") + } +}