Skip to content

Fix #6725: Parsing of poly function with parenthesized body #6726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,9 @@ object desugar {
}

def makePolyFunction(targs: List[Tree], body: Tree): Tree = body match {
case Function(vargs, res) =>
case Parens(body1) =>
makePolyFunction(targs, body1)
case Function(vargs, res) =>
// TODO: Figure out if we need a `PolyFunctionWithMods` instead.
val mods = body match {
case body: FunctionWithMods => body.mods
Expand Down
26 changes: 16 additions & 10 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,12 @@ object Parsers {
*/
def toplevelTyp(): Tree = rejectWildcardType(typ())

private def isFunction(tree: Tree): Boolean = tree match {
case Parens(tree1) => isFunction(tree1)
case _: Function => true
case _ => false
}

/** Type ::= FunType
* | HkTypeParamClause ‘=>>’ Type
* | MatchType
Expand Down Expand Up @@ -946,11 +952,11 @@ object Parsers {
val arrowOffset = in.skipToken()
val body = toplevelTyp()
atSpan(start, arrowOffset) {
body match {
case _: Function => PolyFunction(tparams, body)
case _ =>
syntaxError("Implementation restriction: polymorphic function types must have a value parameter", arrowOffset)
Ident(nme.ERROR.toTypeName)
if (isFunction(body))
PolyFunction(tparams, body)
else {
syntaxError("Implementation restriction: polymorphic function types must have a value parameter", arrowOffset)
Ident(nme.ERROR.toTypeName)
}
}
} else { accept(TLARROW); typ() }
Expand Down Expand Up @@ -1381,11 +1387,11 @@ object Parsers {
val arrowOffset = accept(ARROW)
val body = expr()
atSpan(start, arrowOffset) {
body match {
case _: Function => PolyFunction(tparams, body)
case _ =>
syntaxError("Implementation restriction: polymorphic function literals must have a value parameter", arrowOffset)
errorTermTree
if (isFunction(body))
PolyFunction(tparams, body)
else {
syntaxError("Implementation restriction: polymorphic function literals must have a value parameter", arrowOffset)
errorTermTree
}
}
case _ =>
Expand Down
4 changes: 4 additions & 0 deletions tests/run/polymorphic-functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,8 @@ object Test extends App {
}
val s = [T] => (t: T) => given (st: Show[T]) => st.show(t)
assert(s(23) == "23")

// Parens handling
val tt1: [T] => (T => T) = [T] => (x: T) => x
val tt2: [T] => T => T = [T] => ((x: T) => x)
}