Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interp: fix a missing implicit type conversion for binary expression #1654

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +2310 to +2318
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it ever implicitly return false?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A callback returning false terminates the tree walk. In our case, we just want to skip the node, but continue to process the rest of the expression subtree, looking for untyped expressions, so the callback always returns true.

}, nil)
}

func compDefineX(sc *scope, n *node) error {
l := len(n.child) - 1
types := []*itype{}
Expand Down
Loading