From: Robert Griesemer Date: Thu, 9 Sep 2021 22:43:19 +0000 (-0700) Subject: cmd/compile/internal/syntax: correct follow token for type parameter lists X-Git-Tag: go1.18beta1~1386 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=da1aa650536b188c4dce287003a6f46b0dc4bdd5;p=gostls13.git cmd/compile/internal/syntax: correct follow token for type parameter lists When parsing a type parameter declaration, parts of the code still expected a ) as closing token. Use the correct follow token ) or ] depending on parameter list kind. Also, consistently use tokstring (not tok.String()) for user-facing (error) messages. Follow-up on comment in CL 348730. For #43527. Change-Id: Ib1d4feb526771a1668a54c3bb7a671f6c8a65940 Reviewed-on: https://go-review.googlesource.com/c/go/+/348742 Trust: Robert Griesemer Run-TryBot: Robert Griesemer TryBot-Result: Go Bot Reviewed-by: Robert Findley --- diff --git a/src/cmd/compile/internal/syntax/parser.go b/src/cmd/compile/internal/syntax/parser.go index c836a21c2f..82cb06b180 100644 --- a/src/cmd/compile/internal/syntax/parser.go +++ b/src/cmd/compile/internal/syntax/parser.go @@ -276,7 +276,9 @@ func (p *parser) syntaxErrorAt(pos Pos, msg string) { } // tokstring returns the English word for selected punctuation tokens -// for more readable error messages. +// for more readable error messages. Use tokstring (not tok.String()) +// for user-facing (error) messages; use tok.String() for debugging +// output. func tokstring(tok token) string { switch tok { case _Comma: @@ -1839,7 +1841,7 @@ func (p *parser) embeddedTerm() Expr { } // ParameterDecl = [ IdentifierList ] [ "..." ] Type . -func (p *parser) paramDeclOrNil(name *Name) *Field { +func (p *parser) paramDeclOrNil(name *Name, follow token) *Field { if trace { defer p.trace("paramDecl")() } @@ -1893,8 +1895,8 @@ func (p *parser) paramDeclOrNil(name *Name) *Field { return f } - p.syntaxError("expecting )") - p.advance(_Comma, _Rparen) + p.syntaxError("expecting " + tokstring(follow)) + p.advance(_Comma, follow) return nil } @@ -1911,7 +1913,7 @@ func (p *parser) paramList(name *Name, close token, requireNames bool) (list []* var named int // number of parameters that have an explicit name and type var typed int // number of parameters that have an explicit type end := p.list(_Comma, close, func() bool { - par := p.paramDeclOrNil(name) + par := p.paramDeclOrNil(name, close) name = nil // 1st name was consumed if present if par != nil { if debug && par.Name == nil && par.Type == nil { @@ -2211,7 +2213,7 @@ func (p *parser) header(keyword token) (init SimpleStmt, cond Expr, post SimpleS if p.tok != _Semi { // accept potential varDecl but complain if p.got(_Var) { - p.syntaxError(fmt.Sprintf("var declaration not allowed in %s initializer", keyword.String())) + p.syntaxError(fmt.Sprintf("var declaration not allowed in %s initializer", tokstring(keyword))) } init = p.simpleStmt(nil, keyword) // If we have a range clause, we are done (can only happen for keyword == _For). diff --git a/src/cmd/compile/internal/syntax/testdata/tparams.go2 b/src/cmd/compile/internal/syntax/testdata/tparams.go2 index 8e47ff5ed8..80e155bfe0 100644 --- a/src/cmd/compile/internal/syntax/testdata/tparams.go2 +++ b/src/cmd/compile/internal/syntax/testdata/tparams.go2 @@ -20,3 +20,5 @@ type t interface { func f[ /* ERROR empty type parameter list */ ]() func f[a, b /* ERROR missing type constraint */ ]() func f[a t, b t, c /* ERROR missing type constraint */ ]() + +func f[a b, /* ERROR expecting ] */ 0] ()