mirror of
https://github.com/golang/go.git
synced 2024-09-22 02:48:50 +00:00
go/types, types2: factor out hasDots to check for ... arguments in calls (cleanup)
This further reduces the differences between go/types and types2. Change-Id: Ie651c13dd12ecf043b8be92655d48d1ce32d4c5d Reviewed-on: https://go-review.googlesource.com/c/go/+/562777 Reviewed-by: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Robert Griesemer <gri@google.com> Run-TryBot: Robert Griesemer <gri@google.com> Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
parent
7731fd9cd3
commit
a56834922f
@ -22,7 +22,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
|
|||||||
|
|
||||||
// append is the only built-in that permits the use of ... for the last argument
|
// append is the only built-in that permits the use of ... for the last argument
|
||||||
bin := predeclaredFuncs[id]
|
bin := predeclaredFuncs[id]
|
||||||
if call.HasDots && id != _Append {
|
if hasDots(call) && id != _Append {
|
||||||
//check.errorf(call.Ellipsis, invalidOp + "invalid use of ... with built-in %s", bin.name)
|
//check.errorf(call.Ellipsis, invalidOp + "invalid use of ... with built-in %s", bin.name)
|
||||||
check.errorf(call,
|
check.errorf(call,
|
||||||
InvalidDotDotDot,
|
InvalidDotDotDot,
|
||||||
@ -114,7 +114,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
|
|||||||
// spec: "As a special case, append also accepts a first argument assignable
|
// spec: "As a special case, append also accepts a first argument assignable
|
||||||
// to type []byte with a second argument of string type followed by ... .
|
// to type []byte with a second argument of string type followed by ... .
|
||||||
// This form appends the bytes of the string.
|
// This form appends the bytes of the string.
|
||||||
if nargs == 2 && call.HasDots {
|
if nargs == 2 && hasDots(call) {
|
||||||
if ok, _ := x.assignableTo(check, NewSlice(universeByte), nil); ok {
|
if ok, _ := x.assignableTo(check, NewSlice(universeByte), nil); ok {
|
||||||
y := args[1]
|
y := args[1]
|
||||||
if t := coreString(y.typ); t != nil && isString(t) {
|
if t := coreString(y.typ); t != nil && isString(t) {
|
||||||
|
@ -209,7 +209,7 @@ func (check *Checker) callExpr(x *operand, call *syntax.CallExpr) exprKind {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if call.HasDots {
|
if hasDots(call) {
|
||||||
check.errorf(call.ArgList[0], BadDotDotDotSyntax, "invalid use of ... in conversion to %s", T)
|
check.errorf(call.ArgList[0], BadDotDotDotSyntax, "invalid use of ... in conversion to %s", T)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -468,7 +468,7 @@ func (check *Checker) arguments(call *syntax.CallExpr, sig *Signature, targs []T
|
|||||||
|
|
||||||
nargs := len(args)
|
nargs := len(args)
|
||||||
npars := sig.params.Len()
|
npars := sig.params.Len()
|
||||||
ddd := call.HasDots
|
ddd := hasDots(call)
|
||||||
|
|
||||||
// set up parameters
|
// set up parameters
|
||||||
sigParams := sig.params // adjusted for variadic functions (may be nil for empty parameter lists!)
|
sigParams := sig.params // adjusted for variadic functions (may be nil for empty parameter lists!)
|
||||||
|
@ -20,3 +20,6 @@ import "cmd/compile/internal/syntax"
|
|||||||
// If p and q are in different files, p is before q if the filename
|
// If p and q are in different files, p is before q if the filename
|
||||||
// of p sorts lexicographically before the filename of q.
|
// of p sorts lexicographically before the filename of q.
|
||||||
func cmpPos(p, q syntax.Pos) int { return p.Cmp(q) }
|
func cmpPos(p, q syntax.Pos) int { return p.Cmp(q) }
|
||||||
|
|
||||||
|
// hasDots reports whether the last argument in the call is followed by ...
|
||||||
|
func hasDots(call *syntax.CallExpr) bool { return call.HasDots }
|
||||||
|
@ -22,7 +22,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
|
|||||||
|
|
||||||
// append is the only built-in that permits the use of ... for the last argument
|
// append is the only built-in that permits the use of ... for the last argument
|
||||||
bin := predeclaredFuncs[id]
|
bin := predeclaredFuncs[id]
|
||||||
if call.Ellipsis.IsValid() && id != _Append {
|
if hasDots(call) && id != _Append {
|
||||||
check.errorf(atPos(call.Ellipsis),
|
check.errorf(atPos(call.Ellipsis),
|
||||||
InvalidDotDotDot,
|
InvalidDotDotDot,
|
||||||
invalidOp+"invalid use of ... with built-in %s", bin.name)
|
invalidOp+"invalid use of ... with built-in %s", bin.name)
|
||||||
@ -113,7 +113,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
|
|||||||
// spec: "As a special case, append also accepts a first argument assignable
|
// spec: "As a special case, append also accepts a first argument assignable
|
||||||
// to type []byte with a second argument of string type followed by ... .
|
// to type []byte with a second argument of string type followed by ... .
|
||||||
// This form appends the bytes of the string.
|
// This form appends the bytes of the string.
|
||||||
if nargs == 2 && call.Ellipsis.IsValid() {
|
if nargs == 2 && hasDots(call) {
|
||||||
if ok, _ := x.assignableTo(check, NewSlice(universeByte), nil); ok {
|
if ok, _ := x.assignableTo(check, NewSlice(universeByte), nil); ok {
|
||||||
y := args[1]
|
y := args[1]
|
||||||
if t := coreString(y.typ); t != nil && isString(t) {
|
if t := coreString(y.typ); t != nil && isString(t) {
|
||||||
|
@ -206,7 +206,7 @@ func (check *Checker) callExpr(x *operand, call *ast.CallExpr) exprKind {
|
|||||||
case 1:
|
case 1:
|
||||||
check.expr(nil, x, call.Args[0])
|
check.expr(nil, x, call.Args[0])
|
||||||
if x.mode != invalid {
|
if x.mode != invalid {
|
||||||
if call.Ellipsis.IsValid() {
|
if hasDots(call) {
|
||||||
check.errorf(call.Args[0], BadDotDotDotSyntax, "invalid use of ... in conversion to %s", T)
|
check.errorf(call.Args[0], BadDotDotDotSyntax, "invalid use of ... in conversion to %s", T)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -471,7 +471,7 @@ func (check *Checker) arguments(call *ast.CallExpr, sig *Signature, targs []Type
|
|||||||
|
|
||||||
nargs := len(args)
|
nargs := len(args)
|
||||||
npars := sig.params.Len()
|
npars := sig.params.Len()
|
||||||
ddd := call.Ellipsis.IsValid()
|
ddd := hasDots(call)
|
||||||
|
|
||||||
// set up parameters
|
// set up parameters
|
||||||
sigParams := sig.params // adjusted for variadic functions (may be nil for empty parameter lists!)
|
sigParams := sig.params // adjusted for variadic functions (may be nil for empty parameter lists!)
|
||||||
|
@ -105,7 +105,7 @@ func WriteExpr(buf *bytes.Buffer, x ast.Expr) {
|
|||||||
WriteExpr(buf, x.Fun)
|
WriteExpr(buf, x.Fun)
|
||||||
buf.WriteByte('(')
|
buf.WriteByte('(')
|
||||||
writeExprList(buf, x.Args)
|
writeExprList(buf, x.Args)
|
||||||
if x.Ellipsis.IsValid() {
|
if hasDots(x) {
|
||||||
buf.WriteString("...")
|
buf.WriteString("...")
|
||||||
}
|
}
|
||||||
buf.WriteByte(')')
|
buf.WriteByte(')')
|
||||||
|
@ -9,7 +9,10 @@
|
|||||||
|
|
||||||
package types
|
package types
|
||||||
|
|
||||||
import "go/token"
|
import (
|
||||||
|
"go/ast"
|
||||||
|
"go/token"
|
||||||
|
)
|
||||||
|
|
||||||
// cmpPos compares the positions p and q and returns a result r as follows:
|
// cmpPos compares the positions p and q and returns a result r as follows:
|
||||||
//
|
//
|
||||||
@ -20,3 +23,6 @@ import "go/token"
|
|||||||
// If p and q are in different files, p is before q if the filename
|
// If p and q are in different files, p is before q if the filename
|
||||||
// of p sorts lexicographically before the filename of q.
|
// of p sorts lexicographically before the filename of q.
|
||||||
func cmpPos(p, q token.Pos) int { return int(p - q) }
|
func cmpPos(p, q token.Pos) int { return int(p - q) }
|
||||||
|
|
||||||
|
// hasDots reports whether the last argument in the call is followed by ...
|
||||||
|
func hasDots(call *ast.CallExpr) bool { return call.Ellipsis.IsValid() }
|
||||||
|
Loading…
Reference in New Issue
Block a user