From e1449b6f3e21dce732042a8290d88ad5959b9180 Mon Sep 17 00:00:00 2001 From: odersky Date: Sun, 13 Nov 2022 14:57:14 +0100 Subject: [PATCH] Drop experimental inheritance checking IMO it's a foot gun that prevents useful idioms. Concretely I have an @experimental marker trait `@caps.Pure` that classes can inherit. So far, there was no way to do that except if the inheriting class is also @experimental. This requirement was unconditional. It did not help to be in an experimental scope or to use a snapshot compiler version. This basically means that @experimental base traits are useless, unless one is prepared to pollute the codebase with sprinkling experimental over all inheriting classes. On the other hand, I see no useful property that is established by this restriction. The (now deleted) documentation is also silent about the motivation of all this. --- .../tools/dotc/typer/CrossVersionChecks.scala | 15 ------- .../other-new-features/experimental-defs.md | 11 +---- .../experimentalInheritance.scala | 14 ------ tests/neg/experimentalInheritance.scala | 44 ------------------- tests/neg/experimentalInheritance2.scala | 6 --- 5 files changed, 2 insertions(+), 88 deletions(-) delete mode 100644 tests/neg-custom-args/no-experimental/experimentalInheritance.scala delete mode 100644 tests/neg/experimentalInheritance.scala delete mode 100644 tests/neg/experimentalInheritance2.scala diff --git a/compiler/src/dotty/tools/dotc/typer/CrossVersionChecks.scala b/compiler/src/dotty/tools/dotc/typer/CrossVersionChecks.scala index 00b037a4e259..746b01c934a3 100644 --- a/compiler/src/dotty/tools/dotc/typer/CrossVersionChecks.scala +++ b/compiler/src/dotty/tools/dotc/typer/CrossVersionChecks.scala @@ -115,15 +115,6 @@ class CrossVersionChecks extends MiniPhase: } } - /** Check that classes extending experimental classes or nested in experimental classes have the @experimental annotation. */ - private def checkExperimentalInheritance(cls: ClassSymbol)(using Context): Unit = - if !cls.isAnonymousClass && !cls.isInExperimentalScope then - cls.info.parents.find(_.typeSymbol.isExperimental) match - case Some(parent) => - report.error(em"extension of experimental ${parent.typeSymbol} must have @experimental annotation", cls.srcPos) - case _ => - end checkExperimentalInheritance - override def transformValDef(tree: ValDef)(using Context): ValDef = checkDeprecatedOvers(tree) checkExperimentalAnnots(tree.symbol) @@ -136,12 +127,6 @@ class CrossVersionChecks extends MiniPhase: checkExperimentalSignature(tree.symbol, tree) tree - override def transformTemplate(tree: Template)(using Context): Tree = - val cls = ctx.owner.asClass - checkExperimentalInheritance(cls) - checkExperimentalAnnots(cls) - tree - override def transformIdent(tree: Ident)(using Context): Ident = { checkUndesiredProperties(tree.symbol, tree.srcPos) tree diff --git a/docs/_docs/reference/other-new-features/experimental-defs.md b/docs/_docs/reference/other-new-features/experimental-defs.md index 225b61161652..88815ad1e136 100644 --- a/docs/_docs/reference/other-new-features/experimental-defs.md +++ b/docs/_docs/reference/other-new-features/experimental-defs.md @@ -216,7 +216,7 @@ Experimental definitions can only be referenced in an experimental scope. Experi
Example 1 - + ```scala import scala.annotation.experimental @@ -242,7 +242,7 @@ Experimental definitions can only be referenced in an experimental scope. Experi } } ``` - +
5. Annotations of an experimental definition are in experimental scopes. Examples: @@ -270,13 +270,6 @@ Can use the `-Yno-experimental` compiler flag to disable it and run as a proper In any other situation, a reference to an experimental definition will cause a compilation error. -## Experimental inheritance - -All subclasses of an experimental `class` or `trait` must be marked as [`@experimental`](https://scala-lang.org/api/3.x/scala/annotation/experimental.html) even if they are in an experimental scope. -Anonymous classes and SAMs of experimental classes are considered experimental. - -We require explicit annotations to make sure we do not have completion or cycles issues with nested classes. This restriction could be relaxed in the future. - ## Experimental overriding For an overriding member `M` and overridden member `O`, if `O` is non-experimental then `M` must be non-experimental. diff --git a/tests/neg-custom-args/no-experimental/experimentalInheritance.scala b/tests/neg-custom-args/no-experimental/experimentalInheritance.scala deleted file mode 100644 index f6eab1224310..000000000000 --- a/tests/neg-custom-args/no-experimental/experimentalInheritance.scala +++ /dev/null @@ -1,14 +0,0 @@ -import scala.annotation.experimental - -@experimental def x = 2 - -@experimental class A1(x: Any) -class A2(x: Any) - - -@experimental class B1 extends A1(1) -class B2 // error: extension of experimental class A1 must have @experimental annotation -extends A1(1) // error: class A1 is marked @experimental ... - -@experimental class C1 extends A2(x) -class C2 extends A2(x) // error def x is marked @experimental and therefore diff --git a/tests/neg/experimentalInheritance.scala b/tests/neg/experimentalInheritance.scala deleted file mode 100644 index 8b6c0b11afa3..000000000000 --- a/tests/neg/experimentalInheritance.scala +++ /dev/null @@ -1,44 +0,0 @@ -import scala.annotation.experimental - -@experimental -class A - -@experimental -trait T - -class B extends A // error - -@experimental -class B2 extends A - -class C extends T // error - -@experimental -class C2 extends T - -@experimental -class O: - class X - - @experimental - class Y - - object Z - -@experimental -object O: - class A - - @experimental - class B - - object C - -class OA extends O.A // error -class OB extends O.B // error - -@experimental -class OA2 extends O.A - -@experimental -class OB2 extends O.B diff --git a/tests/neg/experimentalInheritance2.scala b/tests/neg/experimentalInheritance2.scala deleted file mode 100644 index 84668ac5850f..000000000000 --- a/tests/neg/experimentalInheritance2.scala +++ /dev/null @@ -1,6 +0,0 @@ -import scala.annotation.experimental - -@experimental class A - -class B // // error: extension of experimental class A1 must have @experimental annotation - extends A