Skip to content

Commit 11f01d2

Browse files
WojciechMazurKordyjan
authored andcommitted
Deprecate StandardPlugin.init in favor of initialize method taking implicit Context (#20330)
We do deprecate `StandardPlugin.init` in favour of `StandardPlugin.initialize` method tak takes additional `Context` parameter - it would e.g. allow to use reporting mechanism when parsing compiler plugin options. Introduces changes to akka/akka fork used in Community Build [Cherry-picked 1276034]
1 parent 3e1c4de commit 11f01d2

File tree

9 files changed

+26
-11
lines changed

9 files changed

+26
-11
lines changed

compiler/src/dotty/tools/dotc/plugins/Plugin.scala

+15-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import java.io.InputStream
1313
import java.util.Properties
1414

1515
import scala.util.{ Try, Success, Failure }
16+
import scala.annotation.nowarn
1617

1718
trait PluginPhase extends MiniPhase {
1819
def runsBefore: Set[String] = Set.empty
@@ -50,7 +51,20 @@ trait StandardPlugin extends Plugin {
5051
* @param options commandline options to the plugin.
5152
* @return a list of phases to be added to the phase plan
5253
*/
53-
def init(options: List[String]): List[PluginPhase]
54+
@deprecatedOverriding("Method 'init' does not allow to access 'Context', use 'initialize' instead.", since = "Scala 3.5.0")
55+
@deprecated("Use 'initialize' instead.", since = "Scala 3.5.0")
56+
def init(options: List[String]): List[PluginPhase] = Nil
57+
58+
/** Non-research plugins should override this method to return the phases
59+
*
60+
* The phases returned must be freshly constructed (not reused
61+
* and returned again on subsequent calls).
62+
*
63+
* @param options commandline options to the plugin.
64+
* @return a list of phases to be added to the phase plan
65+
*/
66+
@nowarn("cat=deprecation")
67+
def initialize(options: List[String])(using Context): List[PluginPhase] = init(options)
5468
}
5569

5670
/** A research plugin may customize the compilation pipeline freely

compiler/src/dotty/tools/dotc/plugins/Plugins.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ trait Plugins {
125125
}
126126

127127
// schedule plugins according to ordering constraints
128-
val pluginPhases = plugins.collect { case p: StandardPlugin => p }.flatMap { plug => plug.init(options(plug)) }
128+
val pluginPhases = plugins.collect { case p: StandardPlugin => p }.flatMap { plug => plug.initialize(options(plug)) }
129129
val updatedPlan = Plugins.schedule(plan, pluginPhases)
130130

131131
// add research plugins

docs/_docs/reference/changed-features/compiler-plugins.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class DivideZero extends StandardPlugin:
6767
val name: String = "divideZero"
6868
override val description: String = "divide zero check"
6969

70-
def init(options: List[String]): List[PluginPhase] =
70+
override def initialize(options: List[String])(using Context): List[PluginPhase] =
7171
(new DivideZeroPhase) :: Nil
7272

7373
class DivideZeroPhase extends PluginPhase:
@@ -90,7 +90,7 @@ end DivideZeroPhase
9090
```
9191

9292
The plugin main class (`DivideZero`) must extend the trait `StandardPlugin`
93-
and implement the method `init` that takes the plugin's options as argument
93+
and implement the method `initialize` that takes the plugin's options as argument
9494
and returns a list of `PluginPhase`s to be inserted into the compilation pipeline.
9595

9696
Our plugin adds one compiler phase to the pipeline. A compiler phase must extend

docs/_spec/TODOreference/changed-features/compiler-plugins.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class DivideZero extends StandardPlugin:
6767
val name: String = "divideZero"
6868
override val description: String = "divide zero check"
6969

70-
def init(options: List[String]): List[PluginPhase] =
70+
override def initialize(options: List[String])(using Context): List[PluginPhase] =
7171
(new DivideZeroPhase) :: Nil
7272

7373
class DivideZeroPhase extends PluginPhase:
@@ -90,7 +90,7 @@ end DivideZeroPhase
9090
```
9191

9292
The plugin main class (`DivideZero`) must extend the trait `StandardPlugin`
93-
and implement the method `init` that takes the plugin's options as argument
93+
and implement the method `initialize` that takes the plugin's options as argument
9494
and returns a list of `PluginPhase`s to be inserted into the compilation pipeline.
9595

9696
Our plugin adds one compiler phase to the pipeline. A compiler phase must extend

sbt-test/sbt-dotty/analyzer-plugin/plugin/Analyzer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class InitPlugin extends StandardPlugin {
2121
val name: String = "initPlugin"
2222
override val description: String = "checks that under -Yretain-trees we may get tree for all symbols"
2323

24-
def init(options: List[String]): List[PluginPhase] =
24+
override def initialize(options: List[String])(using Context): List[PluginPhase] =
2525
(new SetDefTree) :: (new InitChecker) :: Nil
2626
}
2727

sbt-test/sbt-dotty/compiler-plugin/plugin/DivideZero.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class DivideZero extends PluginPhase with StandardPlugin {
2222
override val runsAfter = Set(Pickler.name)
2323
override val runsBefore = Set(Staging.name)
2424

25-
def init(options: List[String]): List[PluginPhase] = this :: Nil
25+
// We keep using deprecated variant here just to ensure it still works correctly
26+
override def init(options: List[String]): List[PluginPhase] = this :: Nil
2627

2728
private def isNumericDivide(sym: Symbol)(implicit ctx: Context): Boolean = {
2829
def test(tpe: String): Boolean =

tests/plugins/custom/analyzer/Analyzer_1.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class InitChecker extends PluginPhase with StandardPlugin {
5252
override val runsAfter = Set(SetDefTree.name)
5353
override val runsBefore = Set(FirstTransform.name)
5454

55-
def init(options: List[String]): List[PluginPhase] = this :: (new SetDefTree) :: Nil
55+
override def initialize(options: List[String])(using Context): List[PluginPhase] = this :: (new SetDefTree) :: Nil
5656

5757
private def checkDef(tree: Tree)(implicit ctx: Context): Tree = {
5858
if (tree.symbol.defTree.isEmpty)

tests/plugins/neg/divideZero/plugin_1.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class DivideZero extends PluginPhase with StandardPlugin {
2020
override val runsAfter = Set(Pickler.name)
2121
override val runsBefore = Set(PickleQuotes.name)
2222

23-
override def init(options: List[String]): List[PluginPhase] = this :: Nil
23+
override def initialize(options: List[String])(using Context): List[PluginPhase] = this :: Nil
2424

2525
private def isNumericDivide(sym: Symbol)(implicit ctx: Context): Boolean = {
2626
def test(tpe: String): Boolean =

0 commit comments

Comments
 (0)