From 05104eea01b775a8738a8ddef7e5d3168fbbbdd0 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Tue, 30 Jul 2024 19:54:04 +0200 Subject: [PATCH] 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. --- _test/issue-1653.go | 12 ++++++++++++ interp/cfg.go | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 _test/issue-1653.go 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 ce9f6a9c..e5eae1ff 100644 --- a/interp/cfg.go +++ b/interp/cfg.go @@ -982,6 +982,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]) { @@ -2300,6 +2303,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{}