Skip to content

Commit

Permalink
[dev.unified] cmd/compile/internal/noder: implicit conversions for bi…
Browse files Browse the repository at this point in the history
…nary 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>
  • Loading branch information
mdempsky committed Jul 19, 2022
1 parent ebd34e3 commit c846fd8
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/cmd/compile/internal/noder/writer.go
Expand Up @@ -1499,12 +1499,28 @@ func (w *writer) expr(expr syntax.Expr) {
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.op(binOps[expr.Op])
w.expr(expr.X)
w.implicitConvExpr(expr, commonType, expr.X)
w.pos(expr)
w.expr(expr.Y)
w.implicitConvExpr(expr, commonType, expr.Y)

case *syntax.CallExpr:
tv, ok := w.p.info.Types[expr.Fun]
Expand Down

0 comments on commit c846fd8

Please sign in to comment.