internal/shader: bug fix: error in assinments to multiple variables

Closes #2747
This commit is contained in:
Hajime Hoshi 2023-09-04 02:21:16 +09:00
parent 1bbded8653
commit 20ddfba983
2 changed files with 46 additions and 5 deletions

View File

@ -620,16 +620,23 @@ func (cs *compileState) assign(block *block, fname string, pos token.Pos, lhs, r
}
stmts = append(stmts, ss...)
if len(l) != 1 {
cs.addError(pos, fmt.Sprintf("unexpected count of types in lhs: %d", len(l)))
return nil, false
}
if len(lts) != 1 {
cs.addError(pos, fmt.Sprintf("unexpected count of expressions in lhs: %d", len(l)))
return nil, false
}
if l[0].Type == shaderir.Blank {
continue
}
allblank = false
for i, lt := range lts {
if !canAssign(&lt, &rhsTypes[i], rhsExprs[i].Const) {
cs.addError(pos, fmt.Sprintf("cannot use type %s as type %s in variable declaration", rhsTypes[i].String(), lt.String()))
return nil, false
}
if !canAssign(&lts[0], &rhsTypes[i], rhsExprs[i].Const) {
cs.addError(pos, fmt.Sprintf("cannot use type %s as type %s in variable declaration", rhsTypes[i].String(), lts[0].String()))
return nil, false
}
stmts = append(stmts, shaderir.Stmt{

View File

@ -3586,3 +3586,37 @@ func foo() {
t.Error("compileToIR must return an error but did not")
}
}
// Issue #2747
func TestMultipleAssignmentsAndTypeCheck(t *testing.T) {
if _, err := compileToIR([]byte(`package main
func Foo() (float, bool) {
return 0, false
}
func Bar() {
f, b := Foo()
_, _ = f, b
return
}
`)); err != nil {
t.Error(err)
}
if _, err := compileToIR([]byte(`package main
func Foo() (float, bool) {
return 0, false
}
func Bar() {
var f float
var b bool
f, b = Foo()
_, _ = f, b
return
}
`)); err != nil {
t.Error(err)
}
}