Support for offsets in metadata on library, part, type alias.

AFAIK this completes the support for offsets.

Change-Id: If4e8c683b50d004cd79fe2d5c3eee4bc14a05dbe
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/203288
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/src/summary2/bundle_reader.dart b/pkg/analyzer/lib/src/summary2/bundle_reader.dart
index cfc9977..2346e51 100644
--- a/pkg/analyzer/lib/src/summary2/bundle_reader.dart
+++ b/pkg/analyzer/lib/src/summary2/bundle_reader.dart
@@ -124,6 +124,8 @@
 
 class CompilationUnitElementLinkedData
     extends ElementLinkedData<CompilationUnitElementImpl> {
+  ApplyConstantOffsets? applyConstantOffsets;
+
   CompilationUnitElementLinkedData({
     required Reference reference,
     required LibraryReader libraryReader,
@@ -136,6 +138,7 @@
     element.metadata = reader._readAnnotationList(
       unitElement: unitElement,
     );
+    applyConstantOffsets?.perform();
   }
 }
 
@@ -368,6 +371,8 @@
 }
 
 class LibraryElementLinkedData extends ElementLinkedData<LibraryElementImpl> {
+  ApplyConstantOffsets? applyConstantOffsets;
+
   LibraryElementLinkedData({
     required Reference reference,
     required LibraryReader libraryReader,
@@ -402,6 +407,8 @@
     }
 
     element.entryPoint = reader.readElement() as FunctionElement?;
+
+    applyConstantOffsets?.perform();
   }
 }
 
@@ -1762,6 +1769,8 @@
 
 class TypeAliasElementLinkedData
     extends ElementLinkedData<TypeAliasElementImpl> {
+  ApplyConstantOffsets? applyConstantOffsets;
+
   TypeAliasElementLinkedData({
     required Reference reference,
     required LibraryReader libraryReader,
@@ -1777,6 +1786,7 @@
     _readTypeParameters(reader, element.typeParameters);
     element.aliasedElement = reader._readAliasedElement(unitElement);
     element.aliasedType = reader.readRequiredType();
+    applyConstantOffsets?.perform();
   }
 }
 
diff --git a/pkg/analyzer/lib/src/summary2/informative_data.dart b/pkg/analyzer/lib/src/summary2/informative_data.dart
index 9020587..ba09c74 100644
--- a/pkg/analyzer/lib/src/summary2/informative_data.dart
+++ b/pkg/analyzer/lib/src/summary2/informative_data.dart
@@ -413,6 +413,11 @@
       element.typeParameters_unresolved,
       info.typeParameters,
     );
+
+    _setupApplyConstantOffsetsForTypeAlias(
+      element,
+      info.constantOffsets,
+    );
   }
 
   void _applyToGenericTypeAlias(
@@ -427,6 +432,11 @@
       element.typeParameters_unresolved,
       info.typeParameters,
     );
+
+    _setupApplyConstantOffsetsForTypeAlias(
+      element,
+      info.constantOffsets,
+    );
   }
 
   void _applyToLibrary(LibraryElementImpl element, _InfoUnit info) {
@@ -462,6 +472,29 @@
         _applyToCombinators(element.combinators, info.combinators);
       },
     );
+
+    forCorrespondingPairs<CompilationUnitElement, _InfoPart>(
+      element.parts,
+      info.parts,
+      (element, info) {
+        element as CompilationUnitElementImpl;
+        var linkedData = element.linkedData as CompilationUnitElementLinkedData;
+        linkedData.applyConstantOffsets = ApplyConstantOffsets(
+          info.constantOffsets,
+          (applier) {
+            applier.applyToMetadata(element);
+          },
+        );
+      },
+    );
+
+    var linkedData = element.linkedData as LibraryElementLinkedData;
+    linkedData.applyConstantOffsets = ApplyConstantOffsets(
+      info.libraryConstantOffsets,
+      (applier) {
+        applier.applyToMetadata(element);
+      },
+    );
   }
 
   void _applyToMethods(
@@ -562,6 +595,26 @@
       },
     );
   }
+
+  void _setupApplyConstantOffsetsForTypeAlias(
+    TypeAliasElementImpl element,
+    Uint32List constantOffsets,
+  ) {
+    var linkedData = element.linkedData as TypeAliasElementLinkedData;
+    linkedData.applyConstantOffsets = ApplyConstantOffsets(
+      constantOffsets,
+      (applier) {
+        applier.applyToMetadata(element);
+        applier.applyToTypeParameters(element.typeParameters);
+
+        var aliasedElement = element.aliasedElement;
+        if (aliasedElement is FunctionTypedElementImpl) {
+          applier.applyToTypeParameters(aliasedElement.typeParameters);
+          applier.applyToFormalParameters(aliasedElement);
+        }
+      },
+    );
+  }
 }
 
 class _InfoClassDeclaration {
@@ -873,6 +926,7 @@
   final String? documentationComment;
   final List<_InfoTypeParameter> typeParameters;
   final List<_InfoFormalParameter> parameters;
+  final Uint32List constantOffsets;
 
   factory _InfoFunctionTypeAlias(SummaryDataReader reader) {
     return _InfoFunctionTypeAlias._(
@@ -886,6 +940,7 @@
       parameters: reader.readTypedList(
         () => _InfoFormalParameter(reader),
       ),
+      constantOffsets: reader.readUInt30List(),
     );
   }
 
@@ -896,6 +951,7 @@
     required this.documentationComment,
     required this.typeParameters,
     required this.parameters,
+    required this.constantOffsets,
   });
 }
 
@@ -905,6 +961,7 @@
   final int nameOffset;
   final String? documentationComment;
   final List<_InfoTypeParameter> typeParameters;
+  final Uint32List constantOffsets;
 
   factory _InfoGenericTypeAlias(SummaryDataReader reader) {
     return _InfoGenericTypeAlias._(
@@ -915,6 +972,7 @@
       typeParameters: reader.readTypedList(
         () => _InfoTypeParameter(reader),
       ),
+      constantOffsets: reader.readUInt30List(),
     );
   }
 
@@ -924,6 +982,7 @@
     required this.nameOffset,
     required this.documentationComment,
     required this.typeParameters,
+    required this.constantOffsets,
   });
 }
 
@@ -1002,6 +1061,20 @@
   });
 }
 
+class _InfoPart {
+  final Uint32List constantOffsets;
+
+  factory _InfoPart(SummaryDataReader reader) {
+    return _InfoPart._(
+      constantOffsets: reader.readUInt30List(),
+    );
+  }
+
+  _InfoPart._({
+    required this.constantOffsets,
+  });
+}
+
 class _InformativeDataWriter {
   final BufferedSink sink;
 
@@ -1029,6 +1102,12 @@
       _writeCombinators(directive.combinators);
     });
 
+    sink.writeList2<PartDirective>(unit.directives, (node) {
+      _writeOffsets(
+        metadata: node.metadata,
+      );
+    });
+
     sink.writeList2<ClassDeclaration>(unit.declarations, (node) {
       sink.writeUInt30(node.offset);
       sink.writeUInt30(node.length);
@@ -1134,6 +1213,11 @@
       _writeDocumentationComment(node);
       _writeTypeParameters(node.typeParameters);
       _writeFormalParameters(node.parameters);
+      _writeOffsets(
+        metadata: node.metadata,
+        typeParameters: node.typeParameters,
+        formalParameters: node.parameters,
+      );
     });
 
     sink.writeList2<GenericTypeAlias>(unit.declarations, (node) {
@@ -1142,6 +1226,11 @@
       sink.writeUInt30(node.name.offset);
       _writeDocumentationComment(node);
       _writeTypeParameters(node.typeParameters);
+      _writeOffsets(
+        metadata: node.metadata,
+        typeParameters: node.typeParameters,
+        aliasedType: node.type,
+      );
     });
 
     sink.writeList2<MixinDeclaration>(unit.declarations, (node) {
@@ -1278,15 +1367,20 @@
   void _writeLibraryName(CompilationUnit unit) {
     var nameOffset = -1;
     var nameLength = 0;
+    NodeList<Annotation>? metadata;
     for (var directive in unit.directives) {
       if (directive is LibraryDirective) {
         nameOffset = directive.name.offset;
         nameLength = directive.name.length;
+        metadata = directive.metadata;
         break;
       }
     }
     sink.writeUInt30(1 + nameOffset);
     sink.writeUInt30(nameLength);
+    _writeOffsets(
+      metadata: metadata,
+    );
   }
 
   void _writeMethods(List<ClassMember> members) {
@@ -1314,36 +1408,46 @@
   void _writeOffsets({
     NodeList<Annotation>? metadata,
     TypeParameterList? typeParameters,
-    Expression? constantInitializer,
-    List<EnumConstantDeclaration>? enumConstants,
     FormalParameterList? formalParameters,
-    List<ConstructorInitializer>? constructorInitializers,
+    Expression? constantInitializer,
+    NodeList<ConstructorInitializer>? constructorInitializers,
+    NodeList<EnumConstantDeclaration>? enumConstants,
+    TypeAnnotation? aliasedType,
   }) {
     var collector = _OffsetsCollector();
-    metadata?.accept(collector);
-    if (typeParameters != null) {
-      for (var typeParameter in typeParameters.typeParameters) {
-        typeParameter.metadata.accept(collector);
+
+    void addTypeParameters(TypeParameterList? typeParameters) {
+      if (typeParameters != null) {
+        for (var typeParameter in typeParameters.typeParameters) {
+          typeParameter.metadata.accept(collector);
+        }
       }
     }
+
+    void addFormalParameters(FormalParameterList? formalParameters) {
+      if (formalParameters != null) {
+        for (var parameter in formalParameters.parameters) {
+          parameter.metadata.accept(collector);
+          if (parameter is DefaultFormalParameter) {
+            parameter.defaultValue?.accept(collector);
+          }
+        }
+      }
+    }
+
+    metadata?.accept(collector);
+    addTypeParameters(typeParameters);
+    addFormalParameters(formalParameters);
     constantInitializer?.accept(collector);
+    constructorInitializers?.accept(collector);
     if (enumConstants != null) {
       for (var enumConstant in enumConstants) {
         enumConstant.metadata.accept(collector);
       }
     }
-    if (formalParameters != null) {
-      for (var parameter in formalParameters.parameters) {
-        parameter.metadata.accept(collector);
-        if (parameter is DefaultFormalParameter) {
-          parameter.defaultValue?.accept(collector);
-        }
-      }
-    }
-    if (constructorInitializers != null) {
-      for (var constructorInitializer in constructorInitializers) {
-        constructorInitializer.accept(collector);
-      }
+    if (aliasedType is GenericFunctionType) {
+      addTypeParameters(aliasedType.typeParameters);
+      addFormalParameters(aliasedType.parameters);
     }
     sink.writeUint30List(collector.offsets);
   }
@@ -1425,9 +1529,11 @@
   final int codeLength;
   final List<int> lineStarts;
   final _InfoLibraryName libraryName;
+  final Uint32List libraryConstantOffsets;
   final String docComment;
   final List<_InfoImport> imports;
   final List<_InfoExport> exports;
+  final List<_InfoPart> parts;
   final List<_InfoClassDeclaration> classDeclarations;
   final List<_InfoClassTypeAlias> classTypeAliases;
   final List<_InfoEnumDeclaration> enums;
@@ -1445,6 +1551,7 @@
       codeLength: reader.readUInt30(),
       lineStarts: reader.readUInt30List(),
       libraryName: _InfoLibraryName(reader),
+      libraryConstantOffsets: reader.readUInt30List(),
       docComment: reader.readStringUtf8(),
       imports: reader.readTypedList(
         () => _InfoImport(reader),
@@ -1452,6 +1559,9 @@
       exports: reader.readTypedList(
         () => _InfoExport(reader),
       ),
+      parts: reader.readTypedList(
+        () => _InfoPart(reader),
+      ),
       classDeclarations: reader.readTypedList(
         () => _InfoClassDeclaration(reader),
       ),
@@ -1490,9 +1600,11 @@
     required this.codeLength,
     required this.lineStarts,
     required this.libraryName,
+    required this.libraryConstantOffsets,
     required this.docComment,
     required this.imports,
     required this.exports,
+    required this.parts,
     required this.classDeclarations,
     required this.classTypeAliases,
     required this.enums,
@@ -1532,7 +1644,7 @@
     }
   }
 
-  void applyToFormalParameters(ExecutableElement element) {
+  void applyToFormalParameters(FunctionTypedElement element) {
     for (var parameter in element.parameters) {
       applyToMetadata(parameter);
       applyToConstantInitializer(parameter);
diff --git a/pkg/analyzer/test/src/summary/resynthesize_common.dart b/pkg/analyzer/test/src/summary/resynthesize_common.dart
index abeb718..9db2bbc 100644
--- a/pkg/analyzer/test/src/summary/resynthesize_common.dart
+++ b/pkg/analyzer/test/src/summary/resynthesize_common.dart
@@ -5263,14 +5263,14 @@
         metadata
           Annotation
             arguments: ArgumentList
-              leftParenthesis: ( @0
-              rightParenthesis: ) @0
-            atSign.offset: 0
+              leftParenthesis: ( @79
+              rightParenthesis: ) @80
+            atSign.offset: 72
             element: dart:core::@class::Object::@constructor::•
             name: SimpleIdentifier
               staticElement: dart:core::@class::Object
               staticType: null
-              token: Object @-1
+              token: Object @73
         codeOffset: 72
         codeLength: 34
         aliasedType: dynamic Function()
@@ -5281,14 +5281,14 @@
         metadata
           Annotation
             arguments: ArgumentList
-              leftParenthesis: ( @0
-              rightParenthesis: ) @0
-            atSign.offset: 0
+              leftParenthesis: ( @115
+              rightParenthesis: ) @116
+            atSign.offset: 108
             element: dart:core::@class::Object::@constructor::•
             name: SimpleIdentifier
               staticElement: dart:core::@class::Object
               staticType: null
-              token: Object @-1
+              token: Object @109
         codeOffset: 108
         codeLength: 72
         aliasedType: dynamic Function()
@@ -5299,14 +5299,14 @@
         metadata
           Annotation
             arguments: ArgumentList
-              leftParenthesis: ( @0
-              rightParenthesis: ) @0
-            atSign.offset: 0
+              leftParenthesis: ( @219
+              rightParenthesis: ) @220
+            atSign.offset: 212
             element: dart:core::@class::Object::@constructor::•
             name: SimpleIdentifier
               staticElement: dart:core::@class::Object
               staticType: null
-              token: Object @-1
+              token: Object @213
         codeOffset: 182
         codeLength: 72
         aliasedType: dynamic Function()
@@ -5317,14 +5317,14 @@
         metadata
           Annotation
             arguments: ArgumentList
-              leftParenthesis: ( @0
-              rightParenthesis: ) @0
-            atSign.offset: 0
+              leftParenthesis: ( @278
+              rightParenthesis: ) @279
+            atSign.offset: 271
             element: dart:core::@class::Object::@constructor::•
             name: SimpleIdentifier
               staticElement: dart:core::@class::Object
               staticType: null
-              token: Object @-1
+              token: Object @272
         codeOffset: 271
         codeLength: 59
         aliasedType: dynamic Function()
@@ -5383,14 +5383,14 @@
         metadata
           Annotation
             arguments: ArgumentList
-              leftParenthesis: ( @0
-              rightParenthesis: ) @0
-            atSign.offset: 0
+              leftParenthesis: ( @101
+              rightParenthesis: ) @102
+            atSign.offset: 94
             element: dart:core::@class::Object::@constructor::•
             name: SimpleIdentifier
               staticElement: dart:core::@class::Object
               staticType: null
-              token: Object @-1
+              token: Object @95
         codeOffset: 94
         codeLength: 45
         aliasedType: dynamic Function()
@@ -5401,14 +5401,14 @@
         metadata
           Annotation
             arguments: ArgumentList
-              leftParenthesis: ( @0
-              rightParenthesis: ) @0
-            atSign.offset: 0
+              leftParenthesis: ( @148
+              rightParenthesis: ) @149
+            atSign.offset: 141
             element: dart:core::@class::Object::@constructor::•
             name: SimpleIdentifier
               staticElement: dart:core::@class::Object
               staticType: null
-              token: Object @-1
+              token: Object @142
         codeOffset: 141
         codeLength: 83
         aliasedType: dynamic Function()
@@ -5419,14 +5419,14 @@
         metadata
           Annotation
             arguments: ArgumentList
-              leftParenthesis: ( @0
-              rightParenthesis: ) @0
-            atSign.offset: 0
+              leftParenthesis: ( @263
+              rightParenthesis: ) @264
+            atSign.offset: 256
             element: dart:core::@class::Object::@constructor::•
             name: SimpleIdentifier
               staticElement: dart:core::@class::Object
               staticType: null
-              token: Object @-1
+              token: Object @257
         codeOffset: 226
         codeLength: 83
         aliasedType: dynamic Function()
@@ -5437,14 +5437,14 @@
         metadata
           Annotation
             arguments: ArgumentList
-              leftParenthesis: ( @0
-              rightParenthesis: ) @0
-            atSign.offset: 0
+              leftParenthesis: ( @333
+              rightParenthesis: ) @334
+            atSign.offset: 326
             element: dart:core::@class::Object::@constructor::•
             name: SimpleIdentifier
               staticElement: dart:core::@class::Object
               staticType: null
-              token: Object @-1
+              token: Object @327
         codeOffset: 326
         codeLength: 70
         aliasedType: dynamic Function()
@@ -23033,12 +23033,12 @@
       functionTypeAliasBased F @27
         metadata
           Annotation
-            atSign.offset: 0
+            atSign.offset: 16
             element: self::@getter::a
             name: SimpleIdentifier
               staticElement: self::@getter::a
               staticType: null
-              token: a @-1
+              token: a @17
         aliasedType: dynamic Function()
         aliasedElement: GenericFunctionTypeElement
           returnType: dynamic
@@ -23137,19 +23137,19 @@
       F @46
         metadata
           Annotation
-            atSign.offset: 0
+            atSign.offset: 32
             element: self::@getter::a
             name: SimpleIdentifier
               staticElement: self::@getter::a
               staticType: null
-              token: a @-1
+              token: a @33
           Annotation
-            atSign.offset: 0
+            atSign.offset: 35
             element: self::@getter::b
             name: SimpleIdentifier
               staticElement: self::@getter::b
               staticType: null
-              token: b @-1
+              token: b @36
         aliasedType: void Function()
         aliasedElement: GenericFunctionTypeElement
           returnType: void
@@ -23274,12 +23274,12 @@
               type: int
               metadata
                 Annotation
-                  atSign.offset: 0
+                  atSign.offset: 40
                   element: self::@getter::a
                   name: SimpleIdentifier
                     staticElement: self::@getter::a
                     staticType: null
-                    token: a @-1
+                    token: a @41
           returnType: void
     topLevelVariables
       static const a @6
@@ -23350,12 +23350,12 @@
             covariant T @-1
               metadata
                 Annotation
-                  atSign.offset: 0
+                  atSign.offset: 40
                   element: self::@getter::a
                   name: SimpleIdentifier
                     staticElement: self::@getter::a
                     staticType: null
-                    token: a @-1
+                    token: a @41
           parameters
             requiredPositional first @-1
               type: int
@@ -23419,7 +23419,7 @@
       name: SimpleIdentifier
         staticElement: self::@getter::a
         staticType: null
-        token: a @-1
+        token: a @1
   definingUnit
     topLevelVariables
       static const a @20
@@ -24206,10 +24206,13 @@
 
 class A {
   @foo
-  static isNotConst = 1;
+  static isStatic = 1;
 
   @foo
-  static const isConst = 2;
+  static const isStaticConst = 2;
+
+  @foo
+  var isInstance = 3;
 }
 ''');
     checkElementText(library, r'''
@@ -24218,7 +24221,7 @@
     classes
       class A @22
         fields
-          static isNotConst @42
+          static isStatic @42
             metadata
               Annotation
                 atSign.offset: 28
@@ -24228,32 +24231,49 @@
                   staticType: null
                   token: foo @29
             type: int
-          static const isConst @81
+          static const isStaticConst @79
             metadata
               Annotation
-                atSign.offset: 61
+                atSign.offset: 59
                 element: self::@getter::foo
                 name: SimpleIdentifier
                   staticElement: self::@getter::foo
                   staticType: null
-                  token: foo @62
+                  token: foo @60
             type: int
             constantInitializer
               IntegerLiteral
-                literal: 2 @91
+                literal: 2 @95
                 staticType: int
+          isInstance @112
+            metadata
+              Annotation
+                atSign.offset: 101
+                element: self::@getter::foo
+                name: SimpleIdentifier
+                  staticElement: self::@getter::foo
+                  staticType: null
+                  token: foo @102
+            type: int
         constructors
           synthetic @-1
         accessors
-          synthetic static get isNotConst @42
+          synthetic static get isStatic @42
             returnType: int
-          synthetic static set isNotConst @42
+          synthetic static set isStatic @42
             parameters
-              requiredPositional _isNotConst @42
+              requiredPositional _isStatic @42
                 type: int
             returnType: void
-          synthetic static get isConst @81
+          synthetic static get isStaticConst @79
             returnType: int
+          synthetic get isInstance @112
+            returnType: int
+          synthetic set isInstance @112
+            parameters
+              requiredPositional _isInstance @112
+                type: int
+            returnType: void
     topLevelVariables
       static const foo @6
         type: int
@@ -24267,6 +24287,41 @@
 ''');
   }
 
+  test_metadata_offsets_onLibrary() async {
+    var library = await checkLibrary('''
+/// Some documentation.
+@foo
+library my.lib;
+
+const foo = 0;
+''');
+    checkElementText(library, r'''
+library
+  name: my.lib
+  nameOffset: 37
+  documentationComment: /// Some documentation.
+  metadata
+    Annotation
+      atSign.offset: 24
+      element: self::@getter::foo
+      name: SimpleIdentifier
+        staticElement: self::@getter::foo
+        staticType: null
+        token: foo @25
+  definingUnit
+    topLevelVariables
+      static const foo @52
+        type: int
+        constantInitializer
+          IntegerLiteral
+            literal: 0 @58
+            staticType: int
+    accessors
+      synthetic static get foo @52
+        returnType: int
+''');
+  }
+
   test_metadata_offsets_onMixin() async {
     var library = await checkLibrary(r'''
 const foo = 0;
@@ -24315,6 +24370,189 @@
 ''');
   }
 
+  test_metadata_offsets_onTypeAlias_classic() async {
+    var library = await checkLibrary(r'''
+const foo = 0;
+
+@foo
+typedef void F<@foo T>(@foo int a);
+''');
+    checkElementText(library, r'''
+library
+  definingUnit
+    typeAliases
+      functionTypeAliasBased F @34
+        metadata
+          Annotation
+            atSign.offset: 16
+            element: self::@getter::foo
+            name: SimpleIdentifier
+              staticElement: self::@getter::foo
+              staticType: null
+              token: foo @17
+        typeParameters
+          unrelated T @41
+            defaultType: dynamic
+            metadata
+              Annotation
+                atSign.offset: 36
+                element: self::@getter::foo
+                name: SimpleIdentifier
+                  staticElement: self::@getter::foo
+                  staticType: null
+                  token: foo @37
+        aliasedType: void Function(int)
+        aliasedElement: GenericFunctionTypeElement
+          parameters
+            requiredPositional a @-1
+              type: int
+              metadata
+                Annotation
+                  atSign.offset: 44
+                  element: self::@getter::foo
+                  name: SimpleIdentifier
+                    staticElement: self::@getter::foo
+                    staticType: null
+                    token: foo @45
+          returnType: void
+    topLevelVariables
+      static const foo @6
+        type: int
+        constantInitializer
+          IntegerLiteral
+            literal: 0 @12
+            staticType: int
+    accessors
+      synthetic static get foo @6
+        returnType: int
+''');
+  }
+
+  test_metadata_offsets_onTypeAlias_genericFunctionType() async {
+    var library = await checkLibrary(r'''
+const foo = 0;
+
+@foo
+typedef A<@foo T> = void Function<@foo U>(@foo int a);
+''');
+    checkElementText(library, r'''
+library
+  definingUnit
+    typeAliases
+      A @29
+        metadata
+          Annotation
+            atSign.offset: 16
+            element: self::@getter::foo
+            name: SimpleIdentifier
+              staticElement: self::@getter::foo
+              staticType: null
+              token: foo @17
+        typeParameters
+          unrelated T @36
+            defaultType: dynamic
+            metadata
+              Annotation
+                atSign.offset: 31
+                element: self::@getter::foo
+                name: SimpleIdentifier
+                  staticElement: self::@getter::foo
+                  staticType: null
+                  token: foo @32
+        aliasedType: void Function<U>(int)
+        aliasedElement: GenericFunctionTypeElement
+          typeParameters
+            covariant U @-1
+              metadata
+                Annotation
+                  atSign.offset: 55
+                  element: self::@getter::foo
+                  name: SimpleIdentifier
+                    staticElement: self::@getter::foo
+                    staticType: null
+                    token: foo @56
+          parameters
+            requiredPositional a @-1
+              type: int
+              metadata
+                Annotation
+                  atSign.offset: 63
+                  element: self::@getter::foo
+                  name: SimpleIdentifier
+                    staticElement: self::@getter::foo
+                    staticType: null
+                    token: foo @64
+          returnType: void
+    topLevelVariables
+      static const foo @6
+        type: int
+        constantInitializer
+          IntegerLiteral
+            literal: 0 @12
+            staticType: int
+    accessors
+      synthetic static get foo @6
+        returnType: int
+''');
+  }
+
+  test_metadata_offsets_onUnit() async {
+    addSource('/a.dart', '''
+part of my.lib;
+''');
+
+    addSource('/b.dart', '''
+part of my.lib;
+''');
+
+    var library = await checkLibrary('''
+library my.lib;
+
+@foo
+part 'a.dart';
+
+@foo
+part 'b.dart';
+
+const foo = 0;
+''');
+    checkElementText(library, r'''
+library
+  name: my.lib
+  nameOffset: 8
+  definingUnit
+    topLevelVariables
+      static const foo @65
+        type: int
+        constantInitializer
+          IntegerLiteral
+            literal: 0 @71
+            staticType: int
+    accessors
+      synthetic static get foo @65
+        returnType: int
+  parts
+    a.dart
+      metadata
+        Annotation
+          atSign.offset: 17
+          element: self::@getter::foo
+          name: SimpleIdentifier
+            staticElement: self::@getter::foo
+            staticType: null
+            token: foo @18
+    b.dart
+      metadata
+        Annotation
+          atSign.offset: 38
+          element: self::@getter::foo
+          name: SimpleIdentifier
+            staticElement: self::@getter::foo
+            staticType: null
+            token: foo @39
+''');
+  }
+
   test_metadata_offsets_onUnitFunction() async {
     var library = await checkLibrary(r'''
 const foo = 0;
@@ -24542,12 +24780,12 @@
     foo.dart
       metadata
         Annotation
-          atSign.offset: 0
+          atSign.offset: 11
           element: self::@getter::a
           name: SimpleIdentifier
             staticElement: self::@getter::a
             staticType: null
-            token: a @-1
+            token: a @12
 ''');
   }
 
@@ -24910,12 +25148,12 @@
             defaultType: dynamic
             metadata
               Annotation
-                atSign.offset: 0
+                atSign.offset: 26
                 element: self::@getter::a
                 name: SimpleIdentifier
                   staticElement: self::@getter::a
                   staticType: null
-                  token: a @-1
+                  token: a @27
         aliasedType: dynamic Function()
         aliasedElement: GenericFunctionTypeElement
           returnType: dynamic