Update enclosing method signature when normalizing protos

Bug: b/353279141
Change-Id: I58740138a75bd7052f624aaa1a17cdcb23990dcd
diff --git a/src/main/java/com/android/tools/r8/optimize/proto/ProtoNormalizer.java b/src/main/java/com/android/tools/r8/optimize/proto/ProtoNormalizer.java
index 751c6d6..9d63f56 100644
--- a/src/main/java/com/android/tools/r8/optimize/proto/ProtoNormalizer.java
+++ b/src/main/java/com/android/tools/r8/optimize/proto/ProtoNormalizer.java
@@ -15,6 +15,7 @@
 import com.android.tools.r8.graph.DexProgramClass;
 import com.android.tools.r8.graph.DexString;
 import com.android.tools.r8.graph.DexTypeList;
+import com.android.tools.r8.graph.EnclosingMethodAttribute;
 import com.android.tools.r8.graph.GenericSignature.MethodTypeSignature;
 import com.android.tools.r8.graph.ProgramMethod;
 import com.android.tools.r8.graph.proto.RewrittenPrototypeDescription;
@@ -152,9 +153,10 @@
         return TraversalContinuation.doContinue(localReservationState);
       }
     }.run(appView.appInfo().classesWithDeterministicOrder());
-
     if (!lensBuilder.isEmpty()) {
-      appView.rewriteWithLens(lensBuilder.build(), executorService, timing);
+      ProtoNormalizerGraphLens protoNormalizerGraphLens = lensBuilder.build();
+      updateEnclosingMethodAttributes(executorService, protoNormalizerGraphLens);
+      appView.rewriteWithLens(protoNormalizerGraphLens, executorService, timing);
       LirConverter.rewriteLirWithLens(appView, timing, executorService);
       appView.clearCodeRewritings(executorService, timing);
     }
@@ -162,6 +164,27 @@
     timing.end();
   }
 
+  @SuppressWarnings("ReferenceEquality")
+  public void updateEnclosingMethodAttributes(
+      ExecutorService executorService, ProtoNormalizerGraphLens protoNormalizerGraphLens)
+      throws ExecutionException {
+    ThreadUtils.processItems(
+        appView.appInfo().classes(),
+        clazz -> {
+          if (clazz.hasEnclosingMethodAttribute()
+              && clazz.getEnclosingMethodAttribute().hasEnclosingMethod()) {
+            DexMethod enclosingMethod = clazz.getEnclosingMethodAttribute().getEnclosingMethod();
+            DexMethod newMethodSignature =
+                protoNormalizerGraphLens.getNextMethodSignature(enclosingMethod);
+            if (newMethodSignature.isNotIdenticalTo(enclosingMethod)) {
+              clazz.setEnclosingMethodAttribute(new EnclosingMethodAttribute(newMethodSignature));
+            }
+          }
+        },
+        appView.options().getThreadingModule(),
+        executorService);
+  }
+
   private GlobalReservationState computeGlobalReservationState(ExecutorService executorService)
       throws ExecutionException {
     // Tracks how many different parameter lists can be optimized into the same parameter list.
diff --git a/src/test/java/com/android/tools/r8/regress/Regress353279141.java b/src/test/java/com/android/tools/r8/regress/Regress353279141.java
index ff82dcf..505bb35 100644
--- a/src/test/java/com/android/tools/r8/regress/Regress353279141.java
+++ b/src/test/java/com/android/tools/r8/regress/Regress353279141.java
@@ -3,7 +3,8 @@
 // BSD-style license that can be found in the LICENSE file.
 package com.android.tools.r8.regress;
 
-import static org.junit.Assert.assertFalse;
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
 import com.android.tools.r8.CompilationFailedException;
@@ -44,16 +45,7 @@
                       .clazz(TestClass.class.getName() + "$1")
                       .getFinalEnclosingMethod()
                       .asMethodReference();
-
-              // TODO(b/353279141): Rewrite the enclosing method to the correct one.
-              // This does not illustrate the exact issue in the bug, where we would rename a
-              // different
-              // method, with the same proto to the original name, and then have it output twice
-              // in the method table. This does trigger the same issue, the original method is
-              // present in the indexed items though (because we add it when iterating the
-              // annotation)
-              // although there are no classes that have it.
-              assertFalse(codeInspector.clazz(TestClass.class).method(method).isPresent());
+              assertThat(codeInspector.clazz(TestClass.class).method(method), isPresent());
             });
   }