cmd/compile: refactoring mixing untyped type logic

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>
This commit is contained in:
Cuong Manh Le 2020-09-18 13:57:04 +07:00
parent df73945fd2
commit 06f7e655d1
2 changed files with 10 additions and 18 deletions

View File

@ -1072,12 +1072,7 @@ func defaultlit2(l *Node, r *Node, force bool) (*Node, *Node) {
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
@ -1102,6 +1097,14 @@ func ctype(t *types.Type) Ctype {
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

View File

@ -717,18 +717,7 @@ func typecheck1(n *Node, top int) (res *Node) {
}
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)