Skip to content

Commit 9838750

Browse files
committed
Check final and sealed conditions
So far no error was raised for illegal inheritance from final or sealed classes.
1 parent 3512840 commit 9838750

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/dotty/tools/dotc/typer/RefChecks.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,17 @@ object RefChecks {
7171
}
7272
}
7373

74-
/** Check that self type of this class conforms to self types of parents */
75-
private def checkSelfType(clazz: Symbol)(implicit ctx: Context): Unit = clazz.info match {
74+
/** Check that final and sealed restrictions on class parents
75+
* and that self type of this class conforms to self types of parents.
76+
*/
77+
private def checkParents(clazz: Symbol)(implicit ctx: Context): Unit = clazz.info match {
7678
case cinfo: ClassInfo =>
7779
for (parent <- cinfo.classParents) {
80+
val pclazz = parent.classSymbol
81+
if (pclazz.is(Final))
82+
ctx.error(d"cannot extend final $pclazz", clazz.pos)
83+
if (pclazz.is(Sealed) && pclazz.associatedFile != clazz.associatedFile)
84+
ctx.error(d"cannot extend sealed $pclazz in different compilation unit", clazz.pos)
7885
val pself = parent.givenSelfType.asSeenFrom(clazz.thisType, parent.classSymbol)
7986
if (pself.exists && !(cinfo.selfType <:< pself))
8087
ctx.error(d"illegal inheritance: self type ${cinfo.selfType} of $clazz does not conform to self type $pself of parent ${parent.classSymbol}", clazz.pos)
@@ -768,7 +775,7 @@ class RefChecks extends MiniPhase { thisTransformer =>
768775
override def transformTemplate(tree: Template)(implicit ctx: Context, info: TransformerInfo) = {
769776
val cls = ctx.owner
770777
checkOverloadedRestrictions(cls)
771-
checkSelfType(cls)
778+
checkParents(cls)
772779
checkCompanionNameClashes(cls)
773780
checkAllOverrides(cls)
774781
checkAnyValSubclass(cls)

tests/neg/final-sealed.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
final class A
2+
class B extends A
3+
class C extends Option[Int]
4+

0 commit comments

Comments
 (0)