@@ -54,6 +54,7 @@ import 'package:analyzer/src/generated/parser.dart' show ParserErrorCode;
5454import 'package:analyzer/src/generated/this_access_tracker.dart' ;
5555import 'package:analyzer/src/summary2/macro_application_error.dart' ;
5656import 'package:analyzer/src/summary2/macro_type_location.dart' ;
57+ import 'package:analyzer/src/utilities/extensions/element.dart' ;
5758import 'package:analyzer/src/utilities/extensions/object.dart' ;
5859import 'package:analyzer/src/utilities/extensions/string.dart' ;
5960import 'package:collection/collection.dart' ;
@@ -6113,10 +6114,12 @@ class HiddenElements {
61136114/// Information to pass from from the defining unit to augmentations.
61146115class LibraryVerificationContext {
61156116 final duplicationDefinitionContext = DuplicationDefinitionContext ();
6117+ final LibraryFileKind libraryKind;
61166118 final ConstructorFieldsVerifier constructorFieldsVerifier;
61176119 final Map <FileState , UnitAnalysis > units;
61186120
61196121 LibraryVerificationContext ({
6122+ required this .libraryKind,
61206123 required this .constructorFieldsVerifier,
61216124 required this .units,
61226125 });
@@ -6148,6 +6151,10 @@ class LibraryVerificationContext {
61486151 entity: node,
61496152 );
61506153 }
6154+
6155+ bool libraryCycleContains (Uri uri) {
6156+ return libraryKind.libraryCycle.libraryUris.contains (uri);
6157+ }
61516158}
61526159
61536160class _MacroDiagnosticsReporter {
@@ -6162,6 +6169,8 @@ class _MacroDiagnosticsReporter {
61626169 });
61636170
61646171 void report () {
6172+ _reportApplicationFromSameLibraryCycle ();
6173+
61656174 for (final diagnostic in element.macroDiagnostics) {
61666175 switch (diagnostic) {
61676176 case ArgumentMacroDiagnostic ():
@@ -6182,7 +6191,7 @@ class _MacroDiagnosticsReporter {
61826191 final target = object.target;
61836192 switch (target) {
61846193 case ApplicationMacroDiagnosticTarget ():
6185- final node = _annotationNode ( element, target.annotationIndex);
6194+ final node = element. annotationAst ( target.annotationIndex);
61866195 return DiagnosticMessageImpl (
61876196 filePath: element.source! .fullName,
61886197 length: node.length,
@@ -6208,8 +6217,30 @@ class _MacroDiagnosticsReporter {
62086217 }
62096218 }
62106219
6220+ void _reportApplicationFromSameLibraryCycle () {
6221+ for (var annotation in element.metadata) {
6222+ var element = annotation.element;
6223+ if (element is ! ConstructorElementImpl ) continue ;
6224+
6225+ var macroElement = element.enclosingElement;
6226+ if (macroElement is ! ClassElementImpl ) continue ;
6227+ if (! macroElement.isMacro) continue ;
6228+
6229+ var macroUri = macroElement.library.source.uri;
6230+ if (! libraryContext.libraryCycleContains (macroUri)) continue ;
6231+
6232+ errorReporter.atNode (
6233+ _annotationNameIdentifier (annotation),
6234+ CompileTimeErrorCode .MACRO_DEFINITION_APPLICATION_SAME_LIBRARY_CYCLE ,
6235+ arguments: [
6236+ macroElement.name,
6237+ ],
6238+ );
6239+ }
6240+ }
6241+
62116242 void _reportArgument (ArgumentMacroDiagnostic diagnostic) {
6212- var annotation = _annotationNode ( element, diagnostic.annotationIndex);
6243+ var annotation = element. annotationAst ( diagnostic.annotationIndex);
62136244 var arguments = annotation.arguments! .arguments;
62146245 errorReporter.atNode (
62156246 arguments[diagnostic.argumentIndex],
@@ -6231,7 +6262,7 @@ class _MacroDiagnosticsReporter {
62316262 final target = diagnostic.message.target;
62326263 switch (target) {
62336264 case ApplicationMacroDiagnosticTarget ():
6234- var node = _annotationNode ( element, target.annotationIndex);
6265+ var node = element. annotationAst ( target.annotationIndex);
62356266 errorReporter.reportError (
62366267 AnalysisError .forValues (
62376268 source: element.source! ,
@@ -6262,10 +6293,7 @@ class _MacroDiagnosticsReporter {
62626293 if (location == null ) {
62636294 return ;
62646295 }
6265- var node = _annotationNode (
6266- target.element,
6267- target.annotationIndex,
6268- );
6296+ var node = target.element.annotationAst (target.annotationIndex);
62696297 location.unitAnalysis.errorReporter.reportError (
62706298 AnalysisError .forValues (
62716299 source: target.element.source! ,
@@ -6301,7 +6329,7 @@ class _MacroDiagnosticsReporter {
63016329
63026330 void _reportException (ExceptionMacroDiagnostic diagnostic) {
63036331 errorReporter.atNode (
6304- _annotationNode ( element, diagnostic.annotationIndex),
6332+ element. annotationAst ( diagnostic.annotationIndex),
63056333 CompileTimeErrorCode .MACRO_INTERNAL_EXCEPTION ,
63066334 arguments: [
63076335 diagnostic.message,
@@ -6343,24 +6371,30 @@ class _MacroDiagnosticsReporter {
63436371
63446372 void _reportInvalidTarget (InvalidMacroTargetDiagnostic diagnostic) {
63456373 errorReporter.atNode (
6346- _annotationNode ( element, diagnostic.annotationIndex),
6374+ element. annotationAst ( diagnostic.annotationIndex),
63476375 CompileTimeErrorCode .INVALID_MACRO_APPLICATION_TARGET ,
63486376 arguments: [
63496377 diagnostic.supportedKinds.commaSeparatedWithOr,
63506378 ],
63516379 );
63526380 }
63536381
6354- static AnnotationImpl _annotationNode (ElementImpl element, int index) {
6355- var annotation = element.metadata[index];
6356- return annotation.annotationAst;
6382+ static SimpleIdentifier _annotationNameIdentifier (
6383+ ElementAnnotationImpl annotation,
6384+ ) {
6385+ var fullName = annotation.annotationAst.name;
6386+ if (fullName is PrefixedIdentifierImpl ) {
6387+ return fullName.identifier;
6388+ } else {
6389+ return fullName as SimpleIdentifierImpl ;
6390+ }
63576391 }
63586392
63596393 static SimpleIdentifier _macroAnnotationNameIdentifier ({
63606394 required ElementImpl element,
63616395 required int annotationIndex,
63626396 }) {
6363- var annotationNode = _annotationNode ( element, annotationIndex);
6397+ var annotationNode = element. annotationAst ( annotationIndex);
63646398 var fullName = annotationNode.name;
63656399 if (fullName is PrefixedIdentifierImpl ) {
63666400 return fullName.identifier;
0 commit comments