interp: fix default types for runes

When using an untyped rune in an interface, the default type was blindly untyping it. This fixes this issue.

Fixes #1238
This commit is contained in:
Nicholas Wiersma
2021-08-31 06:32:10 -04:00
committed by GitHub
parent 772cd68fea
commit d2569a85a6
2 changed files with 29 additions and 3 deletions

22
_test/rune2.go Normal file
View File

@@ -0,0 +1,22 @@
package main
import "fmt"
const majorVersion = '2'
type hashed struct {
major byte
}
func main() {
fmt.Println(majorVersion)
p := new(hashed)
p.major = majorVersion
fmt.Println(p)
}
// Output:
// 50
// &{50}

View File

@@ -1773,15 +1773,19 @@ func (t *itype) defaultType(v reflect.Value, sc *scope) *itype {
typ := t
// The default type can also be derived from a constant value.
if v.IsValid() && t.TypeOf().Implements(constVal) {
// TODO: find a way to get actual types here
if v.IsValid() && v.Type().Implements(constVal) {
switch v.Interface().(constant.Value).Kind() {
case constant.String:
typ = sc.getType("string")
case constant.Bool:
typ = sc.getType("bool")
case constant.Int:
typ = sc.getType("int")
switch t.cat {
case int32T:
typ = sc.getType("int32")
default:
typ = sc.getType("int")
}
case constant.Float:
typ = sc.getType("float64")
case constant.Complex: