Skip to content

Commit

Permalink
Deprecate StandardPlugin.init in favor of initialize method takin…
Browse files Browse the repository at this point in the history
…g 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
  • Loading branch information
WojciechMazur authored May 8, 2024
1 parent 3393f7e commit 1276034
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 11 deletions.
16 changes: 15 additions & 1 deletion compiler/src/dotty/tools/dotc/plugins/Plugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -50,7 +51,20 @@ 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]
@deprecatedOverriding("Method 'init' does not allow to access 'Context', use 'initialize' instead.", since = "Scala 3.5.0")
@deprecated("Use 'initialize' instead.", since = "Scala 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
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/plugins/Plugins.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docs/_docs/reference/changed-features/compiler-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docs/_spec/TODOreference/changed-features/compiler-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sbt-test/sbt-dotty/analyzer-plugin/plugin/Analyzer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion sbt-test/sbt-dotty/compiler-plugin/plugin/DivideZero.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/custom/analyzer/Analyzer_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/neg/divideZero/plugin_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 1276034

Please sign in to comment.