Skip to content

Commit 6764d5e

Browse files
committed
Add comments to QuotePatterns::checkPattern
1 parent 425be1a commit 6764d5e

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

compiler/src/dotty/tools/dotc/quoted/QuotePatterns.scala

+14-8
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,23 @@ object QuotePatterns:
6161
}
6262
}.apply(Set.empty, quotePattern.body)
6363

64-
// TODO-18271: Refactor this
64+
/*
65+
* This part checks well-formedness of arguments to hoas patterns.
66+
* (1) Type arguments of a hoas patterns must be introduced in the quote pattern.ctxShow
67+
* Examples
68+
* well-formed: '{ [A] => (x : A) => $a[A](x) } // A is introduced in the quote pattern
69+
* ill-formed: '{ (x : Int) => $a[Int](x) } // Int is defined outside of the quote pattern
70+
* (2) If value arguments of a hoas pattern has a type with type variables that are introduced in
71+
* the quote pattern, those type variables should be in type arguments to the hoas patternHole
72+
* Examples
73+
* well-formed: '{ [A] => (x : A) => $a[A](x) } // a : [A] => (x:A) => A
74+
* ill-formed: '{ [A] => (x : A) => $a(x) } // a : (x:A) => A ...but A is undefined; hence ill-formed
75+
*/
6576
new tpd.TreeTraverser {
6677
override def traverse(tree: tpd.Tree)(using Context): Unit = tree match {
6778
case tree: SplicePattern =>
6879
def uncapturedTypeVars(arg: tpd.Tree, capturedTypeVars: List[tpd.Tree]) =
6980
val capturedTypeVarsSet = capturedTypeVars.map(_.symbol).toSet
70-
println("--- uncapturedTypeVars")
71-
println(s"typevars = ${typevars.map(_.show)}")
72-
println(s"capturedTypevars = ${capturedTypeVarsSet.map(_.show)}")
7381
new TypeAccumulator[Set[Type]] {
7482
def apply(x: Set[Type], tp: Type): Set[Type] =
7583
if typevars.contains(tp.typeSymbol) && !capturedTypeVarsSet.contains(tp.typeSymbol) then
@@ -78,13 +86,11 @@ object QuotePatterns:
7886
foldOver(x, tp)
7987
}.apply(Set.empty, arg.tpe)
8088

81-
// Type arguments to a splice patterns must be type variables that are introduced
82-
// inside the quote pattern
83-
for (typearg <- tree.typeargs)
89+
for (typearg <- tree.typeargs) // case (1)
8490
do
8591
if !typevars.contains(typearg.symbol) then
8692
report.error("Type arguments of a hoas pattern needs to be introduced in the quoted pattern", typearg.srcPos)
87-
for (arg <- tree.args)
93+
for (arg <- tree.args) // case (2)
8894
do
8995
if !uncapturedTypeVars(arg, tree.typeargs).isEmpty then
9096
report.error("Type variables that this argument depends on are not captured in this hoas pattern", arg.srcPos)

0 commit comments

Comments
 (0)