Enforce that all ErrorReporter arguments come from a limited set of types.

This change ensures that the only types ever passed to ErrorReporter
arguments are String, DartType, int, Uri, or Element.  A few call
sites had to be changed so that instead of passing in AST nodes they
passed in the result of calling `toString` on those AST nodes.

In future CLs, I plan to move toward a system where instead of passing
the message arguments to ErrorReporter as a list of `Object`, we call
a `withArguments` method that's specific to each error; that method
will have a specific list of argument types that it supports, so that
if we accidentally pass the wrong things into an error message, the
problem will be detected at compile time (instead of the user just
seeing a confusing error message).

The `withArguments` methods will be code generated from annotations in
the analyzer's `messages.yaml` file.  Keeping the set of types
supported by those `withArguments` messages small should help simplify
the code generation process, and provide flexibility for how errors
are represented in the future.

Change-Id: I4a793db2fe5b1344199b770898634b61be41ad11
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/216301
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/error/listener.dart b/pkg/analyzer/lib/error/listener.dart
index ba45f03..6209775 100644
--- a/pkg/analyzer/lib/error/listener.dart
+++ b/pkg/analyzer/lib/error/listener.dart
@@ -165,6 +165,12 @@
         arguments[i] = argument.getDisplayString(
           withNullability: isNonNullableByDefault,
         );
+      } else if (!(argument is String ||
+          argument is DartType ||
+          argument is int ||
+          argument is Uri)) {
+        throw ArgumentError(
+            'Tried to format an error using ${argument.runtimeType}');
       }
     }
   }
diff --git a/pkg/analyzer/lib/src/dart/resolver/function_reference_resolver.dart b/pkg/analyzer/lib/src/dart/resolver/function_reference_resolver.dart
index 7c8bc9f..dedb7b5 100644
--- a/pkg/analyzer/lib/src/dart/resolver/function_reference_resolver.dart
+++ b/pkg/analyzer/lib/src/dart/resolver/function_reference_resolver.dart
@@ -64,7 +64,10 @@
         _errorReporter.reportErrorForNode(
           CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS_CONSTRUCTOR,
           typeArguments,
-          [function.constructorName.type2.name, function.constructorName.name!],
+          [
+            function.constructorName.type2.name.toSource(),
+            function.constructorName.name!.name
+          ],
         );
         _resolve(node: node, rawType: function.staticType);
       }
diff --git a/pkg/analyzer/lib/src/error/best_practices_verifier.dart b/pkg/analyzer/lib/src/error/best_practices_verifier.dart
index 85441dc..2e4ffc1 100644
--- a/pkg/analyzer/lib/src/error/best_practices_verifier.dart
+++ b/pkg/analyzer/lib/src/error/best_practices_verifier.dart
@@ -482,9 +482,10 @@
           // always named, so we can safely assume
           // `overriddenElement.enclosingElement.name` is non-`null`.
           _errorReporter.reportErrorForNode(
-              HintCode.INVALID_OVERRIDE_OF_NON_VIRTUAL_MEMBER,
-              field.name,
-              [field.name, overriddenElement.enclosingElement.name!]);
+              HintCode.INVALID_OVERRIDE_OF_NON_VIRTUAL_MEMBER, field.name, [
+            field.name.name,
+            overriddenElement.enclosingElement.displayName
+          ]);
         }
         if (!_invalidAccessVerifier._inTestDirectory) {
           _checkForAssignmentOfDoNotStore(field.initializer);
@@ -671,7 +672,7 @@
         _errorReporter.reportErrorForNode(
             HintCode.INVALID_OVERRIDE_OF_NON_VIRTUAL_MEMBER,
             node.name,
-            [node.name, overriddenElement.enclosingElement.name!]);
+            [node.name.name, overriddenElement.enclosingElement.displayName]);
       }
 
       super.visitMethodDeclaration(node);
diff --git a/pkg/analyzer/lib/src/error/type_arguments_verifier.dart b/pkg/analyzer/lib/src/error/type_arguments_verifier.dart
index c2f2f09..9db6af2 100644
--- a/pkg/analyzer/lib/src/error/type_arguments_verifier.dart
+++ b/pkg/analyzer/lib/src/error/type_arguments_verifier.dart
@@ -499,7 +499,8 @@
       NodeList<TypeAnnotation> arguments, ErrorCode errorCode) {
     for (TypeAnnotation type in arguments) {
       if (type is NamedType && type.type is TypeParameterType) {
-        _errorReporter.reportErrorForNode(errorCode, type, [type.name]);
+        _errorReporter
+            .reportErrorForNode(errorCode, type, [type.name.toSource()]);
       }
     }
   }
diff --git a/pkg/analyzer/lib/src/generated/element_resolver.dart b/pkg/analyzer/lib/src/generated/element_resolver.dart
index 717940b..e64547d 100644
--- a/pkg/analyzer/lib/src/generated/element_resolver.dart
+++ b/pkg/analyzer/lib/src/generated/element_resolver.dart
@@ -438,7 +438,7 @@
         _errorReporter.reportErrorForNode(
             CompileTimeErrorCode.UNDEFINED_CONSTRUCTOR_IN_INITIALIZER,
             node,
-            [superType, name]);
+            [superType, name.name]);
       } else {
         _errorReporter.reportErrorForNode(
             CompileTimeErrorCode.UNDEFINED_CONSTRUCTOR_IN_INITIALIZER_DEFAULT,
diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart
index ffac3f2..e140ee8 100644
--- a/pkg/analyzer/lib/src/generated/error_verifier.dart
+++ b/pkg/analyzer/lib/src/generated/error_verifier.dart
@@ -795,7 +795,7 @@
         if (parameterType is FunctionType &&
             parameterType.returnType.isDynamic) {
           errorReporter.reportErrorForNode(LanguageCode.IMPLICIT_DYNAMIC_RETURN,
-              node.identifier, [node.identifier]);
+              node.identifier, [node.identifier.name]);
         }
       }
 
@@ -2089,12 +2089,12 @@
       errorReporter.reportErrorForNode(
           CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR,
           name,
-          [className, name]);
+          [className.toSource(), name.name]);
     } else {
       errorReporter.reportErrorForNode(
           CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR_DEFAULT,
           constructorName,
-          [className]);
+          [className.toSource()]);
     }
   }
 
@@ -2593,7 +2593,7 @@
       }
       // Parameters associated with a variable always have a name, so we can
       // safely rely on [id] being non-`null`.
-      errorReporter.reportErrorForNode(errorCode, node, [id!]);
+      errorReporter.reportErrorForNode(errorCode, node, [id!.toSource()]);
     }
   }
 
@@ -2739,18 +2739,18 @@
         errorReporter.reportErrorForNode(
             CompileTimeErrorCode.INITIALIZER_FOR_NON_EXISTENT_FIELD,
             initializer,
-            [fieldName]);
+            [fieldName.name]);
       } else if (staticElement.isStatic) {
         errorReporter.reportErrorForNode(
             CompileTimeErrorCode.INITIALIZER_FOR_STATIC_FIELD,
             initializer,
-            [fieldName]);
+            [fieldName.name]);
       }
     } else {
       errorReporter.reportErrorForNode(
           CompileTimeErrorCode.INITIALIZER_FOR_NON_EXISTENT_FIELD,
           initializer,
-          [fieldName]);
+          [fieldName.name]);
       return;
     }
   }
@@ -3328,12 +3328,12 @@
       errorReporter.reportErrorForNode(
           CompileTimeErrorCode.NEW_WITH_UNDEFINED_CONSTRUCTOR,
           name,
-          [className, name]);
+          [className.toSource(), name.name]);
     } else {
       errorReporter.reportErrorForNode(
           CompileTimeErrorCode.NEW_WITH_UNDEFINED_CONSTRUCTOR_DEFAULT,
           constructorName,
-          [className]);
+          [className.toSource()]);
     }
   }
 
@@ -3651,8 +3651,9 @@
 
       if (treatedAsDouble) {
         // Suggest the nearest valid double (as a BigInt for printing reasons).
-        extraErrorArgs
-            .add(BigInt.from(IntegerLiteralImpl.nearestValidDouble(lexeme)));
+        extraErrorArgs.add(
+            BigInt.from(IntegerLiteralImpl.nearestValidDouble(lexeme))
+                .toString());
       }
 
       errorReporter.reportErrorForNode(
@@ -4063,7 +4064,7 @@
       errorReporter.reportErrorForNode(
           CompileTimeErrorCode.TYPE_ANNOTATION_DEFERRED_CLASS,
           type,
-          [type.name]);
+          [type.name.toSource()]);
     }
   }
 
diff --git a/pkg/analyzer/lib/src/generated/ffi_verifier.dart b/pkg/analyzer/lib/src/generated/ffi_verifier.dart
index 4d6223c..517eabc 100644
--- a/pkg/analyzer/lib/src/generated/ffi_verifier.dart
+++ b/pkg/analyzer/lib/src/generated/ffi_verifier.dart
@@ -108,11 +108,11 @@
         return;
       }
       if (typename.ffiClass != null) {
-        _errorReporter.reportErrorForNode(
-            subtypeOfFfiCode, typename, [node.name, typename.name]);
+        _errorReporter.reportErrorForNode(subtypeOfFfiCode, typename,
+            [node.name.name, typename.name.toSource()]);
       } else if (typename.isCompoundSubtype) {
-        _errorReporter.reportErrorForNode(
-            subtypeOfStructCode, typename, [node.name, typename.name]);
+        _errorReporter.reportErrorForNode(subtypeOfStructCode, typename,
+            [node.name.name, typename.name.toSource()]);
       }
     }
 
@@ -133,7 +133,7 @@
 
     if (inCompound && node.declaredElement!.typeParameters.isNotEmpty) {
       _errorReporter.reportErrorForNode(
-          FfiCode.GENERIC_STRUCT_SUBCLASS, node.name, [node.name]);
+          FfiCode.GENERIC_STRUCT_SUBCLASS, node.name, [node.name.name]);
     }
     super.visitClassDeclaration(node);
   }
diff --git a/pkg/analyzer/test/src/dart/resolution/function_reference_test.dart b/pkg/analyzer/test/src/dart/resolution/function_reference_test.dart
index fc226d1..a2dab45 100644
--- a/pkg/analyzer/test/src/dart/resolution/function_reference_test.dart
+++ b/pkg/analyzer/test/src/dart/resolution/function_reference_test.dart
@@ -311,13 +311,33 @@
 var x = A.foo<int>;
 ''', [
       error(CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS_CONSTRUCTOR, 42,
-          5),
+          5,
+          messageContains: ["'A.foo'"]),
     ]);
 
     assertFunctionReference(findNode.functionReference('A.foo<int>;'),
         findElement.constructor('foo'), 'dynamic');
   }
 
+  test_constructorReference_prefixed() async {
+    await assertErrorsInCode('''
+import 'dart:async' as a;
+var x = a.Future.delayed<int>;
+''', [
+      error(CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS_CONSTRUCTOR, 50,
+          5,
+          messageContains: ["'a.Future.delayed'"]),
+    ]);
+    assertFunctionReference(
+        findNode.functionReference('a.Future.delayed<int>;'),
+        findElement
+            .import('dart:async')
+            .importedLibrary!
+            .getType('Future')!
+            .getNamedConstructor('delayed'),
+        'dynamic');
+  }
+
   test_dynamicTyped() async {
     await assertErrorsInCode('''
 dynamic i = 1;
diff --git a/pkg/analyzer/test/src/diagnostics/const_with_undefined_constructor_test.dart b/pkg/analyzer/test/src/diagnostics/const_with_undefined_constructor_test.dart
index ee5737d..90bd671 100644
--- a/pkg/analyzer/test/src/diagnostics/const_with_undefined_constructor_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/const_with_undefined_constructor_test.dart
@@ -24,7 +24,23 @@
   return const A.noSuchConstructor();
 }
 ''', [
-      error(CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR, 48, 17),
+      error(CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR, 48, 17,
+          messageContains: ["class 'A'", "constructor 'noSuchConstructor'"]),
+    ]);
+  }
+
+  test_named_prefixed() async {
+    await assertErrorsInCode(r'''
+import 'dart:async' as a;
+f() {
+  return const a.Future.noSuchConstructor();
+}
+''', [
+      error(CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR, 56, 17,
+          messageContains: [
+            "class 'a.Future'",
+            "constructor 'noSuchConstructor'"
+          ]),
     ]);
   }
 
@@ -53,7 +69,26 @@
 }
 ''', [
       error(
-          CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR_DEFAULT, 51, 1),
+          CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR_DEFAULT, 51, 1,
+          messageContains: ["'A'"]),
+    ]);
+  }
+
+  test_unnamed_prefixed() async {
+    newFile('$testPackageLibPath/lib1.dart', content: '''
+class A {
+  const A.name();
+}
+''');
+    await assertErrorsInCode(r'''
+import 'lib1.dart' as lib1;
+f() {
+  return const lib1.A();
+}
+''', [
+      error(
+          CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR_DEFAULT, 49, 6,
+          messageContains: ["'lib1.A'"]),
     ]);
   }
 }
diff --git a/pkg/analyzer/test/src/diagnostics/generic_struct_subclass_test.dart b/pkg/analyzer/test/src/diagnostics/generic_struct_subclass_test.dart
index 68e338d..f154d5d 100644
--- a/pkg/analyzer/test/src/diagnostics/generic_struct_subclass_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/generic_struct_subclass_test.dart
@@ -22,7 +22,7 @@
   external Pointer notEmpty;
 }
 ''', [
-      error(FfiCode.GENERIC_STRUCT_SUBCLASS, 25, 1),
+      error(FfiCode.GENERIC_STRUCT_SUBCLASS, 25, 1, messageContains: ["'S'"]),
     ]);
   }
 
@@ -33,7 +33,7 @@
   external Pointer notEmpty;
 }
 ''', [
-      error(FfiCode.GENERIC_STRUCT_SUBCLASS, 25, 1),
+      error(FfiCode.GENERIC_STRUCT_SUBCLASS, 25, 1, messageContains: ["'S'"]),
     ]);
   }
 
diff --git a/pkg/analyzer/test/src/diagnostics/implicit_dynamic_field_test.dart b/pkg/analyzer/test/src/diagnostics/implicit_dynamic_field_test.dart
index fd002a6..1bfd680 100644
--- a/pkg/analyzer/test/src/diagnostics/implicit_dynamic_field_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/implicit_dynamic_field_test.dart
@@ -49,7 +49,8 @@
   final f = (<dynamic>[])[0];
 }
 ''', [
-      error(LanguageCode.IMPLICIT_DYNAMIC_FIELD, 18, 20),
+      error(LanguageCode.IMPLICIT_DYNAMIC_FIELD, 18, 20,
+          messageContains: ["'f'"]),
     ]);
   }
 
diff --git a/pkg/analyzer/test/src/diagnostics/initializer_for_non_existent_field_test.dart b/pkg/analyzer/test/src/diagnostics/initializer_for_non_existent_field_test.dart
index 43361f5..e2b6231 100644
--- a/pkg/analyzer/test/src/diagnostics/initializer_for_non_existent_field_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/initializer_for_non_existent_field_test.dart
@@ -24,7 +24,20 @@
 }
 A a = const A();
 ''', [
-      error(CompileTimeErrorCode.INITIALIZER_FOR_NON_EXISTENT_FIELD, 24, 9),
+      error(CompileTimeErrorCode.INITIALIZER_FOR_NON_EXISTENT_FIELD, 24, 9,
+          messageContains: ["'x'"]),
+    ]);
+  }
+
+  test_getter() async {
+    await assertErrorsInCode('''
+class A {
+  int get x => 0;
+  A() : x = 0;
+}
+''', [
+      error(CompileTimeErrorCode.INITIALIZER_FOR_NON_EXISTENT_FIELD, 36, 5,
+          messageContains: ["'x'"]),
     ]);
   }
 
diff --git a/pkg/analyzer/test/src/diagnostics/initializer_for_static_field_test.dart b/pkg/analyzer/test/src/diagnostics/initializer_for_static_field_test.dart
index 6acd087..d556c0b 100644
--- a/pkg/analyzer/test/src/diagnostics/initializer_for_static_field_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/initializer_for_static_field_test.dart
@@ -33,7 +33,8 @@
   A() : x = 0 {}
 }
 ''', [
-      error(CompileTimeErrorCode.INITIALIZER_FOR_STATIC_FIELD, 38, 5),
+      error(CompileTimeErrorCode.INITIALIZER_FOR_STATIC_FIELD, 38, 5,
+          messageContains: ["'x'"]),
     ]);
   }
 }
diff --git a/pkg/analyzer/test/src/diagnostics/invalid_override_of_non_virtual_member_test.dart b/pkg/analyzer/test/src/diagnostics/invalid_override_of_non_virtual_member_test.dart
index 10a52f8..7c31b18 100644
--- a/pkg/analyzer/test/src/diagnostics/invalid_override_of_non_virtual_member_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/invalid_override_of_non_virtual_member_test.dart
@@ -35,7 +35,8 @@
   int g = 0;
 }
 ''', [
-      error(HintCode.INVALID_OVERRIDE_OF_NON_VIRTUAL_MEMBER, 113, 1),
+      error(HintCode.INVALID_OVERRIDE_OF_NON_VIRTUAL_MEMBER, 113, 1,
+          messageContains: ["member 'g'", "in 'C'"]),
     ]);
   }
 
@@ -70,7 +71,8 @@
   int get g => 0;
 }
 ''', [
-      error(HintCode.INVALID_OVERRIDE_OF_NON_VIRTUAL_MEMBER, 117, 1),
+      error(HintCode.INVALID_OVERRIDE_OF_NON_VIRTUAL_MEMBER, 117, 1,
+          messageContains: ["member 'g'", "in 'C'"]),
     ]);
   }
 
@@ -228,7 +230,8 @@
   void f() {}
 }
 ''', [
-      error(HintCode.INVALID_OVERRIDE_OF_NON_VIRTUAL_MEMBER, 111, 1),
+      error(HintCode.INVALID_OVERRIDE_OF_NON_VIRTUAL_MEMBER, 111, 1,
+          messageContains: ["member 'f'", "in 'M'"]),
     ]);
   }
 
diff --git a/pkg/analyzer/test/src/diagnostics/invalid_type_argument_in_const_list_test.dart b/pkg/analyzer/test/src/diagnostics/invalid_type_argument_in_const_list_test.dart
index a47fffd..85db5dc 100644
--- a/pkg/analyzer/test/src/diagnostics/invalid_type_argument_in_const_list_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/invalid_type_argument_in_const_list_test.dart
@@ -23,7 +23,8 @@
   }
 }
 ''', [
-      error(CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_LIST, 39, 1),
+      error(CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_LIST, 39, 1,
+          messageContains: ["'E'"]),
     ]);
   }
 
diff --git a/pkg/analyzer/test/src/diagnostics/invalid_type_argument_in_const_map_test.dart b/pkg/analyzer/test/src/diagnostics/invalid_type_argument_in_const_map_test.dart
index 3c75e1d..d12b1e8 100644
--- a/pkg/analyzer/test/src/diagnostics/invalid_type_argument_in_const_map_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/invalid_type_argument_in_const_map_test.dart
@@ -23,7 +23,8 @@
   }
 }
 ''', [
-      error(CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_MAP, 39, 1),
+      error(CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_MAP, 39, 1,
+          messageContains: ["'E'"]),
     ]);
   }
 
@@ -45,7 +46,8 @@
   }
 }
 ''', [
-      error(CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_MAP, 47, 1),
+      error(CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_MAP, 47, 1,
+          messageContains: ["'E'"]),
     ]);
   }
 }
diff --git a/pkg/analyzer/test/src/diagnostics/invalid_type_argument_in_const_set_test.dart b/pkg/analyzer/test/src/diagnostics/invalid_type_argument_in_const_set_test.dart
index fe0c076..69d6881 100644
--- a/pkg/analyzer/test/src/diagnostics/invalid_type_argument_in_const_set_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/invalid_type_argument_in_const_set_test.dart
@@ -23,7 +23,8 @@
   }
 }
 ''', [
-      error(CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_SET, 39, 1),
+      error(CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_SET, 39, 1,
+          messageContains: ["'E'"]),
     ]);
   }
 }
diff --git a/pkg/analyzer/test/src/diagnostics/new_with_undefined_constructor_test.dart b/pkg/analyzer/test/src/diagnostics/new_with_undefined_constructor_test.dart
index 4ebf06c..4a7c777 100644
--- a/pkg/analyzer/test/src/diagnostics/new_with_undefined_constructor_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/new_with_undefined_constructor_test.dart
@@ -30,7 +30,8 @@
   new A();
 }
 ''', [
-      error(CompileTimeErrorCode.NEW_WITH_UNDEFINED_CONSTRUCTOR_DEFAULT, 38, 1),
+      error(CompileTimeErrorCode.NEW_WITH_UNDEFINED_CONSTRUCTOR_DEFAULT, 38, 1,
+          messageContains: ["'A'"]),
     ]);
   }
 
@@ -47,6 +48,24 @@
     ]);
   }
 
+  test_default_prefixed() async {
+    newFile('$testPackageLibPath/lib1.dart', content: '''
+class A {
+  A.name() {}
+}
+''');
+    await assertErrorsInCode('''
+import 'lib1.dart' as lib1;
+
+f() {
+  new lib1.A();
+}
+''', [
+      error(CompileTimeErrorCode.NEW_WITH_UNDEFINED_CONSTRUCTOR_DEFAULT, 41, 6,
+          messageContains: ["'lib1.A'"]),
+    ]);
+  }
+
   test_default_unnamedViaNew() async {
     await assertErrorsInCode('''
 class A {
@@ -102,7 +121,25 @@
   new A.name();
 }
 ''', [
-      error(CompileTimeErrorCode.NEW_WITH_UNDEFINED_CONSTRUCTOR, 35, 4),
+      error(CompileTimeErrorCode.NEW_WITH_UNDEFINED_CONSTRUCTOR, 35, 4,
+          messageContains: ["class 'A'", "named 'name'"]),
+    ]);
+  }
+
+  test_named_prefixed() async {
+    newFile('$testPackageLibPath/lib1.dart', content: '''
+class A {
+  A() {}
+}
+''');
+    await assertErrorsInCode('''
+import 'lib1.dart' as lib1;
+f() {
+  new lib1.A.name();
+}
+''', [
+      error(CompileTimeErrorCode.NEW_WITH_UNDEFINED_CONSTRUCTOR, 47, 4,
+          messageContains: ["class 'lib1.A'", "named 'name'"]),
     ]);
   }
 
diff --git a/pkg/analyzer/test/src/diagnostics/subtype_of_ffi_class_test.dart b/pkg/analyzer/test/src/diagnostics/subtype_of_ffi_class_test.dart
index 6e19801..63d6735 100644
--- a/pkg/analyzer/test/src/diagnostics/subtype_of_ffi_class_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/subtype_of_ffi_class_test.dart
@@ -154,7 +154,18 @@
 import 'dart:ffi';
 class C implements Double {}
 ''', [
-      error(FfiCode.SUBTYPE_OF_FFI_CLASS_IN_IMPLEMENTS, 38, 6),
+      error(FfiCode.SUBTYPE_OF_FFI_CLASS_IN_IMPLEMENTS, 38, 6,
+          messageContains: ["class 'C'", "implement 'Double'"]),
+    ]);
+  }
+
+  test_Double_prefixed() async {
+    await assertErrorsInCode(r'''
+import 'dart:ffi' as ffi;
+class C implements ffi.Double {}
+''', [
+      error(FfiCode.SUBTYPE_OF_FFI_CLASS_IN_IMPLEMENTS, 45, 10,
+          messageContains: ["class 'C'", "implement 'ffi.Double'"]),
     ]);
   }
 
@@ -285,7 +296,20 @@
 ''', [
       error(CompileTimeErrorCode.MIXIN_CLASS_DECLARES_CONSTRUCTOR, 32, 6),
       error(CompileTimeErrorCode.MIXIN_INHERITS_FROM_NOT_OBJECT, 32, 6),
-      error(FfiCode.SUBTYPE_OF_FFI_CLASS_IN_WITH, 32, 6),
+      error(FfiCode.SUBTYPE_OF_FFI_CLASS_IN_WITH, 32, 6,
+          messageContains: ["class 'C'", "mix in 'Double'"]),
+    ]);
+  }
+
+  test_Double_prefixed() async {
+    await assertErrorsInCode(r'''
+import 'dart:ffi' as ffi;
+class C with ffi.Double {}
+''', [
+      error(CompileTimeErrorCode.MIXIN_CLASS_DECLARES_CONSTRUCTOR, 39, 10),
+      error(CompileTimeErrorCode.MIXIN_INHERITS_FROM_NOT_OBJECT, 39, 10),
+      error(FfiCode.SUBTYPE_OF_FFI_CLASS_IN_WITH, 39, 10,
+          messageContains: ["class 'C'", "mix in 'ffi.Double'"]),
     ]);
   }
 
diff --git a/pkg/analyzer/test/src/diagnostics/subtype_of_struct_class_test.dart b/pkg/analyzer/test/src/diagnostics/subtype_of_struct_class_test.dart
index 026be64..969c1be 100644
--- a/pkg/analyzer/test/src/diagnostics/subtype_of_struct_class_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/subtype_of_struct_class_test.dart
@@ -52,7 +52,22 @@
 class C implements S {}
 ''', [
       error(FfiCode.EMPTY_STRUCT, 25, 1),
-      error(FfiCode.SUBTYPE_OF_STRUCT_CLASS_IN_IMPLEMENTS, 64, 1),
+      error(FfiCode.SUBTYPE_OF_STRUCT_CLASS_IN_IMPLEMENTS, 64, 1,
+          messageContains: ["class 'C'", "implement 'S'"]),
+    ]);
+  }
+
+  test_implements_struct_prefixed() async {
+    newFile('$testPackageLibPath/lib1.dart', content: '''
+import 'dart:ffi';
+class S extends Struct {}
+''');
+    await assertErrorsInCode(r'''
+import 'lib1.dart' as lib1;
+class C implements lib1.S {}
+''', [
+      error(FfiCode.SUBTYPE_OF_STRUCT_CLASS_IN_IMPLEMENTS, 47, 6,
+          messageContains: ["class 'C'", "implement 'lib1.S'"]),
     ]);
   }
 
@@ -78,7 +93,24 @@
 ''', [
       error(FfiCode.EMPTY_STRUCT, 25, 1),
       error(CompileTimeErrorCode.MIXIN_INHERITS_FROM_NOT_OBJECT, 58, 1),
-      error(FfiCode.SUBTYPE_OF_STRUCT_CLASS_IN_WITH, 58, 1),
+      error(FfiCode.SUBTYPE_OF_STRUCT_CLASS_IN_WITH, 58, 1,
+          messageContains: ["class 'C'", "mix in 'S'"]),
+    ]);
+  }
+
+  test_with_struct_prefixed() async {
+    newFile('$testPackageLibPath/lib1.dart', content: '''
+import 'dart:ffi';
+class S extends Struct {}
+''');
+    await assertErrorsInCode(r'''
+import 'lib1.dart' as lib1;
+
+class C with lib1.S {}
+''', [
+      error(CompileTimeErrorCode.MIXIN_INHERITS_FROM_NOT_OBJECT, 42, 6),
+      error(FfiCode.SUBTYPE_OF_STRUCT_CLASS_IN_WITH, 42, 6,
+          messageContains: ["class 'C'", "mix in 'lib1.S'"]),
     ]);
   }
 
diff --git a/pkg/analyzer/test/src/diagnostics/type_annotation_deferred_class_test.dart b/pkg/analyzer/test/src/diagnostics/type_annotation_deferred_class_test.dart
index c622fb0..b432e45 100644
--- a/pkg/analyzer/test/src/diagnostics/type_annotation_deferred_class_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/type_annotation_deferred_class_test.dart
@@ -25,7 +25,8 @@
 class C<T> { const C(); }
 @C<a.D>() main () {}
 ''', [
-      error(CompileTimeErrorCode.TYPE_ANNOTATION_DEFERRED_CLASS, 77, 3),
+      error(CompileTimeErrorCode.TYPE_ANNOTATION_DEFERRED_CLASS, 77, 3,
+          messageContains: ["'a.D'"]),
     ]);
   }
 
diff --git a/pkg/analyzer/test/src/diagnostics/undefined_constructor_in_initializer_test.dart b/pkg/analyzer/test/src/diagnostics/undefined_constructor_in_initializer_test.dart
index b951089..daee2e5 100644
--- a/pkg/analyzer/test/src/diagnostics/undefined_constructor_in_initializer_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/undefined_constructor_in_initializer_test.dart
@@ -22,7 +22,8 @@
   B() : super.named();
 }
 ''', [
-      error(CompileTimeErrorCode.UNDEFINED_CONSTRUCTOR_IN_INITIALIZER, 39, 13),
+      error(CompileTimeErrorCode.UNDEFINED_CONSTRUCTOR_IN_INITIALIZER, 39, 13,
+          messageContains: ["class 'A'", "named 'named'"]),
     ]);
   }
 
diff --git a/pkg/analyzer/test/src/task/strong/checker_test.dart b/pkg/analyzer/test/src/task/strong/checker_test.dart
index a25a5ac..ce366390 100644
--- a/pkg/analyzer/test/src/task/strong/checker_test.dart
+++ b/pkg/analyzer/test/src/task/strong/checker_test.dart
@@ -1906,13 +1906,18 @@
 var fe0 = (int x) => x as dynamic;
 var fe1 = (int x) => x;
 ''', [
-      error(LanguageCode.IMPLICIT_DYNAMIC_RETURN, 12, 2),
-      error(LanguageCode.IMPLICIT_DYNAMIC_RETURN, 96, 2),
+      error(LanguageCode.IMPLICIT_DYNAMIC_RETURN, 12, 2,
+          messageContains: ["'f0'"]),
+      error(LanguageCode.IMPLICIT_DYNAMIC_RETURN, 96, 2,
+          messageContains: ["'g0'"]),
       error(HintCode.UNUSED_ELEMENT, 96, 2),
       error(HintCode.UNUSED_ELEMENT, 126, 2),
-      error(LanguageCode.IMPLICIT_DYNAMIC_RETURN, 212, 12),
-      error(LanguageCode.IMPLICIT_DYNAMIC_RETURN, 304, 2),
-      error(LanguageCode.IMPLICIT_DYNAMIC_RETURN, 373, 1),
+      error(LanguageCode.IMPLICIT_DYNAMIC_RETURN, 212, 12,
+          messageContains: ["'m0'"]),
+      error(LanguageCode.IMPLICIT_DYNAMIC_RETURN, 304, 2,
+          messageContains: ["'y0'"]),
+      error(LanguageCode.IMPLICIT_DYNAMIC_RETURN, 373, 1,
+          messageContains: ["'f'"]),
     ]);
   }