@@ -2757,7 +2757,33 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
27572757 def memberTypes : List [Symbol ] =
27582758 self.typeRef.decls.filter(_.isType)
27592759 def typeMembers : List [Symbol ] =
2760- lookupPrefix.typeMembers.map(_.symbol).toList
2760+ // lookupPrefix.typeMembers currently returns a Set wrapped into a unsorted Seq,
2761+ // so we try to sort that here (see discussion: https://github.com/scala/scala3/issues/22472),
2762+ // without adding too much of a performance hit.
2763+ // It first sorts by parents, then for type params by their positioning, then for members
2764+ // derived from declarations it sorts them by their name lexicographically
2765+ val parentsMap = lookupPrefix.sortedParents.map(_.typeSymbol).zipWithIndex.toList.toMap
2766+ val unsortedTypeMembers = lookupPrefix.typeMembers.map(_.symbol).filter(_.exists).toList
2767+ unsortedTypeMembers.sortWith {
2768+ case (typeA, typeB) =>
2769+ val msg = " Unknown type member found. Please consider reporting the issue to the compiler. "
2770+ assert(parentsMap.contains(typeA.owner), msg)
2771+ assert(parentsMap.contains(typeB.owner), msg)
2772+ val parentPlacementA = parentsMap(typeA.owner)
2773+ val parentPlacementB = parentsMap(typeB.owner)
2774+ if (parentPlacementA == parentPlacementB) then
2775+ if typeA.isTypeParam && typeB.isTypeParam then
2776+ // put type params at the beginning (and sort them by declaration order)
2777+ val pl = typeA.owner
2778+ val typeParamPositionMap = pl.typeParams.map(_.asInstanceOf [Symbol ]).zipWithIndex.toMap
2779+ typeParamPositionMap(typeA) < typeParamPositionMap(typeB)
2780+ else if typeA.isTypeParam then true
2781+ else if typeB.isTypeParam then false
2782+ else
2783+ // sort by name lexicographically
2784+ typeA.name.toString().compareTo(typeB.name.toString()) < 0
2785+ else parentPlacementA < parentPlacementB
2786+ }.map(_.asInstanceOf [Symbol ])
27612787
27622788 def declarations : List [Symbol ] =
27632789 self.typeRef.info.decls.toList
0 commit comments