@@ -3010,7 +3010,33 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
30103010 def memberTypes : List [Symbol ] =
30113011 self.typeRef.decls.filter(_.isType)
30123012 def typeMembers : List [Symbol ] =
3013- lookupPrefix.typeMembers.map(_.symbol).toList
3013+ // lookupPrefix.typeMembers currently returns a Set wrapped into a unsorted Seq,
3014+ // so we try to sort that here (see discussion: https://github.com/scala/scala3/issues/22472),
3015+ // without adding too much of a performance hit.
3016+ // It first sorts by parents, then for type params by their positioning, then for members
3017+ // derived from declarations it sorts them by their name lexicographically
3018+ val parentsMap = lookupPrefix.sortedParents.map(_.typeSymbol).zipWithIndex.toList.toMap
3019+ val unsortedTypeMembers = lookupPrefix.typeMembers.map(_.symbol).filter(_.exists).toList
3020+ unsortedTypeMembers.sortWith {
3021+ case (typeA, typeB) =>
3022+ val msg = " Unknown type member found. Please consider reporting the issue to the compiler. "
3023+ assert(parentsMap.contains(typeA.owner), msg)
3024+ assert(parentsMap.contains(typeB.owner), msg)
3025+ val parentPlacementA = parentsMap(typeA.owner)
3026+ val parentPlacementB = parentsMap(typeB.owner)
3027+ if (parentPlacementA == parentPlacementB) then
3028+ if typeA.isTypeParam && typeB.isTypeParam then
3029+ // put type params at the beginning (and sort them by declaration order)
3030+ val pl = typeA.owner
3031+ val typeParamPositionMap = pl.typeParams.map(_.asInstanceOf [Symbol ]).zipWithIndex.toMap
3032+ typeParamPositionMap(typeA) < typeParamPositionMap(typeB)
3033+ else if typeA.isTypeParam then true
3034+ else if typeB.isTypeParam then false
3035+ else
3036+ // sort by name lexicographically
3037+ typeA.name.toString().compareTo(typeB.name.toString()) < 0
3038+ else parentPlacementA < parentPlacementB
3039+ }.map(_.asInstanceOf [Symbol ])
30143040
30153041 def declarations : List [Symbol ] =
30163042 self.typeRef.info.decls.toList
0 commit comments