Elements. Remove FragmentDeclaration, add 'declaredFragment' to Declaration.

Change-Id: I33398358d1241114b04995767d63188bb314fd6a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394565
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analysis_server/lib/src/lsp/handlers/custom/handler_augmentation.dart b/pkg/analysis_server/lib/src/lsp/handlers/custom/handler_augmentation.dart
index 7c68e7f..6e22bec 100644
--- a/pkg/analysis_server/lib/src/lsp/handlers/custom/handler_augmentation.dart
+++ b/pkg/analysis_server/lib/src/lsp/handlers/custom/handler_augmentation.dart
@@ -7,7 +7,7 @@
 import 'package:analysis_server/src/lsp/error_or.dart';
 import 'package:analysis_server/src/lsp/handlers/handlers.dart';
 import 'package:analysis_server/src/lsp/mapping.dart';
-import 'package:analyzer/src/dart/ast/ast.dart';
+import 'package:analyzer/src/dart/ast/ast.dart' as ast;
 import 'package:analyzer/src/utilities/extensions/ast.dart';
 
 class AugmentationHandler
@@ -44,7 +44,7 @@
       var node =
           unit.unit
               .nodeCovering(offset: offset)
-              ?.thisOrAncestorOfType<FragmentDeclaration>();
+              ?.thisOrAncestorOfType<ast.Declaration>();
 
       var location = fragmentToLocation(
         uriConverter,
diff --git a/pkg/analysis_server/lib/src/lsp/handlers/custom/handler_augmented.dart b/pkg/analysis_server/lib/src/lsp/handlers/custom/handler_augmented.dart
index 45631c0..daf19dd 100644
--- a/pkg/analysis_server/lib/src/lsp/handlers/custom/handler_augmented.dart
+++ b/pkg/analysis_server/lib/src/lsp/handlers/custom/handler_augmented.dart
@@ -7,7 +7,7 @@
 import 'package:analysis_server/src/lsp/error_or.dart';
 import 'package:analysis_server/src/lsp/handlers/handlers.dart';
 import 'package:analysis_server/src/lsp/mapping.dart';
-import 'package:analyzer/src/dart/ast/ast.dart';
+import 'package:analyzer/src/dart/ast/ast.dart' as ast;
 import 'package:analyzer/src/utilities/extensions/ast.dart';
 
 class AugmentedHandler
@@ -44,7 +44,7 @@
       var node =
           unit.unit
               .nodeCovering(offset: offset)
-              ?.thisOrAncestorOfType<FragmentDeclaration>();
+              ?.thisOrAncestorOfType<ast.Declaration>();
 
       var location = fragmentToLocation(
         uriConverter,
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused.dart
index e755492..5bc7a8a 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused.dart
@@ -38,7 +38,7 @@
     }
 
     Element2? element;
-    if (node is FragmentDeclaration) {
+    if (node is Declaration) {
       element = node.declaredFragment?.element;
     }
     if (element == null) {
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/rename_method_parameter.dart b/pkg/analysis_server/lib/src/services/correction/dart/rename_method_parameter.dart
index 69c82fc..aa4b5655 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/rename_method_parameter.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/rename_method_parameter.dart
@@ -45,7 +45,7 @@
     if (methodParameters == null) return;
 
     Fragment? declaredFragment;
-    if (method.parent case FragmentDeclaration declaration) {
+    if (method.parent case Declaration declaration) {
       declaredFragment = declaration.declaredFragment;
     }
 
diff --git a/pkg/analyzer/lib/src/dart/ast/ast.dart b/pkg/analyzer/lib/src/dart/ast/ast.dart
index 74c06d8..86a1e1b 100644
--- a/pkg/analyzer/lib/src/dart/ast/ast.dart
+++ b/pkg/analyzer/lib/src/dart/ast/ast.dart
@@ -2684,8 +2684,7 @@
 ///    classModifiers ::= 'sealed'
 ///      | 'abstract'? ('base' | 'interface' | 'final')?
 ///      | 'abstract'? 'base'? 'mixin'
-abstract final class ClassDeclaration
-    implements NamedCompilationUnitMember, FragmentDeclaration {
+abstract final class ClassDeclaration implements NamedCompilationUnitMember {
   /// The `abstract` keyword, or `null` if the keyword was absent.
   Token? get abstractKeyword;
 
@@ -2921,7 +2920,7 @@
 ///
 ///    mixinApplication ::=
 ///        [NamedType] [WithClause] [ImplementsClause]? ';'
-abstract final class ClassTypeAlias implements TypeAlias, FragmentDeclaration {
+abstract final class ClassTypeAlias implements TypeAlias {
   /// The token for the `abstract` keyword, or `null` if this isn't defining an
   /// abstract class.
   Token? get abstractKeyword;
@@ -4062,8 +4061,7 @@
 ///
 ///    initializerList ::=
 ///        ':' [ConstructorInitializer] (',' [ConstructorInitializer])*
-abstract final class ConstructorDeclaration
-    implements ClassMember, FragmentDeclaration {
+abstract final class ConstructorDeclaration implements ClassMember {
   /// The `augment` keyword, or `null` if the keyword was absent.
   Token? get augmentKeyword;
 
@@ -4797,6 +4795,12 @@
   /// node corresponds to a list of declarations or if the AST structure hasn't
   /// been resolved.
   Element? get declaredElement;
+
+  /// The fragment declared by this declaration.
+  ///
+  /// Returns `null` if the AST structure hasn't been resolved.
+  @experimental
+  Fragment? get declaredFragment;
 }
 
 sealed class DeclarationImpl extends AnnotatedNodeImpl implements Declaration {
@@ -4859,6 +4863,9 @@
   @override
   LocalVariableElementImpl? declaredElement;
 
+  @override
+  LocalVariableElementImpl? declaredFragment;
+
   /// Initializes a newly created formal parameter.
   ///
   /// Either or both of the [comment] and [metadata] can be `null` if the
@@ -5539,7 +5546,7 @@
 }
 
 /// The declaration of an enum constant.
-abstract final class EnumConstantDeclaration implements FragmentDeclaration {
+abstract final class EnumConstantDeclaration implements Declaration {
   /// The explicit arguments (there are always implicit `index` and `name`
   /// leading arguments) to the invoked constructor, or `null` if this constant
   /// doesn't provide any explicit arguments.
@@ -5641,8 +5648,7 @@
 ///        metadata 'enum' name [TypeParameterList]?
 ///        [WithClause]? [ImplementsClause]? '{' [SimpleIdentifier]
 ///        (',' [SimpleIdentifier])* (';' [ClassMember]+)? '}'
-abstract final class EnumDeclaration
-    implements NamedCompilationUnitMember, FragmentDeclaration {
+abstract final class EnumDeclaration implements NamedCompilationUnitMember {
   /// The `augment` keyword, or `null` if the keyword was absent.
   @experimental
   Token? get augmentKeyword;
@@ -6364,8 +6370,7 @@
 ///        'extension' [SimpleIdentifier]? [TypeParameterList]?
 ///        'on' [TypeAnnotation] [ShowClause]? [HideClause]?
 ///        '{' [ClassMember]* '}'
-abstract final class ExtensionDeclaration
-    implements CompilationUnitMember, FragmentDeclaration {
+abstract final class ExtensionDeclaration implements CompilationUnitMember {
   /// The `augment` keyword, or `null` if the keyword was absent.
   @experimental
   Token? get augmentKeyword;
@@ -6694,7 +6699,7 @@
 ///        '}'
 @experimental
 abstract final class ExtensionTypeDeclaration
-    implements NamedCompilationUnitMember, FragmentDeclaration {
+    implements NamedCompilationUnitMember {
   /// The `augment` keyword, or `null` if the keyword was absent.
   @experimental
   Token? get augmentKeyword;
@@ -6924,6 +6929,9 @@
   Element? get declaredElement => null;
 
   @override
+  Fragment? get declaredFragment => null;
+
+  @override
   Token get endToken => semicolon;
 
   @override
@@ -8073,18 +8081,6 @@
   }
 }
 
-/// A declaration of a fragment of an element.
-@experimental
-abstract final class FragmentDeclaration implements Declaration {
-  // TODO(pq): move `declaredFragment` into `Declaration` and remove this class.
-
-  /// The fragment declared by this declaration.
-  ///
-  /// Returns `null` if the AST structure hasn't been resolved.
-  @experimental
-  Fragment? get declaredFragment;
-}
-
 /// A node representing the body of a function or method.
 ///
 ///    functionBody ::=
@@ -8203,8 +8199,7 @@
 //  augmented and declarations that can't be augmented. This results in getters
 //  that are only sometimes applicable. Consider changing the class hierarchy so
 //  that these two kinds of variables can be distinguished.
-abstract final class FunctionDeclaration
-    implements NamedCompilationUnitMember, FragmentDeclaration {
+abstract final class FunctionDeclaration implements NamedCompilationUnitMember {
   /// The `augment` keyword, or `null` if there is no `augment` keyword.
   @experimental
   Token? get augmentKeyword;
@@ -8742,8 +8737,7 @@
 ///
 ///    functionPrefix ::=
 ///        [TypeAnnotation]? [SimpleIdentifier]
-abstract final class FunctionTypeAlias
-    implements TypeAlias, FragmentDeclaration {
+abstract final class FunctionTypeAlias implements TypeAlias {
   @override
   TypeAliasElement? get declaredElement;
 
@@ -9104,8 +9098,7 @@
 ///    functionTypeAlias ::=
 ///        'typedef' [SimpleIdentifier] [TypeParameterList]? =
 ///        [FunctionType] ';'
-abstract final class GenericTypeAlias
-    implements TypeAlias, FragmentDeclaration {
+abstract final class GenericTypeAlias implements TypeAlias {
   /// The equal sign separating the name being defined from the function type.
   Token get equals;
 
@@ -11798,8 +11791,7 @@
 /// Prior to the 'extension-methods' experiment, these nodes were always
 /// children of a class declaration. When the experiment is enabled, these nodes
 /// can also be children of an extension declaration.
-abstract final class MethodDeclaration
-    implements ClassMember, FragmentDeclaration {
+abstract final class MethodDeclaration implements ClassMember {
   /// The token for the `augment` keyword.
   Token? get augmentKeyword;
 
@@ -12195,8 +12187,7 @@
 ///    mixinDeclaration ::=
 ///        'base'? 'mixin' name [TypeParameterList]?
 ///        [OnClause]? [ImplementsClause]? '{' [ClassMember]* '}'
-abstract final class MixinDeclaration
-    implements NamedCompilationUnitMember, FragmentDeclaration {
+abstract final class MixinDeclaration implements NamedCompilationUnitMember {
   /// The `augment` keyword, or `null` if the keyword was absent.
   Token? get augmentKeyword;
 
@@ -17653,6 +17644,9 @@
   Element? get declaredElement => null;
 
   @override
+  Fragment? get declaredFragment => null;
+
+  @override
   Token get endToken => semicolon;
 
   @override
@@ -18052,7 +18046,7 @@
 ///
 ///    typeParameter ::=
 ///        name ('extends' [TypeAnnotation])?
-abstract final class TypeParameter implements Declaration, FragmentDeclaration {
+abstract final class TypeParameter implements Declaration {
   /// The upper bound for legal arguments, or `null` if there's no explicit
   /// upper bound.
   TypeAnnotation? get bound;
@@ -18302,8 +18296,7 @@
 //  augmented and declarations that can't be augmented. This results in getters
 //  that are only sometimes applicable. Consider changing the class hierarchy so
 //  that these two kinds of variables can be distinguished.
-abstract final class VariableDeclaration
-    implements Declaration, FragmentDeclaration {
+abstract final class VariableDeclaration implements Declaration {
   /// The element declared by this declaration.
   ///
   /// Returns `null` if the AST structure hasn't been resolved or if this node
diff --git a/pkg/linter/lib/src/rules/avoid_returning_this.dart b/pkg/linter/lib/src/rules/avoid_returning_this.dart
index ef77d4d..830112b 100644
--- a/pkg/linter/lib/src/rules/avoid_returning_this.dart
+++ b/pkg/linter/lib/src/rules/avoid_returning_this.dart
@@ -83,7 +83,7 @@
       if (returnType is InterfaceType &&
           returnType.element3 ==
               // ignore: cast_nullable_to_non_nullable
-              (parent as FragmentDeclaration).declaredFragment?.element) {
+              (parent as Declaration).declaredFragment?.element) {
       } else {
         return;
       }
diff --git a/pkg/linter/lib/src/rules/unreachable_from_main.dart b/pkg/linter/lib/src/rules/unreachable_from_main.dart
index af153ee..2f9a481 100644
--- a/pkg/linter/lib/src/rules/unreachable_from_main.dart
+++ b/pkg/linter/lib/src/rules/unreachable_from_main.dart
@@ -53,10 +53,8 @@
       if (declaration is TopLevelVariableDeclaration) {
         declarations.addAll(declaration.variables.variables);
       } else {
-        if (declaration is! FragmentDeclaration) continue;
         declarations.add(declaration);
-        var declaredElement =
-            (declaration as FragmentDeclaration).declaredFragment?.element;
+        var declaredElement = declaration.declaredFragment?.element;
         if (declaredElement == null || declaredElement.isPrivate) {
           continue;
         }
@@ -142,7 +140,7 @@
 /// "References" are most often [SimpleIdentifier]s, but can also be other
 /// nodes which refer to a declaration.
 class _ReferenceVisitor extends RecursiveAstVisitor<void> {
-  Map<Element2, FragmentDeclaration> declarationMap;
+  Map<Element2, Declaration> declarationMap;
 
   Set<Declaration> declarations = {};
 
@@ -463,10 +461,9 @@
     if (entryPoints.isEmpty) return;
 
     // Map each top-level and static element to its declaration.
-    var declarationByElement = <Element2, FragmentDeclaration>{};
+    var declarationByElement = <Element2, Declaration>{};
     for (var declaration in declarations) {
-      var element =
-          (declaration as FragmentDeclaration).declaredFragment?.element;
+      var element = declaration.declaredFragment?.element;
       if (element != null) {
         declarationByElement[element] = declaration;
         if (element is TopLevelVariableElement2) {
@@ -516,8 +513,7 @@
     var unitDeclarations = unitDeclarationGatherer.declarations;
     var unusedDeclarations = unitDeclarations.difference(usedMembers);
     var unusedMembers = unusedDeclarations.where((declaration) {
-      var element =
-          (declaration as FragmentDeclaration).declaredFragment?.element;
+      var element = declaration.declaredFragment?.element;
       return element != null &&
           element.isPublic &&
           !element.hasVisibleForTesting;