From 908ed90072526afa20e577a3de1b5c849f91a1c8 Mon Sep 17 00:00:00 2001 From: "Paolo G. Giarrusso" Date: Tue, 15 Jan 2019 19:39:54 +0100 Subject: [PATCH 1/2] Also highlight non-string after showing them --- compiler/src/dotty/tools/dotc/printing/Formatting.scala | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/printing/Formatting.scala b/compiler/src/dotty/tools/dotc/printing/Formatting.scala index 24c13791524b..7c2150736969 100644 --- a/compiler/src/dotty/tools/dotc/printing/Formatting.scala +++ b/compiler/src/dotty/tools/dotc/printing/Formatting.scala @@ -80,9 +80,8 @@ object Formatting { hl.show case hb: HighlightBuffer => hb.toString - case str: String => - SyntaxHighlighting.highlight(str) - case _ => super.showArg(arg) + case _ => + SyntaxHighlighting.highlight(super.showArg(arg)) } } From a17b0b656d78fc4cd56558165abf32955c20b9a4 Mon Sep 17 00:00:00 2001 From: "Paolo G. Giarrusso" Date: Tue, 21 Aug 2018 03:24:53 +0200 Subject: [PATCH 2/2] Forbid/warn on redundant/illegal object flags Fix #4936 in parser. - Stop using final object in our sources. - Give error for abstract and sealed objects, warning for final ones (as long as - scala/bug#11094 is open). - Address review comments. --- compiler/src/dotty/tools/dotc/core/NameOps.scala | 2 +- compiler/src/dotty/tools/dotc/parsing/Parsers.scala | 10 ++++++++++ compiler/test/dotty/tools/vulpix/Status.scala | 2 +- .../src/dotty/tools/dottydoc/model/references.scala | 2 +- semanticdb/src/dotty/semanticdb/Scala.scala | 2 +- tests/neg-custom-args/fatal-warnings/i4936.scala | 1 + tests/neg/i3471.scala | 1 + tests/neg/i4936.scala | 4 ++++ tests/patmat/i3645.scala | 4 ++-- tests/patmat/i3645b.scala | 6 +++--- tests/patmat/i3645c.scala | 6 +++--- tests/patmat/i3645d.scala | 6 +++--- tests/patmat/i3645e.scala | 4 ++-- tests/patmat/i3645f.scala | 4 ++-- tests/patmat/i3645g.scala | 4 ++-- tests/patmat/t7353.scala | 4 ++-- tests/pos-kind-polymorphism/anykind.scala | 2 +- tests/pos/byname-implicits-13.scala | 2 +- tests/pos/byname-implicits-14.scala | 2 +- tests/pos/i3471.scala | 1 - tests/pos/i3647.scala | 4 ++-- tests/run/erased-frameless.scala | 2 +- tests/run/hmap-covariant.scala | 2 +- tests/run/hmap.scala | 2 +- tests/run/phantom-OnHList.scala | 2 +- 25 files changed, 48 insertions(+), 33 deletions(-) create mode 100644 tests/neg-custom-args/fatal-warnings/i4936.scala create mode 100644 tests/neg/i3471.scala create mode 100644 tests/neg/i4936.scala delete mode 100644 tests/pos/i3471.scala diff --git a/compiler/src/dotty/tools/dotc/core/NameOps.scala b/compiler/src/dotty/tools/dotc/core/NameOps.scala index 9aab83206501..9de9dff9cebb 100644 --- a/compiler/src/dotty/tools/dotc/core/NameOps.scala +++ b/compiler/src/dotty/tools/dotc/core/NameOps.scala @@ -10,7 +10,7 @@ import Definitions._ object NameOps { - final object compactify { + object compactify { lazy val md5: MessageDigest = MessageDigest.getInstance("MD5") /** COMPACTIFY diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index 09c943697950..6a20568d269a 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -2415,6 +2415,16 @@ object Parsers { def objectDefRest(start: Offset, mods: Modifiers, name: TermName): ModuleDef = { val template = templateOpt(emptyConstructor) + + def flagPos(flag: FlagSet) = mods.mods.find(_.flags == flag).get.pos + if (mods is Abstract) + syntaxError(hl"""${Abstract} modifier cannot be used for objects""", flagPos(Abstract)) + if (mods is Sealed) + syntaxError(hl"""${Sealed} modifier is redundant for objects""", flagPos(Sealed)) + // Maybe this should be an error; see https://github.com/scala/bug/issues/11094. + if (mods is Final) + warning(hl"""${Final} modifier is redundant for objects""", source atPos flagPos(Final)) + finalizeDef(ModuleDef(name, template), mods, start) } diff --git a/compiler/test/dotty/tools/vulpix/Status.scala b/compiler/test/dotty/tools/vulpix/Status.scala index 3de7aff2b818..5503bb394aa4 100644 --- a/compiler/test/dotty/tools/vulpix/Status.scala +++ b/compiler/test/dotty/tools/vulpix/Status.scala @@ -4,4 +4,4 @@ package vulpix sealed trait Status final case class Success(output: String) extends Status final case class Failure(output: String) extends Status -final case object Timeout extends Status +case object Timeout extends Status diff --git a/doc-tool/src/dotty/tools/dottydoc/model/references.scala b/doc-tool/src/dotty/tools/dottydoc/model/references.scala index 778ddca035eb..d34b09490bc0 100644 --- a/doc-tool/src/dotty/tools/dottydoc/model/references.scala +++ b/doc-tool/src/dotty/tools/dottydoc/model/references.scala @@ -11,7 +11,7 @@ object references { final case class BoundsReference(low: Reference, high: Reference) extends Reference final case class NamedReference(title: String, ref: Reference, isByName: Boolean = false, isRepeated: Boolean = false) extends Reference final case class ConstantReference(title: String) extends Reference - final case object EmptyReference extends Reference + case object EmptyReference extends Reference /** Use MaterializableLink for entities that need be picklable */ sealed trait MaterializableLink { def title: String } diff --git a/semanticdb/src/dotty/semanticdb/Scala.scala b/semanticdb/src/dotty/semanticdb/Scala.scala index b4bd983022a5..94f2d76b7bc0 100644 --- a/semanticdb/src/dotty/semanticdb/Scala.scala +++ b/semanticdb/src/dotty/semanticdb/Scala.scala @@ -148,7 +148,7 @@ object Scala { } } object Descriptor { - final case object None extends Descriptor { def value: String = "" } + case object None extends Descriptor { def value: String = "" } final case class Term(value: String) extends Descriptor final case class Method(value: String, disambiguator: String) extends Descriptor final case class Type(value: String) extends Descriptor diff --git a/tests/neg-custom-args/fatal-warnings/i4936.scala b/tests/neg-custom-args/fatal-warnings/i4936.scala new file mode 100644 index 000000000000..65c026899e7e --- /dev/null +++ b/tests/neg-custom-args/fatal-warnings/i4936.scala @@ -0,0 +1 @@ +final object Foo // error diff --git a/tests/neg/i3471.scala b/tests/neg/i3471.scala new file mode 100644 index 000000000000..10c94904fa0d --- /dev/null +++ b/tests/neg/i3471.scala @@ -0,0 +1 @@ +sealed object Fun // error diff --git a/tests/neg/i4936.scala b/tests/neg/i4936.scala new file mode 100644 index 000000000000..72201724e437 --- /dev/null +++ b/tests/neg/i4936.scala @@ -0,0 +1,4 @@ +abstract object Foo // error +sealed final abstract case object Bar // error // error + +abstract override object Baz // error diff --git a/tests/patmat/i3645.scala b/tests/patmat/i3645.scala index e009a6de2310..9ad44c27133f 100644 --- a/tests/patmat/i3645.scala +++ b/tests/patmat/i3645.scala @@ -13,8 +13,8 @@ object App { } sealed abstract class K[A] - final case object KAge extends K[Age] - final case object KInt extends K[Int] + case object KAge extends K[Age] + case object KInt extends K[Int] val kint: K[Age] = Age.subst[K](KInt) def get(k: K[Age]): String = k match { diff --git a/tests/patmat/i3645b.scala b/tests/patmat/i3645b.scala index 1f1e146ea8a0..cbdc6e0aa1da 100644 --- a/tests/patmat/i3645b.scala +++ b/tests/patmat/i3645b.scala @@ -13,9 +13,9 @@ object App { type Bar = Foo sealed abstract class K[A] - final case object K1 extends K[Int] - final case object K2 extends K[Foo] - final case object K3 extends K[Bar] + case object K1 extends K[Int] + case object K2 extends K[Foo] + case object K3 extends K[Bar] val foo: K[Int] = Foo.subst[K](K2) def get(k: K[Int]): Unit = k match { diff --git a/tests/patmat/i3645c.scala b/tests/patmat/i3645c.scala index 4b3ba75989c8..f8ae014677fe 100644 --- a/tests/patmat/i3645c.scala +++ b/tests/patmat/i3645c.scala @@ -13,9 +13,9 @@ object App { type Bar = Foo sealed abstract class K[+A] - final case object K1 extends K[Int] - final case object K2 extends K[Foo] - final case object K3 extends K[Bar] + case object K1 extends K[Int] + case object K2 extends K[Foo] + case object K3 extends K[Bar] val foo: K[Int] = Foo.subst[K](K2) def get(k: K[Int]): Unit = k match { diff --git a/tests/patmat/i3645d.scala b/tests/patmat/i3645d.scala index 3bbe29e20545..952ee7252f35 100644 --- a/tests/patmat/i3645d.scala +++ b/tests/patmat/i3645d.scala @@ -13,9 +13,9 @@ object App { type Bar = Foo sealed abstract class K[-A] - final case object K1 extends K[Int] - final case object K2 extends K[Foo] - final case object K3 extends K[Bar] + case object K1 extends K[Int] + case object K2 extends K[Foo] + case object K3 extends K[Bar] val foo: K[Int] = Foo.subst[K](K2) def get(k: K[Int]): Unit = k match { diff --git a/tests/patmat/i3645e.scala b/tests/patmat/i3645e.scala index 7c4b13416712..a4ad9290f3df 100644 --- a/tests/patmat/i3645e.scala +++ b/tests/patmat/i3645e.scala @@ -22,8 +22,8 @@ object App { type Foo = Module.Foo.Type sealed abstract class K[F] - final case object K1 extends K[Int] - final case object K2 extends K[Foo] + case object K1 extends K[Int] + case object K2 extends K[Foo] val kv: K[Foo] = Module.Foo.subst[K](K1) def test(k: K[Foo]): Unit = k match { diff --git a/tests/patmat/i3645f.scala b/tests/patmat/i3645f.scala index 04d31aceea0f..2ac434f78d84 100644 --- a/tests/patmat/i3645f.scala +++ b/tests/patmat/i3645f.scala @@ -23,8 +23,8 @@ object App { type Foo = Module.Foo.Type sealed abstract class K[F] - final case object K1 extends K[Int] - final case object K2 extends K[Foo] + case object K1 extends K[Int] + case object K2 extends K[Foo] val kv: K[Foo] = Module.Foo.subst[K](K1) def test(k: K[Foo]): Unit = k match { diff --git a/tests/patmat/i3645g.scala b/tests/patmat/i3645g.scala index f1c85b5d9e8c..5e02b3ec1fdb 100644 --- a/tests/patmat/i3645g.scala +++ b/tests/patmat/i3645g.scala @@ -22,8 +22,8 @@ object App { type Foo = Module.Foo.Type sealed abstract class K[F] - final case object K1 extends K[Int] - final case object K2 extends K[Foo] + case object K1 extends K[Int] + case object K2 extends K[Foo] val kv: K[Foo] = Module.Foo.subst[K](K1) def test(k: K[Foo]): Unit = k match { diff --git a/tests/patmat/t7353.scala b/tests/patmat/t7353.scala index 7a8fea115fc4..c8a7a9fb8c88 100644 --- a/tests/patmat/t7353.scala +++ b/tests/patmat/t7353.scala @@ -1,8 +1,8 @@ sealed trait EthernetType object EthernetType { - final case object Gigabit extends EthernetType - final case object FastEthernet extends EthernetType + case object Gigabit extends EthernetType + case object FastEthernet extends EthernetType final def toInt(t: EthernetType) = t match { case Gigabit => 1 diff --git a/tests/pos-kind-polymorphism/anykind.scala b/tests/pos-kind-polymorphism/anykind.scala index a8f8e2839218..81edae26a920 100644 --- a/tests/pos-kind-polymorphism/anykind.scala +++ b/tests/pos-kind-polymorphism/anykind.scala @@ -93,7 +93,7 @@ object Test { sealed trait HList final case class ::[+H, +T <: HList](head : H, tail : T) extends HList sealed trait HNil extends HList - final case object HNil extends HNil + case object HNil extends HNil object New { // The Kind Polymorphic List diff --git a/tests/pos/byname-implicits-13.scala b/tests/pos/byname-implicits-13.scala index 4ecc495d7a07..abe4f47c0e05 100644 --- a/tests/pos/byname-implicits-13.scala +++ b/tests/pos/byname-implicits-13.scala @@ -13,7 +13,7 @@ case class Cat(name: String, friend: Either[Cat, Dog]) sealed trait HList extends Product with Serializable final case class ::[+H, +T <: HList](head: H, tail: T) extends HList sealed trait HNil extends HList -final case object HNil extends HNil +case object HNil extends HNil sealed trait Coproduct extends Product with Serializable sealed trait :+:[+H, +T <: Coproduct] extends Coproduct diff --git a/tests/pos/byname-implicits-14.scala b/tests/pos/byname-implicits-14.scala index 122903d7b695..cf1f2084df31 100644 --- a/tests/pos/byname-implicits-14.scala +++ b/tests/pos/byname-implicits-14.scala @@ -13,7 +13,7 @@ case class Cat(name: String, friend: Either[Cat, Dog]) sealed trait HList extends Product with Serializable final case class ::[+H, +T <: HList](head: H, tail: T) extends HList sealed trait HNil extends HList -final case object HNil extends HNil +case object HNil extends HNil sealed trait Coproduct extends Product with Serializable sealed trait :+:[+H, +T <: Coproduct] extends Coproduct diff --git a/tests/pos/i3471.scala b/tests/pos/i3471.scala deleted file mode 100644 index 9bc9e82537db..000000000000 --- a/tests/pos/i3471.scala +++ /dev/null @@ -1 +0,0 @@ -sealed object Fun diff --git a/tests/pos/i3647.scala b/tests/pos/i3647.scala index 5bad3d5beb63..35b633976ebc 100644 --- a/tests/pos/i3647.scala +++ b/tests/pos/i3647.scala @@ -10,8 +10,8 @@ object App { type T = String type Bar[A] = J[A] sealed abstract class J[A] - final case object JName extends J[T] - final case object JInt extends J[Int] + case object JName extends J[T] + case object JInt extends J[Int] def get(k: J[T]): String = k match { case JName => "Age" diff --git a/tests/run/erased-frameless.scala b/tests/run/erased-frameless.scala index 5c18ef378d5f..2f6c6ebe3d68 100644 --- a/tests/run/erased-frameless.scala +++ b/tests/run/erased-frameless.scala @@ -5,7 +5,7 @@ import scala.annotation.implicitNotFound sealed trait HList sealed trait HNil extends HList -final case object HNil extends HNil +case object HNil extends HNil final case class ::[H, T <: HList](h: H, t: T) extends HList /** Generic representation os type T as a labelled sum of product. */ diff --git a/tests/run/hmap-covariant.scala b/tests/run/hmap-covariant.scala index 475cc6ee66fe..75e7333eb452 100644 --- a/tests/run/hmap-covariant.scala +++ b/tests/run/hmap-covariant.scala @@ -1,6 +1,6 @@ trait Tuple case class TCons[+H, +T <: Tuple](h: H, t: T) extends Tuple -final case object TNil extends Tuple +case object TNil extends Tuple // Type level natural numbers ------------------------------------------------- diff --git a/tests/run/hmap.scala b/tests/run/hmap.scala index d84419ce1e8d..e189ca132f46 100644 --- a/tests/run/hmap.scala +++ b/tests/run/hmap.scala @@ -1,6 +1,6 @@ trait Tuple case class TCons[H, T <: Tuple](h: H, t: T) extends Tuple -final case object TNil extends Tuple +case object TNil extends Tuple // Type level natural numbers ------------------------------------------------- diff --git a/tests/run/phantom-OnHList.scala b/tests/run/phantom-OnHList.scala index b97f7f812f56..5c6e76aac08e 100644 --- a/tests/run/phantom-OnHList.scala +++ b/tests/run/phantom-OnHList.scala @@ -24,7 +24,7 @@ sealed trait HNil extends HList // HList values ----------------------------------------------------------------------------------- -final case object HNil extends HNil { +case object HNil extends HNil { val underlying: Array[Any] = Array.empty[Any] override def toString(): String = "()" }