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>
This commit is contained in:
Konstantin Shcheglov 2024-02-09 21:54:16 +00:00 committed by Commit Queue
parent 6547ab18b1
commit e7f79cad7c
13 changed files with 45 additions and 4 deletions

View file

@ -969,6 +969,8 @@ CompileTimeErrorCode.INVALID_INLINE_FUNCTION_TYPE:
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: |-

View file

@ -95,7 +95,7 @@ import 'package:meta/meta.dart';
// 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.

View file

@ -2807,6 +2807,14 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
hasPublishedDocs: true,
);
/// 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 =

View file

@ -301,6 +301,7 @@ const List<ErrorCode> errorCodeValues = [
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,

View file

@ -6143,6 +6143,14 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
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 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
// TODO(scheglov): Handle this case.
throw UnimplementedError();
}
case InvalidMacroTargetDiagnostic():
// TODO(scheglov): Handle this case.
throw UnimplementedError();
}
}
}

View file

@ -2358,6 +2358,7 @@ class ResolutionReader {
);
case MacroDiagnosticKind.invalidTarget:
return InvalidMacroTargetDiagnostic(
annotationIndex: readUInt30(),
supportedKinds: _reader.readStringUtf8List(),
);
case MacroDiagnosticKind.macro:

View file

@ -962,6 +962,7 @@ class ResolutionSink extends _SummaryDataWriter {
writeStringUtf8(diagnostic.stackTrace);
case InvalidMacroTargetDiagnostic():
writeEnum(MacroDiagnosticKind.invalidTarget);
writeUInt30(diagnostic.annotationIndex);
writeStringUtf8Iterable(diagnostic.supportedKinds);
case MacroDiagnostic():
writeEnum(MacroDiagnosticKind.macro);

View file

@ -486,6 +486,7 @@ class LibraryMacroApplier {
if (!instance.supportsDeclarationKind(targetDeclarationKind)) {
macroTarget.element.addMacroDiagnostic(
InvalidMacroTargetDiagnostic(
annotationIndex: annotationIndex,
supportedKinds: macro.DeclarationKind.values
.where(instance.supportsDeclarationKind)
.map((e) => e.name)

View file

@ -87,9 +87,11 @@ final class ExceptionMacroDiagnostic extends AnalyzerMacroDiagnostic {
}
final class InvalidMacroTargetDiagnostic extends AnalyzerMacroDiagnostic {
final int annotationIndex;
final List<String> supportedKinds;
InvalidMacroTargetDiagnostic({
required this.annotationIndex,
required this.supportedKinds,
});
}

View file

@ -8659,6 +8659,11 @@ CompileTimeErrorCode:
```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.

View file

@ -279,6 +279,17 @@ class A3 {}
]);
}
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';

View file

@ -904,6 +904,9 @@ class _ElementWriter {
case InvalidMacroTargetDiagnostic():
_sink.writelnWithIndent('InvalidMacroTargetDiagnostic');
_sink.withIndent(() {
_sink.writelnWithIndent(
'annotationIndex: ${diagnostic.annotationIndex}',
);
_sink.writeElements(
'supportedKinds',
diagnostic.supportedKinds,

View file

@ -3560,6 +3560,7 @@ library
returnType: void
macroDiagnostics
InvalidMacroTargetDiagnostic
annotationIndex: 0
supportedKinds
classType
mixinType