Skip to content

Commit c80f36d

Browse files
committed
add non-research plugin tests
1 parent 7de9503 commit c80f36d

File tree

6 files changed

+61
-15
lines changed

6 files changed

+61
-15
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import core._
55
import Contexts._
66
import Phases._
77
import dotty.tools.io._
8+
import transform.MegaPhase.MiniPhase
89

910
import java.io.InputStream
1011

1112
import scala.collection.mutable
1213
import scala.util.{ Try, Success, Failure }
1314

14-
trait PluginPhase extends Phase {
15+
trait PluginPhase extends MiniPhase {
1516
def runsBefore: Set[Class[_ <: Phase]] = Set.empty
1617
}
1718

@@ -22,9 +23,6 @@ trait Plugin {
2223
/** A one-line description of the plugin */
2324
def description: String
2425

25-
/** The phases that this plugin defines */
26-
def components: List[PluginPhase] = Nil
27-
2826
/** Is this plugin a research plugin?
2927
*
3028
* Research plugin receives a phase plan and return a new phase plan, while
@@ -46,13 +44,13 @@ trait Plugin {
4644
*
4745
* @return a list of phases to be added to the phase plan
4846
*/
49-
def init()(implicit ctx: Context): List[Phase] = Nil
47+
def init()(implicit ctx: Context): List[PluginPhase] = ???
5048

5149
/** Research plugins should override this method to return the new phase plan
5250
*
5351
* @return the new phase plan
5452
*/
55-
def init(phases: List[List[Phase]])(implicit ctx: Context): List[List[Phase]] = phases
53+
def init(phases: List[List[Phase]])(implicit ctx: Context): List[List[Phase]] = ???
5654

5755
/** A description of this plugin's options, suitable as a response
5856
* to the -help command-line option. Conventionally, the options

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ trait Plugins {
148148
// add non-research plugins
149149
var updatedPlan = plan
150150
plugins.filter(!_.research).foreach { plug =>
151-
plug.components.foreach { phase =>
151+
plug.init().foreach { phase =>
152152
updateOrdering(phase)
153153

154154
val beforePhases: MSet[Class[_]] = MSet(phase.runsBefore.toSeq: _*)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Test {
2+
val y = 5 / 0 // error
3+
100 + 6 / 0 // error
4+
6L / 0L // error
5+
val z = 7 / 0.0 // error
6+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import dotty.tools.dotc._
2+
import core._
3+
import Contexts.Context
4+
import plugins.Plugin
5+
import Phases.Phase
6+
import ast.tpd
7+
import transform.MegaPhase.MiniPhase
8+
import Decorators._
9+
import Symbols.Symbol
10+
import Constants.Constant
11+
12+
class DivideZero extends MiniPhase with Plugin {
13+
val name: String = "divideZero"
14+
override val description: String = "divide zero check"
15+
16+
override val research = true
17+
18+
val phaseName = name
19+
20+
override def init(phases: List[List[Phase]])(implicit ctx: Context): List[List[Phase]] = {
21+
val (before, after) = phases.span(ps => !ps.exists(_.phaseName == "pickler"))
22+
before ++ (List(this) :: after)
23+
}
24+
25+
private def isNumericDivide(sym: Symbol)(implicit ctx: Context): Boolean = {
26+
def test(tpe: String): Boolean =
27+
(sym.owner eq ctx.requiredClass(tpe.toTermName)) && sym.name.show == "/"
28+
29+
test("scala.Int") || test("scala.Long") || test("scala.Short") || test("scala.FLoat") || test("scala.Double")
30+
}
31+
32+
override def transformApply(tree: tpd.Apply)(implicit ctx: Context): tpd.Tree = tree match {
33+
case tpd.Apply(fun, tpd.Literal(Constants.Constant(v)) :: Nil) if isNumericDivide(fun.symbol) && v == 0 =>
34+
ctx.error("divide by zero", tree.pos)
35+
tree
36+
case _ =>
37+
tree
38+
}
39+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<plugin>
2+
<name>divideZero</name>
3+
<classname>DivideZero</classname>
4+
</plugin>

tests/plugins/neg/divideZero/plugin_1.scala

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
import dotty.tools.dotc._
22
import core._
33
import Contexts.Context
4-
import plugins.Plugin
4+
import plugins.{Plugin, PluginPhase}
55
import Phases.Phase
66
import ast.tpd
77
import transform.MegaPhase.MiniPhase
88
import Decorators._
99
import Symbols.Symbol
1010
import Constants.Constant
11+
import transform.{LinkAll, Pickler}
1112

12-
class DivideZero extends MiniPhase with Plugin {
13+
class DivideZero extends PluginPhase with Plugin {
1314
val name: String = "divideZero"
1415
override val description: String = "divide zero check"
1516

16-
override val research = true
17-
1817
val phaseName = name
1918

20-
override def init(phases: List[List[Phase]])(implicit ctx: Context): List[List[Phase]] = {
21-
val (before, after) = phases.span(ps => !ps.exists(_.phaseName == "pickler"))
22-
before ++ (List(this) :: after)
23-
}
19+
override val runsAfter = Set(classOf[Pickler])
20+
override val runsBefore = Set(classOf[LinkAll])
21+
22+
override def init()(implicit ctx: Context): List[PluginPhase] = this :: Nil
2423

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

0 commit comments

Comments
 (0)