Macro. Report INVALID_MACRO_APPLICATION_TARGET.

Change-Id: I7597d81cb2c9db9488533d6d372b244b74ff97ba
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/350691
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 1e4aa46..537fa31 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
@@ -969,6 +969,8 @@
   status: needsFix
   notes: |-
     Replace with the equivalent generic function type.
+CompileTimeErrorCode.INVALID_MACRO_APPLICATION_TARGET:
+  status: noFix
 CompileTimeErrorCode.INVALID_MODIFIER_ON_CONSTRUCTOR:
   status: needsFix
   notes: |-
diff --git a/pkg/analyzer/lib/src/dart/analysis/driver.dart b/pkg/analyzer/lib/src/dart/analysis/driver.dart
index 381b57c..55a6677 100644
--- a/pkg/analyzer/lib/src/dart/analysis/driver.dart
+++ b/pkg/analyzer/lib/src/dart/analysis/driver.dart
@@ -95,7 +95,7 @@
 // TODO(scheglov): Clean up the list of implicitly analyzed files.
 class AnalysisDriver {
   /// The version of data format, should be incremented on every format change.
-  static const int DATA_VERSION = 342;
+  static const int DATA_VERSION = 344;
 
   /// The number of exception contexts allowed to write. Once this field is
   /// zero, we stop writing any new exception contexts in this process.
diff --git a/pkg/analyzer/lib/src/error/codes.g.dart b/pkg/analyzer/lib/src/error/codes.g.dart
index 23a9bf4..4b9f26e 100644
--- a/pkg/analyzer/lib/src/error/codes.g.dart
+++ b/pkg/analyzer/lib/src/error/codes.g.dart
@@ -2808,6 +2808,14 @@
   );
 
   ///  Parameters:
+  ///  0: the list of target kind names
+  static const CompileTimeErrorCode INVALID_MACRO_APPLICATION_TARGET =
+      CompileTimeErrorCode(
+    'INVALID_MACRO_APPLICATION_TARGET',
+    "The macro can be applied only to a {0}.",
+  );
+
+  ///  Parameters:
   ///  0: the invalid modifier
   static const CompileTimeErrorCode INVALID_MODIFIER_ON_CONSTRUCTOR =
       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 919522a..aa2b6d9 100644
--- a/pkg/analyzer/lib/src/error/error_code_values.g.dart
+++ b/pkg/analyzer/lib/src/error/error_code_values.g.dart
@@ -301,6 +301,7 @@
   CompileTimeErrorCode.INVALID_IMPLEMENTATION_OVERRIDE,
   CompileTimeErrorCode.INVALID_IMPLEMENTATION_OVERRIDE_SETTER,
   CompileTimeErrorCode.INVALID_INLINE_FUNCTION_TYPE,
+  CompileTimeErrorCode.INVALID_MACRO_APPLICATION_TARGET,
   CompileTimeErrorCode.INVALID_MODIFIER_ON_CONSTRUCTOR,
   CompileTimeErrorCode.INVALID_MODIFIER_ON_SETTER,
   CompileTimeErrorCode.INVALID_OVERRIDE,
diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart
index 9d3af70..7f2e02a 100644
--- a/pkg/analyzer/lib/src/generated/error_verifier.dart
+++ b/pkg/analyzer/lib/src/generated/error_verifier.dart
@@ -6143,6 +6143,14 @@
               diagnostic.stackTrace,
             ],
           );
+        case InvalidMacroTargetDiagnostic():
+          errorReporter.atNode(
+            metadata[diagnostic.annotationIndex],
+            CompileTimeErrorCode.INVALID_MACRO_APPLICATION_TARGET,
+            arguments: [
+              diagnostic.supportedKinds.commaSeparatedWithOr,
+            ],
+          );
         case MacroDiagnostic():
           final errorCode = switch (diagnostic.severity) {
             macro.Severity.info => HintCode.MACRO_INFO,
@@ -6191,9 +6199,6 @@
               // TODO(scheglov): Handle this case.
               throw UnimplementedError();
           }
-        case InvalidMacroTargetDiagnostic():
-          // TODO(scheglov): Handle this case.
-          throw UnimplementedError();
       }
     }
   }
diff --git a/pkg/analyzer/lib/src/summary2/bundle_reader.dart b/pkg/analyzer/lib/src/summary2/bundle_reader.dart
index 605f742..c6d4f9d 100644
--- a/pkg/analyzer/lib/src/summary2/bundle_reader.dart
+++ b/pkg/analyzer/lib/src/summary2/bundle_reader.dart
@@ -2358,6 +2358,7 @@
         );
       case MacroDiagnosticKind.invalidTarget:
         return InvalidMacroTargetDiagnostic(
+          annotationIndex: readUInt30(),
           supportedKinds: _reader.readStringUtf8List(),
         );
       case MacroDiagnosticKind.macro:
diff --git a/pkg/analyzer/lib/src/summary2/bundle_writer.dart b/pkg/analyzer/lib/src/summary2/bundle_writer.dart
index 1ccf831..a212de7 100644
--- a/pkg/analyzer/lib/src/summary2/bundle_writer.dart
+++ b/pkg/analyzer/lib/src/summary2/bundle_writer.dart
@@ -962,6 +962,7 @@
         writeStringUtf8(diagnostic.stackTrace);
       case InvalidMacroTargetDiagnostic():
         writeEnum(MacroDiagnosticKind.invalidTarget);
+        writeUInt30(diagnostic.annotationIndex);
         writeStringUtf8Iterable(diagnostic.supportedKinds);
       case MacroDiagnostic():
         writeEnum(MacroDiagnosticKind.macro);
diff --git a/pkg/analyzer/lib/src/summary2/macro_application.dart b/pkg/analyzer/lib/src/summary2/macro_application.dart
index f6e067a..2aae5ff 100644
--- a/pkg/analyzer/lib/src/summary2/macro_application.dart
+++ b/pkg/analyzer/lib/src/summary2/macro_application.dart
@@ -486,6 +486,7 @@
       if (!instance.supportsDeclarationKind(targetDeclarationKind)) {
         macroTarget.element.addMacroDiagnostic(
           InvalidMacroTargetDiagnostic(
+            annotationIndex: annotationIndex,
             supportedKinds: macro.DeclarationKind.values
                 .where(instance.supportsDeclarationKind)
                 .map((e) => e.name)
diff --git a/pkg/analyzer/lib/src/summary2/macro_application_error.dart b/pkg/analyzer/lib/src/summary2/macro_application_error.dart
index 33bd6f3..26b9ca6 100644
--- a/pkg/analyzer/lib/src/summary2/macro_application_error.dart
+++ b/pkg/analyzer/lib/src/summary2/macro_application_error.dart
@@ -87,9 +87,11 @@
 }
 
 final class InvalidMacroTargetDiagnostic extends AnalyzerMacroDiagnostic {
+  final int annotationIndex;
   final List<String> supportedKinds;
 
   InvalidMacroTargetDiagnostic({
+    required this.annotationIndex,
     required this.supportedKinds,
   });
 }
diff --git a/pkg/analyzer/messages.yaml b/pkg/analyzer/messages.yaml
index 0d8dffb..e4c4c7e 100644
--- a/pkg/analyzer/messages.yaml
+++ b/pkg/analyzer/messages.yaml
@@ -8659,6 +8659,11 @@
       ```dart
       typedef F = int Function(int Function(String));
       ```
+  INVALID_MACRO_APPLICATION_TARGET:
+    problemMessage: "The macro can be applied only to a {0}."
+    comment: |-
+      Parameters:
+      0: the list of target kind names
   INVALID_MODIFIER_ON_CONSTRUCTOR:
     problemMessage: "The modifier '{0}' can't be applied to the body of a constructor."
     correctionMessage: Try removing the modifier.
diff --git a/pkg/analyzer/test/src/dart/resolution/macro_test.dart b/pkg/analyzer/test/src/dart/resolution/macro_test.dart
index ea63d7b..5b6a9b5 100644
--- a/pkg/analyzer/test/src/dart/resolution/macro_test.dart
+++ b/pkg/analyzer/test/src/dart/resolution/macro_test.dart
@@ -279,6 +279,17 @@
     ]);
   }
 
+  test_diagnostic_invalidTarget_wantsClassOrMixin_hasFunction() async {
+    await assertErrorsInCode('''
+import 'diagnostic.dart';
+
+@TargetClassOrMixinMacro()
+void f() {}
+''', [
+      error(CompileTimeErrorCode.INVALID_MACRO_APPLICATION_TARGET, 27, 26),
+    ]);
+  }
+
   test_diagnostic_notSupportedArgument() async {
     await assertErrorsInCode('''
 import 'diagnostic.dart';
diff --git a/pkg/analyzer/test/src/summary/element_text.dart b/pkg/analyzer/test/src/summary/element_text.dart
index d02ce82..007eabd 100644
--- a/pkg/analyzer/test/src/summary/element_text.dart
+++ b/pkg/analyzer/test/src/summary/element_text.dart
@@ -904,6 +904,9 @@
             case InvalidMacroTargetDiagnostic():
               _sink.writelnWithIndent('InvalidMacroTargetDiagnostic');
               _sink.withIndent(() {
+                _sink.writelnWithIndent(
+                  'annotationIndex: ${diagnostic.annotationIndex}',
+                );
                 _sink.writeElements(
                   'supportedKinds',
                   diagnostic.supportedKinds,
diff --git a/pkg/analyzer/test/src/summary/macro_test.dart b/pkg/analyzer/test/src/summary/macro_test.dart
index a954c57..4a73b88 100644
--- a/pkg/analyzer/test/src/summary/macro_test.dart
+++ b/pkg/analyzer/test/src/summary/macro_test.dart
@@ -3560,6 +3560,7 @@
         returnType: void
         macroDiagnostics
           InvalidMacroTargetDiagnostic
+            annotationIndex: 0
             supportedKinds
               classType
               mixinType