internal/shader: bug fix: unary operators should keep the type info

Closes #2705
This commit is contained in:
Hajime Hoshi 2023-07-24 02:19:42 +09:00
parent 5a1109e56a
commit 20123e8420
2 changed files with 18 additions and 7 deletions

View File

@ -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)

View File

@ -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)
}
}