internal/shader: bug fix: crash on assignment mismatches

This commit is contained in:
Hajime Hoshi 2024-03-23 15:13:44 +09:00
parent 66667fe877
commit ea6fce45ff
2 changed files with 58 additions and 15 deletions

View File

@ -492,8 +492,8 @@ func (cs *compileState) assign(block *block, fname string, pos token.Pos, lhs, r
var rhsTypes []shaderir.Type var rhsTypes []shaderir.Type
allblank := true allblank := true
for i, e := range lhs { if len(lhs) == len(rhs) {
if len(lhs) == len(rhs) { for i, e := range lhs {
// Prase RHS first for the order of the statements. // Prase RHS first for the order of the statements.
r, rts, ss, ok := cs.parseExpr(block, fname, rhs[i], true) r, rts, ss, ok := cs.parseExpr(block, fname, rhs[i], true)
if !ok { if !ok {
@ -627,20 +627,21 @@ func (cs *compileState) assign(block *block, fname string, pos token.Pos, lhs, r
}, },
}) })
} }
} else { }
if i == 0 { } else {
var ss []shaderir.Stmt var ss []shaderir.Stmt
var ok bool var ok bool
rhsExprs, rhsTypes, ss, ok = cs.parseExpr(block, fname, rhs[0], true) rhsExprs, rhsTypes, ss, ok = cs.parseExpr(block, fname, rhs[0], true)
if !ok { if !ok {
return nil, false return nil, false
} }
if len(rhsExprs) != len(lhs) { if len(lhs) != len(rhsExprs) {
cs.addError(pos, "single-value context and multiple-value context cannot be mixed") cs.addError(pos, fmt.Sprintf("assignment mismatch: %d variables but %d", len(lhs), len(rhsExprs)))
} return nil, false
stmts = append(stmts, ss...) }
} stmts = append(stmts, ss...)
for i, e := range lhs {
if define { if define {
if _, ok := e.(*ast.Ident); !ok { if _, ok := e.(*ast.Ident); !ok {
cs.addError(pos, "non-name on the left side of :=") cs.addError(pos, "non-name on the left side of :=")

View File

@ -4103,6 +4103,48 @@ func Bar() {
`)); err != nil { `)); err != nil {
t.Error(err) t.Error(err)
} }
if _, err := compileToIR([]byte(`package main
func Foo() {
a, b := 0
_, _ = a, b
}
`)); err == nil {
t.Error("compileToIR must return an error but did not")
}
if _, err := compileToIR([]byte(`package main
func Foo() {
a, b, c := 0, 0
_, _ = a, b, c
}
`)); err == nil {
t.Error("compileToIR must return an error but did not")
}
if _, err := compileToIR([]byte(`package main
func Foo() {
var a, b int
a, b = 0
_, _ = a, b
}
`)); err == nil {
t.Error("compileToIR must return an error but did not")
}
if _, err := compileToIR([]byte(`package main
func Foo() {
var a, b, c int
a, b, c = 0, 0
_, _ = a, b, c
}
`)); err == nil {
t.Error("compileToIR must return an error but did not")
}
} }
func TestSyntaxBitwiseOperator(t *testing.T) { func TestSyntaxBitwiseOperator(t *testing.T) {