@@ -708,15 +708,7 @@ func (p *parser) funcDeclOrNil() *FuncDecl {
708
708
}
709
709
710
710
f .Name = p .name ()
711
- if p .allowGenerics () && p .got (_Lbrack ) {
712
- if p .tok == _Rbrack {
713
- p .syntaxError ("empty type parameter list" )
714
- p .next ()
715
- } else {
716
- f .TParamList = p .paramList (nil , _Rbrack , true )
717
- }
718
- }
719
- f .Type = p .funcType ()
711
+ f .TParamList , f .Type = p .funcType ("" )
720
712
if p .tok == _Lbrace {
721
713
f .Body = p .funcBody ()
722
714
}
@@ -944,7 +936,7 @@ func (p *parser) operand(keep_parens bool) Expr {
944
936
case _Func :
945
937
pos := p .pos ()
946
938
p .next ()
947
- ftyp := p .funcType ()
939
+ _ , ftyp := p .funcType ("function literal" )
948
940
if p .tok == _Lbrace {
949
941
p .xnest ++
950
942
@@ -1284,7 +1276,8 @@ func (p *parser) typeOrNil() Expr {
1284
1276
case _Func :
1285
1277
// fntype
1286
1278
p .next ()
1287
- return p .funcType ()
1279
+ _ , t := p .funcType ("function type" )
1280
+ return t
1288
1281
1289
1282
case _Lbrack :
1290
1283
// '[' oexpr ']' ntype
@@ -1357,18 +1350,34 @@ func (p *parser) typeInstance(typ Expr) Expr {
1357
1350
return x
1358
1351
}
1359
1352
1360
- func (p * parser ) funcType () * FuncType {
1353
+ // If context != "", type parameters are not permitted.
1354
+ func (p * parser ) funcType (context string ) ([]* Field , * FuncType ) {
1361
1355
if trace {
1362
1356
defer p .trace ("funcType" )()
1363
1357
}
1364
1358
1365
1359
typ := new (FuncType )
1366
1360
typ .pos = p .pos ()
1361
+
1362
+ var tparamList []* Field
1363
+ if p .allowGenerics () && p .got (_Lbrack ) {
1364
+ if context != "" {
1365
+ // accept but complain
1366
+ p .syntaxErrorAt (typ .pos , context + " cannot have type parameters" )
1367
+ }
1368
+ if p .tok == _Rbrack {
1369
+ p .syntaxError ("empty type parameter list" )
1370
+ p .next ()
1371
+ } else {
1372
+ tparamList = p .paramList (nil , _Rbrack , true )
1373
+ }
1374
+ }
1375
+
1367
1376
p .want (_Lparen )
1368
1377
typ .ParamList = p .paramList (nil , _Rparen , false )
1369
1378
typ .ResultList = p .funcResult ()
1370
1379
1371
- return typ
1380
+ return tparamList , typ
1372
1381
}
1373
1382
1374
1383
// "[" has already been consumed, and pos is its position.
@@ -1697,11 +1706,13 @@ func (p *parser) methodDecl() *Field {
1697
1706
// already progressed, no need to advance
1698
1707
}
1699
1708
1709
+ const context = "interface method"
1710
+
1700
1711
switch p .tok {
1701
1712
case _Lparen :
1702
1713
// method
1703
1714
f .Name = name
1704
- f .Type = p .funcType ()
1715
+ _ , f .Type = p .funcType (context )
1705
1716
1706
1717
case _Lbrack :
1707
1718
if p .allowGenerics () {
@@ -1721,7 +1732,7 @@ func (p *parser) methodDecl() *Field {
1721
1732
// name[](
1722
1733
p .errorAt (pos , "empty type parameter list" )
1723
1734
f .Name = name
1724
- f .Type = p .funcType ()
1735
+ _ , f .Type = p .funcType (context )
1725
1736
} else {
1726
1737
p .errorAt (pos , "empty type argument list" )
1727
1738
f .Type = name
@@ -1738,7 +1749,7 @@ func (p *parser) methodDecl() *Field {
1738
1749
// as if [] were absent.
1739
1750
if p .tok == _Lparen {
1740
1751
f .Name = name
1741
- f .Type = p .funcType ()
1752
+ _ , f .Type = p .funcType (context )
1742
1753
} else {
1743
1754
f .Type = name
1744
1755
}
@@ -1749,7 +1760,7 @@ func (p *parser) methodDecl() *Field {
1749
1760
if list [0 ].Name != nil {
1750
1761
// generic method
1751
1762
f .Name = name
1752
- f .Type = p .funcType ()
1763
+ _ , f .Type = p .funcType (context )
1753
1764
// TODO(gri) Record list as type parameter list with f.Type
1754
1765
// if we want to type-check the generic method.
1755
1766
// For now, report an error so this is not a silent event.
0 commit comments