Skip to content

Remove implicit tasty.Reflection synthesis #6823

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
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
3 changes: 0 additions & 3 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -805,9 +805,6 @@ class Definitions {
@threadUnsafe lazy val TastyReflectionType: TypeRef = ctx.requiredClassRef("scala.tasty.Reflection")
def TastyReflectionClass(implicit ctx: Context): ClassSymbol = TastyReflectionType.symbol.asClass

@threadUnsafe lazy val TastyReflectionModule: TermSymbol = ctx.requiredModule("scala.tasty.Reflection")
@threadUnsafe lazy val TastyReflection_macroContext: TermSymbol = TastyReflectionModule.requiredMethod("macroContext")

@threadUnsafe lazy val EqlType: TypeRef = ctx.requiredClassRef("scala.Eql")
def EqlClass(implicit ctx: Context): ClassSymbol = EqlType.symbol.asClass
def EqlModule(implicit ctx: Context): Symbol = EqlClass.companionModule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(
case Some(l) =>
l == level ||
level == -1 && (
sym == defn.TastyReflection_macroContext ||
sym == defn.QuoteContext_macroContext ||
// here we assume that Splicer.canBeSpliced was true before going to level -1,
// this implies that all non-inline arguments are quoted and that the following two cases are checked
Expand Down
6 changes: 0 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ object Splicer {
protected def interpretVarargs(args: List[Object])(implicit env: Env): Object =
args.toSeq

protected def interpretTastyContext()(implicit env: Env): Object = ReflectionImpl(ctx, pos)
protected def interpretQuoteContext()(implicit env: Env): Object =
new scala.quoted.QuoteContext(ReflectionImpl(ctx, pos))

Expand Down Expand Up @@ -278,7 +277,6 @@ object Splicer {
protected def interpretTypeQuote(tree: tpd.Tree)(implicit env: Env): Unit = ()
protected def interpretLiteral(value: Any)(implicit env: Env): Unit = ()
protected def interpretVarargs(args: List[Unit])(implicit env: Env): Unit = ()
protected def interpretTastyContext()(implicit env: Env): Unit = ()
protected def interpretQuoteContext()(implicit env: Env): Unit = ()
protected def interpretStaticMethodCall(module: Symbol, fn: Symbol, args: => List[Unit])(implicit env: Env): Unit = args.foreach(identity)
protected def interpretModuleAccess(fn: Symbol)(implicit env: Env): Unit = ()
Expand Down Expand Up @@ -314,7 +312,6 @@ object Splicer {
protected def interpretTypeQuote(tree: Tree)(implicit env: Env): Result
protected def interpretLiteral(value: Any)(implicit env: Env): Result
protected def interpretVarargs(args: List[Result])(implicit env: Env): Result
protected def interpretTastyContext()(implicit env: Env): Result
protected def interpretQuoteContext()(implicit env: Env): Result
protected def interpretStaticMethodCall(module: Symbol, fn: Symbol, args: => List[Result])(implicit env: Env): Result
protected def interpretModuleAccess(fn: Symbol)(implicit env: Env): Result
Expand All @@ -338,9 +335,6 @@ object Splicer {
case Literal(Constant(value)) =>
interpretLiteral(value)

case _ if tree.symbol == defn.TastyReflection_macroContext =>
interpretTastyContext()

case _ if tree.symbol == defn.QuoteContext_macroContext =>
interpretQuoteContext()

Expand Down
6 changes: 0 additions & 6 deletions compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -702,11 +702,6 @@ trait Implicits { self: Typer =>
}
}

lazy val synthesizedTastyContext: SpecialHandler =
(formal, span) => implicit ctx =>
if (ctx.inInlineMethod || enclosingInlineds.nonEmpty) ref(defn.TastyReflection_macroContext)
else EmptyTree

Copy link
Contributor

Choose a reason for hiding this comment

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

Is it too early to remove it? Or we may assume that if people are experimenting with new macros, they can tolerate such changes without a transition period?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For most things we already require a QuoteContext with a stable path which this implicit conversion does not provide. Hence using Reflection may easily lead to confusing errors. Having a single way to do things will be clearer. This change also aims to align fully with the documentation and fail where the code is using a pattern that will probably not work at long terms.

lazy val synthesizedQuoteContext: SpecialHandler =
(formal, span) => implicit ctx =>
if (ctx.inInlineMethod || enclosingInlineds.nonEmpty) ref(defn.QuoteContext_macroContext)
Expand Down Expand Up @@ -1039,7 +1034,6 @@ trait Implicits { self: Typer =>
defn.ClassTagClass -> synthesizedClassTag,
defn.QuotedTypeClass -> synthesizedTypeTag,
defn.QuoteContextClass -> synthesizedQuoteContext,
defn.TastyReflectionClass -> synthesizedTastyContext,
defn.EqlClass -> synthesizedEq,
defn.TupledFunctionClass -> synthesizedTupleFunction,
defn.ValueOfClass -> synthesizedValueOf,
Expand Down
6 changes: 0 additions & 6 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,3 @@ class Reflection(val kernel: Kernel)
}

}

object Reflection {
/** Compiler tasty context available in a top level ~ of an inline macro */
@deprecated("Use scala.quoted.QuoteContext instead", "0.17")
def macroContext: Reflection = throw new Exception("Not in inline macro.")
}
6 changes: 1 addition & 5 deletions tests/run-macros/quote-matching-optimize-1/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import scala.quoted._
import scala.quoted.autolift._

import scala.tasty.Reflection

object Macro {

inline def optimize[T](x: => T): Any = ${ Macro.impl('x) }

def impl[T: Type](x: Expr[T]) given Reflection: Expr[Any] = {
val reflect = the[Reflection]
import reflect._ // TODO remove
def impl[T: Type](x: Expr[T]) given QuoteContext: Expr[Any] = {

def optimize(x: Expr[Any]): Expr[Any] = x match {
case '{ type $t; ($ls: List[`$t`]).filter($f).filter($g) } =>
Expand Down
4 changes: 1 addition & 3 deletions tests/run-macros/quote-matching-optimize-2/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ object Macro {

inline def optimize[T](x: => T): Any = ${ Macro.impl('x) }

def impl[T: Type](x: Expr[T]) given Reflection: Expr[Any] = {
val reflect = the[Reflection]
import reflect._ // TODO remove
def impl[T: Type](x: Expr[T]) given QuoteContext: Expr[Any] = {

def optimize(x: Expr[Any]): Expr[Any] = x match {
case '{ ($ls: List[$t]).filter($f).filter($g) } =>
Expand Down
4 changes: 1 addition & 3 deletions tests/run-macros/quote-matching-optimize-3/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ object Macro {

inline def optimize[T](x: => T): Any = ${ Macro.impl('x) }

def impl[T: Type](x: Expr[T]) given Reflection: Expr[Any] = {
val reflect = the[Reflection]
import reflect._ // TODO remove
def impl[T: Type](x: Expr[T]) given QuoteContext: Expr[Any] = {

def optimize(x: Expr[Any]): Expr[Any] = x match {
case '{ ($ls: List[$t]).filter($f).filter($g) } =>
Expand Down
3 changes: 1 addition & 2 deletions tests/run-macros/quoted-pattern-type/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import scala.quoted._
import scala.tasty.Reflection

object Lib {

inline def foo[T](arg: => T): T = ${ impl('arg) }

private def impl[T: Type](arg: Expr[T])(implicit refl: Reflection): Expr[T] = {
private def impl[T: Type](arg: Expr[T]) given QuoteContext: Expr[T] = {
arg match {
case e @ '{ $x: Boolean } => '{ println("Boolean: " + $e); $e }
case e @ '{ $x: Int } => '{ println("Int: " + $x); $x }
Expand Down
5 changes: 2 additions & 3 deletions tests/run-macros/reflect-lambda/assert_1.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import scala.quoted._
import scala.tasty._

object lib {

inline def assert(condition: => Boolean): Unit = ${ assertImpl('condition, '{""}) }

def assertImpl(cond: Expr[Boolean], clue: Expr[Any])(implicit refl: Reflection): Expr[Unit] = {
import refl._
def assertImpl(cond: Expr[Boolean], clue: Expr[Any]) given (qctx: QuoteContext): Expr[Unit] = {
import qctx.tasty._
import util._

cond.unseal.underlyingArgument match {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import scala.quoted._
import scala.quoted.matching._

import scala.tasty.Reflection

import scala.internal.quoted.Matcher._
import scala.internal.Quoted._

object Macros {

inline def swapFandG(x: => Unit): Unit = ${impl('x)}

private def impl(x: Expr[Unit])(implicit reflect: Reflection): Expr[Unit] = {
private def impl(x: Expr[Unit]) given QuoteContext: Expr[Unit] = {
x match {
case '{ DSL.f[$t]($x) } => '{ DSL.g[$t]($x) }
case '{ DSL.g[$t]($x) } => '{ DSL.f[$t]($x) }
Expand Down
5 changes: 2 additions & 3 deletions tests/run-with-compiler/reflect-sourceCode/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import scala.quoted._
import scala.tasty._

object api {
inline def (x: => T) reflect[T] : String =
${ reflImpl('x) }

private def reflImpl[T](x: Expr[T])(implicit refl: Reflection): Expr[String] = {
import refl._
private def reflImpl[T](x: Expr[T])(implicit qctx: QuoteContext): Expr[String] = {
import qctx.tasty._
x.unseal.pos.sourceCode.toExpr
}
}