From ed811f048ba2598b6970a912f5953537cecf8878 Mon Sep 17 00:00:00 2001 From: Wojciech Mazur Date: Fri, 3 May 2024 15:40:34 +0200 Subject: [PATCH] Deprecate `StandardPlugin.init` in favor of `initialize` method taking an implicit Context parameter. --- .../src/dotty/tools/dotc/plugins/Plugin.scala | 15 ++++++++++++++- .../src/dotty/tools/dotc/plugins/Plugins.scala | 2 +- .../changed-features/compiler-plugins.md | 4 ++-- .../changed-features/compiler-plugins.md | 4 ++-- .../analyzer-plugin/plugin/Analyzer.scala | 2 +- .../compiler-plugin/plugin/DivideZero.scala | 3 ++- tests/plugins/custom/analyzer/Analyzer_1.scala | 2 +- tests/plugins/neg/divideZero/plugin_1.scala | 2 +- 8 files changed, 24 insertions(+), 10 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/plugins/Plugin.scala b/compiler/src/dotty/tools/dotc/plugins/Plugin.scala index ce77a5b9d97a..f33c6d474b54 100644 --- a/compiler/src/dotty/tools/dotc/plugins/Plugin.scala +++ b/compiler/src/dotty/tools/dotc/plugins/Plugin.scala @@ -13,6 +13,7 @@ import java.io.InputStream import java.util.Properties import scala.util.{ Try, Success, Failure } +import scala.annotation.nowarn trait PluginPhase extends MiniPhase { def runsBefore: Set[String] = Set.empty @@ -50,7 +51,19 @@ trait StandardPlugin extends Plugin { * @param options commandline options to the plugin. * @return a list of phases to be added to the phase plan */ - def init(options: List[String]): List[PluginPhase] + @deprecated("`init` does not allow to access `Context`, use `initialize` instead.", since = "3.5.0") + def init(options: List[String]): List[PluginPhase] = Nil + + /** Non-research plugins should override this method to return the phases + * + * The phases returned must be freshly constructed (not reused + * and returned again on subsequent calls). + * + * @param options commandline options to the plugin. + * @return a list of phases to be added to the phase plan + */ + @nowarn("cat=deprecation") + def initialize(options: List[String])(using Context): List[PluginPhase] = init(options) } /** A research plugin may customize the compilation pipeline freely diff --git a/compiler/src/dotty/tools/dotc/plugins/Plugins.scala b/compiler/src/dotty/tools/dotc/plugins/Plugins.scala index 31176bb2fb2c..a6672d475129 100644 --- a/compiler/src/dotty/tools/dotc/plugins/Plugins.scala +++ b/compiler/src/dotty/tools/dotc/plugins/Plugins.scala @@ -125,7 +125,7 @@ trait Plugins { } // schedule plugins according to ordering constraints - val pluginPhases = plugins.collect { case p: StandardPlugin => p }.flatMap { plug => plug.init(options(plug)) } + val pluginPhases = plugins.collect { case p: StandardPlugin => p }.flatMap { plug => plug.initialize(options(plug)) } val updatedPlan = Plugins.schedule(plan, pluginPhases) // add research plugins diff --git a/docs/_docs/reference/changed-features/compiler-plugins.md b/docs/_docs/reference/changed-features/compiler-plugins.md index 6be8a62c7ac4..c0bfccec8172 100644 --- a/docs/_docs/reference/changed-features/compiler-plugins.md +++ b/docs/_docs/reference/changed-features/compiler-plugins.md @@ -67,7 +67,7 @@ class DivideZero extends StandardPlugin: val name: String = "divideZero" override val description: String = "divide zero check" - def init(options: List[String]): List[PluginPhase] = + override def initialize(options: List[String])(using Context): List[PluginPhase] = (new DivideZeroPhase) :: Nil class DivideZeroPhase extends PluginPhase: @@ -90,7 +90,7 @@ end DivideZeroPhase ``` The plugin main class (`DivideZero`) must extend the trait `StandardPlugin` -and implement the method `init` that takes the plugin's options as argument +and implement the method `initialize` that takes the plugin's options as argument and returns a list of `PluginPhase`s to be inserted into the compilation pipeline. Our plugin adds one compiler phase to the pipeline. A compiler phase must extend diff --git a/docs/_spec/TODOreference/changed-features/compiler-plugins.md b/docs/_spec/TODOreference/changed-features/compiler-plugins.md index 20bdb7f49836..719e204fc803 100644 --- a/docs/_spec/TODOreference/changed-features/compiler-plugins.md +++ b/docs/_spec/TODOreference/changed-features/compiler-plugins.md @@ -67,7 +67,7 @@ class DivideZero extends StandardPlugin: val name: String = "divideZero" override val description: String = "divide zero check" - def init(options: List[String]): List[PluginPhase] = + override def initialize(options: List[String])(using Context): List[PluginPhase] = (new DivideZeroPhase) :: Nil class DivideZeroPhase extends PluginPhase: @@ -90,7 +90,7 @@ end DivideZeroPhase ``` The plugin main class (`DivideZero`) must extend the trait `StandardPlugin` -and implement the method `init` that takes the plugin's options as argument +and implement the method `initialize` that takes the plugin's options as argument and returns a list of `PluginPhase`s to be inserted into the compilation pipeline. Our plugin adds one compiler phase to the pipeline. A compiler phase must extend diff --git a/sbt-test/sbt-dotty/analyzer-plugin/plugin/Analyzer.scala b/sbt-test/sbt-dotty/analyzer-plugin/plugin/Analyzer.scala index c1fab5c13f42..01aa57d7a971 100644 --- a/sbt-test/sbt-dotty/analyzer-plugin/plugin/Analyzer.scala +++ b/sbt-test/sbt-dotty/analyzer-plugin/plugin/Analyzer.scala @@ -21,7 +21,7 @@ class InitPlugin extends StandardPlugin { val name: String = "initPlugin" override val description: String = "checks that under -Yretain-trees we may get tree for all symbols" - def init(options: List[String]): List[PluginPhase] = + override def initialize(options: List[String])(using Context): List[PluginPhase] = (new SetDefTree) :: (new InitChecker) :: Nil } diff --git a/sbt-test/sbt-dotty/compiler-plugin/plugin/DivideZero.scala b/sbt-test/sbt-dotty/compiler-plugin/plugin/DivideZero.scala index c6fac6b796c0..3d1698250e5d 100644 --- a/sbt-test/sbt-dotty/compiler-plugin/plugin/DivideZero.scala +++ b/sbt-test/sbt-dotty/compiler-plugin/plugin/DivideZero.scala @@ -22,7 +22,8 @@ class DivideZero extends PluginPhase with StandardPlugin { override val runsAfter = Set(Pickler.name) override val runsBefore = Set(Staging.name) - def init(options: List[String]): List[PluginPhase] = this :: Nil + // We keep using deprecated variant here just to ensure it still works correctly + override def init(options: List[String]): List[PluginPhase] = this :: Nil private def isNumericDivide(sym: Symbol)(implicit ctx: Context): Boolean = { def test(tpe: String): Boolean = diff --git a/tests/plugins/custom/analyzer/Analyzer_1.scala b/tests/plugins/custom/analyzer/Analyzer_1.scala index 0e1cc53290d0..d611972e0e48 100644 --- a/tests/plugins/custom/analyzer/Analyzer_1.scala +++ b/tests/plugins/custom/analyzer/Analyzer_1.scala @@ -52,7 +52,7 @@ class InitChecker extends PluginPhase with StandardPlugin { override val runsAfter = Set(SetDefTree.name) override val runsBefore = Set(FirstTransform.name) - def init(options: List[String]): List[PluginPhase] = this :: (new SetDefTree) :: Nil + override def initialize(options: List[String])(using Context): List[PluginPhase] = this :: (new SetDefTree) :: Nil private def checkDef(tree: Tree)(implicit ctx: Context): Tree = { if (tree.symbol.defTree.isEmpty) diff --git a/tests/plugins/neg/divideZero/plugin_1.scala b/tests/plugins/neg/divideZero/plugin_1.scala index ef8e077fd14d..68b2a8eae478 100644 --- a/tests/plugins/neg/divideZero/plugin_1.scala +++ b/tests/plugins/neg/divideZero/plugin_1.scala @@ -20,7 +20,7 @@ class DivideZero extends PluginPhase with StandardPlugin { override val runsAfter = Set(Pickler.name) override val runsBefore = Set(PickleQuotes.name) - override def init(options: List[String]): List[PluginPhase] = this :: Nil + override def initialize(options: List[String])(using Context): List[PluginPhase] = this :: Nil private def isNumericDivide(sym: Symbol)(implicit ctx: Context): Boolean = { def test(tpe: String): Boolean =