diff --git a/_test/issue-1653.go b/_test/issue-1653.go new file mode 100644 index 00000000..8986d1f6 --- /dev/null +++ b/_test/issue-1653.go @@ -0,0 +1,12 @@ +package main + +func f(b uint) uint { + return uint(1) + (0x1 >> b) +} + +func main() { + println(f(1)) +} + +// Output: +// 1 diff --git a/interp/cfg.go b/interp/cfg.go index 933e2a93..9bed8a00 100644 --- a/interp/cfg.go +++ b/interp/cfg.go @@ -984,6 +984,9 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string // Allocate a new location in frame, and store the result here. n.findex = sc.add(n.typ) } + if n.typ != nil && !n.typ.untyped { + fixUntyped(n, sc) + } case indexExpr: if isBlank(n.child[0]) { @@ -2302,6 +2305,20 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string return initNodes, err } +// fixUntyped propagates implicit type conversions for untyped binary expressions. +func fixUntyped(nod *node, sc *scope) { + nod.Walk(func(n *node) bool { + if n == nod || (n.kind != binaryExpr && n.kind != parenExpr) || !n.typ.untyped { + return true + } + n.typ = nod.typ + if n.findex >= 0 { + sc.types[n.findex] = nod.typ.frameType() + } + return true + }, nil) +} + func compDefineX(sc *scope, n *node) error { l := len(n.child) - 1 types := []*itype{}