Skip to content

Commit 5f574a6

Browse files
author
builduser
committed
Merged branch idea251.release-pavelfatin into idea251.release
2 parents 5196556 + c28b797 commit 5f574a6

File tree

6 files changed

+74
-4
lines changed

6 files changed

+74
-4
lines changed

scala/compiler-plugin/scala-2.12/src/CompilerPlugin.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import CompilerPlugin._
22

3+
import scala.reflect.internal.util.RangePosition
34
import scala.tools.nsc.plugins.{Plugin, PluginComponent}
45
import scala.tools.nsc.reporters.ForwardingReporter
56
import scala.tools.nsc.{Global, Phase}
@@ -39,7 +40,7 @@ class CompilerPlugin(val global: Global) extends Plugin {
3940
private val transformer = new Transformer() {
4041
override def transform(tree: Tree): Tree = {
4142
tree.attachments.get[analyzer.MacroExpansionAttachment] match {
42-
case Some(analyzer.MacroExpansionAttachment(expandee, expanded: Tree)) if isWhiteboxMacro(global)(expandee.symbol) =>
43+
case Some(analyzer.MacroExpansionAttachment(expandee, expanded: Tree)) if expandee.pos.isInstanceOf[RangePosition] && isWhiteboxMacro(global)(expandee.symbol) =>
4344
// If there's a type mismatch, the type checker replaces the tree type with an ErrorType (see TyperErrorGen.issueError)
4445
val tpe = if (tree.tpe.isError) typer.typed(expandee).tpe else tree.tpe
4546
val s = LiteralTypePattern.replaceAllIn(tpe.toString, _.group(1))

scala/compiler-plugin/scala-2.12/test/CompilerPluginTest.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,30 @@ class CompilerPluginTest {
6666
usage("val v1 = Macros.id(1); val v2 = Macros.id(2)"))(
6767
info("Macros.id(1)", tpe("1")), info("Macros.id(2)", tpe("2")))
6868

69+
// Sequence
70+
71+
@Test def sequence1(): Unit = assertMessagesAre(
72+
"""object Macros {
73+
| def f_impl(c: scala.reflect.macros.whitebox.Context)(x: c.Expr[Any]): c.Expr[Any] = x
74+
| def f(x: Any): Any = macro f_impl
75+
| def g_impl(c: scala.reflect.macros.whitebox.Context)(y: c.Expr[Any]): c.Expr[Any] = y
76+
| def g(y: Any): Any = macro g_impl
77+
|}""".stripMargin,
78+
usage("val v = Macros.g(Macros.f(123))"))(
79+
info("Macros.g(Macros.f(123))", tpe("123")))//, info("Macros.f(123)", tpe("123"))) TODO implement
80+
81+
@Test def sequence2(): Unit = assertMessagesAre(
82+
"""object Macros1 {
83+
| def f_impl(c: scala.reflect.macros.whitebox.Context)(x: c.Expr[Any]): c.Expr[Any] = x
84+
| def f(x: Any): Any = macro f_impl
85+
|}""".stripMargin,
86+
"""object Macros2 {
87+
| def g_impl(c: scala.reflect.macros.whitebox.Context)(y: c.Expr[Any]): c.Tree = { import c.universe._; q"{ (); Macros1.f($y) }" }
88+
| def g(y: Any): Any = macro g_impl
89+
|}""".stripMargin,
90+
usage("val v = Macros2.g(123)"))(
91+
info("Macros2.g(123)", tpe("Int"))) // Why not "123" (as in 2.13)?
92+
6993
// Type parameter
7094

7195
@Test def typeParameter(): Unit = assertMessagesAre(

scala/compiler-plugin/scala-2.13/src/CompilerPlugin.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import CompilerPlugin._
22

3+
import scala.reflect.internal.util.RangePosition
34
import scala.tools.nsc.plugins.{Plugin, PluginComponent}
45
import scala.tools.nsc.reporters.ForwardingReporter
56
import scala.tools.nsc.{Global, Phase}
@@ -39,7 +40,7 @@ class CompilerPlugin(val global: Global) extends Plugin {
3940
private val transformer = new Transformer() {
4041
override def transform(tree: Tree): Tree = {
4142
tree.attachments.get[analyzer.MacroExpansionAttachment] match {
42-
case Some(analyzer.MacroExpansionAttachment(expandee, expanded: Tree)) if isWhiteboxMacro(global)(expandee.symbol) =>
43+
case Some(analyzer.MacroExpansionAttachment(expandee, expanded: Tree)) if expandee.pos.isInstanceOf[RangePosition] && isWhiteboxMacro(global)(expandee.symbol) =>
4344
// If there's a type mismatch, the type checker replaces the tree type with an ErrorType (see TyperErrorGen.issueError)
4445
val tpe = if (tree.tpe.isError) typer.typed(expandee).tpe else tree.tpe
4546
val s = LiteralTypePattern.replaceAllIn(tpe.toString, _.group(1))

scala/compiler-plugin/scala-2.13/test/CompilerPluginTest.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,30 @@ class CompilerPluginTest {
6666
usage("val v1 = Macros.id(1); val v2 = Macros.id(2)"))(
6767
info("Macros.id(1)", tpe("1")), info("Macros.id(2)", tpe("2")))
6868

69+
// Sequence
70+
71+
@Test def sequence1(): Unit = assertMessagesAre(
72+
"""object Macros {
73+
| def f_impl(c: scala.reflect.macros.whitebox.Context)(x: c.Expr[Any]): c.Expr[Any] = x
74+
| def f(x: Any): Any = macro f_impl
75+
| def g_impl(c: scala.reflect.macros.whitebox.Context)(y: c.Expr[Any]): c.Expr[Any] = y
76+
| def g(y: Any): Any = macro g_impl
77+
|}""".stripMargin,
78+
usage("val v = Macros.g(Macros.f(123))"))(
79+
info("Macros.g(Macros.f(123))", tpe("123")))//, info("Macros.f(123)", tpe("123"))) TODO implement
80+
81+
@Test def sequence2(): Unit = assertMessagesAre(
82+
"""object Macros1 {
83+
| def f_impl(c: scala.reflect.macros.whitebox.Context)(x: c.Expr[Any]): c.Expr[Any] = x
84+
| def f(x: Any): Any = macro f_impl
85+
|}""".stripMargin,
86+
"""object Macros2 {
87+
| def g_impl(c: scala.reflect.macros.whitebox.Context)(y: c.Expr[Any]): c.Tree = { import c.universe._; q"{ (); Macros1.f($y) }" }
88+
| def g(y: Any): Any = macro g_impl
89+
|}""".stripMargin,
90+
usage("val v = Macros2.g(123)"))(
91+
info("Macros2.g(123)", tpe("123")))
92+
6993
// Type parameter
7094

7195
@Test def typeParameter(): Unit = assertMessagesAre(

scala/compiler-plugin/scala-3.3/src/CompilerPlugin.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ private object CompilerPlugin:
3737
// Print types even if there are errors
3838
override def isRunnable(using Context): Boolean = true
3939

40-
override def transformInlined(tree: tpd.Inlined)(using Context): tpd.Tree =
40+
override def transformInlined(tree: tpd.Inlined)(using context: Context): tpd.Tree =
4141
// Skip recursive inlining
42-
if (!tree.call.isEmpty) {
42+
if (!tree.call.isEmpty && tree.source == context.compilationUnit.source) {
4343
// Use -Ytest-pickler to print canonical types (see PlainPrinter.homogenizedView)
4444
val printer = new TypePrinter(ctx.fresh.setSetting(ctx.settings.YtestPickler, true))
4545
// Handle possible ErrorType("Type mismatch"), e.g. val x: Foo = bar()

scala/compiler-plugin/scala-3.3/test/CompilerPluginTest.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ class CompilerPluginTest {
5858
"val _ = id(1); val _ = id(2)")(
5959
info("id(1)", tpe("1")), info("id(2)", tpe("2")))
6060

61+
// Sequence
62+
63+
@Test def sequence1(): Unit = assertMessagesAre(
64+
"transparent inline def f(x: Any): Any = x",
65+
"transparent inline def g(y: Any): Any = y",
66+
"val _ = g(f(123))")(
67+
info("g(f(123))", tpe("123")))//, info("f(123)", tpe("123"))) TODO implement
68+
69+
70+
@Test def sequence2(): Unit = assertMessagesAre(
71+
"transparent inline def f(x: Any): Any = x",
72+
"transparent inline def g(y: Any): Any = f(y)",
73+
"val _ = g(123)")(
74+
info("g(123)", tpe("123")))
75+
76+
// TODO implement
77+
// @Test def sequence3(): Unit = assertMessagesAre(
78+
// "transparent inline def f(x: Any): Any = x; transparent inline def g(y: Any): Any = f(y); val _ = g(123)")(
79+
// info("g(123)", tpe("123")))
80+
6181
// Complex
6282

6383
@Test def complexMethod(): Unit = assertMessagesAre(

0 commit comments

Comments
 (0)