Skip to content

Commit 062bb98

Browse files
committed
Handle poly functions
1 parent 2f2253d commit 062bb98

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

compiler/src/dotty/tools/dotc/transform/init/Semantic.scala

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,14 @@ class Semantic {
555555
case _ =>
556556
??? // impossible
557557

558+
case PolyFun(body) =>
559+
thisV match
560+
case obj: (ThisRef | Warm) =>
561+
val value = Fun(body, obj, klass)
562+
Result(value, Nil)
563+
case _ =>
564+
??? // impossible
565+
558566
case Block(stats, expr) =>
559567
val ress = eval(stats, thisV, klass)
560568
eval(expr, thisV, klass) ++ ress.flatMap(_.errors)
@@ -829,7 +837,7 @@ object Semantic {
829837
case TypeApply(fn, targs) =>
830838
unapply(fn)
831839

832-
case ref: RefTree if ref.symbol.is(Flags.Method) =>
840+
case ref: RefTree if ref.tpe.widenSingleton.isInstanceOf[MethodicType] =>
833841
Some((ref, Nil))
834842

835843
case _ => None
@@ -844,6 +852,19 @@ object Semantic {
844852
case _ => None
845853
}
846854

855+
object PolyFun {
856+
def unapply(tree: Tree)(using Context): Option[Tree] =
857+
tree match
858+
case Block((cdef: TypeDef) :: Nil, Typed(NewExpr(tref, _, _, _), _))
859+
if tref.symbol.isAnonymousClass && tref <:< defn.PolyFunctionType
860+
=>
861+
val body = cdef.rhs.asInstanceOf[Template].body
862+
val apply = body.head.asInstanceOf[DefDef]
863+
Some(apply.rhs)
864+
case _ =>
865+
None
866+
}
867+
847868
extension (symbol: Symbol) def hasSource(using Context): Boolean =
848869
!symbol.defTree.isEmpty
849870

tests/init/neg/polyfun.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Test {
2+
val m: [T] => (arg: T) => T =
3+
[T] => (arg: T) => {
4+
println(n)
5+
arg
6+
}
7+
val n = m.apply(arg = 23)
8+
}

tests/init/neg/super.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ class Bar extends A, B, C:
2020

2121
override def foo() = n * n
2222

23-
val n = 10
23+
val n = 10 // error
2424

2525
class Qux extends A, B, C:
2626
super.foo()
2727

2828
override def foo() = n * n
2929

30-
val n = 10
30+
val n = 10 // error

0 commit comments

Comments
 (0)