Skip to content

Commit e686f55

Browse files
authored
interp: fix a missing implicit type conversion for binary expression
When parsing binary operator expressions, make sure that implicit type conversions for untyped expressions are performed. It involves walking the sub-expression subtree at post-processing of binary expressions. Fixes #1653.
1 parent 9c4dcfc commit e686f55

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

_test/issue-1653.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
func f(b uint) uint {
4+
return uint(1) + (0x1 >> b)
5+
}
6+
7+
func main() {
8+
println(f(1))
9+
}
10+
11+
// Output:
12+
// 1

interp/cfg.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,9 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
984984
// Allocate a new location in frame, and store the result here.
985985
n.findex = sc.add(n.typ)
986986
}
987+
if n.typ != nil && !n.typ.untyped {
988+
fixUntyped(n, sc)
989+
}
987990

988991
case indexExpr:
989992
if isBlank(n.child[0]) {
@@ -2302,6 +2305,20 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
23022305
return initNodes, err
23032306
}
23042307

2308+
// fixUntyped propagates implicit type conversions for untyped binary expressions.
2309+
func fixUntyped(nod *node, sc *scope) {
2310+
nod.Walk(func(n *node) bool {
2311+
if n == nod || (n.kind != binaryExpr && n.kind != parenExpr) || !n.typ.untyped {
2312+
return true
2313+
}
2314+
n.typ = nod.typ
2315+
if n.findex >= 0 {
2316+
sc.types[n.findex] = nod.typ.frameType()
2317+
}
2318+
return true
2319+
}, nil)
2320+
}
2321+
23052322
func compDefineX(sc *scope, n *node) error {
23062323
l := len(n.child) - 1
23072324
types := []*itype{}

0 commit comments

Comments
 (0)