Rewrite debug info in CF and DEX writer

Bug: 176089426
Change-Id: Ib10dd2d71dd7745c147dea4b7f4c39a370f8c1a7
diff --git a/src/main/java/com/android/tools/r8/dex/DebugBytecodeWriter.java b/src/main/java/com/android/tools/r8/dex/DebugBytecodeWriter.java
index 06dbcfe..2c3e18a 100644
--- a/src/main/java/com/android/tools/r8/dex/DebugBytecodeWriter.java
+++ b/src/main/java/com/android/tools/r8/dex/DebugBytecodeWriter.java
@@ -7,6 +7,7 @@
 import com.android.tools.r8.graph.DexDebugInfo;
 import com.android.tools.r8.graph.DexString;
 import com.android.tools.r8.graph.DexType;
+import com.android.tools.r8.graph.GraphLens;
 import com.android.tools.r8.graph.ObjectToOffsetMapping;
 import com.android.tools.r8.utils.LebUtils;
 import java.nio.ByteBuffer;
@@ -15,12 +16,15 @@
 public class DebugBytecodeWriter {
 
   private final ObjectToOffsetMapping mapping;
+  private final GraphLens graphLens;
   private final DexDebugInfo info;
   private ByteBuffer buffer;
 
-  public DebugBytecodeWriter(DexDebugInfo info, ObjectToOffsetMapping mapping) {
+  public DebugBytecodeWriter(
+      DexDebugInfo info, ObjectToOffsetMapping mapping, GraphLens graphLens) {
     this.info = info;
     this.mapping = mapping;
+    this.graphLens = graphLens;
     // Never allocate a zero-sized buffer, as we need to write the header, and the growth policy
     // requires it to have a positive capacity.
     this.buffer = ByteBuffer.allocate(info.events.length * 5 + 4);
@@ -35,7 +39,7 @@
     }
     // Body.
     for (DexDebugEvent event : info.events) {
-      event.writeOn(this, mapping);
+      event.writeOn(this, mapping, graphLens);
     }
     // Tail.
     putByte(Constants.DBG_END_SEQUENCE);
diff --git a/src/main/java/com/android/tools/r8/dex/FileWriter.java b/src/main/java/com/android/tools/r8/dex/FileWriter.java
index 674d9fd..bbe4a90 100644
--- a/src/main/java/com/android/tools/r8/dex/FileWriter.java
+++ b/src/main/java/com/android/tools/r8/dex/FileWriter.java
@@ -194,7 +194,7 @@
       for (ProgramDexCode code : codes) {
         DexDebugInfoForWriting info = code.getCode().getDebugInfoForWriting();
         if (info != null && seen.add(info)) {
-          writeDebugItem(info);
+          writeDebugItem(info, graphLens);
         }
       }
     }
@@ -487,9 +487,9 @@
     dest.putInt(mixedSectionOffsets.getOffsetFor(staticFieldValues.get(clazz)));
   }
 
-  private void writeDebugItem(DexDebugInfo debugInfo) {
+  private void writeDebugItem(DexDebugInfo debugInfo, GraphLens graphLens) {
     mixedSectionOffsets.setOffsetFor(debugInfo, dest.position());
-    dest.putBytes(new DebugBytecodeWriter(debugInfo, mapping).generate());
+    dest.putBytes(new DebugBytecodeWriter(debugInfo, mapping, graphLens).generate());
   }
 
   private void writeCodeItem(ProgramDexCode code) {
diff --git a/src/main/java/com/android/tools/r8/graph/CfCode.java b/src/main/java/com/android/tools/r8/graph/CfCode.java
index 4234abc..4d45d55 100644
--- a/src/main/java/com/android/tools/r8/graph/CfCode.java
+++ b/src/main/java/com/android/tools/r8/graph/CfCode.java
@@ -347,26 +347,34 @@
       Map<Integer, DebugLocalInfo> parameterInfo = method.getDefinition().getParameterInfo();
       for (Entry<Integer, DebugLocalInfo> entry : parameterInfo.entrySet()) {
         writeLocalVariableEntry(
-            visitor, namingLens, entry.getValue(), parameterLabel, parameterLabel, entry.getKey());
+            visitor,
+            graphLens,
+            namingLens,
+            entry.getValue(),
+            parameterLabel,
+            parameterLabel,
+            entry.getKey());
       }
     } else {
       for (LocalVariableInfo local : localVariables) {
         writeLocalVariableEntry(
-            visitor, namingLens, local.local, local.start, local.end, local.index);
+            visitor, graphLens, namingLens, local.local, local.start, local.end, local.index);
       }
     }
   }
 
   private void writeLocalVariableEntry(
       MethodVisitor visitor,
+      GraphLens graphLens,
       NamingLens namingLens,
       DebugLocalInfo info,
       CfLabel start,
       CfLabel end,
       int index) {
+    DexType rewrittenType = graphLens.lookupType(info.type);
     visitor.visitLocalVariable(
         info.name.toString(),
-        namingLens.lookupDescriptor(info.type).toString(),
+        namingLens.lookupDescriptor(rewrittenType).toString(),
         info.signature == null ? null : info.signature.toString(),
         start.getLabel(),
         end.getLabel(),
diff --git a/src/main/java/com/android/tools/r8/graph/DexDebugEvent.java b/src/main/java/com/android/tools/r8/graph/DexDebugEvent.java
index 9dba226..b213660 100644
--- a/src/main/java/com/android/tools/r8/graph/DexDebugEvent.java
+++ b/src/main/java/com/android/tools/r8/graph/DexDebugEvent.java
@@ -72,7 +72,8 @@
     internalAcceptHashing(visitor);
   }
 
-  public abstract void writeOn(DebugBytecodeWriter writer, ObjectToOffsetMapping mapping);
+  public abstract void writeOn(
+      DebugBytecodeWriter writer, ObjectToOffsetMapping mapping, GraphLens graphLens);
 
   public abstract void accept(DexDebugEventVisitor visitor);
 
@@ -89,7 +90,8 @@
     public final int delta;
 
     @Override
-    public void writeOn(DebugBytecodeWriter writer, ObjectToOffsetMapping mapping) {
+    public void writeOn(
+        DebugBytecodeWriter writer, ObjectToOffsetMapping mapping, GraphLens graphLens) {
       writer.putByte(Constants.DBG_ADVANCE_PC);
       writer.putUleb128(delta);
     }
@@ -138,7 +140,8 @@
     }
 
     @Override
-    public void writeOn(DebugBytecodeWriter writer, ObjectToOffsetMapping mapping) {
+    public void writeOn(
+        DebugBytecodeWriter writer, ObjectToOffsetMapping mapping, GraphLens graphLens) {
       writer.putByte(Constants.DBG_SET_PROLOGUE_END);
     }
 
@@ -182,7 +185,8 @@
     }
 
     @Override
-    public void writeOn(DebugBytecodeWriter writer, ObjectToOffsetMapping mapping) {
+    public void writeOn(
+        DebugBytecodeWriter writer, ObjectToOffsetMapping mapping, GraphLens graphLens) {
       writer.putByte(Constants.DBG_SET_EPILOGUE_BEGIN);
     }
 
@@ -227,7 +231,8 @@
     }
 
     @Override
-    public void writeOn(DebugBytecodeWriter writer, ObjectToOffsetMapping mapping) {
+    public void writeOn(
+        DebugBytecodeWriter writer, ObjectToOffsetMapping mapping, GraphLens graphLens) {
       writer.putByte(Constants.DBG_ADVANCE_LINE);
       writer.putSleb128(delta);
     }
@@ -294,13 +299,14 @@
     }
 
     @Override
-    public void writeOn(DebugBytecodeWriter writer, ObjectToOffsetMapping mapping) {
+    public void writeOn(
+        DebugBytecodeWriter writer, ObjectToOffsetMapping mapping, GraphLens graphLens) {
       writer.putByte(signature == null
           ? Constants.DBG_START_LOCAL
           : Constants.DBG_START_LOCAL_EXTENDED);
       writer.putUleb128(registerNum);
       writer.putString(name);
-      writer.putType(type);
+      writer.putType(graphLens.lookupType(type));
       if (signature != null) {
         writer.putString(signature);
       }
@@ -364,7 +370,8 @@
     }
 
     @Override
-    public void writeOn(DebugBytecodeWriter writer, ObjectToOffsetMapping mapping) {
+    public void writeOn(
+        DebugBytecodeWriter writer, ObjectToOffsetMapping mapping, GraphLens graphLens) {
       writer.putByte(Constants.DBG_END_LOCAL);
       writer.putUleb128(registerNum);
     }
@@ -410,7 +417,8 @@
     }
 
     @Override
-    public void writeOn(DebugBytecodeWriter writer, ObjectToOffsetMapping mapping) {
+    public void writeOn(
+        DebugBytecodeWriter writer, ObjectToOffsetMapping mapping, GraphLens graphLens) {
       writer.putByte(Constants.DBG_RESTART_LOCAL);
       writer.putUleb128(registerNum);
     }
@@ -456,7 +464,8 @@
     }
 
     @Override
-    public void writeOn(DebugBytecodeWriter writer, ObjectToOffsetMapping mapping) {
+    public void writeOn(
+        DebugBytecodeWriter writer, ObjectToOffsetMapping mapping, GraphLens graphLens) {
       writer.putByte(Constants.DBG_SET_FILE);
       writer.putString(fileName);
     }
@@ -514,7 +523,8 @@
     }
 
     @Override
-    public void writeOn(DebugBytecodeWriter writer, ObjectToOffsetMapping mapping) {
+    public void writeOn(
+        DebugBytecodeWriter writer, ObjectToOffsetMapping mapping, GraphLens graphLens) {
       // CallerPosition will not be written.
     }
 
@@ -574,7 +584,8 @@
     }
 
     @Override
-    public void writeOn(DebugBytecodeWriter writer, ObjectToOffsetMapping mapping) {
+    public void writeOn(
+        DebugBytecodeWriter writer, ObjectToOffsetMapping mapping, GraphLens graphLens) {
       writer.putByte(value);
     }
 
diff --git a/src/test/java/com/android/tools/r8/dex/DebugByteCodeWriterTest.java b/src/test/java/com/android/tools/r8/dex/DebugByteCodeWriterTest.java
index 7ea9110..d2a5729 100644
--- a/src/test/java/com/android/tools/r8/dex/DebugByteCodeWriterTest.java
+++ b/src/test/java/com/android/tools/r8/dex/DebugByteCodeWriterTest.java
@@ -64,7 +64,9 @@
   @Test
   public void testEmptyDebugInfo() {
     DexDebugInfo debugInfo = new DexDebugInfo(1, DexString.EMPTY_ARRAY, new DexDebugEvent[]{});
-    DebugBytecodeWriter writer = new DebugBytecodeWriter(debugInfo, emptyObjectTObjectMapping());
+    DebugBytecodeWriter writer =
+        new DebugBytecodeWriter(
+            debugInfo, emptyObjectTObjectMapping(), GraphLens.getIdentityLens());
     Assert.assertEquals(3, writer.generate().length);
   }
 }
diff --git a/src/test/java/com/android/tools/r8/repackage/RepackageDebugMinificationTest.java b/src/test/java/com/android/tools/r8/repackage/RepackageDebugMinificationTest.java
index 4756baf..fe19640 100644
--- a/src/test/java/com/android/tools/r8/repackage/RepackageDebugMinificationTest.java
+++ b/src/test/java/com/android/tools/r8/repackage/RepackageDebugMinificationTest.java
@@ -4,16 +4,13 @@
 
 package com.android.tools.r8.repackage;
 
-import static com.android.tools.r8.DiagnosticsMatcher.diagnosticMessage;
 import static com.android.tools.r8.utils.codeinspector.Matchers.isPresentAndNotRenamed;
 import static com.android.tools.r8.utils.codeinspector.Matchers.isPresentAndRenamed;
-import static org.hamcrest.CoreMatchers.containsString;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assume.assumeTrue;
 
-import com.android.tools.r8.CompilationFailedException;
 import com.android.tools.r8.CompilationMode;
 import com.android.tools.r8.TestParameters;
 import com.android.tools.r8.utils.codeinspector.ClassSubject;
@@ -27,6 +24,8 @@
 @RunWith(Parameterized.class)
 public class RepackageDebugMinificationTest extends RepackageTestBase {
 
+  private static final String EXPECTED = "Hello World!";
+
   public RepackageDebugMinificationTest(
       String flattenPackageHierarchyOrRepackageClasses, TestParameters parameters) {
     super(flattenPackageHierarchyOrRepackageClasses, parameters);
@@ -37,10 +36,10 @@
     testForRuntime(parameters)
         .addProgramClasses(A.class, Main.class)
         .run(parameters.getRuntime(), Main.class)
-        .assertSuccessWithOutputLines("Hello World!");
+        .assertSuccessWithOutputLines(EXPECTED);
   }
 
-  @Test(expected = CompilationFailedException.class)
+  @Test
   public void testR8WithDebugDex() throws Exception {
     assumeTrue(parameters.isDexRuntime());
     testForR8(parameters.getBackend())
@@ -49,14 +48,8 @@
         .setMinApi(parameters.getApiLevel())
         .apply(this::configureRepackaging)
         .addKeepMainRule(Main.class)
-        .compileWithExpectedDiagnostics(
-            diagnostics -> {
-              diagnostics.assertErrorsMatch(
-                  diagnosticMessage(
-                      containsString(
-                          "Missing dependency:"
-                              + " com.android.tools.r8.repackage.RepackageDebugMinificationTest$A")));
-            });
+        .run(parameters.getRuntime(), Main.class)
+        .assertSuccessWithOutputLines(EXPECTED);
   }
 
   @Test
@@ -69,7 +62,7 @@
         .addKeepMainRule(Main.class)
         .apply(this::configureRepackaging)
         .run(parameters.getRuntime(), Main.class)
-        .assertSuccessWithOutputLines("Hello World!")
+        .assertSuccessWithOutputLines(EXPECTED)
         .inspect(
             inspector -> {
               ClassSubject mainClass = inspector.clazz(Main.class);
@@ -83,8 +76,7 @@
               assertEquals(2, localVariableTable.size());
               LocalVariableTableEntry localVariableTableEntry = localVariableTable.get(1);
               assertEquals("localValue", localVariableTableEntry.name);
-              // TODO(b/176089426): This should be the same type.
-              assertFalse(localVariableTableEntry.type.is(aClass));
+              assertTrue(localVariableTableEntry.type.is(aClass));
             });
   }