Skip to content

Commit 2ff1125

Browse files
committed
Fix #6725: Parsing of poly function with parenthesized body
1 parent b8ed0df commit 2ff1125

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,9 @@ object desugar {
14101410
}
14111411

14121412
def makePolyFunction(targs: List[Tree], body: Tree): Tree = body match {
1413-
case Function(vargs, res) =>
1413+
case Parens(body1) =>
1414+
makePolyFunction(targs, body1)
1415+
case Function(vargs, res) =>
14141416
// TODO: Figure out if we need a `PolyFunctionWithMods` instead.
14151417
val mods = body match {
14161418
case body: FunctionWithMods => body.mods

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,12 @@ object Parsers {
864864
*/
865865
def toplevelTyp(): Tree = rejectWildcardType(typ())
866866

867+
private def isFunction(tree: Tree): Boolean = tree match {
868+
case Parens(tree1) => isFunction(tree1)
869+
case _: Function => true
870+
case _ => false
871+
}
872+
867873
/** Type ::= FunType
868874
* | HkTypeParamClause ‘=>>’ Type
869875
* | MatchType
@@ -946,11 +952,11 @@ object Parsers {
946952
val arrowOffset = in.skipToken()
947953
val body = toplevelTyp()
948954
atSpan(start, arrowOffset) {
949-
body match {
950-
case _: Function => PolyFunction(tparams, body)
951-
case _ =>
952-
syntaxError("Implementation restriction: polymorphic function types must have a value parameter", arrowOffset)
953-
Ident(nme.ERROR.toTypeName)
955+
if (isFunction(body))
956+
PolyFunction(tparams, body)
957+
else {
958+
syntaxError("Implementation restriction: polymorphic function types must have a value parameter", arrowOffset)
959+
Ident(nme.ERROR.toTypeName)
954960
}
955961
}
956962
} else { accept(TLARROW); typ() }
@@ -1381,11 +1387,11 @@ object Parsers {
13811387
val arrowOffset = accept(ARROW)
13821388
val body = expr()
13831389
atSpan(start, arrowOffset) {
1384-
body match {
1385-
case _: Function => PolyFunction(tparams, body)
1386-
case _ =>
1387-
syntaxError("Implementation restriction: polymorphic function literals must have a value parameter", arrowOffset)
1388-
errorTermTree
1390+
if (isFunction(body))
1391+
PolyFunction(tparams, body)
1392+
else {
1393+
syntaxError("Implementation restriction: polymorphic function literals must have a value parameter", arrowOffset)
1394+
errorTermTree
13891395
}
13901396
}
13911397
case _ =>

tests/run/polymorphic-functions.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,8 @@ object Test extends App {
9090
}
9191
val s = [T] => (t: T) => given (st: Show[T]) => st.show(t)
9292
assert(s(23) == "23")
93+
94+
// Parens handling
95+
val tt1: [T] => (T => T) = [T] => (x: T) => x
96+
val tt2: [T] => T => T = [T] => ((x: T) => x)
9397
}

0 commit comments

Comments
 (0)