defaultlit2 and typecheck use the same logic for getting mixing untyped
type, so move that logic to a function.
This is a followup of CL 255217.
Passes toolstash-check.
Change-Id: Ic0eadb7ed27a2f0f72e2d28fd5438500bf4c79e0
Reviewed-on: https://go-review.googlesource.com/c/go/+/255897
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
return l, r
}
- nn := l
- if ctype(r.Type) > ctype(l.Type) {
- nn = r
- }
-
- t := defaultType(nn.Type)
+ t := defaultType(mixUntyped(l.Type, r.Type))
l = convlit(l, t)
r = convlit(r, t)
return l, r
panic("unreachable")
}
+func mixUntyped(t1, t2 *types.Type) *types.Type {
+ t := t1
+ if ctype(t2) > ctype(t1) {
+ t = t2
+ }
+ return t
+}
+
func defaultType(t *types.Type) *types.Type {
if !t.IsUntyped() || t.Etype == TNIL {
return t
}
if t.Etype == TIDEAL {
- switch {
- case l.Type == types.Idealcomplex || r.Type == types.Idealcomplex:
- t = types.Idealcomplex
- case l.Type == types.Idealfloat || r.Type == types.Idealfloat:
- t = types.Idealfloat
- case l.Type == types.Idealrune || r.Type == types.Idealrune:
- t = types.Idealrune
- case l.Type == types.Idealint || r.Type == types.Idealint:
- t = types.Idealint
- default:
- Fatalf("bad untyped type: %v", t)
- }
+ t = mixUntyped(l.Type, r.Type)
}
if dt := defaultType(t); !okfor[op][dt.Etype] {
yyerror("invalid operation: %v (operator %v not defined on %v)", n, op, t)