From 87180844d3bbe290eca4496e44ddf53110af3707 Mon Sep 17 00:00:00 2001 From: odersky Date: Wed, 20 Dec 2023 21:47:36 +0100 Subject: [PATCH] Run CheckStatic after UncacheGivenAliases --- compiler/src/dotty/tools/dotc/Compiler.scala | 2 +- .../src/dotty/tools/dotc/transform/CheckStatic.scala | 3 +++ tests/pos/i19304.scala | 9 +++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 tests/pos/i19304.scala diff --git a/compiler/src/dotty/tools/dotc/Compiler.scala b/compiler/src/dotty/tools/dotc/Compiler.scala index aaa14a052936..290df761d117 100644 --- a/compiler/src/dotty/tools/dotc/Compiler.scala +++ b/compiler/src/dotty/tools/dotc/Compiler.scala @@ -64,7 +64,6 @@ class Compiler { new CheckReentrant, // Internal use only: Check that compiled program has no data races involving global vars new ElimPackagePrefixes, // Eliminate references to package prefixes in Select nodes new CookComments, // Cook the comments: expand variables, doc, etc. - new CheckStatic, // Check restrictions that apply to @static members new CheckLoopingImplicits, // Check that implicit defs do not call themselves in an infinite loop new BetaReduce, // Reduce closure applications new InlineVals, // Check right hand-sides of an `inline val`s @@ -76,6 +75,7 @@ class Compiler { List(new ProtectedAccessors, // Add accessors for protected members new ExtensionMethods, // Expand methods of value classes with extension methods new UncacheGivenAliases, // Avoid caching RHS of simple parameterless given aliases + new CheckStatic, // Check restrictions that apply to @static members new ElimByName, // Map by-name parameters to functions new HoistSuperArgs, // Hoist complex arguments of supercalls to enclosing scope new ForwardDepChecks, // Check that there are no forward references to local vals diff --git a/compiler/src/dotty/tools/dotc/transform/CheckStatic.scala b/compiler/src/dotty/tools/dotc/transform/CheckStatic.scala index 26c94407f35b..6c74f302b65d 100644 --- a/compiler/src/dotty/tools/dotc/transform/CheckStatic.scala +++ b/compiler/src/dotty/tools/dotc/transform/CheckStatic.scala @@ -30,6 +30,9 @@ class CheckStatic extends MiniPhase { override def description: String = CheckStatic.description + override def runsAfter: Set[String] = Set(UncacheGivenAliases.name) + // UncachedGivenAliases eliminates static lazy vals, which are flagged as errors here + override def transformTemplate(tree: tpd.Template)(using Context): tpd.Tree = { val defns = tree.body.collect{case t: ValOrDefDef => t} var hadNonStaticField = false diff --git a/tests/pos/i19304.scala b/tests/pos/i19304.scala new file mode 100644 index 000000000000..c8dee26f00fb --- /dev/null +++ b/tests/pos/i19304.scala @@ -0,0 +1,9 @@ +import scala.annotation.static +trait Foo[A] +object Foo: + @static val foo: Foo[String] = new {} + @static given Foo[String] = foo + + def bar = + val foo: Foo[String] = new {} + given Foo[String] = foo