Skip to content
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

Rename scala.quoted.matching.{Bind => Sym} #7332

Merged
merged 1 commit into from
Sep 29, 2019
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: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ class Definitions {

@tu lazy val QuotedTypeModule: Symbol = QuotedTypeClass.companionModule

@tu lazy val QuotedMatchingBindingClass: ClassSymbol = ctx.requiredClass("scala.quoted.matching.Bind")
@tu lazy val QuotedMatchingSymClass: ClassSymbol = ctx.requiredClass("scala.quoted.matching.Sym")
@tu lazy val TastyReflectionClass: ClassSymbol = ctx.requiredClass("scala.tasty.Reflection")

@tu lazy val Unpickler_unpickleExpr: Symbol = ctx.requiredMethod("scala.runtime.quoted.Unpickler.unpickleExpr")
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ trait QuotesAndSplices {
x => t.resType.subst(t, x).toFunctionType())
case t => t
}
val bindingExprTpe = AppliedType(defn.QuotedMatchingBindingClass.typeRef, bindingType :: Nil)
val bindingExprTpe = AppliedType(defn.QuotedMatchingSymClass.typeRef, bindingType :: Nil)
assert(ddef.name.startsWith("$"))
val bindName = ddef.name.toString.stripPrefix("$").toTermName
val sym = ctx0.newPatternBoundSymbol(bindName, bindingExprTpe, ddef.span)
Expand Down
6 changes: 3 additions & 3 deletions library/src/scala/internal/quoted/Matcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package scala.internal.quoted
import scala.annotation.internal.sharable

import scala.quoted._
import scala.quoted.matching.Bind
import scala.quoted.matching.Sym

private[quoted] object Matcher {

Expand All @@ -12,7 +12,7 @@ private[quoted] object Matcher {

private final val debug = false

import qctx.tasty.{Bind => BindPattern, _}
import qctx.tasty._
import Matching._

private type Env = Set[(Symbol, Symbol)]
Expand Down Expand Up @@ -126,7 +126,7 @@ private[quoted] object Matcher {
}

def bindingMatch(sym: Symbol) =
matched(new Bind(sym.name, sym))
matched(new Sym(sym.name, sym))

(scrutinee, pattern) match {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package scala.quoted
package matching

/** Bind of an Expr[T] used to know if some Expr[T] is a reference to the binding
/** Sym of an Expr[T] used to know if some Expr[T] is a reference to the symbol
*
* @param name string name of this binding
* @param name string name of this symbol
* @param id unique id used for equality
*/
class Bind[T <: AnyKind] private[scala](val name: String, private[Bind] val id: Object) { self =>
class Sym[T <: AnyKind] private[scala](val name: String, private[Sym] val id: Object) { self =>

override def equals(obj: Any): Boolean = obj match {
case obj: Bind[_] => obj.id == id
case obj: Sym[_] => obj.id == id
case _ => false
}

override def hashCode(): Int = id.hashCode()

}

object Bind {
object Sym {

def unapply[T](expr: Expr[T])(given qctx: QuoteContext): Option[Bind[T]] = {
import qctx.tasty.{Bind => BindPattern, _}
def unapply[T](expr: Expr[T])(given qctx: QuoteContext): Option[Sym[T]] = {
import qctx.tasty._
expr.unseal match {
case IsIdent(ref) =>
val sym = ref.symbol
Some(new Bind[T](sym.name, sym))
Some(new Sym[T](sym.name, sym))
case _ => None
}
}
Expand Down
12 changes: 6 additions & 6 deletions tests/pos/quotedPatterns.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ object Test {
case '{ 1 + ($y: Int)} => y
case '{ val a = 1 + ($y: Int); 3 } => y
case '{ val $y: Int = $z; println(`$y`); 1 } =>
val a: quoted.matching.Bind[Int] = y
val a: quoted.matching.Sym[Int] = y
z
case '{ (($y: Int) => 1 + `$y` + ($z: Int))(2) } =>
val a: quoted.matching.Bind[Int] = y
val a: quoted.matching.Sym[Int] = y
z
case '{ def $ff: Int = $z; `$ff` } =>
val a: quoted.matching.Bind[Int] = ff
val a: quoted.matching.Sym[Int] = ff
z
case '{ def $ff(i: Int): Int = $z; 2 } =>
val a: quoted.matching.Bind[Int => Int] = ff
val a: quoted.matching.Sym[Int => Int] = ff
z
case '{ def $ff(i: Int)(j: Int): Int = $z; 2 } =>
val a: quoted.matching.Bind[Int => Int => Int] = ff
val a: quoted.matching.Sym[Int => Int => Int] = ff
z
case '{ def $ff[T](i: T): Int = $z; 2 } =>
val a: quoted.matching.Bind[[T] =>> T => Int] = ff
val a: quoted.matching.Sym[[T] =>> T => Int] = ff
z
case '{ poly[$t]($x); 4 } => ???
case '{ poly[${Foo(t)}]($x); 4 } => ???
Expand Down
10 changes: 5 additions & 5 deletions tests/run-macros/quote-matcher-runtime.check
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ Result: Some(List(Expr(((x: scala.Int) => "abc"))))

Scrutinee: ((x: scala.Int) => "abc")
Pattern: ((x: scala.Int @scala.internal.Quoted.patternBindHole) => scala.internal.Quoted.patternHole[scala.Predef.String])
Result: Some(List(Bind(x), Expr("abc")))
Result: Some(List(Sym(x), Expr("abc")))

Scrutinee: scala.StringContext.apply(("abc", "xyz": scala.<repeated>[scala.Predef.String]))
Pattern: scala.StringContext.apply(("abc", "xyz": scala.<repeated>[scala.Predef.String]))
Expand Down Expand Up @@ -270,7 +270,7 @@ Pattern: {
@scala.internal.Quoted.patternBindHole val a: scala.Int = scala.internal.Quoted.patternHole[scala.Int]
()
}
Result: Some(List(Bind(a), Expr(45)))
Result: Some(List(Sym(a), Expr(45)))

Scrutinee: {
val a: scala.Int = 45
Expand Down Expand Up @@ -502,7 +502,7 @@ Pattern: {
@scala.internal.Quoted.patternBindHole def a: scala.Int = scala.internal.Quoted.patternHole[scala.Int]
()
}
Result: Some(List(Bind(a), Expr(45)))
Result: Some(List(Sym(a), Expr(45)))

Scrutinee: {
def a(x: scala.Int): scala.Int = 45
Expand Down Expand Up @@ -572,7 +572,7 @@ Pattern: {
def a(x: scala.Int @scala.internal.Quoted.patternBindHole): scala.Int = 45
()
}
Result: Some(List(Bind(x)))
Result: Some(List(Sym(x)))

Scrutinee: {
def a(x: scala.Int): scala.Int = 45
Expand All @@ -582,7 +582,7 @@ Pattern: {
def a(x: scala.Int @scala.internal.Quoted.patternBindHole): scala.Int = 45
()
}
Result: Some(List(Bind(x)))
Result: Some(List(Sym(x)))

Scrutinee: {
def a(x: scala.Int): scala.Int = x
Expand Down
4 changes: 2 additions & 2 deletions tests/run-macros/quote-matcher-runtime/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ object Macros {
s"Expr(${r.unseal.show})"
case r: quoted.Type[_] =>
s"Type(${r.unseal.show})"
case r: Bind[_] =>
s"Bind(${r.name})"
case r: Sym[_] =>
s"Sym(${r.name})"
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/run-macros/quote-matcher-symantics-2/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object Macros {

private def impl[T: Type](sym: Symantics[T], a: Expr[DSL])(given qctx: QuoteContext): Expr[T] = {

def lift(e: Expr[DSL])(implicit env: Map[Bind[DSL], Expr[T]]): Expr[T] = e match {
def lift(e: Expr[DSL])(implicit env: Map[Sym[DSL], Expr[T]]): Expr[T] = e match {

case '{ LitDSL(${Const(c)}) } => sym.value(c)

Expand All @@ -23,15 +23,15 @@ object Macros {

case '{ val $x: DSL = $value; $body: DSL } => lift(body)(env + (x -> lift(value)))

case Bind(b) if env.contains(b) => env(b)
case Sym(b) if env.contains(b) => env(b)

case _ =>
import qctx.tasty._
error("Expected explicit DSL", e.unseal.pos)
???
}

def liftFun(e: Expr[DSL => DSL])(implicit env: Map[Bind[DSL], Expr[T]]): Expr[T => T] = e match {
def liftFun(e: Expr[DSL => DSL])(implicit env: Map[Sym[DSL], Expr[T]]): Expr[T => T] = e match {
case '{ ($x: DSL) => ($body: DSL) } =>
sym.lam((y: Expr[T]) => lift(body)(env + (x -> y)))

Expand Down
6 changes: 3 additions & 3 deletions tests/run-macros/quote-matcher-symantics-3/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ object Macros {

given ev0 : Env = Map.empty

def envWith[T](id: Bind[T], ref: Expr[R[T]])(given env: Env): Env =
def envWith[T](id: Sym[T], ref: Expr[R[T]])(given env: Env): Env =
env.updated(id, ref)

object FromEnv {
def unapply[T](id: Bind[T])(given Env): Option[Expr[R[T]]] =
def unapply[T](id: Sym[T])(given Env): Option[Expr[R[T]]] =
summon[Env].get(id).asInstanceOf[Option[Expr[R[T]]]] // We can only add binds that have the same type as the refs
}

Expand Down Expand Up @@ -50,7 +50,7 @@ object Macros {
case '{ Symantics.fix[$t, $u]($f) } =>
'{ $sym.fix[$t, $u]((x: R[$t => $u]) => $sym.app(${lift(f)}, x)).asInstanceOf[R[T]] }

case Bind(FromEnv(expr)) => expr.asInstanceOf[Expr[R[T]]]
case Sym(FromEnv(expr)) => expr.asInstanceOf[Expr[R[T]]]

case _ =>
summon[QuoteContext].error("Expected explicit value but got: " + e.show, e)
Expand Down
4 changes: 2 additions & 2 deletions tests/run-macros/quote-type-matcher/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ object Macros {
tup.toArray.toList.map {
case r: quoted.Type[_] =>
s"Type(${r.unseal.show})"
case r: Bind[_] =>
s"Bind(${r.name})"
case r: Sym[_] =>
s"Sym(${r.name})"
}
}

Expand Down