internal/shader: bug fix: forbid duplicated uniform variables

Closes #2648
This commit is contained in:
Hajime Hoshi 2023-08-29 00:43:04 +09:00
parent 446a6dc952
commit 1269315f75
2 changed files with 49 additions and 0 deletions

View File

@ -407,6 +407,12 @@ func (cs *compileState) parseDecl(b *block, fname string, d ast.Decl) ([]shaderi
cs.addError(s.Names[i].Pos(), fmt.Sprintf("global variables must be exposed: %s", v.name))
}
}
for _, name := range cs.ir.UniformNames {
if name == v.name {
cs.addError(s.Pos(), fmt.Sprintf("%s redeclared in this block", name))
return nil, false
}
}
cs.ir.UniformNames = append(cs.ir.UniformNames, v.name)
cs.ir.Uniforms = append(cs.ir.Uniforms, v.typ)
}

View File

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