Skip to content

Commit e52afc7

Browse files
authored
Merge pull request #15744 from dotty-staging/fix-15741
Implementation restriction: No partial functions with CFT results
2 parents ead0fbc + 7996022 commit e52afc7

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

compiler/src/dotty/tools/dotc/transform/ExpandSAMs.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ class ExpandSAMs extends MiniPhase:
6666
tree
6767
}
6868

69+
private def checkNoContextFunction(tpt: Tree)(using Context): Unit =
70+
if defn.isContextFunctionType(tpt.tpe) then
71+
report.error(
72+
em"""Implementation restriction: cannot convert this expression to
73+
|partial function with context function result type $tpt""",
74+
tpt.srcPos)
75+
6976
/** A partial function literal:
7077
*
7178
* ```
@@ -108,6 +115,8 @@ class ExpandSAMs extends MiniPhase:
108115
private def toPartialFunction(tree: Block, tpe: Type)(using Context): Tree = {
109116
val closureDef(anon @ DefDef(_, List(List(param)), _, _)) = tree: @unchecked
110117

118+
checkNoContextFunction(anon.tpt)
119+
111120
// The right hand side from which to construct the partial function. This is always a Match.
112121
// If the original rhs is already a Match (possibly in braces), return that.
113122
// Otherwise construct a match `x match case _ => rhs` where `x` is the parameter of the closure.

tests/neg/i15741.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def get(using Int): String = summon[Int].toString
2+
3+
def pf2: PartialFunction[String, Int ?=> String] = {
4+
case "hoge" => get // error
5+
case "huga" => get
6+
}
7+
8+
type IS = Int ?=> String
9+
10+
def pf3: PartialFunction[String, IS] = {
11+
case "hoge" => get // error
12+
case "huga" => get
13+
}
14+
15+

0 commit comments

Comments
 (0)