Skip to content
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
14 changes: 11 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,14 @@ class ReifyQuotes extends MacroTransformWithImplicits {
def levelOK(sym: Symbol)(implicit ctx: Context): Boolean = levelOf.get(sym) match {
case Some(l) =>
l == level ||
level == -1 && sym == defn.TastyTasty_macroContext
level == -1 && (
sym == defn.TastyTasty_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
// on inline parameters or type parameters.
sym.is(Param) ||
sym.isClass // reference to this in inline methods
)
case None =>
!sym.is(Param) || levelOK(sym.owner)
}
Expand Down Expand Up @@ -584,9 +591,10 @@ class ReifyQuotes extends MacroTransformWithImplicits {
"""Malformed macro.
|
|Expected the ~ to be at the top of the RHS:
| inline def foo(x: X, ..., y: Y): Int = ~impl(x, ... '(y))
| inline def foo(inline x: X, ..., y: Y): Int = ~impl(x, ... '(y))
|
|The contents of the splice must call a static method. Arguments must be quoted or inlined.
| * The contents of the splice must call a static method
| * All arguments must be quoted or inline
""".stripMargin, tree.rhs.pos)
EmptyTree
}
Expand Down
29 changes: 29 additions & 0 deletions tests/neg/quote-this.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import scala.quoted._

class Foo {

def f: Unit = '{
def bar[T](x: T): T = x
bar[
this.type // error
] {
this // error
}
}

inline def i(): Unit = ~Foo.impl[Any]('{
'(this) // error
})

inline def j(that: Foo): Unit = ~Foo.impl[Any]('{
'(that) // error
})

inline def k(): Unit = ~Foo.impl[Any](this) // error
inline def l(that: Foo): Unit = ~Foo.impl[Any](that) // error

}

object Foo {
def impl[T](x: Any): Expr[Unit] = '()
}
28 changes: 28 additions & 0 deletions tests/pos/quote-this.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import scala.quoted._

class Foo {
def a: Expr[Int] = '(1)
def b: Expr[Int] = '{
~this.a
}

def d: Expr[Expr[Int]] = '{ '(1) }
def e: Expr[Expr[Int]] = '{
'( ~(~this.d) )
}

def foo[T](x: T): T = x

def f = '{
~foo[this.type](this).a
}

inline def g(): Unit = ~Foo.impl[this.type](1)
inline def h(): Unit = ~Foo.impl[Any]('(this))
inline def i(that: Foo): Unit = ~Foo.impl[that.type](1)

}

object Foo {
def impl[T](x: Any): Expr[Unit] = '()
}