Skip to content

Deprecate StandardPlugin.init in favor of initialize method taking implicit Context #20330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should keep it abstract? Otherwise, somebody might forget to override one of the variants. Would still make sense, as most of compiler plugins don't use context yet, so new variant can be treated as extended, advanced variant


/** 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
Loading