Skip to content

Commit

Permalink
interp: fix a missing implicit type conversion for binary expression
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvertes authored and Bai Yingjie committed Oct 9, 2024
1 parent b10bee1 commit b96adea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions _test/issue-1653.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

func f(b uint) uint {
return uint(1) + (0x1 >> b)
}

func main() {
println(f(1))
}

// Output:
// 1
17 changes: 17 additions & 0 deletions interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down Expand Up @@ -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{}
Expand Down

0 comments on commit b96adea

Please sign in to comment.