internal/shader: check type redeclaration

This commit is contained in:
Hajime Hoshi 2022-09-24 18:56:01 +09:00
parent 0c19b8d7ae
commit 7f91a681e3
2 changed files with 35 additions and 1 deletions

View File

@ -315,8 +315,15 @@ func (cs *compileState) parseDecl(b *block, fname string, d ast.Decl) ([]shaderi
if !ok { if !ok {
return nil, false return nil, false
} }
n := s.Name.Name
for _, t := range b.types {
if t.name == n {
cs.addError(s.Pos(), fmt.Sprintf("%s redeclared in this block", n))
return nil, false
}
}
b.types = append(b.types, typ{ b.types = append(b.types, typ{
name: s.Name.Name, name: n,
ir: t, ir: t,
}) })
} }

View File

@ -2381,3 +2381,30 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
} }
} }
} }
func TestTypeRedeclaration(t *testing.T) {
cases := []struct {
stmt string
err bool
}{
{stmt: "type Foo int; type Foo int", err: true},
{stmt: "type Foo int; type Foo float", err: true},
{stmt: "type Foo int; type Bar int", err: false},
}
for _, c := range cases {
stmt := c.stmt
src := fmt.Sprintf(`package main
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
%s
return position
}`, stmt)
_, err := compileToIR([]byte(src))
if err == nil && c.err {
t.Errorf("%s must return an error but does not", stmt)
} else if err != nil && !c.err {
t.Errorf("%s must not return nil but returned %v", stmt, err)
}
}
}