Commit a6b6142f authored by Robert Griesemer's avatar Robert Griesemer

gofmt: support for ... after actual arguments

Pending acceptance of the proposed language change.

R=rsc
CC=golang-dev
https://golang.org/cl/2193048
parent 3487495e
......@@ -210,10 +210,11 @@ type (
// A CallExpr node represents an expression followed by an argument list.
CallExpr struct {
Fun Expr // function expression
Lparen token.Position // position of "("
Args []Expr // function arguments
Rparen token.Position // positions of ")"
Fun Expr // function expression
Lparen token.Position // position of "("
Args []Expr // function arguments
Ellipsis token.Position // position of "...", if any
Rparen token.Position // position of ")"
}
// A StarExpr node represents an expression of the form "*" Expression.
......
......@@ -942,8 +942,13 @@ func (p *parser) parseCallOrConversion(fun ast.Expr) *ast.CallExpr {
lparen := p.expect(token.LPAREN)
p.exprLev++
var list vector.Vector
for p.tok != token.RPAREN && p.tok != token.EOF {
var ellipsis token.Position
for p.tok != token.RPAREN && p.tok != token.EOF && !ellipsis.IsValid() {
list.Push(p.parseExpr())
if p.tok == token.ELLIPSIS {
ellipsis = p.pos
p.next()
}
if p.tok != token.COMMA {
break
}
......@@ -952,7 +957,7 @@ func (p *parser) parseCallOrConversion(fun ast.Expr) *ast.CallExpr {
p.exprLev--
rparen := p.expect(token.RPAREN)
return &ast.CallExpr{fun, lparen, makeExprList(&list), rparen}
return &ast.CallExpr{fun, lparen, makeExprList(&list), ellipsis, rparen}
}
......
......@@ -37,6 +37,7 @@ var validPrograms = []interface{}{
`package main; func f(func() func() func())` + "\n",
`package main; func f(...T)` + "\n",
`package main; func f(float, ...int)` + "\n",
`package main; func f(x int, a ...int) { f(0, a...); f(1, a...,) }` + "\n",
`package main; type T []int; var a []bool; func f() { if a[T{42}[0]] {} }` + "\n",
`package main; type T []int; func g(int) bool { return true }; func f() { if g(T{42}[0]) {} }` + "\n",
`package main; type T []int; func f() { for _ = range []int{T{42}[0]} {} }` + "\n",
......
......@@ -686,7 +686,7 @@ func splitSelector(expr ast.Expr) (body, suffix ast.Expr) {
case *ast.CallExpr:
body, suffix = splitSelector(x.Fun)
if body != nil {
suffix = &ast.CallExpr{suffix, x.Lparen, x.Args, x.Rparen}
suffix = &ast.CallExpr{suffix, x.Lparen, x.Args, x.Ellipsis, x.Rparen}
return
}
case *ast.IndexExpr:
......@@ -847,6 +847,9 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi
p.expr1(x.Fun, token.HighestPrec, depth, 0, multiLine)
p.print(x.Lparen, token.LPAREN)
p.exprList(x.Lparen, x.Args, depth, commaSep|commaTerm, multiLine, x.Rparen)
if x.Ellipsis.IsValid() {
p.print(x.Ellipsis, token.ELLIPSIS)
}
p.print(x.Rparen, token.RPAREN)
case *ast.CompositeLit:
......
......@@ -169,6 +169,13 @@ func _() {
}
func f(x int, args ...int) {
f(0, args...)
f(1, args)
f(2, args[0])
}
func _() {
_ = T{}
_ = struct{}{}
......
......@@ -169,6 +169,13 @@ func _() {
}
func f(x int, args ...int) {
f(0, args...)
f(1, args)
f(2, args[0])
}
func _() {
_ = T{}
_ = struct{}{}
......
......@@ -169,6 +169,13 @@ func _() {
}
func f(x int, args ...int) {
f(0, args...)
f(1, args)
f(2, args[0])
}
func _() {
_ = T{}
_ = struct{}{}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment