From 7a0c09f5eb577f742866df3327cec736d434ea16 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 13 Dec 2019 11:18:04 +0100 Subject: [PATCH] fix: detect untyped values when importing from binary packages --- _test/str4.go | 11 +++++++++++ interp/cfg.go | 16 +++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 _test/str4.go diff --git a/_test/str4.go b/_test/str4.go new file mode 100644 index 000000000..ab68b2af8 --- /dev/null +++ b/_test/str4.go @@ -0,0 +1,11 @@ +package main + +import "unicode/utf8" + +func main() { + r, _ := utf8.DecodeRuneInString("Hello") + println(r < utf8.RuneSelf) +} + +// Output: +// true diff --git a/interp/cfg.go b/interp/cfg.go index 7c86081c8..801ff5b11 100644 --- a/interp/cfg.go +++ b/interp/cfg.go @@ -1155,7 +1155,7 @@ func (interp *Interpreter) cfg(root *node) ([]*node, error) { n.typ = &itype{cat: valueT, rtype: s.Type().Elem()} } else { n.kind = rvalueExpr - n.typ = &itype{cat: valueT, rtype: s.Type()} + n.typ = &itype{cat: valueT, rtype: s.Type(), untyped: isValueUntyped(s)} n.rval = s } n.gen = nop @@ -1825,3 +1825,17 @@ func arrayTypeLen(n *node) int { } return max + 1 } + +// isValueUntyped returns true if value is untyped +func isValueUntyped(v reflect.Value) bool { + // Consider only untyped constant values. + if v.CanSet() { + return false + } + // Consider only values of default numerical types. + switch v.Type().Kind() { + case reflect.Int, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float64, reflect.Complex128: + return true + } + return false +}