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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user