Version 1.5.63

Cherry pick: Consider parameter annotations when evaluating keep rules
CL: https://r8-review.googlesource.com/c/r8/+/40923

Bug: 138044583
Change-Id: I4486d3c066c829f8411d67c08a90b5bcfeedfd8e
diff --git a/src/main/java/com/android/tools/r8/Version.java b/src/main/java/com/android/tools/r8/Version.java
index 821a8b6..59fc527 100644
--- a/src/main/java/com/android/tools/r8/Version.java
+++ b/src/main/java/com/android/tools/r8/Version.java
@@ -11,7 +11,7 @@
 
   // This field is accessed from release scripts using simple pattern matching.
   // Therefore, changing this field could break our release scripts.
-  public static final String LABEL = "1.5.62";
+  public static final String LABEL = "1.5.63";
 
   private Version() {
   }
diff --git a/src/main/java/com/android/tools/r8/shaking/ProguardMemberRule.java b/src/main/java/com/android/tools/r8/shaking/ProguardMemberRule.java
index 745b76d..f911e71 100644
--- a/src/main/java/com/android/tools/r8/shaking/ProguardMemberRule.java
+++ b/src/main/java/com/android/tools/r8/shaking/ProguardMemberRule.java
@@ -179,7 +179,7 @@
           break;
         }
         // Annotations check.
-        return RootSetBuilder.containsAnnotation(annotation, field.annotations);
+        return RootSetBuilder.containsAnnotation(annotation, field);
       case FIELD:
         // Name check.
         String name = stringCache.lookupString(originalSignature.name);
@@ -196,7 +196,7 @@
           break;
         }
         // Annotations check
-        if (!RootSetBuilder.containsAnnotation(annotation, field.annotations)) {
+        if (!RootSetBuilder.containsAnnotation(annotation, field)) {
           break;
         }
         return true;
@@ -224,7 +224,7 @@
           break;
         }
         // Annotations check.
-        return RootSetBuilder.containsAnnotation(annotation, method.annotations);
+        return RootSetBuilder.containsAnnotation(annotation, method);
       case METHOD:
         // Check return type.
         if (!type.matches(originalSignature.proto.returnType, appView)) {
@@ -244,7 +244,7 @@
           break;
         }
         // Annotations check.
-        if (!RootSetBuilder.containsAnnotation(annotation, method.annotations)) {
+        if (!RootSetBuilder.containsAnnotation(annotation, method)) {
           break;
         }
         // Parameter types check.
diff --git a/src/main/java/com/android/tools/r8/shaking/RootSetBuilder.java b/src/main/java/com/android/tools/r8/shaking/RootSetBuilder.java
index 40152ab..6e71367 100644
--- a/src/main/java/com/android/tools/r8/shaking/RootSetBuilder.java
+++ b/src/main/java/com/android/tools/r8/shaking/RootSetBuilder.java
@@ -41,7 +41,6 @@
 import java.io.PrintStream;
 import java.util.ArrayDeque;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
@@ -664,7 +663,7 @@
   }
 
   private static boolean satisfyAnnotation(ProguardConfigurationRule rule, DexClass clazz) {
-    return containsAnnotation(rule.getClassAnnotation(), clazz.annotations);
+    return containsAnnotation(rule.getClassAnnotation(), clazz);
   }
 
   private boolean satisfyInheritanceRule(DexClass clazz, ProguardConfigurationRule rule) {
@@ -717,7 +716,7 @@
       // the target class? If so, it is sufficient only to apply the annotation-matcher to the
       // annotations of `class`.
       if (rule.getInheritanceClassName().matches(clazz.type, appView)
-          && containsAnnotation(rule.getInheritanceAnnotation(), clazz.annotations)) {
+          && containsAnnotation(rule.getInheritanceAnnotation(), clazz)) {
         return true;
       }
       type = clazz.superType;
@@ -750,7 +749,7 @@
       // the target class? If so, it is sufficient only to apply the annotation-matcher to the
       // annotations of `ifaceClass`.
       if (rule.getInheritanceClassName().matches(iface, appView)
-          && containsAnnotation(rule.getInheritanceAnnotation(), ifaceClass.annotations)) {
+          && containsAnnotation(rule.getInheritanceAnnotation(), ifaceClass)) {
         return true;
       }
       if (anyImplementedInterfaceMatchesImplementsRule(ifaceClass, rule)) {
@@ -813,10 +812,6 @@
     return false;
   }
 
-  private boolean ruleSatisfiedByMethods(ProguardMemberRule rule, DexEncodedMethod[] methods) {
-    return ruleSatisfiedByMethods(rule, Arrays.asList(methods));
-  }
-
   private boolean ruleSatisfiedByFields(ProguardMemberRule rule, Iterable<DexEncodedField> fields) {
     if (rule.getRuleType().includesFields()) {
       for (DexEncodedField field : fields) {
@@ -828,12 +823,28 @@
     return false;
   }
 
-  private boolean ruleSatisfiedByFields(ProguardMemberRule rule, DexEncodedField[] fields) {
-    return ruleSatisfiedByFields(rule, Arrays.asList(fields));
+  static boolean containsAnnotation(ProguardTypeMatcher classAnnotation, DexClass clazz) {
+    return containsAnnotation(classAnnotation, clazz.annotations);
   }
 
-  static boolean containsAnnotation(ProguardTypeMatcher classAnnotation,
-      DexAnnotationSet annotations) {
+  static boolean containsAnnotation(ProguardTypeMatcher classAnnotation, DexEncodedMethod method) {
+    if (containsAnnotation(classAnnotation, method.annotations)) {
+      return true;
+    }
+    for (int i = 0; i < method.parameterAnnotationsList.size(); i++) {
+      if (containsAnnotation(classAnnotation, method.parameterAnnotationsList.get(i))) {
+        return true;
+      }
+    }
+    return false;
+  }
+
+  static boolean containsAnnotation(ProguardTypeMatcher classAnnotation, DexEncodedField field) {
+    return containsAnnotation(classAnnotation, field.annotations);
+  }
+
+  private static boolean containsAnnotation(
+      ProguardTypeMatcher classAnnotation, DexAnnotationSet annotations) {
     if (classAnnotation == null) {
       return true;
     }
diff --git a/src/test/java/com/android/tools/r8/shaking/annotations/AnnotationsOnParameterTest.java b/src/test/java/com/android/tools/r8/shaking/annotations/AnnotationsOnParameterTest.java
new file mode 100644
index 0000000..7a7f119
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/shaking/annotations/AnnotationsOnParameterTest.java
@@ -0,0 +1,68 @@
+// Copyright (c) 2019, 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 com.android.tools.r8.shaking.annotations;
+
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.codeinspector.ClassSubject;
+import com.android.tools.r8.utils.codeinspector.MethodSubject;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class AnnotationsOnParameterTest extends TestBase {
+
+  private final TestParameters parameters;
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withAllRuntimes().build();
+  }
+
+  public AnnotationsOnParameterTest(TestParameters parameters) {
+    this.parameters = parameters;
+  }
+
+  @Test
+  public void test() throws Exception {
+    testForR8(parameters.getBackend())
+        .addProgramClasses(TestClass.class, Keep.class)
+        .addKeepAttributes("*Annotations*")
+        .addKeepRules("-keep class * { @" + Keep.class.getTypeName() + " *; }")
+        .setMinApi(parameters.getRuntime())
+        .compile()
+        .inspect(
+            inspector -> {
+              ClassSubject classSubject = inspector.clazz(TestClass.class);
+              assertThat(classSubject, isPresent());
+
+              MethodSubject mainSubject = classSubject.mainMethod();
+              assertThat(mainSubject, isPresent());
+            })
+        .run(parameters.getRuntime(), TestClass.class)
+        .assertSuccessWithOutputLines("Hello world!");
+  }
+
+  static class TestClass {
+
+    public static void main(@Keep String[] args) {
+      System.out.println("Hello world!");
+    }
+  }
+
+  @Retention(RetentionPolicy.CLASS)
+  @Target({ElementType.PARAMETER})
+  @interface Keep {}
+}