Handle exceptional edges with CF verification errors.

Bug: b/261967650
Change-Id: Ic41a6aee1bcaa9da15602b93d79ac69e666c8d57
diff --git a/src/main/java/com/android/tools/r8/cf/code/CfFrameVerifier.java b/src/main/java/com/android/tools/r8/cf/code/CfFrameVerifier.java
index 44f3fde..58e9d2a 100644
--- a/src/main/java/com/android/tools/r8/cf/code/CfFrameVerifier.java
+++ b/src/main/java/com/android/tools/r8/cf/code/CfFrameVerifier.java
@@ -117,6 +117,11 @@
         if (appView.options().enableCheckAllInstructionsDuringStackMapVerification
             || instruction.canThrow()) {
           state = checkExceptionEdges(state, labelToFrameMap);
+          if (state.isError()) {
+            return fail(
+                CfCodeStackMapValidatingException.invalidStackMapForInstruction(
+                    method, i, instruction, state.asError().getMessage(), appView));
+          }
         }
       }
       eventConsumer.acceptInstructionState(instruction, state);
diff --git a/src/test/java/com/android/tools/r8/cf/stackmap/UndefinedTypesAssignmentTest.java b/src/test/java/com/android/tools/r8/cf/stackmap/UndefinedTypesAssignmentTest.java
new file mode 100644
index 0000000..9e8c086e
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/cf/stackmap/UndefinedTypesAssignmentTest.java
@@ -0,0 +1,84 @@
+// Copyright (c) 2022, 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.cf.stackmap;
+
+import static com.android.tools.r8.DiagnosticsMatcher.diagnosticType;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.AndroidApiLevel;
+import com.android.tools.r8.utils.StringUtils;
+import com.android.tools.r8.utils.UnverifiableCfCodeDiagnostic;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+/** Regression test for b/b/261967650 */
+@RunWith(Parameterized.class)
+public class UndefinedTypesAssignmentTest extends TestBase {
+
+  static final String EXPECTED = StringUtils.lines("Hello, world");
+
+  private final TestParameters parameters;
+
+  @Parameterized.Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters()
+        .withDefaultRuntimes()
+        .withApiLevel(AndroidApiLevel.B)
+        .enableApiLevelsForCf()
+        .build();
+  }
+
+  public UndefinedTypesAssignmentTest(TestParameters parameters) {
+    this.parameters = parameters;
+  }
+
+  @Test
+  public void testReference() throws Exception {
+    testForD8(parameters.getBackend())
+        .addProgramClasses(TestClass.class)
+        .setMinApi(parameters.getApiLevel())
+        .compile()
+        .addRunClasspathClasses(I.class, A.class)
+        .run(parameters.getRuntime(), TestClass.class)
+        .assertSuccessWithOutput(EXPECTED);
+  }
+
+  @Test
+  public void testR8() throws Exception {
+    testForR8(parameters.getBackend())
+        .addProgramClasses(TestClass.class)
+        .addDontWarn(I.class, A.class)
+        .addKeepMainRule(TestClass.class)
+        .setMinApi(parameters.getApiLevel())
+        .allowDiagnosticWarningMessages()
+        .compileWithExpectedDiagnostics(
+            diagnostics ->
+                diagnostics
+                    .assertOnlyWarnings()
+                    .assertWarningsMatch(diagnosticType(UnverifiableCfCodeDiagnostic.class)))
+        .addRunClasspathClasses(I.class, A.class)
+        .run(parameters.getRuntime(), TestClass.class)
+        .assertSuccessWithOutput(EXPECTED);
+  }
+
+  interface I {}
+
+  static class A implements I {}
+
+  static class TestClass {
+
+    public static void main(String[] args) {
+      I i = null;
+      try {
+        i = new A();
+        System.out.println("Hello, world");
+      } catch (Exception e) {
+        System.out.println(i);
+      }
+    }
+  }
+}