Skip to content

Make sure that QuoteContext are stable #8492

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
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
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ trait QuotesAndSplices {
case _ =>
}
val qctx = inferImplicitArg(defn.QuoteContextClass.typeRef, tree.span)
if (level == 0 && qctx.tpe.isInstanceOf[SearchFailureType])

if qctx.tpe.isInstanceOf[SearchFailureType] then
ctx.error(missingArgMsg(qctx, defn.QuoteContextClass.typeRef, ""), ctx.source.atSpan(tree.span))
else if !qctx.tpe.isStable then
ctx.error(em"Quotes require stable QuoteContext, but found non stable $qctx", qctx.sourcePos)

val tree1 =
if ctx.mode.is(Mode.Pattern) && level == 0 then
typedQuotePattern(tree, pt, qctx)
Expand Down
6 changes: 4 additions & 2 deletions tests/neg-macros/quote-this.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ class Foo {
}

inline def i(): Unit = ${ Foo.impl[Any]('{
given QuoteContext = ???
val x: QuoteContext = ???
given x.type = x
Copy link
Contributor

Choose a reason for hiding this comment

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

/cc @odersky : this seems to be a friction between given and path-dependent types.

'this // error
}) }

inline def j(that: Foo): Unit = ${ Foo.impl[Any]('{
given QuoteContext = ???
val x: QuoteContext = ???
given x.type = x
'that // error
}) }

Expand Down
9 changes: 7 additions & 2 deletions tests/neg/i4044a.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ def test(using QuoteContext) = {

val a = '{1}
'{
given QuoteContext = ???
val qctx: QuoteContext = ???
given qctx.type = qctx
a // error
$a
'{$a} // error
'{ given QuoteContext = ???; '{$a} } // error
'{
val qctx: QuoteContext = ???
given qctx.type = qctx
'{$a} // error
}
}

}
12 changes: 9 additions & 3 deletions tests/neg/i4044b.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ import scala.quoted._
def test(using QuoteContext) = {

'{
given QuoteContext = ???
val qctx: QuoteContext = ???
given qctx.type = qctx

val b = '{3}

'{
given QuoteContext = ???
val qctx: QuoteContext = ???
given qctx.type = qctx

b // error
${b}
${ '{b} } // error
'{ given QuoteContext = ???; '{$b} } // error
'{
val qctx: QuoteContext = ???
given qctx.type = qctx
'{$b} // error
}
}

}
Expand Down
3 changes: 2 additions & 1 deletion tests/neg/i7052b.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import scala.quoted._
class Test {
def foo(str: String)(using QuoteContext) = '{
given QuoteContext = ???
val qctx: QuoteContext = ???
given qctx.type = qctx
'{
@deprecated(str, "") // error
def bar = ???
Expand Down
3 changes: 2 additions & 1 deletion tests/neg/quote-0.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ def test(using QuoteContext) = {
val x: Int = 0

'{
given QuoteContext = ???
val qctx: QuoteContext = ???
given qctx.type = qctx

'{x + 1} // error: wrong staging level

Expand Down
3 changes: 1 addition & 2 deletions tests/pos/i4380b.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import scala.quoted._

object Test {
given QuoteContext = ???
class Test(using qctx: QuoteContext) {
def step(k: (String => Expr[Unit])): Expr[Unit] = '{}
def meth(): Unit = '{
(i: Int) => ${ step(el => '{} ) }
Expand Down
3 changes: 1 addition & 2 deletions tests/pos/quote-1.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import scala.quoted._

object Test {
given QuoteContext = ???
class Test(using QuoteContext) {

def f[T](x: Expr[T])(implicit t: Type[T]) = '{
val y: $t = $x
Expand Down
3 changes: 1 addition & 2 deletions tests/pos/quote-lift.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import scala.quoted._

object Test {
given QuoteContext = ???
class Test(using QuoteContext) {

'{ ${implicitly[Liftable[Int]].toExpr(1)} }

Expand Down
3 changes: 1 addition & 2 deletions tests/pos/quoted-inline-quote.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import scala.quoted._
class Foo {
class Foo(using QuoteContext) {
inline def foo(x: Expr[String])(using QuoteContext) = '{ println(${x}) }

given QuoteContext = ???
foo('{"abc"})
}