Skip to content

Commit 9a0c822

Browse files
Merge pull request #3473 from maseev/iss1589-sealed-class-extension-violation
Add an error message class for sealed classes
2 parents 78c60a7 + f64f0bb commit 9a0c822

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public enum ErrorMessageID {
117117
UnapplyInvalidNumberOfArgumentsID,
118118
StaticFieldsOnlyAllowedInObjectsID,
119119
CyclicInheritanceID,
120+
UnableToExtendSealedClassID,
120121
;
121122

122123
public int errorNumber() {

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,4 +1966,10 @@ object messages {
19661966
|impossible to instantiate an object of this class"""
19671967
}
19681968
}
1969+
1970+
case class UnableToExtendSealedClass(pclazz: Symbol)(implicit ctx: Context) extends Message(UnableToExtendSealedClassID) {
1971+
val kind = "Syntax"
1972+
val msg = hl"Cannot extend ${"sealed"} $pclazz in a different source file"
1973+
val explanation = "A sealed class or trait can only be extended in the same file as its declaration"
1974+
}
19691975
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ class Namer { typer: Typer =>
877877
if (pclazz.is(Final))
878878
ctx.error(ExtendFinalClass(cls, pclazz), cls.pos)
879879
if (pclazz.is(Sealed) && pclazz.associatedFile != cls.associatedFile)
880-
ctx.error(em"cannot extend sealed $pclazz in different compilation unit", cls.pos)
880+
ctx.error(UnableToExtendSealedClass(pclazz), cls.pos)
881881
pt
882882
}
883883
}

0 commit comments

Comments
 (0)