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

Implementation restriction: No partial functions with CFT results #15744

Merged
merged 2 commits into from
Aug 22, 2022
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
9 changes: 9 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/ExpandSAMs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ class ExpandSAMs extends MiniPhase:
tree
}

private def checkNoContextFunction(tpt: Tree)(using Context): Unit =
if defn.isContextFunctionType(tpt.tpe) then
report.error(
em"""Implementation restriction: cannot convert this expression to
|partial function with context function result type $tpt""",
tpt.srcPos)

/** A partial function literal:
*
* ```
Expand Down Expand Up @@ -108,6 +115,8 @@ class ExpandSAMs extends MiniPhase:
private def toPartialFunction(tree: Block, tpe: Type)(using Context): Tree = {
val closureDef(anon @ DefDef(_, List(List(param)), _, _)) = tree: @unchecked

checkNoContextFunction(anon.tpt)

// The right hand side from which to construct the partial function. This is always a Match.
// If the original rhs is already a Match (possibly in braces), return that.
// Otherwise construct a match `x match case _ => rhs` where `x` is the parameter of the closure.
Expand Down
15 changes: 15 additions & 0 deletions tests/neg/i15741.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def get(using Int): String = summon[Int].toString

def pf2: PartialFunction[String, Int ?=> String] = {
case "hoge" => get // error
case "huga" => get
}

type IS = Int ?=> String

def pf3: PartialFunction[String, IS] = {
case "hoge" => get // error
case "huga" => get
}