From 4f73ec58e174f15ecc87763b0f631f2d05fbb1dc Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Mon, 15 May 2023 16:23:27 -0700 Subject: [PATCH 1/2] Regression test and simplify --- .../dotc/core/unpickleScala2/Scala2Unpickler.scala | 4 ++-- .../src/dotty/tools/dotc/transform/Erasure.scala | 6 +++--- tests/neg/no-unit.check | 14 ++++++++++++++ tests/neg/no-unit.scala | 7 +++++++ 4 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 tests/neg/no-unit.check create mode 100644 tests/neg/no-unit.scala diff --git a/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala b/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala index 5087044f73e8..bf076aa65fdd 100644 --- a/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala +++ b/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala @@ -1002,14 +1002,14 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas /** Read an annotation argument, which is pickled either * as a Constant or a Tree. */ - protected def readAnnotArg(i: Int)(using Context): untpd.Tree = untpd.TypedSplice(bytes(index(i)) match + protected def readAnnotArg(i: Int)(using Context): untpd.Tree = untpd.TypedSplice: + bytes(index(i)) match case TREE => at(i, () => readTree()) case _ => at(i, () => readConstant() match case c: Constant => Literal(c) case tp: TermRef => ref(tp) ) - ) /** Read a ClassfileAnnotArg (argument to a classfile annotation) */ diff --git a/compiler/src/dotty/tools/dotc/transform/Erasure.scala b/compiler/src/dotty/tools/dotc/transform/Erasure.scala index b46c780480ff..0c2a902ed725 100644 --- a/compiler/src/dotty/tools/dotc/transform/Erasure.scala +++ b/compiler/src/dotty/tools/dotc/transform/Erasure.scala @@ -561,15 +561,15 @@ object Erasure { report.error(msg, tree.srcPos) tree.symbol.getAnnotation(defn.CompileTimeOnlyAnnot) match case Some(annot) => - val message = annot.argumentConstant(0) match - case Some(c) => + val message = annot.argumentConstantString(0) match + case Some(msg) => val addendum = tree match case tree: RefTree if tree.symbol == defn.Compiletime_deferred && tree.name != nme.deferred => i".\nNote that `deferred` can only be used under its own name when implementing a given in a trait; `${tree.name}` is not accepted." case _ => "" - (c.stringValue ++ addendum).toMessage + (msg + addendum).toMessage case _ => em"""Reference to ${tree.symbol.showLocated} should not have survived, |it should have been processed and eliminated during expansion of an enclosing macro or term erasure.""" diff --git a/tests/neg/no-unit.check b/tests/neg/no-unit.check new file mode 100644 index 000000000000..59a54e44b436 --- /dev/null +++ b/tests/neg/no-unit.check @@ -0,0 +1,14 @@ +-- [E190] Potential Issue Warning: tests/neg/no-unit.scala:7:2 --------------------------------------------------------- +7 | Unit // error + | ^^^^ + | Discarded non-Unit value of type object Unit. Add `: Unit` to discard silently. + | + | longer explanation available when compiling with `-explain` +-- Error: tests/neg/no-unit.scala:1:8 ---------------------------------------------------------------------------------- +1 |val u = Unit // error + | ^^^^ + | `Unit` companion object is not allowed in source; instead, use `()` for the unit value +-- Error: tests/neg/no-unit.scala:7:2 ---------------------------------------------------------------------------------- +7 | Unit // error + | ^^^^ + | `Unit` companion object is not allowed in source; instead, use `()` for the unit value diff --git a/tests/neg/no-unit.scala b/tests/neg/no-unit.scala new file mode 100644 index 000000000000..437620ed9074 --- /dev/null +++ b/tests/neg/no-unit.scala @@ -0,0 +1,7 @@ +val u = Unit // error + +inline def a(inline f: Unit ?=> Unit): Unit = f(using ()) + +def s5bug = a: + print(2) + Unit // error From 6788b36420a852c79259ff5fd23a7a61ae107419 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Thu, 6 Nov 2025 17:31:14 -0800 Subject: [PATCH 2/2] Accommodate Scala 2 annotation value --- compiler/src/dotty/tools/dotc/core/Annotations.scala | 2 +- tests/neg/serialversionuid-not-const.scala | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/Annotations.scala b/compiler/src/dotty/tools/dotc/core/Annotations.scala index 1615679a036e..5d32fab337c0 100644 --- a/compiler/src/dotty/tools/dotc/core/Annotations.scala +++ b/compiler/src/dotty/tools/dotc/core/Annotations.scala @@ -38,7 +38,7 @@ object Annotations { if (i < args.length) Some(args(i)) else None } def argumentConstant(i: Int)(using Context): Option[Constant] = - for (case ConstantType(c) <- argument(i) map (_.tpe.widenTermRefExpr.normalized)) yield c + for case ConstantType(c) <- argument(i).map(stripCast(_).tpe.widenTermRefExpr.normalized) yield c def argumentConstantString(i: Int)(using Context): Option[String] = for (case Constant(s: String) <- argumentConstant(i)) yield s diff --git a/tests/neg/serialversionuid-not-const.scala b/tests/neg/serialversionuid-not-const.scala index 88e87221d21f..227bce01f79d 100644 --- a/tests/neg/serialversionuid-not-const.scala +++ b/tests/neg/serialversionuid-not-const.scala @@ -1,6 +1,6 @@ @SerialVersionUID(13l.toLong) class C1 extends Serializable // OK because toLong is constant-folded @SerialVersionUID(13l) class C2 extends Serializable // OK -@SerialVersionUID(13.asInstanceOf[Long]) class C3 extends Serializable // error +@SerialVersionUID(13.asInstanceOf[Long]) class C3 extends Serializable // OK was error @SerialVersionUID(Test.bippy) class C4 extends Serializable // error object Test {