Skip to content

Commit aa715fe

Browse files
authored
Merge pull request #5896 from dotty-staging/fix-#5888
Fix #5888: Disallow SAM type creation of final and sealed classes
2 parents 9f86c88 + 6653295 commit aa715fe

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,9 @@ object Flags {
638638
/** value that's final or inline */
639639
final val FinalOrInline: FlagSet = Final | Inline
640640

641+
/** class that's final or sealed */
642+
final val FinalOrSealed: FlagSet = Final | Sealed
643+
641644
/** A covariant type parameter instance */
642645
final val LocalCovariant: FlagConjunction = allOf(Local, Covariant)
643646

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,11 @@ class Typer extends Namer
985985
case SAMType(sam)
986986
if !defn.isFunctionType(pt) && mt <:< sam =>
987987
if (!isFullyDefined(pt, ForceDegree.all))
988-
ctx.error(ex"result type of closure is an underspecified SAM type $pt", tree.sourcePos)
988+
ctx.error(ex"result type of lambda is an underspecified SAM type $pt", tree.sourcePos)
989+
else if (pt.classSymbol.is(FinalOrSealed)) {
990+
val offendingFlag = pt.classSymbol.flags & FinalOrSealed
991+
ctx.error(ex"lambda cannot implement $offendingFlag ${pt.classSymbol}", tree.sourcePos)
992+
}
989993
TypeTree(pt)
990994
case _ =>
991995
if (mt.isParamDependent) {

tests/neg/i5888.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
val x: String <:< Int = _.toInt // error
2+
3+
abstract final class Foo { def f(x: Int): Int }
4+
5+
val foo: Foo = x => x // error
6+
7+
val foo2: Foo = Predef.identity // error
8+

0 commit comments

Comments
 (0)