No generated unused keep rules for L8

Bug: b/248371950
Bug: b/158635415
Change-Id: Ifff7993ace7238650ea39b46602d5be2f9e6d07a
diff --git a/src/main/java/com/android/tools/r8/dex/CodeToKeep.java b/src/main/java/com/android/tools/r8/dex/CodeToKeep.java
index 5a0ec34..a5d6098 100644
--- a/src/main/java/com/android/tools/r8/dex/CodeToKeep.java
+++ b/src/main/java/com/android/tools/r8/dex/CodeToKeep.java
@@ -4,6 +4,8 @@
 
 package com.android.tools.r8.dex;
 
+import static com.android.tools.r8.ir.desugar.desugaredlibrary.apiconversion.DesugaredLibraryAPIConverter.isVivifiedType;
+
 import com.android.tools.r8.errors.Unreachable;
 import com.android.tools.r8.graph.AppView;
 import com.android.tools.r8.graph.DexField;
@@ -149,6 +151,9 @@
 
     private void keepClass(DexType type) {
       DexType baseType = type.lookupBaseType(appView.dexItemFactory());
+      if (isVivifiedType(baseType)) {
+        return;
+      }
       toKeep.putIfAbsent(baseType, new KeepStruct());
     }
 
diff --git a/src/test/java/com/android/tools/r8/L8TestBuilder.java b/src/test/java/com/android/tools/r8/L8TestBuilder.java
index f5a808a..7d2ea31 100644
--- a/src/test/java/com/android/tools/r8/L8TestBuilder.java
+++ b/src/test/java/com/android/tools/r8/L8TestBuilder.java
@@ -255,15 +255,23 @@
       // The rewriting confuses the generic signatures in some methods. Such signatures are never
       // used by tools (they use the non library desugared version) and are stripped when compiling
       // with R8 anyway.
+      if (info instanceof UnusedProguardKeepRuleDiagnostic) {
+        // The default keep rules on desugared library may be unused. They should all be defined
+        // with keepclassmembers.
+        if (info.getDiagnosticMessage().contains("keepclassmembers")) {
+          continue;
+        }
+        // We allow info regarding the extended version of desugared library for JDK11 testing.
+        if (info.getDiagnosticMessage().contains("org.testng.")) {
+          continue;
+        }
+        fail("Unexpected unused proguard keep rule diagnostic: " + info.getDiagnosticMessage());
+      }
       // TODO(b/243483320): Investigate the Invalid signature.
       if (info.getDiagnosticMessage().contains("Invalid signature ")) {
         continue;
       }
-      // TODO(b/248371950): Should we have L8 shinking builds with these?
-      if (info instanceof UnusedProguardKeepRuleDiagnostic) {
-        continue;
-      }
-      fail("Unexpected info diagnostic: " + info);
+      fail("Unexpected info diagnostic: " + info.getDiagnosticMessage());
     }
   }
 
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/DesugaredLibraryWarningTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/DesugaredLibraryWarningTest.java
index 9d0d78e..40942f7 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/DesugaredLibraryWarningTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/DesugaredLibraryWarningTest.java
@@ -5,7 +5,6 @@
 package com.android.tools.r8.desugar.desugaredlibrary;
 
 import static com.android.tools.r8.DiagnosticsMatcher.diagnosticMessage;
-import static com.android.tools.r8.DiagnosticsMatcher.diagnosticType;
 import static com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification.D8_L8DEBUG;
 import static com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification.D8_L8SHRINK;
 import static com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification.JDK8;
@@ -15,7 +14,6 @@
 import com.android.tools.r8.TestParameters;
 import com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification;
 import com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification;
-import com.android.tools.r8.errors.UnusedProguardKeepRuleDiagnostic;
 import com.google.common.collect.ImmutableList;
 import java.util.List;
 import org.junit.Test;
@@ -26,14 +24,32 @@
 @RunWith(Parameterized.class)
 public class DesugaredLibraryWarningTest extends DesugaredLibraryTestBase {
 
-  private static final String FUNCTION_KEEP =
-      "-keep class j$.util.function.Function$-CC {\n"
-          + "    j$.util.function.Function $default$compose(j$.util.function.Function,"
-          + " j$.util.function.Function);\n"
-          + "    j$.util.function.Function $default$andThen(j$.util.function.Function,"
-          + " j$.util.function.Function);\n"
-          + "}\n"
-          + "-keep class j$.util.function.Function { *; }";
+  private static final String getFunctionKeep(String prefix) {
+    return "-keep class j$.util.function.Function$-CC {\n"
+        + "    "
+        + prefix
+        + ".util.function.Function $default$compose("
+        + prefix
+        + ".util.function.Function,"
+        + " "
+        + prefix
+        + ".util.function.Function);\n"
+        + "    "
+        + prefix
+        + ".util.function.Function $default$andThen("
+        + prefix
+        + ".util.function.Function,"
+        + " "
+        + prefix
+        + ".util.function.Function);\n"
+        + "}\n"
+        + "-keep class "
+        + prefix
+        + ".util.function.Function { *; }";
+  }
+
+  private static final String FUNCTION_KEEP_J$ = getFunctionKeep("j$");
+  private static final String FUNCTION_KEEP_JAVA = getFunctionKeep("java");
 
   private final TestParameters parameters;
   private final CompilationSpecification compilationSpecification;
@@ -62,7 +78,13 @@
         .apply(
             l8TestBuilder ->
                 libraryDesugaringSpecification.configureL8TestBuilder(
-                    l8TestBuilder, compilationSpecification.isL8Shrink(), FUNCTION_KEEP))
+                    l8TestBuilder,
+                    compilationSpecification.isL8Shrink(),
+                    libraryDesugaringSpecification.hasEmulatedInterfaceDesugaring(parameters)
+                        ? libraryDesugaringSpecification.hasJDollarFunction(parameters)
+                            ? FUNCTION_KEEP_J$
+                            : FUNCTION_KEEP_JAVA
+                        : ""))
         .compile()
         .inspectDiagnosticMessages(
             diagnosticsHandler -> {
@@ -73,9 +95,7 @@
               } else {
                 diagnosticsHandler.assertNoWarnings();
               }
-              // TODO(b/248371950): Should we have L8 shinking builds with these?
-              diagnosticsHandler.assertAllInfosMatch(
-                  diagnosticType(UnusedProguardKeepRuleDiagnostic.class));
+              diagnosticsHandler.assertNoInfos();
             });
   }
 }
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/DontKeepBootstrapClassesTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/DontKeepBootstrapClassesTest.java
index 542c71e..058f6cf 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/DontKeepBootstrapClassesTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/DontKeepBootstrapClassesTest.java
@@ -61,9 +61,6 @@
                         .anyMatch(
                             kr ->
                                 kr.contains("-keep class " + prefix + ".util.function.Consumer")));
-                // TODO(b/158635415): Don't generate keep rules targeting items outside desugared
-                // library.
-                assertTrue(keepRule.stream().anyMatch(kr -> kr.contains("-keep class java.util")));
               }
             });
   }