internal/shader: bug fix: untyped const bool couldn't be an if condition

Closes #2993
This commit is contained in:
Hajime Hoshi 2024-07-26 02:03:03 +09:00
parent 09cefc6e71
commit 5d47863a27
2 changed files with 33 additions and 1 deletions

View File

@ -251,7 +251,7 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
if !ok {
return nil, false
}
if len(ts) != 1 || ts[0].Main != shaderir.Bool {
if len(ts) != 1 {
var tss []string
for _, t := range ts {
tss = append(tss, t.String())
@ -259,6 +259,10 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
cs.addError(stmt.Pos(), fmt.Sprintf("if-condition must be bool but: %s", strings.Join(tss, ", ")))
return nil, false
}
if !(ts[0].Main == shaderir.Bool || (ts[0].Main == shaderir.None && exprs[0].Const != nil && exprs[0].Const.Kind() == gconstant.Bool)) {
cs.addError(stmt.Pos(), fmt.Sprintf("if-condition must be bool but: %s", ts[0].String()))
return nil, false
}
stmts = append(stmts, ss...)
var bs []*shaderir.Block

View File

@ -4335,3 +4335,31 @@ func Bar() float {
t.Error("compileToIR must return an error but did not")
}
}
// Issue #2993
func TestSyntaxIfAndConstBool(t *testing.T) {
if _, err := compileToIR([]byte(`package main
func Foo() int {
const X = true
if X {
return 1
}
return 0
}
`)); err != nil {
t.Error(err)
}
if _, err := compileToIR([]byte(`package main
func Foo() int {
const X bool = true
if X {
return 1
}
return 0
}
`)); err != nil {
t.Error(err)
}
}