@@ -847,7 +847,7 @@ func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
847
847
colonPos = p .pos
848
848
p .next ()
849
849
} else {
850
- if p .tok != token .RPAREN {
850
+ if p .tok != token .RPAREN && p . tok != token . RBRACK {
851
851
p .expect (token .COLON )
852
852
}
853
853
}
@@ -941,6 +941,33 @@ func (p *parser) parseResult(scope *ast.Scope) *ast.FieldList {
941
941
return nil
942
942
}
943
943
944
+ func (p * parser ) parseTypeParams (scope * ast.Scope ) (tparams * ast.FieldList ) {
945
+ if p .trace {
946
+ defer un (trace (p , "TypeParams" ))
947
+ }
948
+
949
+ // 数组和切片歧义, 必须加 ':' 分隔
950
+ // type byteReplacer :[256]byte
951
+ // type byteReplacer :[]byte
952
+
953
+ if p .tok != token .LBRACK {
954
+ return nil
955
+ }
956
+
957
+ lparen := p .expect (token .LBRACK )
958
+
959
+ if p .tok == token .RBRACK {
960
+ p .next ()
961
+ return nil
962
+ }
963
+
964
+ params := p .parseParameterList (scope , false )
965
+ rparen := p .expect (token .RBRACK )
966
+
967
+ tparams = & ast.FieldList {Opening : lparen , List : params , Closing : rparen }
968
+ return
969
+ }
970
+
944
971
func (p * parser ) parseSignature (scope * ast.Scope ) (params , results * ast.FieldList , arrowPos token.Pos ) {
945
972
if p .trace {
946
973
defer un (trace (p , "Signature" ))
@@ -986,13 +1013,16 @@ func (p *parser) parseFuncType() (*ast.FuncType, *ast.Scope) {
986
1013
987
1014
pos := p .expect (token .FUNC )
988
1015
scope := ast .NewScope (p .topScope ) // function scope
1016
+
1017
+ tparams := p .parseTypeParams (scope )
989
1018
params , results , arrowPos := p .parseSignature (scope )
990
1019
991
1020
return & ast.FuncType {
992
- Func : pos ,
993
- Params : params ,
994
- ArrowPos : arrowPos ,
995
- Results : results ,
1021
+ Func : pos ,
1022
+ TypeParams : tparams ,
1023
+ Params : params ,
1024
+ ArrowPos : arrowPos ,
1025
+ Results : results ,
996
1026
}, scope
997
1027
}
998
1028
@@ -2439,12 +2469,20 @@ func (p *parser) parseTypeSpec(doc *ast.CommentGroup, _ token.Token, _ int) ast.
2439
2469
// containing block.
2440
2470
// (Global identifiers are resolved in a separate phase after parsing.)
2441
2471
spec := & ast.TypeSpec {Doc : doc , Name : ident }
2472
+ spec .TypeParams = p .parseTypeParams (p .topScope )
2442
2473
p .declare (spec , nil , p .topScope , ast .Typ , ident )
2443
2474
if p .tok == token .COLON {
2444
2475
spec .ColonPos = p .pos
2445
2476
p .next ()
2446
2477
}
2447
2478
spec .Type = p .parseType ()
2479
+
2480
+ if _ , ok := spec .Type .(* ast.StructType ); ! ok {
2481
+ if spec .TypeParams != nil {
2482
+ p .error (spec .TypeParams .Opening , "type params only support struct type" )
2483
+ }
2484
+ }
2485
+
2448
2486
p .expectSemi () // call before accessing p.linecomment
2449
2487
spec .Comment = p .lineComment
2450
2488
@@ -2523,6 +2561,7 @@ func (p *parser) parseFuncDecl() *ast.FuncDecl {
2523
2561
}
2524
2562
}
2525
2563
2564
+ tparams := p .parseTypeParams (scope )
2526
2565
params , results , arrowPos := p .parseSignature (scope )
2527
2566
2528
2567
var body * ast.BlockStmt
@@ -2536,10 +2575,11 @@ func (p *parser) parseFuncDecl() *ast.FuncDecl {
2536
2575
Recv : recv ,
2537
2576
Name : ident ,
2538
2577
Type : & ast.FuncType {
2539
- Func : pos ,
2540
- Params : params ,
2541
- ArrowPos : arrowPos ,
2542
- Results : results ,
2578
+ Func : pos ,
2579
+ TypeParams : tparams ,
2580
+ Params : params ,
2581
+ ArrowPos : arrowPos ,
2582
+ Results : results ,
2543
2583
},
2544
2584
Body : body ,
2545
2585
}
0 commit comments