Macro. Report MACRO_NOT_ALLOWED_DECLARATION.

Change-Id: I806d5ebc93c585a5b9597eb5a815692c300400f5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/366020
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
index fdf0cac..f77a9e3 100644
--- a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
+++ b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
@@ -1060,6 +1060,8 @@
   status: noFix
 CompileTimeErrorCode.MACRO_INTERNAL_EXCEPTION:
   status: noFix
+CompileTimeErrorCode.MACRO_NOT_ALLOWED_DECLARATION:
+  status: noFix
 CompileTimeErrorCode.MAIN_FIRST_POSITIONAL_PARAMETER_TYPE:
   status: needsFix
   notes: |-
diff --git a/pkg/analyzer/lib/src/error/codes.g.dart b/pkg/analyzer/lib/src/error/codes.g.dart
index c70826f..fe359fc 100644
--- a/pkg/analyzer/lib/src/error/codes.g.dart
+++ b/pkg/analyzer/lib/src/error/codes.g.dart
@@ -3162,6 +3162,18 @@
     "{0} {1}",
   );
 
+  ///  Parameters:
+  ///  0: the macro phase
+  ///  1: the list of ranges in the code
+  ///  2: the generated code
+  static const CompileTimeErrorCode MACRO_NOT_ALLOWED_DECLARATION =
+      CompileTimeErrorCode(
+    'MACRO_NOT_ALLOWED_DECLARATION',
+    "The macro attempted to add declaration(s) not allowed during the {0} "
+        "phase.\nLocations: {1}\n---\n{2}\n---",
+    correctionMessage: "Try adding these declaration during an earlier phase.",
+  );
+
   ///  No parameters.
   static const CompileTimeErrorCode MAIN_FIRST_POSITIONAL_PARAMETER_TYPE =
       CompileTimeErrorCode(
diff --git a/pkg/analyzer/lib/src/error/error_code_values.g.dart b/pkg/analyzer/lib/src/error/error_code_values.g.dart
index 86f63e6..3759776 100644
--- a/pkg/analyzer/lib/src/error/error_code_values.g.dart
+++ b/pkg/analyzer/lib/src/error/error_code_values.g.dart
@@ -337,6 +337,7 @@
   CompileTimeErrorCode.MACRO_DEFINITION_APPLICATION_SAME_LIBRARY_CYCLE,
   CompileTimeErrorCode.MACRO_ERROR,
   CompileTimeErrorCode.MACRO_INTERNAL_EXCEPTION,
+  CompileTimeErrorCode.MACRO_NOT_ALLOWED_DECLARATION,
   CompileTimeErrorCode.MAIN_FIRST_POSITIONAL_PARAMETER_TYPE,
   CompileTimeErrorCode.MAIN_HAS_REQUIRED_NAMED_PARAMETERS,
   CompileTimeErrorCode.MAIN_HAS_TOO_MANY_REQUIRED_POSITIONAL_PARAMETERS,
diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart
index 94995c6..203d6fb 100644
--- a/pkg/analyzer/lib/src/generated/error_verifier.dart
+++ b/pkg/analyzer/lib/src/generated/error_verifier.dart
@@ -6645,8 +6645,17 @@
   void _reportNotAllowedDeclaration(
     NotAllowedDeclarationDiagnostic diagnostic,
   ) {
-    // TODO(scheglov): implement
-    throw UnimplementedError();
+    errorReporter.atNode(
+      element.annotationAst(diagnostic.annotationIndex),
+      CompileTimeErrorCode.MACRO_NOT_ALLOWED_DECLARATION,
+      arguments: [
+        diagnostic.phase.name,
+        diagnostic.nodeRanges
+            .map((r) => '(${r.offset}, ${r.length})')
+            .join(' '),
+        diagnostic.code.trimRight(),
+      ],
+    );
   }
 
   static SimpleIdentifier _annotationNameIdentifier(
diff --git a/pkg/analyzer/messages.yaml b/pkg/analyzer/messages.yaml
index d92552d..6a333625 100644
--- a/pkg/analyzer/messages.yaml
+++ b/pkg/analyzer/messages.yaml
@@ -9675,6 +9675,20 @@
       Parameters:
       0: the message
       1: the stack trace
+  MACRO_NOT_ALLOWED_DECLARATION:
+    problemMessage: |-
+      The macro attempted to add declaration(s) not allowed during the {0} phase.
+      Locations: {1}
+      ---
+      {2}
+      ---
+    correctionMessage: Try adding these declaration during an earlier phase.
+    hasPublishedDocs: false
+    comment: |-
+      Parameters:
+      0: the macro phase
+      1: the list of ranges in the code
+      2: the generated code
   MACRO_DECLARATIONS_PHASE_INTROSPECTION_CYCLE:
     problemMessage: "The declaration '{0}' can't be introspected because there is a cycle of macro applications."
     correctionMessage: Try removing one or more macro applications to break the cycle.
diff --git a/pkg/analyzer/test/src/dart/resolution/macro_test.dart b/pkg/analyzer/test/src/dart/resolution/macro_test.dart
index afbe03d..81f33e1 100644
--- a/pkg/analyzer/test/src/dart/resolution/macro_test.dart
+++ b/pkg/analyzer/test/src/dart/resolution/macro_test.dart
@@ -317,6 +317,19 @@
     ]);
   }
 
+  test_diagnostic_notAllowedDeclaration_declarations_class() async {
+    await assertErrorsInCode('''
+import 'append.dart';
+
+class A {
+  @DeclareInLibrary('class B {}')
+  void foo() {}
+}
+''', [
+      error(CompileTimeErrorCode.MACRO_NOT_ALLOWED_DECLARATION, 35, 31),
+    ]);
+  }
+
   test_diagnostic_notSupportedArgument() async {
     await assertErrorsInCode('''
 import 'diagnostic.dart';