Skip to content

Commit 3e85eb7

Browse files
Allow contextual functions with erased parameters to be integrated (#17313)
## Fix #17147 - Allow contextual functions with erased parameters to be integrated <!-- TODO description of the change --> <!-- Ideally should have a called "Fix #XYZ: A SHORT FIX DESCRIPTION" -->
2 parents e8ea8cf + 5a1d2d3 commit 3e85eb7

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,6 @@ class Definitions {
15091509

15101510
/** Is an context function class.
15111511
* - ContextFunctionN for N >= 0
1512-
* - ErasedContextFunctionN for N > 0
15131512
*/
15141513
def isContextFunctionClass(cls: Symbol): Boolean = scalaClassName(cls).isContextFunction
15151514

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ object ContextFunctionResults:
2020
*/
2121
def annotateContextResults(mdef: DefDef)(using Context): Unit =
2222
def contextResultCount(rhs: Tree, tp: Type): Int = tp match
23-
case defn.ContextFunctionType(_, resTpe, erasedParams) if !erasedParams.contains(true) /* Only enable for non-erased functions */ =>
23+
case defn.ContextFunctionType(_, resTpe, _) =>
2424
rhs match
2525
case closureDef(meth) => 1 + contextResultCount(meth.rhs, resTpe)
2626
case _ => 0
@@ -116,8 +116,14 @@ object ContextFunctionResults:
116116
atPhase(erasurePhase)(integrateSelect(tree, n))
117117
else tree match
118118
case Select(qual, name) =>
119-
if name == nme.apply && defn.isContextFunctionClass(tree.symbol.maybeOwner) then
120-
integrateSelect(qual, n + 1)
119+
if name == nme.apply then
120+
qual.tpe match
121+
case defn.ContextFunctionType(_, _, _) =>
122+
integrateSelect(qual, n + 1)
123+
case _ if defn.isContextFunctionClass(tree.symbol.maybeOwner) => // for TermRefs
124+
integrateSelect(qual, n + 1)
125+
case _ =>
126+
n > 0 && contextResultCount(tree.symbol) >= n
121127
else
122128
n > 0 && contextResultCount(tree.symbol) >= n
123129
case Ident(name) =>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.annotation.tailrec
2+
3+
erased class Foo1
4+
class Foo2
5+
6+
@tailrec
7+
final def test1(n: Int, acc: Int): (Foo1, Foo2) ?=> Int =
8+
if n <= 0 then acc
9+
else test1(n - 1, acc * n)
10+
11+
@tailrec
12+
final def test2(n: Int, acc: Int): Foo1 ?=> Int =
13+
if n <= 0 then acc
14+
else test2(n - 1, acc * n)
15+
16+
@main def Test() =
17+
given Foo1 = Foo1()
18+
given Foo2 = Foo2()
19+
test1(10, 0)
20+
test2(10, 0)

0 commit comments

Comments
 (0)