internal/shader: bug fix: wrong for-loop should fail compilation

Closes #2680
This commit is contained in:
Hajime Hoshi 2023-08-08 23:37:57 +09:00
parent 98cb77d94f
commit 2de54c556b
2 changed files with 28 additions and 1 deletions

View File

@ -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

View File

@ -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")
}
}