Postpone renaming of emulated interfaces

Bug: 183998768
Change-Id: I42aaff7c32bddd3243f71a57145e50f560d178f1
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/itf/EmulatedInterfaceProcessor.java b/src/main/java/com/android/tools/r8/ir/desugar/itf/EmulatedInterfaceProcessor.java
index 5e200e5..559172a 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/itf/EmulatedInterfaceProcessor.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/itf/EmulatedInterfaceProcessor.java
@@ -95,11 +95,7 @@
   // of each emulated interface they implement. Such change should have no effect on the look-up
   // results, since each class implementing an emulated interface should also implement the
   // rewritten one.
-  void replaceInterfacesInEmulatedInterface(DexProgramClass emulatedInterface) {
-    assert rewriter.isEmulatedInterface(emulatedInterface.type);
-    if (appView.isAlreadyLibraryDesugared(emulatedInterface)) {
-      return;
-    }
+  private void replaceInterfacesInEmulatedInterface(DexProgramClass emulatedInterface) {
     List<GenericSignature.ClassTypeSignature> newInterfaces = new ArrayList<>();
     ClassSignature classSignature = emulatedInterface.getClassSignature();
     for (int i = 0; i < emulatedInterface.interfaces.size(); i++) {
@@ -121,8 +117,7 @@
     emulatedInterface.replaceInterfaces(newInterfaces);
   }
 
-  void renameEmulatedInterface(DexProgramClass emulatedInterface) {
-    assert rewriter.isEmulatedInterface(emulatedInterface.type);
+  private void renameEmulatedInterface(DexProgramClass emulatedInterface) {
     DexType newType = emulatedInterfaces.get(emulatedInterface.type);
     assert newType != null;
     emulatedInterface.type = newType;
@@ -285,13 +280,17 @@
   }
 
   @Override
-  public void process(DexProgramClass clazz, ProgramMethodSet synthesizedMethods) {
-    // TODO(b/183998768): Due to sequential dependencies we cannot generateEmulateInterfaceLibrary
-    // and replaceInterfacesInEmulatedInterface here. We need to merge this into a single loop.
-    // Uncomment these two lines instead of running them separately.
-    // generateEmulateInterfaceLibrary(clazz);
-    // replaceInterfacesInEmulatedInterface(clazz);
-    renameEmulatedInterface(clazz);
+  public void process(DexProgramClass emulatedInterface, ProgramMethodSet synthesizedMethods) {
+    assert rewriter.isEmulatedInterface(emulatedInterface.type);
+    if (appView.isAlreadyLibraryDesugared(emulatedInterface)) {
+      return;
+    }
+    // TODO(b/183998768): Due to sequential dependencies we cannot generateEmulateInterfaceLibrary.
+    //  We need to merge this into a single loop. Uncomment the following line instead of running
+    //  it separately.
+    //  generateEmulateInterfaceLibrary(emulatedInterface);
+    replaceInterfacesInEmulatedInterface(emulatedInterface);
+    renameEmulatedInterface(emulatedInterface);
   }
 
   @Override
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/itf/InterfaceMethodRewriter.java b/src/main/java/com/android/tools/r8/ir/desugar/itf/InterfaceMethodRewriter.java
index 8f8f607..8fd5d23 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/itf/InterfaceMethodRewriter.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/itf/InterfaceMethodRewriter.java
@@ -963,7 +963,7 @@
   public void desugarInterfaceMethods(
       Builder<?> builder, Flavor flavour, ExecutorService executorService)
       throws ExecutionException {
-    // TODO(b/183998768): Merge the following five sequential loops into a single one.
+    // TODO(b/183998768): Merge the following four sequential loops into a single one.
 
     EmulatedInterfaceProcessor emulatedInterfaceProcessor =
         new EmulatedInterfaceProcessor(appView, this);
@@ -974,10 +974,6 @@
     // replace desugared default interface methods.
     process(new ClassProcessor(appView, this), builder, flavour);
 
-    // TODO(b/183998768): Move this to emulatedInterfaceProcessor#process
-    forEachProgramEmulatedInterface(
-        emulatedInterfaceProcessor::replaceInterfacesInEmulatedInterface);
-
     // Process interfaces, create companion or dispatch class if needed, move static
     // methods to companion class, copy default interface methods to companion classes,
     // make original default methods abstract, remove bridge methods, create dispatch