Skip to content
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

Better error message for polytypes wrapping capturing types #21843

Merged
merged 1 commit into from
Oct 27, 2024
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
38 changes: 18 additions & 20 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,22 @@ object Parsers {
tree
}

def makePolyFunction(tparams: List[Tree], body: Tree,
kind: String, errorTree: => Tree,
start: Offset, arrowOffset: Offset): Tree =
atSpan(start, arrowOffset):
getFunction(body) match
case None =>
syntaxError(em"Implementation restriction: polymorphic function ${kind}s must have a value parameter", arrowOffset)
errorTree
Comment on lines +526 to +528
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add a test for this case too

case Some(Function(_, _: CapturesAndResult)) =>
// A function tree like this will be desugared
// into a capturing type in the typer.
syntaxError(em"Implementation restriction: polymorphic function types cannot wrap function types that have capture sets", arrowOffset)
errorTree
case Some(f) =>
PolyFunction(tparams, body)

/* --------------- PLACEHOLDERS ------------------------------------------- */

/** The implicit parameters introduced by `_` in the current expression.
Expand Down Expand Up @@ -1539,11 +1555,6 @@ object Parsers {
private def getFunction(tree: Tree): Option[Function] = tree match {
case Parens(tree1) => getFunction(tree1)
case Block(Nil, tree1) => getFunction(tree1)
case Function(_, _: CapturesAndResult) =>
// A function tree like this will be desugared
// into a capturing type in the typer,
// so None is returned.
None
case t: Function => Some(t)
case _ => None
}
Expand Down Expand Up @@ -1757,13 +1768,7 @@ object Parsers {
else if in.token == ARROW || isPureArrow(nme.PUREARROW) then
val arrowOffset = in.skipToken()
val body = toplevelTyp(nestedIntoOK(in.token))
atSpan(start, arrowOffset):
getFunction(body) match
case Some(f) =>
PolyFunction(tparams, body)
case None =>
syntaxError(em"Implementation restriction: polymorphic function types must have a value parameter", arrowOffset)
Ident(nme.ERROR.toTypeName)
makePolyFunction(tparams, body, "type", Ident(nme.ERROR.toTypeName), start, arrowOffset)
else
accept(TLARROW)
typ()
Expand Down Expand Up @@ -2360,14 +2365,7 @@ object Parsers {
val tparams = typeParamClause(ParamOwner.Type)
val arrowOffset = accept(ARROW)
val body = expr(location)
atSpan(start, arrowOffset) {
getFunction(body) match
case Some(f) =>
PolyFunction(tparams, f)
case None =>
syntaxError(em"Implementation restriction: polymorphic function literals must have a value parameter", arrowOffset)
errorTermTree(arrowOffset)
}
makePolyFunction(tparams, body, "literal", errorTermTree(arrowOffset), start, arrowOffset)
case _ =>
val saved = placeholderParams
placeholderParams = Nil
Expand Down
8 changes: 8 additions & 0 deletions tests/neg-custom-args/captures/polyCaptures.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Error: tests/neg-custom-args/captures/polyCaptures.scala:4:22 -------------------------------------------------------
4 |val runOpsCheck: [C^] -> (ops: List[() ->{C^} Unit]) ->{C^} Unit = runOps // error
| ^
| Implementation restriction: polymorphic function types cannot wrap function types that have capture sets
-- Error: tests/neg-custom-args/captures/polyCaptures.scala:5:23 -------------------------------------------------------
5 |val runOpsCheck2: [C^] => (ops: List[() ->{C^} Unit]) ->{C^} Unit = runOps // error
| ^
| Implementation restriction: polymorphic function types cannot wrap function types that have capture sets
7 changes: 7 additions & 0 deletions tests/neg-custom-args/captures/polyCaptures.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Box[X](val elem: X)

val runOps = [C^] => (b: Box[() ->{C^} Unit]) => b.elem()
val runOpsCheck: [C^] -> (ops: List[() ->{C^} Unit]) ->{C^} Unit = runOps // error
val runOpsCheck2: [C^] => (ops: List[() ->{C^} Unit]) ->{C^} Unit = runOps // error


Loading