Skip to content

Commit

Permalink
internal/shader: bug fix: wrong for-loop should fail compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Aug 8, 2023
1 parent 98cb77d commit 2de54c5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
7 changes: 6 additions & 1 deletion internal/shader/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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

Expand Down
22 changes: 22 additions & 0 deletions internal/shader/syntax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit 2de54c5

Please sign in to comment.