From 721223fb5235a9b00432dc54ac9b6d35d2086a0d Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 9 Mar 2020 19:19:14 +0100 Subject: [PATCH] Remove quoted.matching.Sym Since quoted patterns do not return open quotes, this abstraction does not have any purpose. The only useful part of it is extracting the name of a binding, therefore instead of returning a Sym we return the name as a String directly. --- .../dotty/tools/dotc/core/Definitions.scala | 1 - .../tools/dotc/typer/QuotesAndSplices.scala | 5 ++- .../src/scala/internal/quoted/Matcher.scala | 3 +- library/src/scala/quoted/matching/Sym.scala | 34 ------------------- tests/pos/quotedPatterns.scala | 12 +++---- tests/run-macros/quote-matcher-runtime.check | 10 +++--- .../quote-matcher-runtime/quoted_1.scala | 6 ++-- .../quote-type-matcher/quoted_1.scala | 6 ++-- 8 files changed, 18 insertions(+), 59 deletions(-) delete mode 100644 library/src/scala/quoted/matching/Sym.scala diff --git a/compiler/src/dotty/tools/dotc/core/Definitions.scala b/compiler/src/dotty/tools/dotc/core/Definitions.scala index 38e1b671764e..f7ac72382a83 100644 --- a/compiler/src/dotty/tools/dotc/core/Definitions.scala +++ b/compiler/src/dotty/tools/dotc/core/Definitions.scala @@ -708,7 +708,6 @@ class Definitions { @tu lazy val QuotedTypeModule: Symbol = QuotedTypeClass.companionModule - @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.internal.quoted.Unpickler.unpickleExpr") diff --git a/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala b/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala index b98f39b191da..62317485090e 100644 --- a/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala +++ b/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala @@ -242,11 +242,10 @@ trait QuotesAndSplices { x => t.resType.subst(t, x).toFunctionType()) case t => t } - 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) - patBuf += Bind(sym, untpd.Ident(nme.WILDCARD).withType(bindingExprTpe)).withSpan(ddef.span) + val sym = ctx0.newPatternBoundSymbol(bindName, defn.StringType, ddef.span) + patBuf += Bind(sym, untpd.Ident(nme.WILDCARD).withType(defn.StringType)).withSpan(ddef.span) } super.transform(tree) case tdef: TypeDef if tdef.symbol.hasAnnotation(defn.InternalQuoted_patternBindHoleAnnot) => diff --git a/library/src/scala/internal/quoted/Matcher.scala b/library/src/scala/internal/quoted/Matcher.scala index 39bd7d6b9523..9b788ae93354 100644 --- a/library/src/scala/internal/quoted/Matcher.scala +++ b/library/src/scala/internal/quoted/Matcher.scala @@ -3,7 +3,6 @@ package scala.internal.quoted import scala.annotation.internal.sharable import scala.quoted._ -import scala.quoted.matching.Sym private[quoted] object Matcher { @@ -134,7 +133,7 @@ private[quoted] object Matcher { } def bindingMatch(sym: Symbol) = - matched(new Sym(sym.name, sym)) + matched(sym.name) (scrutinee, pattern) match { diff --git a/library/src/scala/quoted/matching/Sym.scala b/library/src/scala/quoted/matching/Sym.scala deleted file mode 100644 index 3fe0fbe8e7ee..000000000000 --- a/library/src/scala/quoted/matching/Sym.scala +++ /dev/null @@ -1,34 +0,0 @@ -package scala.quoted -package matching - -/** Sym of an Expr[T] used to know if some Expr[T] is a reference to the symbol - * - * @param name string name of this symbol - * @param id unique id used for equality - */ -class Sym[T <: AnyKind] private[scala](val name: String, private[Sym] val id: Object) { self => - - override def toString: String = s"Sym($name)@${id.hashCode}" - - override def equals(obj: Any): Boolean = obj match { - case obj: Sym[_] => obj.id == id - case _ => false - } - - override def hashCode(): Int = id.hashCode() - -} - -object Sym { - - def unapply[T](expr: Expr[T])(using qctx: QuoteContext): Option[Sym[T]] = { - import qctx.tasty._ - expr.unseal match { - case ref: Ident => - val sym = ref.symbol - Some(new Sym[T](sym.name, sym)) - case _ => None - } - } - -} diff --git a/tests/pos/quotedPatterns.scala b/tests/pos/quotedPatterns.scala index e6e4630f62fe..0bfe97736f16 100644 --- a/tests/pos/quotedPatterns.scala +++ b/tests/pos/quotedPatterns.scala @@ -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.Sym[Int] = y + val a: String = y z case '{ (($y: Int) => 1 + `$y` + ($z: Int))(2) } => - val a: quoted.matching.Sym[Int] = y + val a: String = y z case '{ def $ff: Int = $z; `$ff` } => - val a: quoted.matching.Sym[Int] = ff + val a: String = ff z case '{ def $ff(i: Int): Int = $z; 2 } => - val a: quoted.matching.Sym[Int => Int] = ff + val a: String = ff z case '{ def $ff(i: Int)(j: Int): Int = $z; 2 } => - val a: quoted.matching.Sym[Int => Int => Int] = ff + val a: String = ff z case '{ def $ff[T](i: T): Int = $z; 2 } => - val a: quoted.matching.Sym[[T] =>> T => Int] = ff + val a: String = ff z case '{ poly[$t]($x); 4 } => ??? case '{ type $X; poly[`$X`]($x); 4 } => ??? diff --git a/tests/run-macros/quote-matcher-runtime.check b/tests/run-macros/quote-matcher-runtime.check index 554c5bd669d7..2cab7037b4b6 100644 --- a/tests/run-macros/quote-matcher-runtime.check +++ b/tests/run-macros/quote-matcher-runtime.check @@ -238,7 +238,7 @@ Result: Some(List(Expr(((x: scala.Int) => "abc")))) Scrutinee: ((x: scala.Int) => "abc") Pattern: ((x: scala.Int @scala.internal.quoted.CompileTime.patternBindHole) => scala.internal.quoted.CompileTime.patternHole[scala.Predef.String]) -Result: Some(List(Sym(x), Expr("abc"))) +Result: Some(List(String(x), Expr("abc"))) Scrutinee: scala.StringContext.apply("abc", "xyz") Pattern: scala.StringContext.apply("abc", "xyz") @@ -270,7 +270,7 @@ Pattern: { @scala.internal.quoted.CompileTime.patternBindHole val a: scala.Int = scala.internal.quoted.CompileTime.patternHole[scala.Int] () } -Result: Some(List(Sym(a), Expr(45))) +Result: Some(List(String(a), Expr(45))) Scrutinee: { val a: scala.Int = 45 @@ -502,7 +502,7 @@ Pattern: { @scala.internal.quoted.CompileTime.patternBindHole def a: scala.Int = scala.internal.quoted.CompileTime.patternHole[scala.Int] () } -Result: Some(List(Sym(a), Expr(45))) +Result: Some(List(String(a), Expr(45))) Scrutinee: { def a(x: scala.Int): scala.Int = 45 @@ -572,7 +572,7 @@ Pattern: { def a(x: scala.Int @scala.internal.quoted.CompileTime.patternBindHole): scala.Int = 45 () } -Result: Some(List(Sym(x))) +Result: Some(List(String(x))) Scrutinee: { def a(x: scala.Int): scala.Int = 45 @@ -582,7 +582,7 @@ Pattern: { def a(x: scala.Int @scala.internal.quoted.CompileTime.patternBindHole): scala.Int = 45 () } -Result: Some(List(Sym(x))) +Result: Some(List(String(x))) Scrutinee: { def a(x: scala.Int): scala.Int = x diff --git a/tests/run-macros/quote-matcher-runtime/quoted_1.scala b/tests/run-macros/quote-matcher-runtime/quoted_1.scala index 1901c2c9e8b0..314b12a97b6b 100644 --- a/tests/run-macros/quote-matcher-runtime/quoted_1.scala +++ b/tests/run-macros/quote-matcher-runtime/quoted_1.scala @@ -1,6 +1,4 @@ import scala.quoted._ -import scala.quoted.matching._ - object Macros { @@ -15,8 +13,8 @@ object Macros { s"Expr(${r.unseal.show})" case r: quoted.Type[_] => s"Type(${r.unseal.show})" - case r: Sym[_] => - s"Sym(${r.name})" + case r: String => + s"String($r)" } } diff --git a/tests/run-macros/quote-type-matcher/quoted_1.scala b/tests/run-macros/quote-type-matcher/quoted_1.scala index 35f82a1181f2..baa9749e14eb 100644 --- a/tests/run-macros/quote-type-matcher/quoted_1.scala +++ b/tests/run-macros/quote-type-matcher/quoted_1.scala @@ -1,6 +1,4 @@ import scala.quoted._ -import scala.quoted.matching._ - object Macros { @@ -13,8 +11,8 @@ object Macros { tup.toArray.toList.map { case r: quoted.Type[_] => s"Type(${r.unseal.show})" - case r: Sym[_] => - s"Sym(${r.name})" + case r: String => + s"String($r)" } }