Skip to content

Handle this prefix in objects (in quote patterns) #14732

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 1 commit into from
Mar 23, 2022
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
2 changes: 2 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuoteMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ object QuoteMatcher {
case ref: Ident =>
ref.tpe match
case TermRef(qual: TermRef, _) => tpd.ref(qual) =?= qual2
case TermRef(qual: ThisType, _) if qual.classSymbol.is(Module, butNot = Package) =>
tpd.ref(qual.classSymbol.companionModule) =?= qual2
case _ => matched
/* Match reference */
case _: Ident if symbolMatch(scrutinee, pattern) => matched
Expand Down
11 changes: 11 additions & 0 deletions tests/pos-macros/i14536/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import quoted.*

inline def isFoo(inline x: Any): Boolean = ${ isFooImpl('x) }

def isFooImpl(x: Expr[Any])(using Quotes): Expr[Boolean] =
x match
case '{ ($p: Parent).foo } => '{ true }
case _ => '{ false }

trait Parent:
def foo = 0
2 changes: 2 additions & 0 deletions tests/pos-macros/i14536/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
object Child extends Parent:
def bar = isFoo(foo)
48 changes: 48 additions & 0 deletions tests/pos-macros/i14536b/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import quoted.*
import scala.compiletime.testing.{typeChecks, typeCheckErrors}
import scala.compiletime.testing.{Error, ErrorKind}

transparent inline def assertCompiles(inline code: String): Unit =
${ assertCompilesImpl('code, '{typeCheckErrors(code)}) }

given FromExpr[ErrorKind] with {
def unapply(expr: Expr[ErrorKind])(using Quotes) = expr match {
case '{ ErrorKind.Parser } => Some(ErrorKind.Parser)
case '{ ErrorKind.Typer } => Some(ErrorKind.Typer)
case _ => None
}
}

given FromExpr[Error] with {
def unapply(expr: Expr[Error])(using Quotes) = expr match {
case '{ Error(${Expr(msg)}, ${Expr(line)}, ${Expr(col)}, ${Expr(kind)}) } => Some(Error(msg, line, col, kind))
case _ => None
}
}

private def assertCompilesImpl(self: Expr[_], typeChecked: Expr[List[Error]])(using Quotes): Expr[Unit] = {
import quotes.reflect._

def checkCompile(code: String): Expr[Unit] = {
// For some reason `typeChecked.valueOrError` is failing here, so instead we grab
// the varargs argument to List.apply and use that to extract the list of errors
val errors = typeChecked.asTerm.underlyingArgument match {
case Apply(TypeApply(Select(Ident("List"), "apply"), _), List(seq)) =>
seq.asExprOf[Seq[Error]].valueOrError.toList
}

'{}
}

self.asTerm.underlyingArgument match {

case Literal(StringConstant(code)) =>
checkCompile(code.toString)

case Apply(Select(_, "stripMargin"), List(Literal(StringConstant(code)))) =>
checkCompile(code.toString.stripMargin)

case _ =>
report.throwError("The 'assertCompiles' function only works with String literals.")
}
}
1 change: 1 addition & 0 deletions tests/pos-macros/i14536b/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def test = assertCompiles("val a: String = 2")