Skip to content

Commit aba6b87

Browse files
Merge pull request #8692 from dotty-staging/use-correct-annotation-for-type-patterns
Use the correct annotation for type pattern definitions
2 parents c2e86d3 + c1af599 commit aba6b87

File tree

8 files changed

+43
-31
lines changed

8 files changed

+43
-31
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,14 +942,14 @@ object desugar {
942942
*
943943
* to
944944
*
945-
* @patternBindHole <mods> type $T >: Low <: Hi
945+
* @patternType <mods> type $T >: Low <: Hi
946946
*
947947
* if the type is a type splice.
948948
*/
949949
def quotedPatternTypeDef(tree: TypeDef)(implicit ctx: Context): TypeDef = {
950950
assert(ctx.mode.is(Mode.QuotedPattern))
951951
if (tree.name.startsWith("$") && !tree.isBackquoted) {
952-
val patternBindHoleAnnot = New(ref(defn.InternalQuoted_patternBindHoleAnnot.typeRef)).withSpan(tree.span)
952+
val patternBindHoleAnnot = New(ref(defn.InternalQuoted_patternTypeAnnot.typeRef)).withSpan(tree.span)
953953
val mods = tree.mods.withAddedAnnotation(patternBindHoleAnnot)
954954
tree.withMods(mods)
955955
}

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ class Definitions {
696696
@tu lazy val InternalQuoted_typeQuote : Symbol = InternalQuotedModule.requiredMethod("typeQuote")
697697
@tu lazy val InternalQuoted_patternHole: Symbol = InternalQuotedModule.requiredMethod("patternHole")
698698
@tu lazy val InternalQuoted_patternBindHoleAnnot: ClassSymbol = InternalQuotedModule.requiredClass("patternBindHole")
699+
@tu lazy val InternalQuoted_patternTypeAnnot: ClassSymbol = InternalQuotedModule.requiredClass("patternType")
699700
@tu lazy val InternalQuoted_QuoteTypeTagAnnot: ClassSymbol = InternalQuotedModule.requiredClass("quoteTypeTag")
700701
@tu lazy val InternalQuoted_fromAboveAnnot: ClassSymbol = InternalQuotedModule.requiredClass("fromAbove")
701702

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,6 +1964,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
19641964

19651965
def Definitions_InternalQuoted_patternHole: Symbol = defn.InternalQuoted_patternHole
19661966
def Definitions_InternalQuoted_patternBindHoleAnnot: Symbol = defn.InternalQuoted_patternBindHoleAnnot
1967+
def Definitions_InternalQuoted_patternTypeAnnot: Symbol = defn.InternalQuoted_patternTypeAnnot
19671968
def Definitions_InternalQuoted_fromAboveAnnot: Symbol = defn.InternalQuoted_fromAboveAnnot
19681969

19691970
// Types

compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ trait QuotesAndSplices {
133133
case pt: TypeBounds => pt
134134
case _ => TypeBounds.empty
135135
val typeSym = ctx.newSymbol(spliceOwner(ctx), name, EmptyFlags, typeSymInfo, NoSymbol, tree.expr.span)
136-
typeSym.addAnnotation(Annotation(New(ref(defn.InternalQuoted_patternBindHoleAnnot.typeRef)).withSpan(tree.expr.span)))
136+
typeSym.addAnnotation(Annotation(New(ref(defn.InternalQuoted_patternTypeAnnot.typeRef)).withSpan(tree.expr.span)))
137137
val pat = typedPattern(tree.expr, defn.QuotedTypeClass.typeRef.appliedTo(typeSym.typeRef))(
138138
using spliceContext.retractMode(Mode.QuotedPattern).withOwner(spliceOwner(ctx)))
139139
pat.select(tpnme.splice)
@@ -236,7 +236,7 @@ trait QuotesAndSplices {
236236
patBuf += Bind(sym, untpd.Ident(nme.WILDCARD).withType(defn.StringType)).withSpan(ddef.span)
237237
}
238238
super.transform(tree)
239-
case tdef: TypeDef if tdef.symbol.hasAnnotation(defn.InternalQuoted_patternBindHoleAnnot) =>
239+
case tdef: TypeDef if tdef.symbol.hasAnnotation(defn.InternalQuoted_patternTypeAnnot) =>
240240
transformTypeBindingTypeDef(tdef, typePatBuf)
241241
case tree @ AppliedTypeTree(tpt, args) =>
242242
val args1: List[Tree] = args.zipWithConserve(tpt.tpe.typeParams.map(_.paramVarianceSign)) { (arg, v) =>

library/src/scala/internal/quoted/Matcher.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,17 @@ private[quoted] object Matcher {
7474
case _ => false
7575
}
7676

77+
private def hasPatternTypeAnnotation(sym: Symbol) = sym.annots.exists(isPatternTypeAnnotation)
78+
7779
private def hasBindAnnotation(sym: Symbol) = sym.annots.exists(isBindAnnotation)
7880

7981
private def hasFromAboveAnnotation(sym: Symbol) = sym.annots.exists(isFromAboveAnnotation)
8082

83+
private def isPatternTypeAnnotation(tree: Tree): Boolean = tree match {
84+
case New(tpt) => tpt.symbol == internal.Definitions_InternalQuoted_patternTypeAnnot
85+
case annot => annot.symbol.owner == internal.Definitions_InternalQuoted_patternTypeAnnot
86+
}
87+
8188
private def isBindAnnotation(tree: Tree): Boolean = tree match {
8289
case New(tpt) => tpt.symbol == internal.Definitions_InternalQuoted_patternBindHoleAnnot
8390
case annot => annot.symbol.owner == internal.Definitions_InternalQuoted_patternBindHoleAnnot
@@ -423,7 +430,7 @@ private[quoted] object Matcher {
423430
}
424431

425432
private def isTypeBinding(tree: Tree): Boolean = tree match {
426-
case tree: TypeDef => hasBindAnnotation(tree.symbol)
433+
case tree: TypeDef => hasPatternTypeAnnotation(tree.symbol)
427434
case _ => false
428435
}
429436
}

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,9 @@ trait CompilerInterface {
15001500
/** Symbol of scala.internal.Quoted.patternBindHole */
15011501
def Definitions_InternalQuoted_patternBindHoleAnnot: Symbol
15021502

1503+
/** Symbol of scala.internal.Quoted.patternType */
1504+
def Definitions_InternalQuoted_patternTypeAnnot: Symbol
1505+
15031506
/** Symbol of scala.internal.Quoted.fromAbove */
15041507
def Definitions_InternalQuoted_fromAboveAnnot: Symbol
15051508

tests/run-macros/quote-matcher-runtime.check

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -716,21 +716,21 @@ Result: Some(List(Expr(1), Expr(2)))
716716

717717
Scrutinee: scala.List.apply[scala.Int](1, 2, 3).foreach[scala.Unit](((x: scala.Int) => scala.Predef.println(x)))
718718
Pattern: {
719-
@scala.internal.quoted.CompileTime.patternBindHole type T
719+
@scala.internal.quoted.CompileTime.patternType type T
720720
scala.internal.quoted.CompileTime.patternHole[scala.List[scala.Int]].foreach[T](scala.internal.quoted.CompileTime.patternHole[scala.Function1[scala.Int, T]])
721721
}
722722
Result: Some(List(Type(scala.Unit), Expr(scala.List.apply[scala.Int](1, 2, 3)), Expr(((x: scala.Int) => scala.Predef.println(x)))))
723723

724724
Scrutinee: scala.List.apply[scala.Int](1, 2, 3).foreach[scala.Unit](((x: scala.Int) => scala.Predef.println(x)))
725725
Pattern: {
726-
@scala.internal.quoted.CompileTime.patternBindHole type T = scala.Unit
726+
@scala.internal.quoted.CompileTime.patternType type T = scala.Unit
727727
scala.internal.quoted.CompileTime.patternHole[scala.List[scala.Int]].foreach[T](scala.internal.quoted.CompileTime.patternHole[scala.Function1[scala.Int, T]])
728728
}
729729
Result: Some(List(Type(scala.Unit), Expr(scala.List.apply[scala.Int](1, 2, 3)), Expr(((x: scala.Int) => scala.Predef.println(x)))))
730730

731731
Scrutinee: scala.List.apply[scala.Int](1, 2, 3).foreach[scala.Unit](((x: scala.Int) => scala.Predef.println(x)))
732732
Pattern: {
733-
@scala.internal.quoted.CompileTime.patternBindHole type T <: scala.Predef.String
733+
@scala.internal.quoted.CompileTime.patternType type T <: scala.Predef.String
734734
scala.internal.quoted.CompileTime.patternHole[scala.List[scala.Int]].foreach[T](scala.internal.quoted.CompileTime.patternHole[scala.Function1[scala.Int, T]])
735735
}
736736
Result: None
@@ -741,7 +741,7 @@ Scrutinee: {
741741
()
742742
}
743743
Pattern: {
744-
@scala.internal.quoted.CompileTime.patternBindHole type T
744+
@scala.internal.quoted.CompileTime.patternType type T
745745
val a: T = scala.internal.quoted.CompileTime.patternHole[T]
746746
val b: T = scala.internal.quoted.CompileTime.patternHole[T]
747747
()
@@ -754,7 +754,7 @@ Scrutinee: {
754754
()
755755
}
756756
Pattern: {
757-
@scala.internal.quoted.CompileTime.patternBindHole type T
757+
@scala.internal.quoted.CompileTime.patternType type T
758758
val a: T = scala.internal.quoted.CompileTime.patternHole[T]
759759
val b: T = scala.internal.quoted.CompileTime.patternHole[T]
760760
()
@@ -767,7 +767,7 @@ Scrutinee: {
767767
()
768768
}
769769
Pattern: {
770-
@scala.internal.quoted.CompileTime.patternBindHole type T
770+
@scala.internal.quoted.CompileTime.patternType type T
771771
val a: T = scala.internal.quoted.CompileTime.patternHole[T]
772772
val b: T = scala.internal.quoted.CompileTime.patternHole[T]
773773
()
@@ -780,7 +780,7 @@ Scrutinee: {
780780
()
781781
}
782782
Pattern: {
783-
@scala.internal.quoted.CompileTime.patternBindHole type T <: scala.Int
783+
@scala.internal.quoted.CompileTime.patternType type T <: scala.Int
784784
val a: T = scala.internal.quoted.CompileTime.patternHole[T]
785785
val b: T = scala.internal.quoted.CompileTime.patternHole[T]
786786
()
@@ -789,41 +789,41 @@ Result: None
789789

790790
Scrutinee: scala.List.apply[scala.Int](1, 2, 3).map[scala.Double](((x: scala.Int) => x.toDouble./(2))).map[java.lang.String](((y: scala.Double) => y.toString()))
791791
Pattern: {
792-
@scala.internal.quoted.CompileTime.patternBindHole type T
793-
@scala.internal.quoted.CompileTime.patternBindHole type U
794-
@scala.internal.quoted.CompileTime.patternBindHole type V
792+
@scala.internal.quoted.CompileTime.patternType type T
793+
@scala.internal.quoted.CompileTime.patternType type U
794+
@scala.internal.quoted.CompileTime.patternType type V
795795

796796
(scala.internal.quoted.CompileTime.patternHole[scala.List[T]].map[U](scala.internal.quoted.CompileTime.patternHole[scala.Function1[T, U]]).map[V](scala.internal.quoted.CompileTime.patternHole[scala.Function1[U, V]]): scala.collection.immutable.List[scala.Any])
797797
}
798798
Result: Some(List(Type(scala.Int), Type(scala.Double), Type(java.lang.String), Expr(scala.List.apply[scala.Int](1, 2, 3)), Expr(((x: scala.Int) => x.toDouble./(2))), Expr(((y: scala.Double) => y.toString()))))
799799

800800
Scrutinee: ((x: scala.Int) => x)
801801
Pattern: {
802-
@scala.internal.quoted.CompileTime.patternBindHole type T
802+
@scala.internal.quoted.CompileTime.patternType type T
803803

804804
(scala.internal.quoted.CompileTime.patternHole[scala.Function1[T, T]]: scala.Function1[scala.Nothing, scala.Any])
805805
}
806806
Result: Some(List(Type(scala.Int), Expr(((x: scala.Int) => x))))
807807

808808
Scrutinee: ((x: scala.Int) => x.toString())
809809
Pattern: {
810-
@scala.internal.quoted.CompileTime.patternBindHole type T
810+
@scala.internal.quoted.CompileTime.patternType type T
811811

812812
(scala.internal.quoted.CompileTime.patternHole[scala.Function1[T, T]]: scala.Function1[scala.Nothing, scala.Any])
813813
}
814814
Result: None
815815

816816
Scrutinee: ((x: scala.Any) => scala.Predef.???)
817817
Pattern: {
818-
@scala.internal.quoted.CompileTime.patternBindHole type T
818+
@scala.internal.quoted.CompileTime.patternType type T
819819

820820
(scala.internal.quoted.CompileTime.patternHole[scala.Function1[T, T]]: scala.Function1[scala.Nothing, scala.Any])
821821
}
822822
Result: Some(List(Type(scala.Nothing), Expr(((x: scala.Any) => scala.Predef.???))))
823823

824824
Scrutinee: ((x: scala.Nothing) => (1: scala.Any))
825825
Pattern: {
826-
@scala.internal.quoted.CompileTime.patternBindHole type T
826+
@scala.internal.quoted.CompileTime.patternType type T
827827

828828
(scala.internal.quoted.CompileTime.patternHole[scala.Function1[T, T]]: scala.Function1[scala.Nothing, scala.Any])
829829
}

tests/run-macros/quote-matcher-runtime/quoted_2.scala

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,18 @@ object Test {
134134
matches(try 1 finally 2, try 1 finally 2)
135135
matches(try 1 catch { case _ => 2 }, try patternHole[Int] catch { case _ => patternHole[Int] })
136136
matches(try 1 finally 2, try patternHole[Int] finally patternHole[Int])
137-
matches(List(1, 2, 3).foreach(x => println(x)), { @patternBindHole type T; patternHole[List[Int]].foreach[T](patternHole[Int => T]) })
138-
matches(List(1, 2, 3).foreach(x => println(x)), { @patternBindHole type T = Unit; patternHole[List[Int]].foreach[T](patternHole[Int => T]) })
139-
matches(List(1, 2, 3).foreach(x => println(x)), { @patternBindHole type T <: String; patternHole[List[Int]].foreach[T](patternHole[Int => T]) })
140-
matches({ val a: Int = 4; val b: Int = 4 }, { @patternBindHole type T; { val a: T = patternHole[T]; val b: T = patternHole[T] } })
141-
matches({ val a: Int = 4; val b: Int = 5 }, { @patternBindHole type T; { val a: T = patternHole[T]; val b: T = patternHole[T] } })
142-
matches({ val a: Int = 4; val b: String = "x" }, { @patternBindHole type T; { val a: T = patternHole[T]; val b: T = patternHole[T] } })
143-
matches({ val a: Int = 4; val b: String = "x" }, { @patternBindHole type T <: Int; { val a: T = patternHole[T]; val b: T = patternHole[T] } })
144-
matches(List(1, 2, 3).map(x => x.toDouble / 2).map(y => y.toString), { @patternBindHole type T; @patternBindHole type U; @patternBindHole type V; patternHole[List[T]].map(patternHole[T => U]).map(patternHole[U => V]) })
145-
matches((x: Int) => x, { @patternBindHole type T; patternHole[T => T] })
146-
matches((x: Int) => x.toString, { @patternBindHole type T; patternHole[T => T] })
147-
matches((x: Any) => ???, { @patternBindHole type T; patternHole[T => T] })
148-
matches((x: Nothing) => (1 : Any), { @patternBindHole type T; patternHole[T => T] })
137+
matches(List(1, 2, 3).foreach(x => println(x)), { @patternType type T; patternHole[List[Int]].foreach[T](patternHole[Int => T]) })
138+
matches(List(1, 2, 3).foreach(x => println(x)), { @patternType type T = Unit; patternHole[List[Int]].foreach[T](patternHole[Int => T]) })
139+
matches(List(1, 2, 3).foreach(x => println(x)), { @patternType type T <: String; patternHole[List[Int]].foreach[T](patternHole[Int => T]) })
140+
matches({ val a: Int = 4; val b: Int = 4 }, { @patternType type T; { val a: T = patternHole[T]; val b: T = patternHole[T] } })
141+
matches({ val a: Int = 4; val b: Int = 5 }, { @patternType type T; { val a: T = patternHole[T]; val b: T = patternHole[T] } })
142+
matches({ val a: Int = 4; val b: String = "x" }, { @patternType type T; { val a: T = patternHole[T]; val b: T = patternHole[T] } })
143+
matches({ val a: Int = 4; val b: String = "x" }, { @patternType type T <: Int; { val a: T = patternHole[T]; val b: T = patternHole[T] } })
144+
matches(List(1, 2, 3).map(x => x.toDouble / 2).map(y => y.toString), { @patternType type T; @patternType type U; @patternType type V; patternHole[List[T]].map(patternHole[T => U]).map(patternHole[U => V]) })
145+
matches((x: Int) => x, { @patternType type T; patternHole[T => T] })
146+
matches((x: Int) => x.toString, { @patternType type T; patternHole[T => T] })
147+
matches((x: Any) => ???, { @patternType type T; patternHole[T => T] })
148+
matches((x: Nothing) => (1 : Any), { @patternType type T; patternHole[T => T] })
149149

150150
}
151151
}

0 commit comments

Comments
 (0)