From 853d94c3ef085c834f39ff8b5b867864b001654e Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 9 Apr 2022 00:32:25 +0900 Subject: [PATCH] internal/shader: implement strict type checks when calling a function Closes #2032 --- internal/shader/expr.go | 5 ++++ internal/shader/syntax_test.go | 44 ++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/internal/shader/expr.go b/internal/shader/expr.go index f53aff37d..d79ea28b6 100644 --- a/internal/shader/expr.go +++ b/internal/shader/expr.go @@ -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 diff --git a/internal/shader/syntax_test.go b/internal/shader/syntax_test.go index a1dee303c..be4265ed0 100644 --- a/internal/shader/syntax_test.go +++ b/internal/shader/syntax_test.go @@ -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") + } +}