Revert "Use generated covariant methods to synthesize methods on library"

This reverts commit 1cd798bd6b4e38e38b7cecb9f770b0425931d02b.

Revert "Only add covariant library methods if all referenced types exist"

This reverts commit 7dc76023d82cea94cd781fb94d2acb6b3ef0f3a3.

Change-Id: If37e46570054f20f467f8dc50a3099f73b665f05
diff --git a/src/main/java/com/android/tools/r8/graph/DexClass.java b/src/main/java/com/android/tools/r8/graph/DexClass.java
index 717ac38d..f10a654 100644
--- a/src/main/java/com/android/tools/r8/graph/DexClass.java
+++ b/src/main/java/com/android/tools/r8/graph/DexClass.java
@@ -287,10 +287,6 @@
     return Iterables.filter(virtualMethods(), predicate::test);
   }
 
-  public void addVirtualMethod(DexEncodedMethod method) {
-    methodCollection.addVirtualMethod(method);
-  }
-
   public void addVirtualMethods(Collection<DexEncodedMethod> methods) {
     methodCollection.addVirtualMethods(methods);
   }
diff --git a/src/main/java/com/android/tools/r8/graph/DexProgramClass.java b/src/main/java/com/android/tools/r8/graph/DexProgramClass.java
index 5e6c879..b49abbe 100644
--- a/src/main/java/com/android/tools/r8/graph/DexProgramClass.java
+++ b/src/main/java/com/android/tools/r8/graph/DexProgramClass.java
@@ -694,6 +694,10 @@
     methodCollection.addMethod(method);
   }
 
+  public void addVirtualMethod(DexEncodedMethod virtualMethod) {
+    methodCollection.addVirtualMethod(virtualMethod);
+  }
+
   public void replaceVirtualMethod(
       DexMethod virtualMethod, Function<DexEncodedMethod, DexEncodedMethod> replacement) {
     methodCollection.replaceVirtualMethod(virtualMethod, replacement);
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/library/LibraryOptimizationInfoInitializer.java b/src/main/java/com/android/tools/r8/ir/optimize/library/LibraryOptimizationInfoInitializer.java
index 0c41415..df2c436 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/library/LibraryOptimizationInfoInitializer.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/library/LibraryOptimizationInfoInitializer.java
@@ -6,6 +6,7 @@
 
 import com.android.tools.r8.graph.AppView;
 import com.android.tools.r8.graph.DexClass;
+import com.android.tools.r8.graph.DexEncodedField;
 import com.android.tools.r8.graph.DexEncodedMethod;
 import com.android.tools.r8.graph.DexItemFactory;
 import com.android.tools.r8.graph.DexItemFactory.EnumMembers;
@@ -13,6 +14,7 @@
 import com.android.tools.r8.graph.DexType;
 import com.android.tools.r8.ir.analysis.type.DynamicType;
 import com.android.tools.r8.ir.analysis.value.AbstractValueFactory;
+import com.android.tools.r8.ir.analysis.value.objectstate.ObjectState;
 import com.android.tools.r8.ir.optimize.info.LibraryOptimizationInfoInitializerFeedback;
 import com.android.tools.r8.ir.optimize.info.field.EmptyInstanceFieldInitializationInfoCollection;
 import com.android.tools.r8.ir.optimize.info.field.InstanceFieldInitializationInfoCollection;
@@ -85,6 +87,16 @@
     }
   }
 
+  private void modelStaticFinalLibraryFields(Set<DexEncodedField> finalLibraryFields) {
+    for (DexEncodedField field : finalLibraryFields) {
+      if (field.isStatic()) {
+        feedback.recordLibraryFieldHasAbstractValue(
+            field,
+            abstractValueFactory.createSingleFieldValue(field.getReference(), ObjectState.empty()));
+      }
+    }
+  }
+
   private void modelLibraryMethodsNonNullParamOrThrow() {
     dexItemFactory.libraryMethodsNonNullParamOrThrow.forEach(
         (method, nonNullParamOrThrow) -> {
diff --git a/src/main/java/com/android/tools/r8/shaking/Enqueuer.java b/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
index 761a52e..3addb2d 100644
--- a/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
+++ b/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
@@ -16,8 +16,6 @@
 import static java.util.Collections.emptySet;
 
 import com.android.tools.r8.Diagnostic;
-import com.android.tools.r8.androidapi.ComputedApiLevel;
-import com.android.tools.r8.androidapi.CovariantReturnTypeMethods;
 import com.android.tools.r8.cf.code.CfInstruction;
 import com.android.tools.r8.cf.code.CfInvoke;
 import com.android.tools.r8.contexts.CompilationContext.MethodProcessingContext;
@@ -74,7 +72,6 @@
 import com.android.tools.r8.graph.LookupMethodTarget;
 import com.android.tools.r8.graph.LookupResult;
 import com.android.tools.r8.graph.LookupTarget;
-import com.android.tools.r8.graph.MethodAccessFlags;
 import com.android.tools.r8.graph.MethodAccessInfoCollection;
 import com.android.tools.r8.graph.MethodResolutionResult;
 import com.android.tools.r8.graph.MethodResolutionResult.FailedResolutionResult;
@@ -674,17 +671,6 @@
     return definitionFor(type, context, this::recordNonProgramClass, this::reportMissingClass);
   }
 
-  public DexLibraryClass definitionForLibraryClassOrIgnore(DexType type) {
-    assert type.isClassType();
-    ClassResolutionResult classResolutionResult =
-        appInfo().contextIndependentDefinitionForWithResolutionResult(type);
-    return classResolutionResult.hasClassResolutionResult()
-            && !classResolutionResult.isMultipleClassResolutionResult()
-        ? DexLibraryClass.asLibraryClassOrNull(
-            classResolutionResult.toSingleClassWithProgramOverLibrary())
-        : null;
-  }
-
   public boolean hasAlternativeLibraryDefinition(DexProgramClass programClass) {
     ClassResolutionResult classResolutionResult =
         internalDefinitionFor(
@@ -3560,8 +3546,9 @@
     includeMinimumKeepInfo(rootSet);
 
     if (mode.isInitialTreeShaking()) {
-      // Amend library methods with covariant return types.
-      modelLibraryMethodsWithCovariantReturnTypes();
+      // This is simulating the effect of the "root set" applied rules.
+      // This is done only in the initial pass, in subsequent passes the "rules" are reapplied
+      // by iterating the instances.
     } else if (appView.getKeepInfo() != null) {
       EnqueuerEvent preconditionEvent = UnconditionalKeepInfoEvent.get();
       appView
@@ -3603,39 +3590,6 @@
             this::recordDependentMinimumKeepInfo);
   }
 
-  private void modelLibraryMethodsWithCovariantReturnTypes() {
-    CovariantReturnTypeMethods.registerMethodsWithCovariantReturnType(
-        appView.dexItemFactory(),
-        method -> {
-          DexLibraryClass libraryClass =
-              DexLibraryClass.asLibraryClassOrNull(
-                  appView.appInfo().definitionForWithoutExistenceAssert(method.getHolderType()));
-          if (libraryClass == null) {
-            return;
-          }
-          // Check if the covariant method exists on the class.
-          DexEncodedMethod covariantMethod = libraryClass.lookupMethod(method);
-          if (covariantMethod != null) {
-            return;
-          }
-          // Check if all type references exists otherwise bail out since the bridge could not exist
-          // in the android jar anyway.
-          for (DexType referencedType : method.getReferencedTypes()) {
-            DexType baseReferencedType = referencedType.toBaseType(appView.dexItemFactory());
-            if (baseReferencedType.isClassType()
-                && definitionForLibraryClassOrIgnore(referencedType) == null) {
-              return;
-            }
-          }
-          libraryClass.addVirtualMethod(
-              DexEncodedMethod.builder()
-                  .setMethod(method)
-                  .setAccessFlags(MethodAccessFlags.builder().setPublic().build())
-                  .setApiLevelForDefinition(ComputedApiLevel.notSet())
-                  .build());
-        });
-  }
-
   private void applyMinimumKeepInfo(DexProgramClass clazz) {
     EnqueuerEvent preconditionEvent = UnconditionalKeepInfoEvent.get();
     KeepClassInfo.Joiner minimumKeepInfoForClass =
diff --git a/src/test/java/com/android/tools/r8/shaking/LibraryMethodOverrideCovariantTest.java b/src/test/java/com/android/tools/r8/shaking/LibraryMethodOverrideCovariantTest.java
index 89bad76..23febd2 100644
--- a/src/test/java/com/android/tools/r8/shaking/LibraryMethodOverrideCovariantTest.java
+++ b/src/test/java/com/android/tools/r8/shaking/LibraryMethodOverrideCovariantTest.java
@@ -10,8 +10,8 @@
 import com.android.tools.r8.TestBase;
 import com.android.tools.r8.TestParameters;
 import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.TestRunResult;
 import com.android.tools.r8.ToolHelper.DexVm.Version;
-import com.android.tools.r8.utils.AndroidApiLevel;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentHashMap.KeySetView;
 import org.junit.Test;
@@ -57,17 +57,18 @@
         .addLibraryClasses(LibraryUser.class)
         .addKeepMainRule(Main.class)
         .setMinApi(parameters.getApiLevel())
-        .applyIf(
-            parameters.isDexRuntime() && parameters.getApiLevel().isLessThan(AndroidApiLevel.N),
-            builder -> builder.addDontWarn(KeySetView.class))
         .enableInliningAnnotations()
         .compile()
         .addRunClasspathFiles(buildOnDexRuntime(parameters, LibraryUser.class))
         .run(parameters.getRuntime(), Main.class)
+        .assertFailureWithErrorThatMatchesIf(
+            parameters.isCfRuntime(), containsString("Hello World"))
+        .assertFailureWithErrorThatThrowsIf(
+            !supportsKeySetView() && parameters.isDexRuntime(), NoSuchMethodError.class)
+        // TODO(b/234579501): We fail to keep the library method override.
         .applyIf(
-            supportsKeySetView(),
-            result -> result.assertFailureWithErrorThatMatches(containsString("Hello World")),
-            result -> result.assertFailureWithErrorThatThrows(NoSuchMethodError.class));
+            parameters.isDexRuntime() && supportsKeySetView(),
+            TestRunResult::assertSuccessWithEmptyOutput);
   }
 
   private byte[] getMainWithoutSyntheticBridgeForKeySet() throws Exception {