Skip to content

Allow contextual functions with erased parameters to be integrated #17313

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
Apr 21, 2023
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
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,6 @@ class Definitions {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object ContextFunctionResults:
*/
def annotateContextResults(mdef: DefDef)(using Context): Unit =
def contextResultCount(rhs: Tree, tp: Type): Int = tp match
case defn.ContextFunctionType(_, resTpe, erasedParams) if !erasedParams.contains(true) /* Only enable for non-erased functions */ =>
case defn.ContextFunctionType(_, resTpe, _) =>
rhs match
case closureDef(meth) => 1 + contextResultCount(meth.rhs, resTpe)
case _ => 0
Expand Down Expand Up @@ -116,8 +116,14 @@ object ContextFunctionResults:
atPhase(erasurePhase)(integrateSelect(tree, n))
else tree match
case Select(qual, name) =>
if name == nme.apply && defn.isContextFunctionClass(tree.symbol.maybeOwner) then
integrateSelect(qual, n + 1)
if name == nme.apply then
qual.tpe match
case defn.ContextFunctionType(_, _, _) =>
integrateSelect(qual, n + 1)
case _ if defn.isContextFunctionClass(tree.symbol.maybeOwner) => // for TermRefs
integrateSelect(qual, n + 1)
case _ =>
n > 0 && contextResultCount(tree.symbol) >= n
else
n > 0 && contextResultCount(tree.symbol) >= n
case Ident(name) =>
Expand Down
20 changes: 20 additions & 0 deletions tests/pos-custom-args/erased/tailrec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import scala.annotation.tailrec

erased class Foo1
class Foo2

@tailrec
final def test1(n: Int, acc: Int): (Foo1, Foo2) ?=> Int =
if n <= 0 then acc
else test1(n - 1, acc * n)

@tailrec
final def test2(n: Int, acc: Int): Foo1 ?=> Int =
if n <= 0 then acc
else test2(n - 1, acc * n)

@main def Test() =
given Foo1 = Foo1()
given Foo2 = Foo2()
test1(10, 0)
test2(10, 0)