diff --git a/internal/shader/stmt.go b/internal/shader/stmt.go index 8d64a9ef2..448aef3e6 100644 --- a/internal/shader/stmt.go +++ b/internal/shader/stmt.go @@ -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 diff --git a/internal/shader/syntax_test.go b/internal/shader/syntax_test.go index 4128aa0f8..b865bfb8c 100644 --- a/internal/shader/syntax_test.go +++ b/internal/shader/syntax_test.go @@ -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) + } +}