Skip to content

Commit

Permalink
fix: correct type inference in composite literal init
Browse files Browse the repository at this point in the history
  • Loading branch information
mvertes authored Feb 11, 2020
1 parent 902af47 commit 0596031
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
16 changes: 16 additions & 0 deletions _test/composite5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import "fmt"

type T struct {
m uint16
}

var t = T{1<<2 | 1<<3}

func main() {
fmt.Println(t)
}

// Output:
// {12}
20 changes: 20 additions & 0 deletions _test/composite6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"fmt"

"github.com/containous/yaegi/_test/ct1"
)

type T struct {
m uint16
}

var t = T{1 << ct1.R}

func main() {
fmt.Println(t)
}

// Output:
// {2}
9 changes: 9 additions & 0 deletions _test/ct1/ct1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ct1

type Class uint

const (
L Class = iota
R
AL
)
9 changes: 8 additions & 1 deletion interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,13 @@ func (interp *Interpreter) cfg(root *node, pkgID string) ([]*node, error) {
}
// Propagate type to children, to handle implicit types
for _, c := range n.child {
c.typ = n.typ
switch c.kind {
case binaryExpr, unaryExpr:
// Do not attempt to propagate composite type to operator expressions,
// it breaks constant folding.
default:
c.typ = n.typ
}
}

case forStmt0, forRangeStmt:
Expand Down Expand Up @@ -1175,6 +1181,7 @@ func (interp *Interpreter) cfg(root *node, pkgID string) ([]*node, error) {
n.gen = nop
n.typ = sym.typ
n.sym = sym
n.rval = sym.rval
} else {
err = n.cfgErrorf("undefined selector: %s.%s", pkg, name)
}
Expand Down

0 comments on commit 0596031

Please sign in to comment.