Skip to content

Commit ad79bee

Browse files
authored
Use given pattern names when compiling patterns (#841)
~~Addresses one part of #793:~~ Resolves #793 completely. > I also think we could sometimes use the original pattern names, but I'm not 100% certain of that, would need to investigate. I'm doing this since it helps me to debug problems just a little bit more easily :) Note that this does not actually use the names of the fields as declared. --- ```scala def isShorterThan(x: List[Int], y: List[Int]): Bool = (x, y) match { case (_head, Nil()) => false case (Nil(), _tail) => true case (Cons(_x, xs),Cons(_y, ys)) => isShorterThan(xs, ys) } ``` Here's a comparison before (red) vs after (green). Just to drive my point home further, I removed the numbers after the identifier :) <img width="478" alt="Screenshot 2025-02-21 at 22 37 44" src="https://github.com/user-attachments/assets/9c3db8a9-1c1d-4994-999e-7508be530af0" />
1 parent 063c3c9 commit ad79bee

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

effekt/shared/src/main/scala/effekt/core/PatternMatchingCompiler.scala

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,25 @@ object PatternMatchingCompiler {
179179

180180
// used to make up new scrutinees
181181
val varsFor = mutable.Map.empty[Id, List[ValueVar]]
182-
def fieldVarsFor(constructor: Id, fieldTypes: List[ValueType]): List[ValueVar] =
183-
varsFor.getOrElseUpdate(constructor, fieldTypes.map { tpe => ValueVar(TmpValue("y"), tpe) })
182+
def fieldVarsFor(constructor: Id, fieldInfo: List[((Pattern, ValueType), String)]): List[ValueVar] =
183+
varsFor.getOrElseUpdate(
184+
constructor,
185+
fieldInfo.map {
186+
// if it's a pattern named by the user in the program, use the supplied name
187+
case ((Pattern.Any(id), tpe), _) => ValueVar(Id(id.name.name), tpe)
188+
// otherwise, use the field name of the given field
189+
case ((_, tpe), fieldName) => ValueVar(Id(fieldName), tpe)
190+
}
191+
)
184192

185193
normalized.foreach {
186194
case Clause(Split(Pattern.Tag(constructor, patternsAndTypes), restPatterns, restConds), label, args) =>
187-
val fieldVars = fieldVarsFor(constructor, patternsAndTypes.map { case (pat, tpe) => tpe })
195+
// NOTE: Ideally, we would use a `DeclarationContext` here, but we cannot: we're currently in the Source->Core transformer, so we do not have all of the details yet.
196+
val fieldNames: List[String] = constructor match {
197+
case c: symbols.Constructor => c.fields.map(_.name.name)
198+
case _ => List.fill(patternsAndTypes.size) { "y" } // NOTE: Only reached in PatternMatchingTests
199+
}
200+
val fieldVars = fieldVarsFor(constructor, patternsAndTypes.zip(fieldNames))
188201
val nestedMatches = fieldVars.zip(patternsAndTypes.map { case (pat, tpe) => pat }).toMap
189202
addClause(constructor,
190203
// it is important to add nested matches first, since they might include substitutions for the rest.

0 commit comments

Comments
 (0)