Use StateError in LegacyTypeAsserter.

AssertionError does not print on the console its message, at least in
IntelliJ. Also tweak tests to use throwsStateError.

R=brianwilkerson@google.com

Change-Id: Ifbe2ed0b1d3deba1feaecc7c462f470e8608b70c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/106204
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/src/dart/resolver/legacy_type_asserter.dart b/pkg/analyzer/lib/src/dart/resolver/legacy_type_asserter.dart
index 14dc50d..cd09795 100644
--- a/pkg/analyzer/lib/src/dart/resolver/legacy_type_asserter.dart
+++ b/pkg/analyzer/lib/src/dart/resolver/legacy_type_asserter.dart
@@ -158,7 +158,7 @@
       return;
     }
 
-    throw AssertionError('Expected all legacy types, but got '
+    throw StateError('Expected all legacy types, but got '
         '${(type as TypeImpl).toString(withNullability: true)} '
         '(${type.runtimeType})');
   }
diff --git a/pkg/analyzer/test/src/dart/resolver/legacy_type_asserter_test.dart b/pkg/analyzer/test/src/dart/resolver/legacy_type_asserter_test.dart
index 7c7baab..2447515 100644
--- a/pkg/analyzer/test/src/dart/resolver/legacy_type_asserter_test.dart
+++ b/pkg/analyzer/test/src/dart/resolver/legacy_type_asserter_test.dart
@@ -34,12 +34,9 @@
     var identifier = AstTestFactory.identifier3('foo');
     var unit = _wrapExpression(identifier);
     identifier.staticType = BottomTypeImpl.instance;
-    try {
+    expect(() {
       LegacyTypeAsserter.assertLegacyTypes(unit);
-      fail('expected an exception');
-    } on AssertionError {
-      // expected
-    }
+    }, throwsStateError);
   }
 
   test_nullableUnit_expressionStaticType_bottomQuestion() async {
@@ -61,12 +58,9 @@
     var unit = _wrapExpression(identifier);
     identifier.staticType = (typeProvider.intType as TypeImpl)
         .withNullability(NullabilitySuffix.none);
-    try {
+    expect(() {
       LegacyTypeAsserter.assertLegacyTypes(unit);
-      fail('expected an exception');
-    } on AssertionError {
-      // expected
-    }
+    }, throwsStateError);
   }
 
   test_nullableUnit_expressionStaticType_nonNullTypeArgument() async {
@@ -77,12 +71,9 @@
           .withNullability(NullabilitySuffix.question)
     ]);
 
-    try {
+    expect(() {
       LegacyTypeAsserter.assertLegacyTypes(unit);
-      fail('expected an exception');
-    } on AssertionError {
-      // expected
-    }
+    }, throwsStateError);
   }
 
   test_nullableUnit_expressionStaticType_nonNullTypeParameter() async {
@@ -99,12 +90,9 @@
             .type
             .toString(withNullability: true),
         'E');
-    try {
+    expect(() {
       LegacyTypeAsserter.assertLegacyTypes(unit);
-      fail('expected an exception');
-    } on AssertionError {
-      // expected
-    }
+    }, throwsStateError);
   }
 
   test_nullableUnit_expressionStaticType_nonNullTypeParameterBound() async {
@@ -122,12 +110,9 @@
             .bound
             .toString(withNullability: true),
         'int');
-    try {
+    expect(() {
       LegacyTypeAsserter.assertLegacyTypes(unit);
-      fail('expected an exception');
-    } on AssertionError {
-      // expected
-    }
+    }, throwsStateError);
   }
 
   test_nullableUnit_expressionStaticType_null() async {
@@ -142,12 +127,9 @@
     var unit = _wrapExpression(identifier);
     identifier.staticType = (typeProvider.intType as TypeImpl)
         .withNullability(NullabilitySuffix.question);
-    try {
+    expect(() {
       LegacyTypeAsserter.assertLegacyTypes(unit);
-      fail('expected an exception');
-    } on AssertionError {
-      // expected
-    }
+    }, throwsStateError);
   }
 
   test_nullableUnit_expressionStaticType_star() async {