internal/shader: bug fix: forbide assigning to a uniform variable

Closes #2648
This commit is contained in:
Hajime Hoshi 2023-05-09 01:38:39 +09:00
parent 0785502be3
commit ad23ae81c1
2 changed files with 40 additions and 0 deletions

View File

@ -74,6 +74,11 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
}
stmts = append(stmts, ss...)
if lhs[0].Type == shaderir.UniformVariable {
cs.addError(stmt.Pos(), fmt.Sprintf("a uniform variable cannot be assigned"))
return nil, false
}
var op shaderir.Op
switch stmt.Tok {
case token.ADD_ASSIGN:

View File

@ -3138,3 +3138,38 @@ func bar(x vec2) {
t.Error("compileToIR must return an error but did not")
}
}
// Issue #2648
func TestAssignToUniformVariables(t *testing.T) {
if _, err := compileToIR([]byte(`package main
var Foo float
func foo(x vec2) {
Foo = 0
}`)); err == nil {
t.Error("compileToIR must return an error but did not")
}
if _, err := compileToIR([]byte(`package main
var Foo float
func foo(x vec2) {
var x int
x, Foo = 0, 0
_ = x
}`)); err == nil {
t.Error("compileToIR must return an error but did not")
}
if _, err := compileToIR([]byte(`package main
var Foo float
func foo(x vec2) {
Foo += 0
}`)); err == nil {
t.Error("compileToIR must return an error but did not")
}
}