Fix assertion errors in SystemUI

Fixes: b/379072500
Bug: b/380029173
Change-Id: I97d5b7285a7ca7df162e2668540ec42e62046c88
diff --git a/src/main/java/com/android/tools/r8/optimize/argumentpropagation/ArgumentPropagatorOptimizationInfoPopulator.java b/src/main/java/com/android/tools/r8/optimize/argumentpropagation/ArgumentPropagatorOptimizationInfoPopulator.java
index be9d7c5..f18dc04 100644
--- a/src/main/java/com/android/tools/r8/optimize/argumentpropagation/ArgumentPropagatorOptimizationInfoPopulator.java
+++ b/src/main/java/com/android/tools/r8/optimize/argumentpropagation/ArgumentPropagatorOptimizationInfoPopulator.java
@@ -142,7 +142,18 @@
     } else if (fieldType.isClassType()) {
       ConcreteClassTypeValueState classState = state.asClassState();
       setAbstractValue(field, classState.getAbstractValue(appView));
-      setDynamicType(field, classState.getDynamicType());
+
+      DynamicTypeWithUpperBound existingDynamicType =
+          field.getOptimizationInfo().getDynamicType().asDynamicTypeWithUpperBound();
+      DynamicType computedDynamicType = classState.getDynamicType();
+      if (existingDynamicType == null
+          || existingDynamicType.isUnknown()
+          || (computedDynamicType.isDynamicTypeWithUpperBound()
+              && computedDynamicType
+                  .asDynamicTypeWithUpperBound()
+                  .strictlyLessThan(existingDynamicType, appView))) {
+        setDynamicType(field, computedDynamicType);
+      }
     } else {
       assert fieldType.isPrimitiveType();
       ConcretePrimitiveTypeValueState primitiveState = state.asPrimitiveState();
diff --git a/src/main/java/com/android/tools/r8/shaking/RootSetUtils.java b/src/main/java/com/android/tools/r8/shaking/RootSetUtils.java
index de54647..27e13b0 100644
--- a/src/main/java/com/android/tools/r8/shaking/RootSetUtils.java
+++ b/src/main/java/com/android/tools/r8/shaking/RootSetUtils.java
@@ -218,6 +218,9 @@
         DexClass clazz,
         ProguardConfigurationRule rule,
         ProguardIfRulePreconditionMatch ifRulePreconditionMatch) {
+      if (!satisfyNonSyntheticClass(clazz, appView)) {
+        return;
+      }
       if (!satisfyClassType(rule, clazz)) {
         return;
       }
@@ -864,6 +867,11 @@
       out.close();
     }
 
+    static boolean satisfyNonSyntheticClass(DexClass clazz, AppView<?> appView) {
+      return !clazz.isProgramClass()
+          || !appView.getSyntheticItems().isSynthetic(clazz.asProgramClass());
+    }
+
     static boolean satisfyClassType(ProguardConfigurationRule rule, DexClass clazz) {
       return rule.getClassType().matches(clazz) != rule.getClassTypeNegated();
     }
diff --git a/src/test/examplesJava17/records/EmptyRecordAnnotationTest.java b/src/test/examplesJava17/records/EmptyRecordAnnotationTest.java
index 1759f97..691b91c 100644
--- a/src/test/examplesJava17/records/EmptyRecordAnnotationTest.java
+++ b/src/test/examplesJava17/records/EmptyRecordAnnotationTest.java
@@ -10,28 +10,25 @@
 import com.android.tools.r8.TestParametersCollection;
 import com.android.tools.r8.TestRuntime.CfVm;
 import com.android.tools.r8.utils.StringUtils;
+import com.android.tools.r8.utils.codeinspector.ClassSubject;
+import com.android.tools.r8.utils.codeinspector.CodeInspector;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 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 EmptyRecordAnnotationTest extends TestBase {
 
-  private static final String EXPECTED_RESULT_NATIVE_OR_PARTIALLY_DESUGARED_RECORD =
-      StringUtils.lines("class java.lang.Record", "class records.EmptyRecordAnnotationTest$Empty");
-  private static final String EXPECTED_RESULT_DESUGARED_RECORD =
-      StringUtils.lines(
-          "class com.android.tools.r8.RecordTag", "class records.EmptyRecordAnnotationTest$Empty");
-
   private final TestParameters parameters;
 
   public EmptyRecordAnnotationTest(TestParameters parameters) {
     this.parameters = parameters;
   }
 
-  @Parameterized.Parameters(name = "{0}")
+  @Parameters(name = "{0}")
   public static TestParametersCollection data() {
     return getTestParameters()
         .withCfRuntimesStartingFromIncluding(CfVm.JDK17)
@@ -46,7 +43,7 @@
     testForJvm(parameters)
         .addInnerClassesAndStrippedOuter(getClass())
         .run(parameters.getRuntime(), TestClass.class)
-        .assertSuccessWithOutput(EXPECTED_RESULT_NATIVE_OR_PARTIALLY_DESUGARED_RECORD);
+        .apply(rr -> rr.assertSuccessWithOutput(getExpectedOutput(rr.inspector(), false, false)));
   }
 
   @Test
@@ -57,10 +54,11 @@
         .setMinApi(parameters)
         .compile()
         .run(parameters.getRuntime(), TestClass.class)
-        .applyIf(
-            isRecordsFullyDesugaredForD8(parameters),
-            r -> r.assertSuccessWithOutput(EXPECTED_RESULT_DESUGARED_RECORD),
-            r -> r.assertSuccessWithOutput(EXPECTED_RESULT_NATIVE_OR_PARTIALLY_DESUGARED_RECORD));
+        .apply(
+            rr ->
+                rr.assertSuccessWithOutput(
+                    getExpectedOutput(
+                        rr.inspector(), isRecordsFullyDesugaredForD8(parameters), true)));
   }
 
   @Test
@@ -74,17 +72,32 @@
         .addKeepRules("-keepattributes *Annotation*")
         .addKeepRules("-keep class records.EmptyRecordAnnotationTest$Empty")
         .addKeepMainRule(TestClass.class)
-        // This is used to avoid renaming com.android.tools.r8.RecordTag.
-        .applyIf(
-            isRecordsFullyDesugaredForR8(parameters),
-            b -> b.addKeepRules("-keep class java.lang.Record"))
         .compile()
         .applyIf(parameters.isCfRuntime(), r -> r.inspect(RecordTestUtils::assertRecordsAreRecords))
         .run(parameters.getRuntime(), TestClass.class)
-        .applyIf(
-            isRecordsFullyDesugaredForR8(parameters),
-            r -> r.assertSuccessWithOutput(EXPECTED_RESULT_DESUGARED_RECORD),
-            r -> r.assertSuccessWithOutput(EXPECTED_RESULT_NATIVE_OR_PARTIALLY_DESUGARED_RECORD));
+        .apply(
+            rr ->
+                rr.assertSuccessWithOutput(
+                    getExpectedOutput(
+                        rr.inspector(), isRecordsFullyDesugaredForD8(parameters), false)));
+  }
+
+  private String getExpectedOutput(CodeInspector inspector, boolean isDesugared, boolean isD8) {
+    if (isDesugared) {
+      ClassSubject recordClass = inspector.clazz("java.lang.Record");
+      String recordName;
+      if (recordClass.isPresent() && !isD8) {
+        recordName = recordClass.getFinalName();
+      } else if (parameters.isCfRuntime()) {
+        recordName = "java.lang.Record";
+      } else {
+        recordName = "com.android.tools.r8.RecordTag";
+      }
+      return StringUtils.lines(
+          "class " + recordName, "class records.EmptyRecordAnnotationTest$Empty");
+    }
+    return StringUtils.lines(
+        "class java.lang.Record", "class records.EmptyRecordAnnotationTest$Empty");
   }
 
   record Empty() {}
diff --git a/src/test/java/com/android/tools/r8/internal/benchmarks/appdumps/SystemUIBenchmarks.java b/src/test/java/com/android/tools/r8/internal/benchmarks/appdumps/SystemUIBenchmarks.java
index 045537d..abeb770 100644
--- a/src/test/java/com/android/tools/r8/internal/benchmarks/appdumps/SystemUIBenchmarks.java
+++ b/src/test/java/com/android/tools/r8/internal/benchmarks/appdumps/SystemUIBenchmarks.java
@@ -83,4 +83,9 @@
   public void testBenchmarks() throws Exception {
     super.testBenchmarks();
   }
+
+  @Test
+  public void testSystemUIApp() throws Exception {
+    testBenchmarkWithName("SystemUIApp");
+  }
 }
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/inliner/InlineLibraryInterfaceMethodTest.java b/src/test/java/com/android/tools/r8/ir/optimize/inliner/InlineLibraryInterfaceMethodTest.java
index 9ec6561..7e61ae9 100644
--- a/src/test/java/com/android/tools/r8/ir/optimize/inliner/InlineLibraryInterfaceMethodTest.java
+++ b/src/test/java/com/android/tools/r8/ir/optimize/inliner/InlineLibraryInterfaceMethodTest.java
@@ -57,8 +57,8 @@
             });
 
     // TODO(b/126323172) Add test here to check the same with desugared lambdas.
-    assertEquals(1, counts.run);
-    assertEquals(1, counts.println);
+    assertEquals(0, counts.run);
+    assertEquals(2, counts.println);
   }
 
   private static long countInvokesWithName(MethodSubject methodSubject, String name) {
diff --git a/src/test/java/com/android/tools/r8/kotlin/KotlinClassInlinerTest.java b/src/test/java/com/android/tools/r8/kotlin/KotlinClassInlinerTest.java
index 41c2ec4..10ce745 100644
--- a/src/test/java/com/android/tools/r8/kotlin/KotlinClassInlinerTest.java
+++ b/src/test/java/com/android/tools/r8/kotlin/KotlinClassInlinerTest.java
@@ -13,6 +13,7 @@
 import static com.android.tools.r8.utils.codeinspector.Matchers.isAbsent;
 import static com.android.tools.r8.utils.codeinspector.Matchers.isAbsentIf;
 import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresentIf;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
@@ -280,14 +281,14 @@
                 assertThat(
                     inspector.clazz(
                         "class_inliner_lambda_k_style.MainKt$testKotlinSequencesStateless$1"),
-                    isAbsent());
+                    isPresentIf(testParameters.isDexRuntime()));
                 assertThat(
                     inspector.clazz(
                         "class_inliner_lambda_k_style.MainKt$testKotlinSequencesStateful$1"),
                     isAbsent());
                 assertThat(
                     inspector.clazz("class_inliner_lambda_k_style.MainKt$testBigExtraMethod$1"),
-                    isPresent());
+                    isPresentIf(testParameters.isCfRuntime()));
               }
             });
   }
diff --git a/src/test/java/com/android/tools/r8/kotlin/lambda/KotlinLambdaMergingTrivialKotlinStyleTest.java b/src/test/java/com/android/tools/r8/kotlin/lambda/KotlinLambdaMergingTrivialKotlinStyleTest.java
index 11b692e..01b94ee 100644
--- a/src/test/java/com/android/tools/r8/kotlin/lambda/KotlinLambdaMergingTrivialKotlinStyleTest.java
+++ b/src/test/java/com/android/tools/r8/kotlin/lambda/KotlinLambdaMergingTrivialKotlinStyleTest.java
@@ -5,7 +5,6 @@
 package com.android.tools.r8.kotlin.lambda;
 
 import static junit.framework.TestCase.assertEquals;
-import static org.hamcrest.CoreMatchers.containsString;
 import static org.junit.Assume.assumeFalse;
 import static org.junit.Assume.assumeTrue;
 
@@ -100,36 +99,21 @@
                   .assertNoOtherClassesMerged());
     } else {
       ClassReference mainKt = Reference.classFromTypeName(getMainClassName());
-      inspector.applyIf(
-          kotlinParameters.getLambdaGeneration().isClass(),
-          i -> {
-            i.assertIsCompleteMergeGroup(
-                lambdasInInput.getKStyleLambdaReferenceFromTypeName(
-                    getTestName(), "MainKt$testStateless$3"),
-                lambdasInInput.getKStyleLambdaReferenceFromTypeName(
-                    getTestName(), "MainKt$testStateless$4"),
-                lambdasInInput.getKStyleLambdaReferenceFromTypeName(
-                    getTestName(), "MainKt$testStateless$5"),
-                lambdasInInput.getKStyleLambdaReferenceFromTypeName(
-                    getTestName(), "MainKt$testStateless$6"),
-                lambdasInInput.getKStyleLambdaReferenceFromTypeName(
-                    getTestName(), "MainKt$testStateless$7"),
-                lambdasInInput.getKStyleLambdaReferenceFromTypeName(
-                    getTestName(), "MainKt$testStateless$8"),
-                lambdasInInput.getKStyleLambdaReferenceFromTypeName(
-                    getTestName(), "MainKt$testStateless$9"),
-                lambdasInInput.getKStyleLambdaReferenceFromTypeName(
-                    getTestName(), "MainKt$testStateless$10"),
-                lambdasInInput.getKStyleLambdaReferenceFromTypeName(
-                    getTestName(), "MainKt$testStateless$11"),
-                lambdasInInput.getKStyleLambdaReferenceFromTypeName(
-                    getTestName(), "MainKt$testStateless$12"));
-          },
-          i ->
-              i.assertIsCompleteMergeGroup(
+      inspector
+          .applyIf(
+              kotlinParameters.getLambdaGeneration().isClass(),
+              i -> {
+                i.assertIsCompleteMergeGroup(
+                    lambdasInInput.getKStyleLambdaReferenceFromTypeName(
+                        getTestName(), "MainKt$testStateless$11"),
+                    lambdasInInput.getKStyleLambdaReferenceFromTypeName(
+                        getTestName(), "MainKt$testStateless$12"));
+              },
+              i ->
+                  i.assertIsCompleteMergeGroup(
                       SyntheticItemsTestUtils.syntheticLambdaClass(mainKt, 0),
-                      SyntheticItemsTestUtils.syntheticLambdaClass(mainKt, 1))
-                  .assertNoOtherClassesMerged());
+                      SyntheticItemsTestUtils.syntheticLambdaClass(mainKt, 1)))
+          .assertNoOtherClassesMerged();
     }
   }
 
@@ -143,7 +127,7 @@
     assertEquals(
         kotlinParameters.getLambdaGeneration().isInvokeDynamic()
             ? 0
-            : allowAccessModification && parameters.isCfRuntime() ? 1 : 2,
+            : allowAccessModification && parameters.isCfRuntime() ? 1 : 1,
         lambdasInOutput.size());
   }