Skip to content

Commit cafb49a

Browse files
cia-ranaRobert Griesemer
authored and
Robert Griesemer
committed
go/parser: allow trailing commas in embedded instantiated types
go/parser can correctly parse interfaces that instantiate and embed generic interfaces, but not structs. This is because in the case of structs, it does not expect RBRACK as a token trailing COMMA in the type argument, even though it is allowed by the spec. For example, go/parser produces an error for the type declaration below: type A struct { B[byte, []byte,] } Fixes #56748 Change-Id: Ibb2addd6cf9b381d8470a6d20eedb93f13f93cd6 Reviewed-on: https://go-review.googlesource.com/c/go/+/450175 Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent f3ae7ac commit cafb49a

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/go/parser/parser.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,15 +587,19 @@ func (p *parser) parseArrayFieldOrTypeInstance(x *ast.Ident) (*ast.Ident, ast.Ex
587587
defer un(trace(p, "ArrayFieldOrTypeInstance"))
588588
}
589589

590-
// TODO(gri) Should we allow a trailing comma in a type argument
591-
// list such as T[P,]? (We do in parseTypeInstance).
592590
lbrack := p.expect(token.LBRACK)
591+
trailingComma := token.NoPos // if valid, the position of a trailing comma preceding the ']'
593592
var args []ast.Expr
594593
if p.tok != token.RBRACK {
595594
p.exprLev++
596595
args = append(args, p.parseRhs())
597596
for p.tok == token.COMMA {
597+
comma := p.pos
598598
p.next()
599+
if p.tok == token.RBRACK {
600+
trailingComma = comma
601+
break
602+
}
599603
args = append(args, p.parseRhs())
600604
}
601605
p.exprLev--
@@ -613,6 +617,10 @@ func (p *parser) parseArrayFieldOrTypeInstance(x *ast.Ident) (*ast.Ident, ast.Ex
613617
elt := p.tryIdentOrType()
614618
if elt != nil {
615619
// x [P]E
620+
if trailingComma.IsValid() {
621+
// Trailing commas are invalid in array type fields.
622+
p.error(trailingComma, "unexpected comma; expecting ]")
623+
}
616624
return x, &ast.ArrayType{Lbrack: lbrack, Len: args[0], Elt: elt}
617625
}
618626
}

src/go/parser/testdata/tparams.go2

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@ type _[a t, b t, c /* ERROR "type parameters must be named" */ ] struct{}
99
type _ struct {
1010
t [n]byte
1111
t[a]
12+
t[a,]
1213
t[a, b]
14+
t[a, b,]
15+
}
16+
type _ struct {
17+
t [n, /* ERROR "unexpected comma; expecting ]" */ ]byte
1318
}
1419
type _ interface {
1520
t[a]
21+
t[a,]
1622
m[ /* ERROR "method must have no type parameters" */ _ _, /* ERROR mixed */ _]()
1723
t[a, b]
24+
t[a, b,]
1825
}
1926

2027
func _[] /* ERROR "empty type parameter list" */ ()

0 commit comments

Comments
 (0)