1
0
mirror of https://github.com/golang/go synced 2024-11-23 16:20:04 -07:00

[dev.unified] cmd/compile/internal/noder: implicit conversions for binary exprs

Binary operations (except for shifts) require one operand to be
assignable to the other's type. In particular, for equality
comparisons, this can imply a conversion to interface type.

Change-Id: Ic973c8287a40fdaefcf11458378574fdcd243b17
Reviewed-on: https://go-review.googlesource.com/c/go/+/415577
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
This commit is contained in:
Matthew Dempsky 2022-06-30 19:16:23 -07:00
parent ebd34e3e45
commit c846fd8e13

View File

@ -1499,12 +1499,28 @@ func (w *writer) expr(expr syntax.Expr) {
break break
} }
// TODO(mdempsky): Implicit conversions to common type. var commonType types2.Type
switch expr.Op {
case syntax.Shl, syntax.Shr:
// ok: operands are allowed to have different types
default:
xtyp := w.p.typeOf(expr.X)
ytyp := w.p.typeOf(expr.Y)
switch {
case types2.AssignableTo(xtyp, ytyp):
commonType = ytyp
case types2.AssignableTo(ytyp, xtyp):
commonType = xtyp
default:
w.p.fatalf(expr, "failed to find common type between %v and %v", xtyp, ytyp)
}
}
w.Code(exprBinaryOp) w.Code(exprBinaryOp)
w.op(binOps[expr.Op]) w.op(binOps[expr.Op])
w.expr(expr.X) w.implicitConvExpr(expr, commonType, expr.X)
w.pos(expr) w.pos(expr)
w.expr(expr.Y) w.implicitConvExpr(expr, commonType, expr.Y)
case *syntax.CallExpr: case *syntax.CallExpr:
tv, ok := w.p.info.Types[expr.Fun] tv, ok := w.p.info.Types[expr.Fun]