@@ -518,6 +518,22 @@ object Parsers {
518
518
tree
519
519
}
520
520
521
+ def makePolyFunction (tparams : List [Tree ], body : Tree ,
522
+ kind : String , errorTree : => Tree ,
523
+ start : Offset , arrowOffset : Offset ): Tree =
524
+ atSpan(start, arrowOffset):
525
+ getFunction(body) match
526
+ case None =>
527
+ syntaxError(em " Implementation restriction: polymorphic function ${kind}s must have a value parameter " , arrowOffset)
528
+ errorTree
529
+ case Some (Function (_, _ : CapturesAndResult )) =>
530
+ // A function tree like this will be desugared
531
+ // into a capturing type in the typer.
532
+ syntaxError(em " Implementation restriction: polymorphic function types cannot wrap function types that have capture sets " , arrowOffset)
533
+ errorTree
534
+ case Some (f) =>
535
+ PolyFunction (tparams, body)
536
+
521
537
/* --------------- PLACEHOLDERS ------------------------------------------- */
522
538
523
539
/** The implicit parameters introduced by `_` in the current expression.
@@ -1539,11 +1555,6 @@ object Parsers {
1539
1555
private def getFunction (tree : Tree ): Option [Function ] = tree match {
1540
1556
case Parens (tree1) => getFunction(tree1)
1541
1557
case Block (Nil , tree1) => getFunction(tree1)
1542
- case Function (_, _ : CapturesAndResult ) =>
1543
- // A function tree like this will be desugared
1544
- // into a capturing type in the typer,
1545
- // so None is returned.
1546
- None
1547
1558
case t : Function => Some (t)
1548
1559
case _ => None
1549
1560
}
@@ -1757,13 +1768,7 @@ object Parsers {
1757
1768
else if in.token == ARROW || isPureArrow(nme.PUREARROW ) then
1758
1769
val arrowOffset = in.skipToken()
1759
1770
val body = toplevelTyp(nestedIntoOK(in.token))
1760
- atSpan(start, arrowOffset):
1761
- getFunction(body) match
1762
- case Some (f) =>
1763
- PolyFunction (tparams, body)
1764
- case None =>
1765
- syntaxError(em " Implementation restriction: polymorphic function types must have a value parameter " , arrowOffset)
1766
- Ident (nme.ERROR .toTypeName)
1771
+ makePolyFunction(tparams, body, " type" , Ident (nme.ERROR .toTypeName), start, arrowOffset)
1767
1772
else
1768
1773
accept(TLARROW )
1769
1774
typ()
@@ -2360,14 +2365,7 @@ object Parsers {
2360
2365
val tparams = typeParamClause(ParamOwner .Type )
2361
2366
val arrowOffset = accept(ARROW )
2362
2367
val body = expr(location)
2363
- atSpan(start, arrowOffset) {
2364
- getFunction(body) match
2365
- case Some (f) =>
2366
- PolyFunction (tparams, f)
2367
- case None =>
2368
- syntaxError(em " Implementation restriction: polymorphic function literals must have a value parameter " , arrowOffset)
2369
- errorTermTree(arrowOffset)
2370
- }
2368
+ makePolyFunction(tparams, body, " literal" , errorTermTree(arrowOffset), start, arrowOffset)
2371
2369
case _ =>
2372
2370
val saved = placeholderParams
2373
2371
placeholderParams = Nil
0 commit comments