Skip to content

Commit 13487ca

Browse files
Merge pull request #4899 from dotty-staging/fix-#4515
Fix #4515: Synthesize implicit type tags in transparent methods
2 parents 2e834ea + ef22195 commit 13487ca

File tree

31 files changed

+69
-45
lines changed

31 files changed

+69
-45
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,8 +686,11 @@ class Definitions {
686686
def Unpickler_liftedExpr = ctx.requiredMethod("scala.runtime.quoted.Unpickler.liftedExpr")
687687
def Unpickler_unpickleType = ctx.requiredMethod("scala.runtime.quoted.Unpickler.unpickleType")
688688

689-
lazy val TastyTopLevelSpliceModule = ctx.requiredModule("scala.tasty.TopLevelSplice")
690-
lazy val TastyTopLevelSplice_tastyContext = TastyTopLevelSpliceModule.requiredMethod("tastyContext")
689+
lazy val TastyTastyType = ctx.requiredClassRef("scala.tasty.Tasty")
690+
def TastyTastyClass(implicit ctx: Context) = TastyTastyType.symbol.asClass
691+
692+
lazy val TastyTastyModule = ctx.requiredModule("scala.tasty.Tasty")
693+
lazy val TastyTasty_macroContext = TastyTastyModule.requiredMethod("macroContext")
691694

692695
lazy val EqType = ctx.requiredClassRef("scala.Eq")
693696
def EqClass(implicit ctx: Context) = EqType.symbol.asClass

compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class ReifyQuotes extends MacroTransformWithImplicits {
237237
def levelOK(sym: Symbol)(implicit ctx: Context): Boolean = levelOf.get(sym) match {
238238
case Some(l) =>
239239
l == level ||
240-
level == -1 && sym == defn.TastyTopLevelSplice_tastyContext
240+
level == -1 && sym == defn.TastyTasty_macroContext
241241
case None =>
242242
!sym.is(Param) || levelOK(sym.owner)
243243
}

compiler/src/dotty/tools/dotc/transform/Splicer.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ object Splicer {
268268
case Literal(Constant(value)) =>
269269
interpretLiteral(value)
270270

271-
case _ if tree.symbol == defn.TastyTopLevelSplice_tastyContext =>
271+
case _ if tree.symbol == defn.TastyTasty_macroContext =>
272272
interpretTastyContext()
273273

274274
case StaticMethodCall(fn, args) =>
@@ -286,6 +286,8 @@ object Splicer {
286286
case NamedArg(_, arg) => interpretTree(arg)
287287
case Ident(name) if env.contains(name) => env(name)
288288

289+
case Inlined(EmptyTree, Nil, expansion) => interpretTree(expansion)
290+
289291
case _ => unexpectedTree(tree)
290292
}
291293

compiler/src/dotty/tools/dotc/typer/Implicits.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,10 +620,16 @@ trait Implicits { self: Typer =>
620620
val tag = bindFreeVars(arg)
621621
if (bindFreeVars.ok) ref(defn.QuotedType_apply).appliedToType(tag)
622622
else EmptyTree
623+
case arg :: Nil if ctx.inRewriteMethod =>
624+
ref(defn.QuotedType_apply).appliedToType(arg)
623625
case _ =>
624626
EmptyTree
625627
}
626628

629+
def synthesizedTastyContext(formal: Type): Tree =
630+
if (ctx.inRewriteMethod || enclosingInlineds.nonEmpty) ref(defn.TastyTasty_macroContext)
631+
else EmptyTree
632+
627633
/** If `formal` is of the form Eq[T, U], where no `Eq` instance exists for
628634
* either `T` or `U`, synthesize `Eq.eqAny[T, U]` as solution.
629635
*/
@@ -694,7 +700,8 @@ trait Implicits { self: Typer =>
694700
else
695701
trySpecialCase(defn.ClassTagClass, synthesizedClassTag,
696702
trySpecialCase(defn.QuotedTypeClass, synthesizedTypeTag,
697-
trySpecialCase(defn.EqClass, synthesizedEq, failed)))
703+
trySpecialCase(defn.TastyTastyClass, synthesizedTastyContext,
704+
trySpecialCase(defn.EqClass, synthesizedEq, failed))))
698705
}
699706
}
700707

library/src/scala/tasty/Tasty.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ abstract class Tasty
1919
with TreeOps
2020
with TypeOrBoundsTreeOps
2121
with TypeOrBoundsOps
22+
23+
object Tasty {
24+
/** Compiler tasty context available in a top level ~ of a transparent macro */
25+
def macroContext: Tasty = throw new Exception("Not in transparent macro.")
26+
}

library/src/scala/tasty/TopLevelSplice.scala

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/neg/tasty-macro-assert/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ object Asserts {
1212
object Ops
1313

1414
rewrite def macroAssert(cond: => Boolean): Unit =
15-
~impl('(cond))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
15+
~impl('(cond))
1616

1717
def impl(cond: Expr[Boolean])(implicit tasty: Tasty): Expr[Unit] = {
1818
import tasty._

tests/run-with-compiler/tasty-extractors-constants-2/quoted_1.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import scala.tasty.util._
66

77
object Macros {
88

9-
rewrite def testMacro: Unit =
10-
~impl(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
9+
rewrite def testMacro: Unit = ~impl
1110

1211
def impl(implicit tasty: Tasty): Expr[Unit] = {
1312
// 2 is a lifted constant

tests/run/i4515/Macro_1.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
object Macro {
3+
rewrite def foo[X](x: X): Unit = ~fooImpl('(x))
4+
def fooImpl[X: quoted.Type](x: quoted.Expr[X]): quoted.Expr[Unit] = '()
5+
}

tests/run/i4515/Test_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
object Test {
3+
def main(args: Array[String]): Unit = {
4+
Macro.foo(4)
5+
}
6+
}

tests/run/i4515b/Macro_1.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import scala.tasty.Tasty
2+
3+
object Macro {
4+
rewrite def foo: Unit = ~fooImpl
5+
def fooImpl(implicit tasty: Tasty): quoted.Expr[Unit] = '()
6+
}

tests/run/i4515b/Test_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
object Test {
3+
def main(args: Array[String]): Unit = {
4+
Macro.foo
5+
}
6+
}

tests/run/tasty-custom-show/quoted_1.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import scala.quoted._
22

3-
import scala.tasty.TopLevelSplice
43
import scala.tasty.Tasty
54
import scala.tasty.util.{TreeTraverser, Show}
65

76
object Macros {
87

98
implicit rewrite def printOwners[T](x: => T): Unit =
10-
~impl('(x))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
9+
~impl('(x))
1110

1211
def impl[T](x: Expr[T])(implicit tasty: Tasty): Expr[Unit] = {
1312
import tasty._

tests/run/tasty-definitions-1/quoted_1.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import scala.tasty.util.TreeTraverser
55

66
object Macros {
77

8-
rewrite def testDefinitions(): Unit =
9-
~testDefinitionsImpl(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
8+
rewrite def testDefinitions(): Unit = ~testDefinitionsImpl
109

1110
def testDefinitionsImpl(implicit tasty: Tasty): Expr[Unit] = {
1211
import tasty._

tests/run/tasty-definitions-2/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import scala.tasty._
44
object Foo {
55

66
rewrite def inspectBody(i: => Int): String =
7-
~inspectBodyImpl('(i))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
7+
~inspectBodyImpl('(i))
88

99
def inspectBodyImpl(x: Expr[Int])(implicit tasty: Tasty): Expr[String] = {
1010
import tasty._

tests/run/tasty-definitions-3/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import scala.tasty._
44
object Foo {
55

66
rewrite def inspectBody(i: => Int): String =
7-
~inspectBodyImpl('(i))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
7+
~inspectBodyImpl('(i))
88

99
def inspectBodyImpl(x: Expr[Int])(implicit tasty: Tasty): Expr[String] = {
1010
import tasty._

tests/run/tasty-eval/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import scala.tasty.util.TreeTraverser
66
object Macros {
77

88
implicit rewrite def foo(i: Int): String =
9-
~impl('(i))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
9+
~impl('(i))
1010

1111
def impl(i: Expr[Int])(implicit tasty: Tasty): Expr[String] = {
1212
value(i).toString.toExpr

tests/run/tasty-extractors-1/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import scala.tasty._
55
object Macros {
66

77
implicit rewrite def printTree[T](x: => T): Unit =
8-
~impl('(x))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
8+
~impl('(x))
99

1010
def impl[T](x: Expr[T])(implicit tasty: Tasty): Expr[Unit] = {
1111
import tasty._

tests/run/tasty-extractors-2/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import scala.tasty._
55
object Macros {
66

77
implicit rewrite def printTree[T](x: => T): Unit =
8-
~impl('(x))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
8+
~impl('(x))
99

1010
def impl[T](x: Expr[T])(implicit tasty: Tasty): Expr[Unit] = {
1111
import tasty._

tests/run/tasty-extractors-3/quoted_1.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import scala.quoted._
22

3-
import scala.tasty.TopLevelSplice
43
import scala.tasty.Tasty
54
import scala.tasty.util.TreeTraverser
65

76
object Macros {
87

98
implicit rewrite def printTypes[T](x: => T): Unit =
10-
~impl('(x))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
9+
~impl('(x))
1110

1211
def impl[T](x: Expr[T])(implicit tasty: Tasty): Expr[Unit] = {
1312
import tasty._

tests/run/tasty-extractors-constants-1/quoted_1.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import scala.tasty.util._
55

66
object Macros {
77

8-
implicit rewrite def testMacro: Unit =
9-
~impl(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
8+
implicit rewrite def testMacro: Unit = ~impl
109

1110
def impl(implicit tasty: Tasty): Expr[Unit] = {
1211
import tasty._

tests/run/tasty-extractors-owners/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import scala.tasty.util.TreeTraverser
66
object Macros {
77

88
implicit rewrite def printOwners[T](x: => T): Unit =
9-
~impl('(x))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
9+
~impl('(x))
1010

1111
def impl[T](x: Expr[T])(implicit tasty: Tasty): Expr[Unit] = {
1212
import tasty._

tests/run/tasty-extractors-types/quoted_1.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import scala.tasty.util.TreeTraverser
55

66
object Macros {
77

8-
implicit rewrite def printType[T]: Unit =
9-
~impl('[T])(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
8+
implicit rewrite def printType[T]: Unit = ~impl('[T])
109

1110
def impl[T](x: Type[T])(implicit tasty: Tasty): Expr[Unit] = {
1211
import tasty._

tests/run/tasty-getfile/Macro_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import scala.quoted._
2-
import scala.tasty.{Tasty, TopLevelSplice}
2+
import scala.tasty.Tasty
33

44
object SourceFiles {
55

66
implicit rewrite def getThisFile: String =
7-
~getThisFileImpl(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
7+
~getThisFileImpl
88

99
private def getThisFileImpl(implicit tasty: Tasty): Expr[String] = {
1010
import tasty._

tests/run/tasty-indexed-map/quoted_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ object Index {
2424

2525
implicit def zero[K, T]: Index[K, (K, T)] = new Index(0)
2626

27-
implicit rewrite def succ[K, H, T](implicit prev: => Index[K, T]): Index[K, (H, T)] = ~succImpl(TopLevelSplice.tastyContext)('[K], '[H], '[T])
27+
implicit rewrite def succ[K, H, T](implicit prev: => Index[K, T]): Index[K, (H, T)] = ~succImpl[K, H, T]
2828

29-
def succImpl[K, H, T](tasty: Tasty)(implicit k: Type[K], h: Type[H], t: Type[T]): Expr[Index[K, (H, T)]] = {
29+
def succImpl[K, H, T](implicit tasty: Tasty, k: Type[K], h: Type[H], t: Type[T]): Expr[Index[K, (H, T)]] = {
3030
import tasty._
3131

3232
def name(tp: TypeOrBounds): String = tp match {

tests/run/tasty-linenumber-2/quoted_1.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ class LineNumber(val value: Int) {
88

99
object LineNumber {
1010

11-
implicit rewrite def line: LineNumber =
12-
~lineImpl(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
11+
implicit rewrite def line: LineNumber = ~lineImpl
1312

1413
def lineImpl(implicit tasty: Tasty): Expr[LineNumber] = {
1514
import tasty._

tests/run/tasty-linenumber/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class LineNumber(val value: Int) {
99
object LineNumber {
1010

1111
implicit rewrite def line[T >: Unit <: Unit]: LineNumber =
12-
~lineImpl('[T])(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
12+
~lineImpl('[T])
1313

1414
def lineImpl(x: Type[Unit])(implicit tasty: Tasty): Expr[LineNumber] = {
1515
import tasty._

tests/run/tasty-location/quoted_1.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ case class Location(owners: List[String])
66

77
object Location {
88

9-
implicit rewrite def location: Location =
10-
~impl(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
9+
implicit rewrite def location: Location = ~impl
1110

1211
def impl(implicit tasty: Tasty): Expr[Location] = {
1312
import tasty._

tests/run/tasty-macro-assert/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ object Asserts {
1212
object Ops
1313

1414
rewrite def macroAssert(cond: => Boolean): Unit =
15-
~impl('(cond))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
15+
~impl('(cond))
1616

1717
def impl(cond: Expr[Boolean])(implicit tasty: Tasty): Expr[Unit] = {
1818
import tasty._

tests/run/tasty-positioned/quoted_1.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ case class Positioned[T](value: T, position: Position)
99

1010
object Positioned {
1111

12-
implicit rewrite def apply[T](x: => T): Positioned[T] =
13-
~impl('(x))('[T], TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
12+
implicit rewrite def apply[T](x: => T): Positioned[T] = ~impl('(x))
1413

1514
def impl[T](x: Expr[T])(implicit ev: Type[T], tasty: Tasty): Expr[Positioned[T]] = {
1615
import tasty.{Position => _, _}

tests/run/tasty-subtyping/quoted_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import scala.tasty._
55
object Macros {
66

77
rewrite def isTypeEqual[T, U]: Boolean =
8-
~isTypeEqualImpl('[T], '[U])(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
8+
~isTypeEqualImpl('[T], '[U])
99

1010
rewrite def isSubTypeOf[T, U]: Boolean =
11-
~isSubTypeOfImpl('[T], '[U])(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
11+
~isSubTypeOfImpl('[T], '[U])
1212

1313
def isTypeEqualImpl[T, U](t: Type[T], u: Type[U])(implicit tasty: Tasty): Expr[Boolean] = {
1414
import tasty._

0 commit comments

Comments
 (0)