From 2a1d23d926d00cf24d0ae5adb2c34d7982e003dd Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 26 Apr 2023 22:07:40 +0900 Subject: [PATCH] internal/shader: bug fix: panic when an assignment mismatch happens Closes #2654 --- internal/shader/stmt.go | 9 +++++++++ internal/shader/syntax_test.go | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/internal/shader/stmt.go b/internal/shader/stmt.go index 1196f7689..e25ee8f28 100644 --- a/internal/shader/stmt.go +++ b/internal/shader/stmt.go @@ -639,6 +639,15 @@ func (cs *compileState) assign(block *block, fname string, pos token.Pos, lhs, r } stmts = append(stmts, ss...) + if len(l) != len(r) { + if len(r) == 0 { + cs.addError(pos, fmt.Sprintf("right-hand side (no value) used as value")) + } else { + cs.addError(pos, fmt.Sprintf("assignment mismatch: %d variables but the right-hand side has %d values", len(l), len(r))) + } + return nil, false + } + if l[0].Type == shaderir.Blank { continue } diff --git a/internal/shader/syntax_test.go b/internal/shader/syntax_test.go index 93327a738..290da0317 100644 --- a/internal/shader/syntax_test.go +++ b/internal/shader/syntax_test.go @@ -3122,3 +3122,19 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { } } } + +// Issue #2654 +func TestOmittedReturnType(t *testing.T) { + if _, err := compileToIR([]byte(`package main + +func foo(x vec2) { + x = bar(x) + _ = x +} + +func bar(x vec2) { + return x +}`)); err == nil { + t.Error("compileToIR must return an error but did not") + } +}