Skip to content

Commit df0bb24

Browse files
authored
Merge pull request #8429 from dotty-staging/serialvuid-trait
Fix #8427: Disallow SerialVersionUID on a trait
2 parents b448db2 + cd5bf83 commit df0bb24

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -796,11 +796,16 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
796796
for (f <- toDenot(sym).info.decls.toList if f.isMethod && f.isTerm && !f.isModule) yield f
797797
def serialVUID: Option[Long] =
798798
sym.getAnnotation(defn.SerialVersionUIDAnnot).flatMap { annot =>
799-
val vuid = annot.argumentConstant(0).map(_.longValue)
800-
if (vuid.isEmpty)
801-
ctx.error("The argument passed to @SerialVersionUID must be a constant",
802-
annot.argument(0).getOrElse(annot.tree).sourcePos)
803-
vuid
799+
if (sym.is(Flags.Trait)) {
800+
ctx.error("@SerialVersionUID does nothing on a trait", annot.tree.sourcePos)
801+
None
802+
} else {
803+
val vuid = annot.argumentConstant(0).map(_.longValue)
804+
if (vuid.isEmpty)
805+
ctx.error("The argument passed to @SerialVersionUID must be a constant",
806+
annot.argument(0).getOrElse(annot.tree).sourcePos)
807+
vuid
808+
}
804809
}
805810

806811
def freshLocal(cunit: CompilationUnit, name: String, tpe: Type, pos: Position, flags: Flags): Symbol = {

tests/neg/i8427.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@SerialVersionUID(1L) // error
2+
trait T
3+
4+
object Test {
5+
var t: T = _
6+
def main(args: Array[String]) = println("hi")
7+
}

0 commit comments

Comments
 (0)