Add regression test for non determinism in sorting classes for naming

When we sort the classes differently we will get a different name assignment.

The reason is that when we evaluate the javac generated bridge in A
and C, the order matters because the makeCommand method on A will
reserve the makeCommand name with arity 0.

The reservation is happening on the current frontier (which is
Foo). When we get to C this is reserved and we don't rename.

If we sort C first we did not yet reserve this.

Bug: b/326837090
Change-Id: I3e65311063aa21ff4a751838d9fbb64d2afa1794
diff --git a/src/main/java/com/android/tools/r8/graph/DexApplication.java b/src/main/java/com/android/tools/r8/graph/DexApplication.java
index da73661..0d83712 100644
--- a/src/main/java/com/android/tools/r8/graph/DexApplication.java
+++ b/src/main/java/com/android/tools/r8/graph/DexApplication.java
@@ -121,7 +121,11 @@
   }
 
   public Collection<DexProgramClass> classesWithDeterministicOrder() {
-    return classesWithDeterministicOrder(new ArrayList<>(programClasses()));
+    Comparator<ClassDefinition> comparator = Comparator.comparing(ClassDefinition::getType);
+    if (options.testing.reverseClassSortingForDeterminism) {
+      comparator = comparator.reversed();
+    }
+    return classesWithDeterministicOrder(new ArrayList<>(programClasses()), comparator);
   }
 
   public static <T extends ClassDefinition> List<T> classesWithDeterministicOrder(
@@ -135,6 +139,12 @@
     return classes;
   }
 
+  private static Collection<DexProgramClass> classesWithDeterministicOrder(
+      List<DexProgramClass> classes, Comparator<ClassDefinition> comparator) {
+    classes.sort(comparator);
+    return classes;
+  }
+
   public DexApplicationReadFlags getFlags() {
     return flags;
   }
diff --git a/src/main/java/com/android/tools/r8/utils/InternalOptions.java b/src/main/java/com/android/tools/r8/utils/InternalOptions.java
index a09100a..b8b3ddf 100644
--- a/src/main/java/com/android/tools/r8/utils/InternalOptions.java
+++ b/src/main/java/com/android/tools/r8/utils/InternalOptions.java
@@ -2145,6 +2145,7 @@
 
     public boolean enableExtractedKeepAnnotations = false;
     public boolean enableEmbeddedKeepAnnotations = false;
+    public boolean reverseClassSortingForDeterminism = false;
 
     public boolean isKeepAnnotationsEnabled() {
       return enableExtractedKeepAnnotations || enableEmbeddedKeepAnnotations;
diff --git a/src/test/java/com/android/tools/r8/regress/Regress326837090.java b/src/test/java/com/android/tools/r8/regress/Regress326837090.java
new file mode 100644
index 0000000..b1a1192
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/regress/Regress326837090.java
@@ -0,0 +1,102 @@
+// Copyright (c) 2024, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package com.android.tools.r8.regress;
+
+import static org.junit.Assert.assertEquals;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.keepanno.annotations.KeepForApi;
+import com.android.tools.r8.utils.codeinspector.FoundMethodSubject;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class Regress326837090 extends TestBase {
+
+  private final TestParameters parameters;
+
+  @Parameterized.Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withDefaultCfRuntime().build();
+  }
+
+  public Regress326837090(TestParameters parameters) {
+    this.parameters = parameters;
+  }
+
+  @Test
+  public void testR8() throws Exception {
+    runTest(false);
+    runTest(true);
+  }
+
+  private void runTest(boolean sortBackwards)
+      throws IOException, ExecutionException, CompilationFailedException {
+    testForR8(parameters.getBackend())
+        .enableExperimentalKeepAnnotations()
+        .addInnerClasses(getClass())
+        .addKeepMainRule(TestClass.class)
+        .enableInliningAnnotations()
+        .addOptionsModification(o -> o.testing.reverseClassSortingForDeterminism = sortBackwards)
+        .compile()
+        .run(TestClass.class)
+        .assertSuccessWithOutputLines("foo", "foo")
+        .inspect(
+            codeInspector -> {
+              List<FoundMethodSubject> makeCommand =
+                  codeInspector
+                      .clazz(C.class)
+                      .allMethods(
+                          p ->
+                              p.getOriginalMethodName().contains("makeCommand")
+                                  && !p.isSynthetic());
+              assertEquals(1, makeCommand.size());
+              assertEquals(makeCommand.get(0).isRenamed(), sortBackwards);
+            });
+  }
+
+  public static class TestClass {
+    public static void main(String[] args) {
+      new A().makeCommand().foo();
+      ArrayList<Foo> arrayList = new ArrayList<>();
+      arrayList.add(new C());
+      arrayList.get(0).makeCommand().foo();
+    }
+  }
+
+  @KeepForApi
+  public abstract static class Foo<S extends Foo> {
+    abstract S makeCommand();
+
+    public void foo() {
+      System.out.println("foo");
+    }
+  }
+
+  @KeepForApi
+  public static class A extends Foo<A> {
+    @Override
+    protected A makeCommand() {
+      return this;
+    }
+  }
+
+  @KeepForApi
+  public static class C extends Foo<C> {
+    @Override
+    @NeverInline
+    C makeCommand() {
+      return this;
+    }
+  }
+}