@@ -87,6 +87,8 @@ func (p *parser) init(file *PosBase, r io.Reader, errh ErrorHandler, pragh Pragm
87
87
p .indent = nil
88
88
}
89
89
90
+ func (p * parser ) allowGenerics () bool { return p .mode & AllowGenerics != 0 }
91
+
90
92
// takePragma returns the current parsed pragmas
91
93
// and clears them from the parser state.
92
94
func (p * parser ) takePragma () Pragma {
@@ -597,7 +599,7 @@ func (p *parser) typeDecl(group *Group) Decl {
597
599
p .xnest ++
598
600
x := p .expr ()
599
601
p .xnest --
600
- if name0 , ok := x .(* Name ); p .mode & AllowGenerics != 0 && ok && p .tok != _Rbrack {
602
+ if name0 , ok := x .(* Name ); p .allowGenerics () && ok && p .tok != _Rbrack {
601
603
// generic type
602
604
d .TParamList = p .paramList (name0 , _Rbrack , true )
603
605
pos := p .pos ()
@@ -687,7 +689,7 @@ func (p *parser) funcDeclOrNil() *FuncDecl {
687
689
}
688
690
689
691
f .Name = p .name ()
690
- if p .mode & AllowGenerics != 0 && p .got (_Lbrack ) {
692
+ if p .allowGenerics () && p .got (_Lbrack ) {
691
693
if p .tok == _Rbrack {
692
694
p .syntaxError ("empty type parameter list" )
693
695
p .next ()
@@ -1425,7 +1427,7 @@ func (p *parser) interfaceType() *InterfaceType {
1425
1427
switch p .tok {
1426
1428
case _Name :
1427
1429
f := p .methodDecl ()
1428
- if f .Name == nil && p .mode & AllowGenerics != 0 {
1430
+ if f .Name == nil && p .allowGenerics () {
1429
1431
f = p .embeddedElem (f )
1430
1432
}
1431
1433
typ .MethodList = append (typ .MethodList , f )
@@ -1443,14 +1445,14 @@ func (p *parser) interfaceType() *InterfaceType {
1443
1445
return false
1444
1446
1445
1447
case _Operator :
1446
- if p .op == Tilde && p .mode & AllowGenerics != 0 {
1448
+ if p .op == Tilde && p .allowGenerics () {
1447
1449
typ .MethodList = append (typ .MethodList , p .embeddedElem (nil ))
1448
1450
return false
1449
1451
}
1450
1452
1451
1453
case _Type :
1452
1454
// TODO(gri) remove TypeList syntax if we accept #45346
1453
- if p .mode & AllowGenerics != 0 && p .mode & AllowTypeLists != 0 {
1455
+ if p .allowGenerics () && p .mode & AllowTypeLists != 0 {
1454
1456
type_ := NewName (p .pos (), "type" ) // cannot have a method named "type"
1455
1457
p .next ()
1456
1458
if p .tok != _Semi && p .tok != _Rbrace {
@@ -1473,7 +1475,7 @@ func (p *parser) interfaceType() *InterfaceType {
1473
1475
}
1474
1476
1475
1477
default :
1476
- if p .mode & AllowGenerics != 0 {
1478
+ if p .allowGenerics () {
1477
1479
pos := p .pos ()
1478
1480
if t := p .typeOrNil (); t != nil {
1479
1481
f := new (Field )
@@ -1485,7 +1487,7 @@ func (p *parser) interfaceType() *InterfaceType {
1485
1487
}
1486
1488
}
1487
1489
1488
- if p .mode & AllowGenerics != 0 {
1490
+ if p .allowGenerics () {
1489
1491
if p .mode & AllowTypeLists != 0 {
1490
1492
p .syntaxError ("expecting method, type list, or embedded element" )
1491
1493
p .advance (_Semi , _Rbrace , _Type )
@@ -1570,7 +1572,7 @@ func (p *parser) fieldDecl(styp *StructType) {
1570
1572
1571
1573
// Careful dance: We don't know if we have an embedded instantiated
1572
1574
// type T[P1, P2, ...] or a field T of array/slice type [P]E or []E.
1573
- if p .mode & AllowGenerics != 0 && len (names ) == 1 && p .tok == _Lbrack {
1575
+ if p .allowGenerics () && len (names ) == 1 && p .tok == _Lbrack {
1574
1576
typ = p .arrayOrTArgs ()
1575
1577
if typ , ok := typ .(* IndexExpr ); ok {
1576
1578
// embedded type T[P1, P2, ...]
@@ -1708,7 +1710,7 @@ func (p *parser) methodDecl() *Field {
1708
1710
f .Type = p .funcType ()
1709
1711
1710
1712
case _Lbrack :
1711
- if p .mode & AllowGenerics != 0 {
1713
+ if p .allowGenerics () {
1712
1714
// Careful dance: We don't know if we have a generic method m[T C](x T)
1713
1715
// or an embedded instantiated type T[P1, P2] (we accept generic methods
1714
1716
// for generality and robustness of parsing).
@@ -1846,38 +1848,60 @@ func (p *parser) paramDeclOrNil(name *Name, follow token) *Field {
1846
1848
defer p .trace ("paramDecl" )()
1847
1849
}
1848
1850
1849
- f := new (Field )
1851
+ // type set notation is ok in type parameter lists
1852
+ typeSetsOk := p .mode & AllowTypeSets != 0 && follow == _Rbrack
1853
+
1854
+ pos := p .pos ()
1850
1855
if name != nil {
1851
- f .pos = name .pos
1852
- } else {
1853
- f .pos = p .pos ()
1856
+ pos = name .pos
1857
+ } else if typeSetsOk && p .tok == _Operator && p .op == Tilde {
1858
+ // "~" ...
1859
+ return p .embeddedElem (nil )
1854
1860
}
1855
1861
1862
+ f := new (Field )
1863
+ f .pos = pos
1864
+
1856
1865
if p .tok == _Name || name != nil {
1866
+ // name
1857
1867
if name == nil {
1858
1868
name = p .name ()
1859
1869
}
1860
1870
1861
- if p .mode & AllowGenerics != 0 && p .tok == _Lbrack {
1871
+ if p .allowGenerics () && p .tok == _Lbrack {
1872
+ // name "[" ...
1862
1873
f .Type = p .arrayOrTArgs ()
1863
1874
if typ , ok := f .Type .(* IndexExpr ); ok {
1875
+ // name "[" ... "]"
1864
1876
typ .X = name
1865
1877
} else {
1878
+ // name "[" n "]" E
1866
1879
f .Name = name
1867
1880
}
1868
1881
return f
1869
1882
}
1870
1883
1871
1884
if p .tok == _Dot {
1872
- // name_or_type
1885
+ // name "." ...
1873
1886
f .Type = p .qualifiedName (name )
1887
+ if typeSetsOk && p .tok == _Operator && p .op == Or {
1888
+ // name "." name "|" ...
1889
+ f = p .embeddedElem (f )
1890
+ }
1874
1891
return f
1875
1892
}
1876
1893
1894
+ if typeSetsOk && p .tok == _Operator && p .op == Or {
1895
+ // name "|" ...
1896
+ f .Type = name
1897
+ return p .embeddedElem (f )
1898
+ }
1899
+
1877
1900
f .Name = name
1878
1901
}
1879
1902
1880
1903
if p .tok == _DotDotDot {
1904
+ // [name] "..." ...
1881
1905
t := new (DotsType )
1882
1906
t .pos = p .pos ()
1883
1907
p .next ()
@@ -1890,7 +1914,17 @@ func (p *parser) paramDeclOrNil(name *Name, follow token) *Field {
1890
1914
return f
1891
1915
}
1892
1916
1917
+ if typeSetsOk && p .tok == _Operator && p .op == Tilde {
1918
+ // [name] "~" ...
1919
+ f .Type = p .embeddedElem (nil ).Type
1920
+ return f
1921
+ }
1922
+
1893
1923
f .Type = p .typeOrNil ()
1924
+ if typeSetsOk && p .tok == _Operator && p .op == Or && f .Type != nil {
1925
+ // [name] type "|"
1926
+ f = p .embeddedElem (f )
1927
+ }
1894
1928
if f .Name != nil || f .Type != nil {
1895
1929
return f
1896
1930
}
@@ -1952,7 +1986,7 @@ func (p *parser) paramList(name *Name, close token, requireNames bool) (list []*
1952
1986
if par .Type != nil {
1953
1987
typ = par .Type
1954
1988
if par .Name == nil {
1955
- pos = typ . Pos ( )
1989
+ pos = StartPos ( typ )
1956
1990
par .Name = NewName (pos , "_" )
1957
1991
}
1958
1992
} else if typ != nil {
@@ -2654,7 +2688,7 @@ func (p *parser) qualifiedName(name *Name) Expr {
2654
2688
x = s
2655
2689
}
2656
2690
2657
- if p .mode & AllowGenerics != 0 && p .tok == _Lbrack {
2691
+ if p .allowGenerics () && p .tok == _Lbrack {
2658
2692
x = p .typeInstance (x )
2659
2693
}
2660
2694
0 commit comments