shader: Reland: Bug fix: Checking unused variables defined with var

This commit is contained in:
Hajime Hoshi 2020-09-17 17:29:31 +09:00
parent ac971f77c5
commit ab004031d0
4 changed files with 26 additions and 1 deletions

View File

@ -322,8 +322,11 @@ func (cs *compileState) parseDecl(b *block, d ast.Decl) ([]shaderir.Stmt, bool)
continue continue
} }
// base must be obtained before adding the variables.
base := b.totalLocalVariableNum() base := b.totalLocalVariableNum()
b.vars = append(b.vars, vs...) for _, v := range vs {
b.addNamedLocalVariable(v.name, v.typ, d.Pos())
}
if len(inits) > 0 { if len(inits) > 0 {
for i := range vs { for i := range vs {

View File

@ -5,6 +5,7 @@ func Foo() [3]vec2 {
{ {
var b [2]vec2 var b [2]vec2
b = a b = a
_ = b
} }
var c [3]vec2 var c [3]vec2
return c return c

View File

@ -15,6 +15,7 @@ func Foo(foo vec2) vec4 {
r.y = foo.y r.y = foo.y
var foo vec4 var foo vec4
r.z = bar.z r.z = bar.z
_ = foo
} }
} }
return r return r

View File

@ -588,6 +588,26 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
`)); err != nil { `)); err != nil {
t.Error(err) t.Error(err)
} }
if _, err := NewShader([]byte(`package main
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
var a int
return vec4(0)
}
`)); err == nil {
t.Errorf("error must be non-nil but was nil")
}
if _, err := NewShader([]byte(`package main
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
var a, b int
return vec4(0)
}
`)); err == nil {
t.Errorf("error must be non-nil but was nil")
}
} }
func TestShaderBlankLhs(t *testing.T) { func TestShaderBlankLhs(t *testing.T) {