Handle field collisions in class merger

Change-Id: Ica3207e70d7860826632ca4af6b29179f3fdda73
diff --git a/src/test/examples/classmerging/FieldCollisionTest.java b/src/test/examples/classmerging/FieldCollisionTest.java
new file mode 100644
index 0000000..5bb2260
--- /dev/null
+++ b/src/test/examples/classmerging/FieldCollisionTest.java
@@ -0,0 +1,43 @@
+// Copyright (c) 2018, 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 classmerging;
+
+public class FieldCollisionTest {
+
+  private static final B SENTINEL_A = new B("A");
+  private static final B SENTINEL_B = new B("B");
+
+  public static void main(String[] args) {
+    B obj = new B();
+    System.out.println(obj.toString());
+  }
+
+  // Will be merged into B.
+  public static class A {
+
+    // After class merging, this field will have the same name and type as the field B.obj,
+    // unless we handle the collision.
+    protected final A obj = SENTINEL_A;
+  }
+
+  public static class B extends A {
+
+    protected final String message;
+    protected final B obj = SENTINEL_B;
+
+    public B() {
+      this(null);
+    }
+
+    public B(String message) {
+      this.message = message;
+    }
+
+    @Override
+    public String toString() {
+      return obj.message + System.lineSeparator() + ((B) super.obj).message;
+    }
+  }
+}
diff --git a/src/test/examples/classmerging/MethodCollisionTest.java b/src/test/examples/classmerging/MethodCollisionTest.java
new file mode 100644
index 0000000..a9010a4
--- /dev/null
+++ b/src/test/examples/classmerging/MethodCollisionTest.java
@@ -0,0 +1,57 @@
+// Copyright (c) 2018, 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 classmerging;
+
+public class MethodCollisionTest {
+
+  public static void main(String[] args) {
+    new B().m();
+    new D().m();
+  }
+
+  public static class A {
+
+    // After class merging, this method will have the same signature as the method B.m,
+    // unless we handle the collision.
+    private A m() {
+      System.out.println("A.m");
+      return null;
+    }
+
+    public void invokeM() {
+      m();
+    }
+  }
+
+  public static class B extends A {
+
+    private B m() {
+      System.out.println("B.m");
+      invokeM();
+      return null;
+    }
+  }
+
+  public static class C {
+
+    // After class merging, this method will have the same signature as the method D.m,
+    // unless we handle the collision.
+    public C m() {
+      System.out.println("C.m");
+      return null;
+    }
+  }
+
+  public static class D extends C {
+
+    public D m() {
+      System.out.println("D.m");
+      super.m();
+      return null;
+    }
+  }
+
+
+}
diff --git a/src/test/examples/classmerging/keep-rules.txt b/src/test/examples/classmerging/keep-rules.txt
index b3e0ad1..4ba014e 100644
--- a/src/test/examples/classmerging/keep-rules.txt
+++ b/src/test/examples/classmerging/keep-rules.txt
@@ -19,6 +19,12 @@
 -keep public class classmerging.ExceptionTest {
   public static void main(...);
 }
+-keep public class classmerging.FieldCollisionTest {
+  public static void main(...);
+}
+-keep public class classmerging.MethodCollisionTest {
+  public static void main(...);
+}
 -keep public class classmerging.RewritePinnedMethodTest {
   public static void main(...);
 }