p.xnest--
if name0, ok := x.(*Name); p.mode&AllowGenerics != 0 && ok && p.tok != _Rbrack {
// generic type
- d.TParamList = p.paramList(name0, _Rbrack)
+ d.TParamList = p.paramList(name0, _Rbrack, true)
pos := p.pos()
if p.gotAssign() {
p.syntaxErrorAt(pos, "generic type cannot be alias")
f.Pragma = p.takePragma()
if p.got(_Lparen) {
- rcvr := p.paramList(nil, _Rparen)
+ rcvr := p.paramList(nil, _Rparen, false)
switch len(rcvr) {
case 0:
p.error("method has no receiver")
p.syntaxError("empty type parameter list")
p.next()
} else {
- f.TParamList = p.paramList(nil, _Rbrack)
+ f.TParamList = p.paramList(nil, _Rbrack, true)
}
}
f.Type = p.funcType()
typ := new(FuncType)
typ.pos = p.pos()
p.want(_Lparen)
- typ.ParamList = p.paramList(nil, _Rparen)
+ typ.ParamList = p.paramList(nil, _Rparen, false)
typ.ResultList = p.funcResult()
return typ
}
if p.got(_Lparen) {
- return p.paramList(nil, _Rparen)
+ return p.paramList(nil, _Rparen, false)
}
pos := p.pos()
// A type argument list looks like a parameter list with only
// types. Parse a parameter list and decide afterwards.
- list := p.paramList(nil, _Rbrack)
+ list := p.paramList(nil, _Rbrack, false)
if len(list) == 0 {
// The type parameter list is not [] but we got nothing
// due to other errors (reported by paramList). Treat
// ParameterList = ParameterDecl { "," ParameterDecl } .
// "(" or "[" has already been consumed.
// If name != nil, it is the first name after "(" or "[".
-func (p *parser) paramList(name *Name, close token) (list []*Field) {
+// In the result list, either all fields have a name, or no field has a name.
+func (p *parser) paramList(name *Name, close token, requireNames bool) (list []*Field) {
if trace {
defer p.trace("paramList")()
}
return false
})
- // distribute parameter types
+ if len(list) == 0 {
+ return
+ }
+
+ // distribute parameter types (len(list) > 0)
if named == 0 {
// all unnamed => found names are named types
for _, par := range list {
par.Name = nil
}
}
+ if requireNames {
+ p.syntaxErrorAt(list[0].Type.Pos(), "type parameters must be named")
+ }
} else if named != len(list) {
// some named => all must have names and types
- var pos Pos // error position (or unknown)
+ var pos Pos // left-most error position (or unknown)
var typ Expr
for i := len(list) - 1; i >= 0; i-- {
if par := list[i]; par.Type != nil {
}
}
if pos.IsKnown() {
- p.syntaxErrorAt(pos, "mixed named and unnamed parameters")
+ var msg string
+ if requireNames {
+ msg = "type parameters must be named"
+ } else {
+ msg = "mixed named and unnamed parameters"
+ }
+ p.syntaxErrorAt(pos, msg)
}
}
--- /dev/null
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+type t[ /* ERROR type parameters must be named */ a, b] struct{}
+type t[a t, b t, /* ERROR type parameters must be named */ c] struct{}
+type t struct {
+ t [n]byte
+ t[a]
+ t[a, b]
+}
+type t interface {
+ t[a]
+ m /* ERROR method cannot have type parameters */ [_ _, /* ERROR mixed */ _]()
+ t[a, b]
+}
+
+func f[ /* ERROR empty type parameter list */ ]()
+func f[ /* ERROR type parameters must be named */ a, b]()
+func f[a t, b t, /* ERROR type parameters must be named */ c]()