Skip to content

Remove reflect refinement form nested quotes #10456

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
18 changes: 2 additions & 16 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3697,21 +3697,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

}

/** Type of a Quotes provided by a splice within a quote that took this context.
* It is only required if working with the reflection API.
*
* Usually it is infered by the quotes an splices typing. But sometimes it is necessary
* to explicitly state that a context is nested as in the following example:
*
* ```scala
* def run(using Quotes)(tree: qctx.reflect.Tree): Unit =
* def nested()(using qctx.Nested): Expr[Int] = '{ ${ makeExpr(tree) } + 1 }
* '{ ${ nested() } + 2 }
* def makeExpr(using Quotes)(tree: qctx.reflect.Tree): Expr[Int] = ???
* ```
*/
type Nested = Quotes {
val reflect: self.reflect.type
}
/** Type of a `Quotes` provided by a splice within a quote that took this context. */
type Nested = Quotes

}
5 changes: 5 additions & 0 deletions tests/neg-macros/i8045.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import scala.quoted._
object Test
def run(using Quotes)(tree: quotes.reflect.Tree): Unit =
'{ ${ makeExpr(tree) } + 1 } // error
def makeExpr(using Quotes)(tree: quotes.reflect.Tree): Expr[Int] = ???
8 changes: 8 additions & 0 deletions tests/neg-macros/i8045b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.quoted._
object Test
def run(using q: Quotes)(tree: q.reflect.Tree): Unit =
def nested()(using q.Nested): Expr[Int] =
'{ ${ makeExpr(tree) } + 1 } // error
'{ ${ nested() } + 2 }

def makeExpr(using q: Quotes)(tree: q.reflect.Tree): Expr[Int] = ???
2 changes: 1 addition & 1 deletion tests/pos-macros/i8045.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import scala.quoted._
object Test
def run(using Quotes)(tree: quotes.reflect.Tree): Unit =
def makeExpr(tree: quotes.reflect.Tree): Expr[Int] = ???
'{ ${ makeExpr(tree) } + 1 }
def makeExpr(using Quotes)(tree: quotes.reflect.Tree): Expr[Int] = ???
2 changes: 1 addition & 1 deletion tests/pos-macros/i8045b.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import scala.quoted._
object Test
def run(using q: Quotes)(tree: q.reflect.Tree): Unit =
def makeExpr(tree: q.reflect.Tree): Expr[Int] = ???
def nested()(using q.Nested): Expr[Int] =
'{ ${ makeExpr(tree) } + 1 }
'{ ${ nested() } + 2 }

def makeExpr(using q: Quotes)(tree: q.reflect.Tree): Expr[Int] = ???
8 changes: 4 additions & 4 deletions tests/run-macros/tasty-macro-positions/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ object Macros {

def impl(x: Expr[Any])(using Quotes) : Expr[Unit] = {
import quotes.reflect._
val pos = Term.of(x).underlyingArgument.pos
val pos = posStr(Term.of(x).underlyingArgument.pos)
val code = Term.of(x).underlyingArgument.show
'{
println(${posStr(pos)})
println($pos)
println(${Expr(code)})
}
}

def impl2[T](using x: Type[T])(using Quotes) : Expr[Unit] = {
import quotes.reflect._
val pos = TypeTree.of[T].pos
val pos = posStr(TypeTree.of[T].pos)
val code = TypeTree.of[T].show
'{
println(${posStr(pos)})
println($pos)
println(${Expr(code)})
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/run-staging/multi-staging.check
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
stage1 code: ((q1: scala.quoted.Quotes) ?=> {
val x1: scala.Int = 2
scala.quoted.runtime.Expr.quote[scala.Int](1.+(scala.quoted.runtime.Expr.nestedSplice[scala.Int](q1)(((evidence$5: q1.Nested) ?=> scala.quoted.Expr.apply[scala.Int](x1)(scala.quoted.Liftable.IntLiftable[scala.Int])(evidence$5))))).apply(using q1)
scala.quoted.runtime.Expr.quote[scala.Int](1.+(scala.quoted.runtime.Expr.nestedSplice[scala.Int](q1)(((evidence$5: scala.quoted.Quotes) ?=> scala.quoted.Expr.apply[scala.Int](x1)(scala.quoted.Liftable.IntLiftable[scala.Int])(evidence$5))))).apply(using q1)
})
3
2 changes: 1 addition & 1 deletion tests/run-staging/quote-nested-2.check
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
((q: scala.quoted.Quotes) ?=> {
val a: scala.quoted.Expr[scala.Int] = scala.quoted.runtime.Expr.quote[scala.Int](4).apply(using q)
((evidence$2: q.Nested) ?=> a).asInstanceOf[scala.ContextFunction1[scala.quoted.Quotes, scala.quoted.Expr[scala.Int]]].apply(using q)
((evidence$2: scala.quoted.Quotes) ?=> a).asInstanceOf[scala.ContextFunction1[scala.quoted.Quotes, scala.quoted.Expr[scala.Int]]].apply(using q)
})
2 changes: 1 addition & 1 deletion tests/run-staging/quote-nested-5.check
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
((q: scala.quoted.Quotes) ?=> {
val a: scala.quoted.Expr[scala.Int] = scala.quoted.runtime.Expr.quote[scala.Int](4).apply(using q)
((q2: scala.quoted.Quotes) ?=> ((evidence$3: q2.Nested) ?=> a).asInstanceOf[scala.ContextFunction1[scala.quoted.Quotes, scala.quoted.Expr[scala.Int]]].apply(using q2)).apply(using q)
((q2: scala.quoted.Quotes) ?=> ((evidence$3: scala.quoted.Quotes) ?=> a).asInstanceOf[scala.ContextFunction1[scala.quoted.Quotes, scala.quoted.Expr[scala.Int]]].apply(using q2)).apply(using q)
})