[analyzer] Report error on the first unresolved identifier

When reporting the "The name 'X' isn't a class." error in instance creation expressions, the mentioned name was the last name even when the previous name was unresolved. This lead to the misleading message the "The name 'named' isn't a class." for `new Class.named()` when `Class` was *not* resolved.

This changes the messaging to handle the case where a prefix of the instance creation is not resolved and report "Undefined name 'X'." in such cases, since we don't know whether 'X' was meant to be a class or just a prefix.

Change-Id: I57ef1dfa229647424855b1f9f8d43e1890094563
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/448301
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
diff --git a/pkg/analyzer/lib/src/dart/resolver/named_type_resolver.dart b/pkg/analyzer/lib/src/dart/resolver/named_type_resolver.dart
index 92b49f8..4f9f94d 100644
--- a/pkg/analyzer/lib/src/dart/resolver/named_type_resolver.dart
+++ b/pkg/analyzer/lib/src/dart/resolver/named_type_resolver.dart
@@ -538,14 +538,32 @@
       var instanceCreation = constructorName.parent;
       if (instanceCreation is InstanceCreationExpression) {
         var errorRange = _getErrorRange(node, skipImportPrefix: true);
-        diagnosticReporter.atOffset(
-          offset: errorRange.offset,
-          length: errorRange.length,
-          diagnosticCode: instanceCreation.isConst
-              ? CompileTimeErrorCode.constWithNonType
-              : CompileTimeErrorCode.newWithNonType,
-          arguments: [node.name.lexeme],
-        );
+        var importPrefix = node.importPrefix;
+        if (importPrefix != null && importPrefix.element == null) {
+          // The constructor name is in two or three parts and the first part,
+          // which is either a prefix or a class name, is unresolved. In this
+          // case, report that the first name is undefined, instead of reporting
+          // that the last name is not a class.
+          // TODO(johnniwinther): We could report "Undefined prefix 'x'." when
+          // we know it can only be a prefix, for instance in `x.y.z()`.
+          String prefixOrClassName = importPrefix.name.lexeme;
+          diagnosticReporter.atOffset(
+            offset: errorRange.offset,
+            length: errorRange.length,
+            diagnosticCode: CompileTimeErrorCode.undefinedIdentifier,
+            arguments: [prefixOrClassName],
+          );
+        } else {
+          String className = node.name.lexeme;
+          diagnosticReporter.atOffset(
+            offset: errorRange.offset,
+            length: errorRange.length,
+            diagnosticCode: instanceCreation.isConst
+                ? CompileTimeErrorCode.constWithNonType
+                : CompileTimeErrorCode.newWithNonType,
+            arguments: [className],
+          );
+        }
         return true;
       }
     }
diff --git a/pkg/analyzer/test/src/dart/resolution/instance_creation_test.dart b/pkg/analyzer/test/src/dart/resolution/instance_creation_test.dart
index a5b3100..b9cf095 100644
--- a/pkg/analyzer/test/src/dart/resolution/instance_creation_test.dart
+++ b/pkg/analyzer/test/src/dart/resolution/instance_creation_test.dart
@@ -2540,7 +2540,7 @@
 }
 
 ''',
-      [error(CompileTimeErrorCode.newWithNonType, 17, 16)],
+      [error(CompileTimeErrorCode.undefinedIdentifier, 17, 16)],
     );
 
     var node = findNode.singleInstanceCreationExpression;
@@ -2577,7 +2577,7 @@
 }
 
 ''',
-      [error(CompileTimeErrorCode.newWithNonType, 17, 14)],
+      [error(CompileTimeErrorCode.undefinedIdentifier, 17, 14)],
     );
 
     var node = findNode.singleInstanceCreationExpression;
diff --git a/pkg/analyzer/test/src/diagnostics/creation_with_non_type_test.dart b/pkg/analyzer/test/src/diagnostics/creation_with_non_type_test.dart
new file mode 100644
index 0000000..5496e4d8
--- /dev/null
+++ b/pkg/analyzer/test/src/diagnostics/creation_with_non_type_test.dart
@@ -0,0 +1,1365 @@
+// Copyright (c) 2025, the Dart 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.
+
+import 'package:analyzer/src/error/codes.dart';
+import 'package:test_reflective_loader/test_reflective_loader.dart';
+
+import '../dart/resolution/context_collection_resolution.dart';
+
+main() {
+  defineReflectiveSuite(() {
+    defineReflectiveTests(CreationWithNonTypeTest);
+  });
+}
+
+@reflectiveTest
+class CreationWithNonTypeTest extends PubPackageResolutionTest {
+  test_const_nonPrefix_generic() async {
+    await assertErrorsInCode(
+      r'''
+void nonPrefix() {}
+f() {
+  const nonPrefix.Class<int>();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.prefixShadowedByLocalDeclaration,
+          34,
+          9,
+          messageContains: ["'nonPrefix'"],
+        ),
+      ],
+    );
+  }
+
+  test_const_nonPrefix_genericNamed() async {
+    await assertErrorsInCode(
+      r'''
+void nonPrefix() {}
+f() {
+  const nonPrefix.Class<int>.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.prefixShadowedByLocalDeclaration,
+          34,
+          9,
+          messageContains: ["'nonPrefix'"],
+        ),
+      ],
+    );
+  }
+
+  test_const_nonPrefix_named() async {
+    await assertErrorsInCode(
+      r'''
+void nonPrefix() {}
+f() {
+  const nonPrefix.Class.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.prefixShadowedByLocalDeclaration,
+          34,
+          9,
+          messageContains: ["'nonPrefix'"],
+        ),
+      ],
+    );
+  }
+
+  test_const_nonPrefix_unnamed() async {
+    await assertErrorsInCode(
+      r'''
+void nonPrefix() {}
+f() {
+  const nonPrefix.Class();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.prefixShadowedByLocalDeclaration,
+          34,
+          9,
+          messageContains: ["'nonPrefix"],
+        ),
+      ],
+    );
+  }
+
+  test_const_nonType_generic() async {
+    await assertErrorsInCode(
+      r'''
+void NonType<T>() {}
+f() {
+  const NonType<int>();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.constWithNonType,
+          35,
+          7,
+          text: "The name 'NonType' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_const_nonType_genericNamed() async {
+    await assertErrorsInCode(
+      r'''
+void NonType<T>() {}
+f() {
+  const NonType<int>.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.constWithNonType,
+          35,
+          7,
+          text: "The name 'NonType' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_const_nonType_named() async {
+    await assertErrorsInCode(
+      r'''
+void NonType() {}
+f() {
+  const NonType.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.prefixShadowedByLocalDeclaration,
+          32,
+          7,
+          messageContains: ["'NonType"],
+        ),
+      ],
+    );
+  }
+
+  test_const_nonType_prefixed() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+void NonType() {}
+f() {
+  const prefix.NonType();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.constWithNonType,
+          70,
+          7,
+          text: "The name 'NonType' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_const_nonType_prefixedGeneric() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+void NonType<T>() {}
+f() {
+  const prefix.NonType<int>();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.constWithNonType,
+          73,
+          7,
+          text: "The name 'NonType' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_const_nonType_prefixedGenericNamed() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+void NonType<T>() {}
+f() {
+  const prefix.NonType<int>.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.constWithNonType,
+          73,
+          7,
+          text: "The name 'NonType' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_const_nonType_prefixedNamed() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+void NonType() {}
+f() {
+  const prefix.NonType.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.constWithNonType,
+          70,
+          7,
+          text: "The name 'NonType' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_const_nonType_unnamed() async {
+    await assertErrorsInCode(
+      r'''
+void NonType() {}
+f() {
+  const NonType();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.constWithNonType,
+          32,
+          7,
+          text: "The name 'NonType' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_const_unresolvedClass_generic() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  const UnresolvedClass<int>();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.constWithNonType,
+          14,
+          15,
+          text: "The name 'UnresolvedClass' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_const_unresolvedClass_genericNamed() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  const UnresolvedClass<int>.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.constWithNonType,
+          14,
+          15,
+          text: "The name 'UnresolvedClass' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_const_unresolvedClass_named() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  const UnresolvedClass.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedIdentifier,
+          14,
+          21,
+          text: "Undefined name 'UnresolvedClass'.",
+        ),
+      ],
+    );
+  }
+
+  test_const_unresolvedClass_prefixed() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+f() {
+  const prefix.UnresolvedClass();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.constWithNonType,
+          52,
+          15,
+          text: "The name 'UnresolvedClass' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_const_unresolvedClass_prefixedGeneric() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+f() {
+  const prefix.UnresolvedClass<int>();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.constWithNonType,
+          52,
+          15,
+          text: "The name 'UnresolvedClass' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_const_unresolvedClass_prefixedGenericNamed() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+f() {
+  const prefix.UnresolvedClass<int>.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.constWithNonType,
+          52,
+          15,
+          text: "The name 'UnresolvedClass' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_const_unresolvedClass_prefixedNamed() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+f() {
+  const prefix.UnresolvedClass.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.constWithNonType,
+          52,
+          15,
+          text: "The name 'UnresolvedClass' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_const_unresolvedClass_unnamed() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  const UnresolvedClass();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.constWithNonType,
+          14,
+          15,
+          text: "The name 'UnresolvedClass' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_const_unresolvedPrefix_generic() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  const unresolved.Class<int>();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedIdentifier,
+          14,
+          16,
+          text: "Undefined name 'unresolved'.",
+        ),
+      ],
+    );
+  }
+
+  test_const_unresolvedPrefix_genericNamed() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  const unresolved.Class<int>.named();
+}
+''',
+      [
+        error(
+          // TODO(johnniwinther): This could be
+          //  "Undefined prefix 'unresolved'.".
+          CompileTimeErrorCode.undefinedIdentifier,
+          14,
+          16,
+          text: "Undefined name 'unresolved'.",
+        ),
+      ],
+    );
+  }
+
+  test_const_unresolvedPrefix_named() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  const unresolved.Class.named();
+}
+''',
+      [
+        error(
+          // TODO(johnniwinther): This could be
+          //  "Undefined prefix 'unresolved'.".
+          CompileTimeErrorCode.undefinedIdentifier,
+          14,
+          16,
+          text: "Undefined name 'unresolved'.",
+        ),
+      ],
+    );
+  }
+
+  test_const_unresolvedPrefix_unnamed() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  const unresolved.Class();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedIdentifier,
+          14,
+          16,
+          text: "Undefined name 'unresolved'.",
+        ),
+      ],
+    );
+  }
+
+  test_implicit_nonPrefix_generic() async {
+    await assertErrorsInCode(
+      r'''
+void nonPrefix() {}
+f() {
+  nonPrefix.Class<int>();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedMethod,
+          38,
+          5,
+          messageContains: ["'Class'"],
+        ),
+      ],
+    );
+  }
+
+  test_implicit_nonPrefix_genericNamed() async {
+    await assertErrorsInCode(
+      r'''
+void nonPrefix() {}
+f() {
+  nonPrefix.Class<int>.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedGetter,
+          38,
+          5,
+          messageContains: ["'Class'"],
+        ),
+      ],
+    );
+  }
+
+  test_implicit_nonPrefix_named() async {
+    await assertErrorsInCode(
+      r'''
+void nonPrefix() {}
+f() {
+  nonPrefix.Class.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedGetter,
+          38,
+          5,
+          messageContains: ["'Class'"],
+        ),
+      ],
+    );
+  }
+
+  test_implicit_nonPrefix_unnamed() async {
+    await assertErrorsInCode(
+      r'''
+void nonPrefix() {}
+f() {
+  nonPrefix.Class();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedMethod,
+          38,
+          5,
+          messageContains: ["'Class'"],
+        ),
+      ],
+    );
+  }
+
+  test_implicit_nonType_generic() async {
+    await assertNoErrorsInCode(r'''
+void NonType<T>() {}
+f() {
+  NonType<int>();
+}
+''');
+  }
+
+  test_implicit_nonType_genericNamed() async {
+    await assertErrorsInCode(
+      r'''
+void NonType<T>() {}
+f() {
+  NonType<int>.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedMethod,
+          42,
+          5,
+          messageContains: ["'named"],
+        ),
+      ],
+    );
+  }
+
+  test_implicit_nonType_named() async {
+    await assertErrorsInCode(
+      r'''
+void NonType() {}
+f() {
+  NonType.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedMethod,
+          34,
+          5,
+          messageContains: ["'named'"],
+        ),
+      ],
+    );
+  }
+
+  test_implicit_nonType_prefixed() async {
+    await assertNoErrorsInCode(r'''
+import 'test.dart' as prefix;
+
+void NonType() {}
+f() {
+  prefix.NonType();
+}
+''');
+  }
+
+  test_implicit_nonType_prefixedGeneric() async {
+    await assertNoErrorsInCode(r'''
+import 'test.dart' as prefix;
+
+void NonType<T>() {}
+f() {
+  prefix.NonType<int>();
+}
+''');
+  }
+
+  test_implicit_nonType_prefixedGenericNamed() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+void NonType<T>() {}
+f() {
+  prefix.NonType<int>.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedMethod,
+          80,
+          5,
+          messageContains: ["'named'"],
+        ),
+      ],
+    );
+  }
+
+  test_implicit_nonType_prefixedNamed() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+void NonType() {}
+f() {
+  prefix.NonType.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedMethod,
+          72,
+          5,
+          messageContains: ["'named'"],
+        ),
+      ],
+    );
+  }
+
+  test_implicit_nonType_unnamed() async {
+    await assertNoErrorsInCode(r'''
+void NonType() {}
+f() {
+  NonType();
+}
+''');
+  }
+
+  test_implicit_unresolvedClass_generic() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  UnresolvedClass<int>();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedFunction,
+          8,
+          15,
+          text: "The function 'UnresolvedClass' isn't defined.",
+        ),
+      ],
+    );
+  }
+
+  test_implicit_unresolvedClass_genericNamed() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  UnresolvedClass<int>.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.newWithNonType,
+          8,
+          15,
+          text: "The name 'UnresolvedClass' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_implicit_unresolvedClass_named() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  UnresolvedClass.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedIdentifier,
+          8,
+          15,
+          text: "Undefined name 'UnresolvedClass'.",
+        ),
+      ],
+    );
+  }
+
+  test_implicit_unresolvedClass_prefixed() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+f() {
+  prefix.UnresolvedClass();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedFunction,
+          46,
+          15,
+          text: "The function 'UnresolvedClass' isn't defined.",
+        ),
+      ],
+    );
+  }
+
+  test_implicit_unresolvedClass_prefixedGeneric() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+f() {
+  prefix.UnresolvedClass<int>();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedFunction,
+          46,
+          15,
+          text: "The function 'UnresolvedClass' isn't defined.",
+        ),
+      ],
+    );
+  }
+
+  test_implicit_unresolvedClass_prefixedGenericNamed() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+f() {
+  prefix.UnresolvedClass<int>.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.newWithNonType,
+          46,
+          15,
+          text: "The name 'UnresolvedClass' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_implicit_unresolvedClass_prefixedNamed() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+f() {
+  prefix.UnresolvedClass.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedPrefixedName,
+          46,
+          15,
+          messageContains: ["'UnresolvedClass'"],
+        ),
+      ],
+    );
+  }
+
+  test_implicit_unresolvedClass_unnamed() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  UnresolvedClass();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedFunction,
+          8,
+          15,
+          text: "The function 'UnresolvedClass' isn't defined.",
+        ),
+      ],
+    );
+  }
+
+  test_implicit_unresolvedPrefix_generic() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  unresolved.Class<int>();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedIdentifier,
+          8,
+          10,
+          text: "Undefined name 'unresolved'.",
+        ),
+      ],
+    );
+  }
+
+  test_implicit_unresolvedPrefix_genericNamed() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  unresolved.Class<int>.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedIdentifier,
+          8,
+          10,
+          text: "Undefined name 'unresolved.Class'.",
+        ),
+      ],
+    );
+  }
+
+  test_implicit_unresolvedPrefix_named() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  unresolved.Class.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedIdentifier,
+          8,
+          10,
+          text: "Undefined name 'unresolved'.",
+        ),
+      ],
+    );
+  }
+
+  test_implicit_unresolvedPrefix_unnamed() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  unresolved.Class();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedIdentifier,
+          8,
+          10,
+          text: "Undefined name 'unresolved'.",
+        ),
+      ],
+    );
+  }
+
+  test_new_nonPrefix_generic() async {
+    await assertErrorsInCode(
+      r'''
+void nonPrefix() {}
+f() {
+  new nonPrefix.Class<int>();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.prefixShadowedByLocalDeclaration,
+          32,
+          9,
+          messageContains: ["'nonPrefix'"],
+        ),
+      ],
+    );
+  }
+
+  test_new_nonPrefix_genericNamed() async {
+    await assertErrorsInCode(
+      r'''
+void nonPrefix() {}
+f() {
+  new nonPrefix.Class<int>.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.prefixShadowedByLocalDeclaration,
+          32,
+          9,
+          messageContains: ["'nonPrefix'"],
+        ),
+      ],
+    );
+  }
+
+  test_new_nonPrefix_named() async {
+    await assertErrorsInCode(
+      r'''
+void nonPrefix() {}
+f() {
+  new nonPrefix.Class.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.prefixShadowedByLocalDeclaration,
+          32,
+          9,
+          messageContains: ["'nonPrefix'"],
+        ),
+      ],
+    );
+  }
+
+  test_new_nonPrefix_unnamed() async {
+    await assertErrorsInCode(
+      r'''
+void nonPrefix() {}
+f() {
+  new nonPrefix.Class();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.prefixShadowedByLocalDeclaration,
+          32,
+          9,
+          messageContains: ["nonPrefix"],
+        ),
+      ],
+    );
+  }
+
+  test_new_nonType_generic() async {
+    await assertErrorsInCode(
+      r'''
+void nonPrefix() {}
+f() {
+  new NonType<int>();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.newWithNonType,
+          32,
+          7,
+          text: "The name 'NonType' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_new_nonType_genericNamed() async {
+    await assertErrorsInCode(
+      r'''
+void NonType<T>() {}
+f() {
+  new NonType<int>.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.newWithNonType,
+          33,
+          7,
+          text: "The name 'NonType' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_new_nonType_named() async {
+    await assertErrorsInCode(
+      r'''
+void NonType() {}
+f() {
+  new NonType.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.prefixShadowedByLocalDeclaration,
+          30,
+          7,
+          messageContains: ["'NonType'"],
+        ),
+      ],
+    );
+  }
+
+  test_new_nonType_prefixed() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+void NonType() {}
+f() {
+  new prefix.NonType();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.newWithNonType,
+          68,
+          7,
+          text: "The name 'NonType' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_new_nonType_prefixedGeneric() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+void NonType<T>() {}
+f() {
+  new prefix.NonType<int>();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.newWithNonType,
+          71,
+          7,
+          text: "The name 'NonType' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_new_nonType_prefixedGenericNamed() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+void NonType<T>() {}
+f() {
+  new prefix.NonType<int>.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.newWithNonType,
+          71,
+          7,
+          text: "The name 'NonType' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_new_nonType_prefixedNamed() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+void NonType() {}
+f() {
+  new prefix.NonType.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.newWithNonType,
+          68,
+          7,
+          text: "The name 'NonType' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_new_nonType_unnamed() async {
+    await assertErrorsInCode(
+      r'''
+void NonType() {}
+f() {
+  new NonType();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.newWithNonType,
+          30,
+          7,
+          text: "The name 'NonType' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_new_unresolvedClass_generic() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  new UnresolvedClass<int>();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.newWithNonType,
+          12,
+          15,
+          text: "The name 'UnresolvedClass' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_new_unresolvedClass_genericNamed() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  new UnresolvedClass<int>.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.newWithNonType,
+          12,
+          15,
+          text: "The name 'UnresolvedClass' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_new_unresolvedClass_named() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  new UnresolvedClass.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedIdentifier,
+          12,
+          21,
+          text: "Undefined name 'UnresolvedClass'.",
+        ),
+      ],
+    );
+  }
+
+  test_new_unresolvedClass_prefixed() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+f() {
+  new prefix.UnresolvedClass();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.newWithNonType,
+          50,
+          15,
+          text: "The name 'UnresolvedClass' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_new_unresolvedClass_prefixedGeneric() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+f() {
+  new prefix.UnresolvedClass<int>();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.newWithNonType,
+          50,
+          15,
+          text: "The name 'UnresolvedClass' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_new_unresolvedClass_prefixedGenericNamed() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+f() {
+  new prefix.UnresolvedClass<int>.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.newWithNonType,
+          50,
+          15,
+          text: "The name 'UnresolvedClass' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_new_unresolvedClass_prefixedNamed() async {
+    await assertErrorsInCode(
+      r'''
+import 'test.dart' as prefix;
+
+f() {
+  new prefix.UnresolvedClass.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.newWithNonType,
+          50,
+          15,
+          text: "The name 'UnresolvedClass' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_new_unresolvedClass_unnamed() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  new UnresolvedClass();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.newWithNonType,
+          12,
+          15,
+          text: "The name 'UnresolvedClass' isn't a class.",
+        ),
+      ],
+    );
+  }
+
+  test_new_unresolvedPrefix_generic() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  new unresolved.Class<int>();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedIdentifier,
+          12,
+          16,
+          text: "Undefined name 'unresolved'.",
+        ),
+      ],
+    );
+  }
+
+  test_new_unresolvedPrefix_genericNamed() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  new unresolved.Class<int>.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedIdentifier,
+          12,
+          16,
+          // TODO(johnniwinther): This could be
+          //  "Undefined prefix 'unresolved'.".
+          text: "Undefined name 'unresolved'.",
+        ),
+      ],
+    );
+  }
+
+  test_new_unresolvedPrefix_named() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  new unresolved.Class.named();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedIdentifier,
+          12,
+          16,
+          // TODO(johnniwinther): This could be
+          //  "Undefined prefix 'unresolved'.".
+          text: "Undefined name 'unresolved'.",
+        ),
+      ],
+    );
+  }
+
+  test_new_unresolvedPrefix_unnamed() async {
+    await assertErrorsInCode(
+      r'''
+f() {
+  new unresolved.Class();
+}
+''',
+      [
+        error(
+          CompileTimeErrorCode.undefinedIdentifier,
+          12,
+          16,
+          text: "Undefined name 'unresolved'.",
+        ),
+      ],
+    );
+  }
+}
diff --git a/pkg/analyzer/test/src/diagnostics/test_all.dart b/pkg/analyzer/test/src/diagnostics/test_all.dart
index 93278c4..9f78786 100644
--- a/pkg/analyzer/test/src/diagnostics/test_all.dart
+++ b/pkg/analyzer/test/src/diagnostics/test_all.dart
@@ -166,6 +166,7 @@
 import 'continue_label_invalid_test.dart' as continue_label_invalid;
 import 'could_not_infer_test.dart' as could_not_infer;
 import 'creation_of_struct_or_union_test.dart' as creation_of_struct_or_union;
+import 'creation_with_non_type_test.dart' as creation_with_non_type;
 import 'dead_code_test.dart' as dead_code;
 import 'dead_null_aware_expression_test.dart' as dead_null_aware_expression;
 import 'default_value_in_function_type_test.dart'
@@ -1063,6 +1064,7 @@
     continue_label_invalid.main();
     could_not_infer.main();
     creation_of_struct_or_union.main();
+    creation_with_non_type.main();
     dead_code.main();
     dead_null_aware_expression.main();
     default_value_in_function_type.main();
diff --git a/tests/language/call/constructor_on_unresolvable_class_error_test.dart b/tests/language/call/constructor_on_unresolvable_class_error_test.dart
index 6b9012b..32d0156 100644
--- a/tests/language/call/constructor_on_unresolvable_class_error_test.dart
+++ b/tests/language/call/constructor_on_unresolvable_class_error_test.dart
@@ -15,10 +15,10 @@
   // [cfe] Couldn't find constructor 'A'.
   new A.foo();
   //  ^^^^^
-  // [analyzer] COMPILE_TIME_ERROR.CREATION_WITH_NON_TYPE
+  // [analyzer] COMPILE_TIME_ERROR.UNDEFINED_IDENTIFIER
   // [cfe] Couldn't find constructor 'A.foo'.
   new lib.A();
   //  ^^^^^
-  // [analyzer] COMPILE_TIME_ERROR.CREATION_WITH_NON_TYPE
+  // [analyzer] COMPILE_TIME_ERROR.UNDEFINED_IDENTIFIER
   // [cfe] Couldn't find constructor 'lib.A'.
 }
diff --git a/tests/language/malformed/malformed_test.dart b/tests/language/malformed/malformed_test.dart
index 4eea589..59d50ac 100644
--- a/tests/language/malformed/malformed_test.dart
+++ b/tests/language/malformed/malformed_test.dart
@@ -105,11 +105,11 @@
 
   new undeclared_prefix.Unresolved();
   //  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-  // [analyzer] COMPILE_TIME_ERROR.CREATION_WITH_NON_TYPE
+  // [analyzer] COMPILE_TIME_ERROR.UNDEFINED_IDENTIFIER
   // [cfe] Couldn't find constructor 'undeclared_prefix.Unresolved'.
   new undeclared_prefix.Unresolved<int>();
   //  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-  // [analyzer] COMPILE_TIME_ERROR.CREATION_WITH_NON_TYPE
+  // [analyzer] COMPILE_TIME_ERROR.UNDEFINED_IDENTIFIER
   // [cfe] Couldn't find constructor 'undeclared_prefix.Unresolved'.
 
   try {
diff --git a/tests/language/prefix/transitive_import_prefix_test.dart b/tests/language/prefix/transitive_import_prefix_test.dart
index ea9ce16..f2bf89f 100644
--- a/tests/language/prefix/transitive_import_prefix_test.dart
+++ b/tests/language/prefix/transitive_import_prefix_test.dart
@@ -7,7 +7,7 @@
   // Library prefixes in the imported libraries should not be visible here.
   new lib11.Library11(1);
   //  ^^^^^^^^^^^^^^^
-  // [analyzer] COMPILE_TIME_ERROR.CREATION_WITH_NON_TYPE
+  // [analyzer] COMPILE_TIME_ERROR.UNDEFINED_IDENTIFIER
   // [cfe] Couldn't find constructor 'lib11.Library11'.
   lib11.Library11.static_func();
   // [error column 3, length 5]