Skip to content

Commit

Permalink
internal/shader: bug fix: forbid duplicated uniform variables
Browse files Browse the repository at this point in the history
Closes #2648
  • Loading branch information
hajimehoshi committed Aug 28, 2023
1 parent 446a6dc commit 1269315
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/shader/shader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
43 changes: 43 additions & 0 deletions internal/shader/syntax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit 1269315

Please sign in to comment.