Skip to content

Commit a0ec7b2

Browse files
committed
Add error for partial functions with refined terms
1 parent 157b3d4 commit a0ec7b2

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class ExpandSAMs extends MiniPhaseTransform { thisTransformer =>
3535
tpt.tpe match {
3636
case NoType => tree // it's a plain function
3737
case tpe @ SAMType(_) if tpe.isRef(defn.PartialFunctionClass) =>
38+
checkRefinements(tpe, fn.pos)
3839
toPartialFunction(tree)
3940
case tpe @ SAMType(_) if isPlatformSam(tpe.classSymbol.asClass) =>
4041
checkRefinements(tpe, fn.pos)
@@ -90,7 +91,7 @@ class ExpandSAMs extends MiniPhaseTransform { thisTransformer =>
9091
private def checkRefinements(tpe: Type, pos: Position)(implicit ctx: Context): Unit = tpe match {
9192
case RefinedType(parent, name, _) =>
9293
if (name.isTermName && tpe.member(name).symbol.ownersIterator.isEmpty) // if member defined in the refinement
93-
ctx.error(s"Cannot refine $name on a lambda", pos)
94+
ctx.error("Lambda does not define " + name, pos)
9495
checkRefinements(parent, pos)
9596
case _ =>
9697
}

tests/neg/i2539.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ object Foo {
1212
val fun0a: Fun0[Int] = () => 42
1313
val fun0b: Fun0[Int] { def foo(): Int } = () => 42 // error
1414

15+
val pf: PartialFunction[Int, Int]{ def foo(): Int } = { case x1 => 42 } // error
16+
1517
}

0 commit comments

Comments
 (0)