From 13ae8f58727129c68f9294b75eba21355be255fe Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 19 Aug 2022 01:37:45 +0900 Subject: [PATCH] internal/shader: add type checks for the builtin function faceforward Updates #2184 --- internal/shader/expr.go | 7 ++++- internal/shader/syntax_test.go | 56 ++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/internal/shader/expr.go b/internal/shader/expr.go index fef0ee7cd..e6ec076f3 100644 --- a/internal/shader/expr.go +++ b/internal/shader/expr.go @@ -447,7 +447,7 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar }) return nil, nil, stmts, true - case shaderir.Clamp, shaderir.Mix, shaderir.Smoothstep: + case shaderir.Clamp, shaderir.Mix, shaderir.Smoothstep, shaderir.Faceforward: // 3 arguments if len(args) != 3 { cs.addError(e.Pos(), fmt.Sprintf("number of %s's arguments must be 3 but %d", callee.BuiltinFunc, len(args))) @@ -485,6 +485,11 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar cs.addError(e.Pos(), fmt.Sprintf("the first and the second arguments for %s must equal to the third argument %s or float but %s and %s", callee.BuiltinFunc, argts[2].String(), argts[0].String(), argts[1].String())) return nil, nil, nil, false } + default: + if !argts[0].Equal(&argts[1]) || !argts[0].Equal(&argts[2]) { + cs.addError(e.Pos(), fmt.Sprintf("all the argument types for %s must be the same but %s, %s, and %s", callee.BuiltinFunc, argts[0].String(), argts[1].String(), argts[2].String())) + return nil, nil, nil, false + } } switch callee.BuiltinFunc { diff --git a/internal/shader/syntax_test.go b/internal/shader/syntax_test.go index 607920a41..f8fd7c49c 100644 --- a/internal/shader/syntax_test.go +++ b/internal/shader/syntax_test.go @@ -1842,6 +1842,62 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { } } +// Issue #2184 +func TestSyntaxBuiltinFuncTripleArgsType(t *testing.T) { + cases := []struct { + stmt string + err bool + }{ + {stmt: "a := {{.Func}}(); _ = a", err: true}, + {stmt: "a := {{.Func}}(1); _ = a", err: true}, + {stmt: "a := {{.Func}}(false, false); _ = a", err: true}, + {stmt: "a := {{.Func}}(1, 1); _ = a", err: true}, + {stmt: "a := {{.Func}}(false, false, false); _ = a", err: true}, + {stmt: "a := {{.Func}}(1, 1, 1); _ = a", err: false}, + {stmt: "a := {{.Func}}(1.0, 1, 1); _ = a", err: false}, + {stmt: "a := {{.Func}}(1, 1.0, 1); _ = a", err: false}, + {stmt: "a := {{.Func}}(1, 1, 1.0); _ = a", err: false}, + {stmt: "a := {{.Func}}(1, vec2(1), 1); _ = a", err: true}, + {stmt: "a := {{.Func}}(1, 1, vec2(1)); _ = a", err: true}, + {stmt: "a := {{.Func}}(1, vec2(1), vec2(1)); _ = a", err: true}, + {stmt: "a := {{.Func}}(vec2(1), 1, 1); _ = a", err: true}, + {stmt: "a := {{.Func}}(vec2(1), 1, vec2(1)); _ = a", err: true}, + {stmt: "a := {{.Func}}(vec2(1), vec2(1), 1); _ = a", err: true}, + {stmt: "a := {{.Func}}(vec2(1), vec2(1), vec2(1)); _ = a", err: false}, + {stmt: "a := {{.Func}}(vec2(1), vec2(1), vec3(1)); _ = a", err: true}, + {stmt: "a := {{.Func}}(vec3(1), 1, 1); _ = a", err: true}, + {stmt: "a := {{.Func}}(vec3(1), 1, vec3(1)); _ = a", err: true}, + {stmt: "a := {{.Func}}(vec3(1), vec3(1), 1); _ = a", err: true}, + {stmt: "a := {{.Func}}(vec3(1), vec3(1), vec3(1)); _ = a", err: false}, + {stmt: "a := {{.Func}}(vec4(1), 1, 1); _ = a", err: true}, + {stmt: "a := {{.Func}}(vec4(1), 1, vec4(1)); _ = a", err: true}, + {stmt: "a := {{.Func}}(vec4(1), vec4(1), 1); _ = a", err: true}, + {stmt: "a := {{.Func}}(vec4(1), vec4(1), vec4(1)); _ = a", err: false}, + {stmt: "a := {{.Func}}(1, 1, 1, 1); _ = a", err: true}, + } + + funcs := []string{ + "faceforward", + } + for _, c := range cases { + for _, f := range funcs { + stmt := strings.ReplaceAll(c.stmt, "{{.Func}}", f) + 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) + } + } + } +} + // Issue #2184 func TestSyntaxBuiltinFuncClampType(t *testing.T) { cases := []struct {