internal/shader: implement strict type checks when calling a function

Closes #2032
This commit is contained in:
Hajime Hoshi 2022-04-09 00:32:25 +09:00
parent 15fe7158fd
commit 853d94c3ef
2 changed files with 49 additions and 0 deletions

View File

@ -444,6 +444,11 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr, markLocalVariable
return nil, nil, nil, false
}
}
if !canAssign(&args[i], &p, &argts[i]) {
cs.addError(e.Pos(), fmt.Sprintf("cannot use type %s as type %s in argument", argts[i].String(), p.String()))
return nil, nil, nil, false
}
}
var outParams []int

View File

@ -1307,3 +1307,47 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
t.Errorf("error must be non-nil but was nil")
}
}
// Issue #2032
func TestSyntaxTypeFuncCall(t *testing.T) {
if _, err := compileToIR([]byte(`package main
func Foo(x vec2) {
}
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
Foo(0)
return color
}
`)); err == nil {
t.Errorf("error must be non-nil but was nil")
}
if _, err := compileToIR([]byte(`package main
func Foo(x vec2, y vec3) {
}
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
Foo(0, 1)
return color
}
`)); err == nil {
t.Errorf("error must be non-nil but was nil")
}
if _, err := compileToIR([]byte(`package main
func Foo(x vec2, y vec3) {
}
func Bar() (int, int) {
return 0, 1
}
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
Foo(Bar())
return color
}
`)); err == nil {
t.Errorf("error must be non-nil but was nil")
}
}