Final synthetic ordering based on internal types.

Bug: 217727847
Change-Id: I441a9201faae602256e96525a0e1158d4858cd5a
diff --git a/src/main/java/com/android/tools/r8/synthesis/SyntheticFinalization.java b/src/main/java/com/android/tools/r8/synthesis/SyntheticFinalization.java
index 5cc5876..533e24f 100644
--- a/src/main/java/com/android/tools/r8/synthesis/SyntheticFinalization.java
+++ b/src/main/java/com/android/tools/r8/synthesis/SyntheticFinalization.java
@@ -575,10 +575,7 @@
         });
     groupsPerPrefix.forEach(
         (externalSyntheticTypePrefix, groups) -> {
-          // Sort the equivalence groups that go into 'context' including the context type of the
-          // representative which is equal to 'context' here (see assert below).
-          Comparator<EquivalenceGroup<T>> comparator =
-              (a, b) -> a.compareToIncludingContext(b, appView.graphLens(), classToFeatureSplitMap);
+          Comparator<EquivalenceGroup<T>> comparator = this::compareForFinalGroupSorting;
           ListUtils.destructiveSort(groups, comparator);
           for (int i = 0; i < groups.size(); i++) {
             EquivalenceGroup<T> group = groups.get(i);
@@ -588,9 +585,7 @@
                 .equals(externalSyntheticTypePrefix);
             // Two equivalence groups in same context type must be distinct otherwise the assignment
             // of the synthetic name will be non-deterministic between the two.
-            assert i == 0
-                || checkGroupsAreDistinct(
-                    groups.get(i - 1), group, appView.graphLens(), classToFeatureSplitMap);
+            assert i == 0 || checkGroupsAreDistinct(groups.get(i - 1), group, comparator);
             SyntheticKind kind = group.getRepresentative().getKind();
             DexType representativeType =
                 intermediate
@@ -617,6 +612,17 @@
     return equivalences;
   }
 
+  private <T extends SyntheticDefinition<?, T, ?>> int compareForFinalGroupSorting(
+      EquivalenceGroup<T> a, EquivalenceGroup<T> b) {
+    // Sort the equivalence groups based on the representative types. The representatives are
+    // deterministically chosen and the internal synthetics deterministically named so using
+    // the internal type as the order is deterministic.
+    return a.getRepresentative()
+        .getHolder()
+        .getType()
+        .compareTo(b.getRepresentative().getHolder().getType());
+  }
+
   private static <T extends SyntheticDefinition<?, T, ?>> List<EquivalenceGroup<T>> groupEquivalent(
       AppView<?> appView,
       List<T> potentialEquivalence,
@@ -695,13 +701,11 @@
   }
 
   private static <T extends SyntheticDefinition<?, T, ?>> boolean checkGroupsAreDistinct(
-      EquivalenceGroup<T> g1,
-      EquivalenceGroup<T> g2,
-      GraphLens graphLens,
-      ClassToFeatureSplitMap classToFeatureSplitMap) {
-    int order = g1.compareToIncludingContext(g2, graphLens, classToFeatureSplitMap);
-    assert order != 0;
-    assert order != g2.compareToIncludingContext(g1, graphLens, classToFeatureSplitMap);
+      EquivalenceGroup<T> g1, EquivalenceGroup<T> g2, Comparator<EquivalenceGroup<T>> comparator) {
+    int smaller = comparator.compare(g1, g2);
+    assert smaller < 0;
+    int bigger = comparator.compare(g2, g1);
+    assert bigger > 0;
     return true;
   }