interp: fix default type for constants from runtime

The default type must be derived from the constant value when necessary,
otherwise the type check fails wrongly.

Fixes #1026.
This commit is contained in:
Marc Vertes
2021-02-03 11:48:03 +01:00
committed by GitHub
parent b9b0897d95
commit 3f4e1665b1
3 changed files with 31 additions and 6 deletions

View File

@@ -1502,10 +1502,25 @@ func (t *itype) implements(it *itype) bool {
}
// defaultType returns the default type of an untyped type.
func (t *itype) defaultType() *itype {
func (t *itype) defaultType(v reflect.Value) *itype {
if !t.untyped {
return t
}
// The default type can also be derived from a constant value.
if v.IsValid() && t.TypeOf().Implements(constVal) {
switch v.Interface().(constant.Value).Kind() {
case constant.String:
t = untypedString()
case constant.Bool:
t = untypedBool()
case constant.Int:
t = untypedInt()
case constant.Float:
t = untypedFloat()
case constant.Complex:
t = untypedComplex()
}
}
typ := *t
typ.untyped = false
return &typ