Skip to content

Commit

Permalink
go/types: factor out opPos computation
Browse files Browse the repository at this point in the history
Adjust Checker.overflow call sites to match types2
where possible.

Change-Id: Iaa0d423f2ebf642428c745c4ac4f712e4136dffb
Reviewed-on: https://go-review.googlesource.com/c/go/+/610956
Reviewed-by: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Tim King <taking@google.com>
  • Loading branch information
griesemer authored and gopherbot committed Sep 5, 2024
1 parent a17356c commit 0f12e51
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/go/types/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ func (check *Checker) op(m opPredicates, x *operand, op token.Token) bool {
return true
}

// opPos returns the position of the operator if x is an operation;
// otherwise it returns the start position of x.
func opPos(x ast.Expr) token.Pos {
switch op := x.(type) {
case nil:
return nopos // don't crash
case *ast.BinaryExpr:
return op.OpPos
default:
return x.Pos()
}
}

// opName returns the name of the operation if x is an operation
// that might overflow; otherwise it returns the empty string.
func opName(e ast.Expr) string {
Expand Down Expand Up @@ -196,7 +209,7 @@ func (check *Checker) unary(x *operand, e *ast.UnaryExpr) {
}
x.val = constant.UnaryOp(op, x.val, prec)
x.expr = e
check.overflow(x, x.Pos())
check.overflow(x, opPos(x.expr))
return
}

Expand Down Expand Up @@ -707,11 +720,7 @@ func (check *Checker) shift(x, y *operand, e ast.Expr, op token.Token) {
// x is a constant so xval != nil and it must be of Int kind.
x.val = constant.Shift(xval, op, uint(s))
x.expr = e
opPos := x.Pos()
if b, _ := e.(*ast.BinaryExpr); b != nil {
opPos = b.OpPos
}
check.overflow(x, opPos)
check.overflow(x, opPos(x.expr))
return
}

Expand Down Expand Up @@ -1098,7 +1107,8 @@ func (check *Checker) exprInternal(T *target, x *operand, e ast.Expr, hint Type)
goto Error
}
// Ensure that integer values don't overflow (go.dev/issue/54280).
check.overflow(x, e.Pos())
x.expr = e // make sure that check.overflow below has an error position
check.overflow(x, opPos(x.expr))

case *ast.FuncLit:
check.funcLit(x, e)
Expand Down

0 comments on commit 0f12e51

Please sign in to comment.