diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java index 10d52e01497f..4f5ed0ef4ab6 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java @@ -117,6 +117,7 @@ public enum ErrorMessageID { UnapplyInvalidNumberOfArgumentsID, StaticFieldsOnlyAllowedInObjectsID, CyclicInheritanceID, + UnableToExtendSealedClassID, ; public int errorNumber() { diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index a8c9a9f2ee85..94192d13ee57 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -1966,4 +1966,10 @@ object messages { |impossible to instantiate an object of this class""" } } + + case class UnableToExtendSealedClass(pclazz: Symbol)(implicit ctx: Context) extends Message(UnableToExtendSealedClassID) { + val kind = "Syntax" + val msg = hl"Cannot extend ${"sealed"} $pclazz in a different source file" + val explanation = "A sealed class or trait can only be extended in the same file as its declaration" + } } diff --git a/compiler/src/dotty/tools/dotc/typer/Namer.scala b/compiler/src/dotty/tools/dotc/typer/Namer.scala index d16645980a19..4d8910347fc1 100644 --- a/compiler/src/dotty/tools/dotc/typer/Namer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Namer.scala @@ -877,7 +877,7 @@ class Namer { typer: Typer => if (pclazz.is(Final)) ctx.error(ExtendFinalClass(cls, pclazz), cls.pos) if (pclazz.is(Sealed) && pclazz.associatedFile != cls.associatedFile) - ctx.error(em"cannot extend sealed $pclazz in different compilation unit", cls.pos) + ctx.error(UnableToExtendSealedClass(pclazz), cls.pos) pt } }