Skip to content

Make quote pattern type variable order deterministic #17938

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

Merged
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
22 changes: 14 additions & 8 deletions compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ trait QuotesAndSplices {
* )
* ```
*/
private def splitQuotePattern(quoted: Tree)(using Context): (Map[Symbol, Bind], Tree, List[Tree]) = {
private def splitQuotePattern(quoted: Tree)(using Context): (collection.Map[Symbol, Bind], Tree, List[Tree]) = {
val ctx0 = ctx

val typeBindings: collection.mutable.Map[Symbol, Bind] = collection.mutable.Map.empty
val typeBindings: mutable.Map[Symbol, Bind] = mutable.LinkedHashMap.empty
def getBinding(sym: Symbol): Bind =
typeBindings.getOrElseUpdate(sym, {
val bindingBounds = sym.info
Expand Down Expand Up @@ -296,13 +296,19 @@ trait QuotesAndSplices {
}
}
val shape0 = splitter.transform(quoted)
val patterns = (splitter.freshTypePatBuf.iterator ++ splitter.typePatBuf.iterator ++ splitter.patBuf.iterator).toList
val patterns = (splitter.typePatBuf.iterator ++ splitter.freshTypePatBuf.iterator ++ splitter.patBuf.iterator).toList
val freshTypeBindings = splitter.freshTypeBindingsBuff.result()

val shape1 = seq(
freshTypeBindings,
shape0
)
val shape1 = shape0 match
case Block(stats @ ((tdef: TypeDef) :: rest), expr) if tdef.symbol.hasAnnotation(defn.QuotedRuntimePatterns_patternTypeAnnot) =>
val (bindings, otherStats) = stats.span {
case tdef: TypeDef => tdef.symbol.hasAnnotation(defn.QuotedRuntimePatterns_patternTypeAnnot)
case _ => true
}
cpy.Block(shape0)(bindings ::: freshTypeBindings ::: otherStats, expr)
case _ =>
seq(freshTypeBindings, shape0)

val shape2 =
if (freshTypeBindings.isEmpty) shape1
else {
Expand All @@ -319,7 +325,7 @@ trait QuotesAndSplices {
new TreeTypeMap(typeMap = typeMap).transform(shape1)
}

(typeBindings.toMap, shape2, patterns)
(typeBindings, shape2, patterns)
}

/** Type a quote pattern `case '{ <quoted> } =>` qiven the a current prototype. Typing the pattern
Expand Down