From 20123e8420fc87ed1065a832335674e060538f4b Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 24 Jul 2023 02:19:42 +0900 Subject: [PATCH] internal/shader: bug fix: unary operators should keep the type info Closes #2705 --- internal/shader/expr.go | 12 +++++------- internal/shader/syntax_test.go | 13 +++++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/internal/shader/expr.go b/internal/shader/expr.go index cb2eb2288..f94af87f7 100644 --- a/internal/shader/expr.go +++ b/internal/shader/expr.go @@ -883,7 +883,7 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar }, []shaderir.Type{t}, stmts, true case *ast.UnaryExpr: - exprs, t, stmts, ok := cs.parseExpr(block, fname, e.X, markLocalVariableUsed) + exprs, ts, stmts, ok := cs.parseExpr(block, fname, e.X, markLocalVariableUsed) if !ok { return nil, nil, nil, false } @@ -894,16 +894,14 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar if exprs[0].Const != nil { v := gconstant.UnaryOp(e.Op, exprs[0].Const, 0) - t := shaderir.Type{Main: shaderir.Int} - if v.Kind() == gconstant.Float { - t = shaderir.Type{Main: shaderir.Float} - } + // Use the original type as it is. + // Keep the type untyped if the original expression is untyped (#2705). return []shaderir.Expr{ { Type: shaderir.NumberExpr, Const: v, }, - }, []shaderir.Type{t}, stmts, true + }, ts[:1], stmts, true } var op shaderir.Op @@ -924,7 +922,7 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar Op: op, Exprs: exprs, }, - }, t, stmts, true + }, ts[:1], stmts, true case *ast.CompositeLit: t, ok := cs.parseType(block, fname, e.Type) diff --git a/internal/shader/syntax_test.go b/internal/shader/syntax_test.go index bfeec5fe3..2aa23c310 100644 --- a/internal/shader/syntax_test.go +++ b/internal/shader/syntax_test.go @@ -3236,3 +3236,16 @@ func foo(x vec2) { t.Error("compileToIR must return an error but did not") } } + +// Issue #2705 +func TestSyntaxInitWithNegativeInteger(t *testing.T) { + if _, err := compileToIR([]byte(`package main + +func Fragment(position vec4, texCoord vec2, color vec4) vec4 { + var x float = -0 + _ = x + return position +}`)); err != nil { + t.Error(err) + } +}