Skip to content

Commit

Permalink
internal/shader: bug fix: error in assinments to multiple variables
Browse files Browse the repository at this point in the history
Closes #2747
  • Loading branch information
hajimehoshi committed Sep 3, 2023
1 parent 1bbded8 commit 20ddfba
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
17 changes: 12 additions & 5 deletions internal/shader/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
34 changes: 34 additions & 0 deletions internal/shader/syntax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 20ddfba

Please sign in to comment.