fix: correct type inference in composite literal init

This commit is contained in:
Marc Vertes
2020-02-11 10:10:04 +01:00
committed by GitHub
parent 902af477b8
commit 05960316f8
4 changed files with 53 additions and 1 deletions

16
_test/composite5.go Normal file
View File

@@ -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
_test/composite6.go Normal file
View File

@@ -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
_test/ct1/ct1.go Normal file
View File

@@ -0,0 +1,9 @@
package ct1
type Class uint
const (
L Class = iota
R
AL
)

View File

@@ -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:
@@ -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)
}