Skip to content

Fix #10464: Match overridden definitions #10531

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
Nov 30, 2020
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
23 changes: 15 additions & 8 deletions compiler/src/scala/quoted/runtime/impl/Matcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ object Matcher {
// TODO improve performance

// TODO use flag from qctx.reflect. Maybe -debug or add -debug-macros
private final val debug = false
private inline val debug = false

import qctx.reflect._
import Matching._
Expand Down Expand Up @@ -231,7 +231,7 @@ object Matcher {
scrutinee =?= expr2

/* Match selection */
case (ref: Ref, Select(qual2, _)) if scrutinee.symbol == pattern.symbol || summon[Env].get(scrutinee.symbol).contains(pattern.symbol) =>
case (ref: Ref, Select(qual2, _)) if symbolMatch(scrutinee.symbol, pattern.symbol) =>
ref match
case Select(qual1, _) => qual1 =?= qual2
case ref: Ident =>
Expand All @@ -240,7 +240,7 @@ object Matcher {
case _ => matched

/* Match reference */
case (_: Ref, _: Ident) if scrutinee.symbol == pattern.symbol || summon[Env].get(scrutinee.symbol).contains(pattern.symbol) =>
case (_: Ref, _: Ident) if symbolMatch(scrutinee.symbol, pattern.symbol) =>
matched

/* Match application */
Expand Down Expand Up @@ -329,23 +329,30 @@ object Matcher {
s""">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|Scrutinee
| ${scrutinee.show}
|
|${scrutinee.showExtractors}
|
|did not match pattern
| ${pattern.show}
|
|${pattern.showExtractors}
|
|with environment: ${summon[Env]}
|
|Scrutinee: ${scrutinee.showExtractors}
|Pattern: ${pattern.showExtractors}
|
|""".stripMargin)
notMatched
}
}
end extension

/** Does the scrutenne symbol match the pattern symbol? It matches if:
* - They are the same symbol
* - The scrutinee has is in the environment and they are equivalent
* - The scrutinee overrides the symbol of the pattern
*/
private def symbolMatch(scrutinee: Symbol, pattern: Symbol)(using Env): Boolean =
scrutinee == pattern
|| summon[Env].get(scrutinee).contains(pattern)
|| scrutinee.allOverriddenSymbols.contains(pattern)
Copy link
Contributor

Choose a reason for hiding this comment

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

The semantics still seem incomplete in this implementation.

Consider:

trait A { def foo: Int }
trait B extends A { def foo: Int = 1 }

val e0: Expr[A] = '{ new B }
val e1: Expr[Int] = '{ $e0.foo }

The quoted expression e1 will not match a pattern like '{ ($x: B).foo }, but it should.


private object ClosedPatternTerm {
/** Matches a term that does not contain free variables defined in the pattern (i.e. not defined in `Env`) */
def unapply(term: Term)(using Env): Option[term.type] =
Expand Down
1 change: 1 addition & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2348,6 +2348,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler

def paramSymss: List[List[Symbol]] = self.denot.paramSymss
def primaryConstructor: Symbol = self.denot.primaryConstructor
def allOverriddenSymbols: Iterator[Symbol] = self.denot.allOverriddenSymbols

def caseFields: List[Symbol] =
if !self.isClass then Nil
Expand Down
3 changes: 3 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3625,6 +3625,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*/
def paramSymss: List[List[Symbol]]

/** Returns all symbols overridden by this symbol. */
def allOverriddenSymbols: Iterator[Symbol]

/** The primary constructor of a class or trait, `noSymbol` if not applicable. */
def primaryConstructor: Symbol

Expand Down
5 changes: 5 additions & 0 deletions tests/run-macros/i10464.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
matched!
matched!
matched!
not matched
not matched
16 changes: 16 additions & 0 deletions tests/run-macros/i10464/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import scala.quoted._

object MatchMac {

inline def apply(inline any: Any): Unit = ${ printMacImpl('any) }

def printMacImpl(any: Expr[Any])(using Quotes): Expr[Unit] = {
import quotes.reflect._
val res = any match {
case '{ ($f: Person).name } => "matched!"
case _ => "not matched"
}
'{ println(${Expr(res)}) }
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The subtle semantics in quoted patterns may cause problems.

Maybe the programmer can change to Reflection for such use cases?

Or, instead of quoted patterns, add extractors to support pattern match on Expr[T] with clear intention and more predictable semantics.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, less common more precise matching can and must be done using reflection.

The intended semantics have not changed (#10464 (comment)), it was just a bug in the implementation.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, thanks for the explanation 👍


}
5 changes: 5 additions & 0 deletions tests/run-macros/i10464/Person_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trait Person:
def name: String

case class PersonA(name: String) extends Person
case class PersonB(name: String) extends Person
11 changes: 11 additions & 0 deletions tests/run-macros/i10464/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

@main def Test = {
val personA: PersonA = PersonA("JoeA")
val personB: PersonB = PersonB("JoeB")
val person: Person = personA
MatchMac(personA.name)
MatchMac(personB.name)
MatchMac(person.name)
MatchMac(PersonA("JoeA").name) // optimized to MatchMac("JoeA")
MatchMac(PersonB("JoeB").name) // optimized to MatchMac("JoeB")
}