Elements. Remove V1 CompilationUnitElement.

Change-Id: I9efdbd57bfabab0f7830a3e765e81da5cd4afc15
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/423423
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/api.txt b/pkg/analyzer/api.txt
index 9a8e51b..f0f327c 100644
--- a/pkg/analyzer/api.txt
+++ b/pkg/analyzer/api.txt
@@ -2971,16 +2971,6 @@
     new (constructor: ClassMemberElement Function())
     enclosingElement3 (getter: Element)
     isStatic (getter: bool)
-  CompilationUnitElement (class extends Object implements UriReferencedElement, deprecated):
-    new (constructor: CompilationUnitElement Function())
-    enclosingElement3 (getter: CompilationUnitElement?)
-    libraryExports (getter: List<LibraryExportElement>)
-    libraryImportPrefixes (getter: List<PrefixElement>)
-    libraryImports (getter: List<LibraryImportElement>)
-    lineInfo (getter: LineInfo)
-    parts (getter: List<PartElement>)
-    scope (getter: Scope)
-    session (getter: AnalysisSession)
   DeferredImportElementPrefix (class extends Object implements ImportElementPrefix, deprecated):
     new (constructor: DeferredImportElementPrefix Function())
   DirectiveUri (class extends Object):
@@ -3001,7 +2991,6 @@
   DirectiveUriWithUnit (class extends DirectiveUriWithSource):
     new (constructor: DirectiveUriWithUnit Function())
     libraryFragment (getter: LibraryFragment, experimental)
-    unit (getter: CompilationUnitElement, deprecated)
   Element (class extends Object implements AnalysisTarget, deprecated):
     new (constructor: Element Function())
     context (getter: AnalysisContext@2)
@@ -3149,7 +3138,6 @@
     element (getter: PrefixElement)
   LibraryElement (class extends Object implements _ExistingElement, deprecated):
     new (constructor: LibraryElement Function())
-    definingCompilationUnit (getter: CompilationUnitElement, deprecated)
     enclosingElement3 (getter: Null)
     exportNamespace (getter: Namespace)
     exportedLibraries (getter: List<LibraryElement>)
@@ -3167,18 +3155,15 @@
     topLevelElements (getter: Iterable<Element>)
     typeProvider (getter: TypeProvider)
     typeSystem (getter: TypeSystem)
-    units (getter: List<CompilationUnitElement>, deprecated)
   LibraryExportElement (class extends Object implements _ExistingElement, deprecated):
     new (constructor: LibraryExportElement Function())
     combinators (getter: List<NamespaceCombinator>)
-    enclosingElement3 (getter: CompilationUnitElement, deprecated)
     exportKeywordOffset (getter: int)
     exportedLibrary (getter: LibraryElement?)
     uri (getter: DirectiveUri)
   LibraryImportElement (class extends Object implements _ExistingElement, deprecated):
     new (constructor: LibraryImportElement Function())
     combinators (getter: List<NamespaceCombinator>)
-    enclosingElement3 (getter: CompilationUnitElement, deprecated)
     importKeywordOffset (getter: int)
     importedLibrary (getter: LibraryElement?)
     namespace (getter: Namespace)
@@ -3224,7 +3209,6 @@
     uri (getter: DirectiveUri)
   PrefixElement (class extends Object implements _ExistingElement, deprecated):
     new (constructor: PrefixElement Function())
-    enclosingElement3 (getter: CompilationUnitElement, deprecated)
     imports (getter: List<LibraryImportElement>)
     name (getter: String)
     scope (getter: Scope)
diff --git a/pkg/analyzer/lib/dart/element/element.dart b/pkg/analyzer/lib/dart/element/element.dart
index 904e2d1..3bb859b 100644
--- a/pkg/analyzer/lib/dart/element/element.dart
+++ b/pkg/analyzer/lib/dart/element/element.dart
@@ -47,7 +47,6 @@
 import 'package:analyzer/dart/element/type_provider.dart';
 import 'package:analyzer/dart/element/type_system.dart';
 import 'package:analyzer/error/error.dart';
-import 'package:analyzer/source/line_info.dart';
 import 'package:analyzer/source/source.dart';
 import 'package:analyzer/src/dart/constant/evaluation.dart';
 import 'package:analyzer/src/dart/element/element.dart'
@@ -84,43 +83,6 @@
   bool get isStatic;
 }
 
-/// An element representing a compilation unit.
-///
-/// Clients may not extend, implement or mix-in this class.
-@Deprecated(elementModelDeprecationMsg)
-abstract class CompilationUnitElement implements UriReferencedElement {
-  /// The [CompilationUnitElement] that uses `part` directive to include this
-  /// element, or `null` if this element is the defining unit of the library.
-  @override
-  CompilationUnitElement? get enclosingElement3;
-
-  /// The libraries exported by this unit.
-  List<LibraryExportElement> get libraryExports;
-
-  /// The prefixes used by [libraryImports].
-  ///
-  /// Each prefix can be used in more than one `import` directive.
-  List<PrefixElement> get libraryImportPrefixes;
-
-  /// The libraries imported by this unit.
-  List<LibraryImportElement> get libraryImports;
-
-  /// The [LineInfo] for the [source].
-  LineInfo get lineInfo;
-
-  /// The parts included by this unit.
-  List<PartElement> get parts;
-
-  /// The scope used to resolve names within this compilation unit.
-  ///
-  /// It includes all of the elements that are declared in the library, and all
-  /// of the elements imported into this unit or parent units.
-  Scope get scope;
-
-  @override
-  AnalysisSession get session;
-}
-
 /// [ImportElementPrefix] that is used together with `deferred`.
 ///
 /// Clients may not extend, implement or mix-in this class.
@@ -176,10 +138,6 @@
   /// The library fragment referenced by the [source].
   @experimental
   LibraryFragment get libraryFragment;
-
-  /// The unit referenced by the [source].
-  @Deprecated('Use libraryFragment instead')
-  CompilationUnitElement get unit;
 }
 
 /// The base class for all of the elements in the element model. Generally
@@ -816,10 +774,6 @@
 /// Clients may not extend, implement or mix-in this class.
 @Deprecated('Use LibraryElement2 instead')
 abstract class LibraryElement implements _ExistingElement {
-  /// The compilation unit that defines this library.
-  @Deprecated(elementModelDeprecationMsg)
-  CompilationUnitElement get definingCompilationUnit;
-
   /// Returns `null`, because libraries are the top-level elements in the model.
   @override
   Null get enclosingElement3;
@@ -883,13 +837,6 @@
 
   /// The [TypeSystem] that is used in this library.
   TypeSystem get typeSystem;
-
-  /// The compilation units this library consists of.
-  ///
-  /// This includes the defining compilation unit and units included using the
-  /// `part` directive.
-  @Deprecated(elementModelDeprecationMsg)
-  List<CompilationUnitElement> get units;
 }
 
 /// A single export directive within a library.
@@ -901,10 +848,6 @@
   /// the order in which they were specified.
   List<NamespaceCombinator> get combinators;
 
-  @Deprecated(elementModelDeprecationMsg)
-  @override
-  CompilationUnitElement get enclosingElement3;
-
   /// The [LibraryElement], if [uri] is a [DirectiveUriWithLibrary].
   LibraryElement? get exportedLibrary;
 
@@ -924,10 +867,6 @@
   /// the order in which they were specified.
   List<NamespaceCombinator> get combinators;
 
-  @Deprecated(elementModelDeprecationMsg)
-  @override
-  CompilationUnitElement get enclosingElement3;
-
   /// The [LibraryElement], if [uri] is a [DirectiveUriWithLibrary].
   LibraryElement? get importedLibrary;
 
@@ -1109,10 +1048,6 @@
 /// Clients may not extend, implement or mix-in this class.
 @Deprecated('Use PrefixElement2 instead')
 abstract class PrefixElement implements _ExistingElement {
-  @Deprecated(elementModelDeprecationMsg)
-  @override
-  CompilationUnitElement get enclosingElement3;
-
   /// The imports that share this prefix.
   List<LibraryImportElement> get imports;
 
diff --git a/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart b/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart
index fc7c071..6a485b0 100644
--- a/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart
+++ b/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart
@@ -266,7 +266,7 @@
       if (directive is PartDirectiveImpl) {
         var uri = directive.partInclude?.uri;
         if (uri is DirectiveUriWithUnitImpl) {
-          var partUnit = elementToUnit[uri.unit];
+          var partUnit = elementToUnit[uri.libraryFragment];
           if (partUnit != null) {
             var shouldReport = false;
             var partOverrideToken = partUnit.languageVersionToken;
@@ -1102,7 +1102,7 @@
     _resolveDirectives(
       enclosingFile: enclosingFile,
       fileKind: includedKind,
-      fileElement: partElementUri.unit,
+      fileElement: partElementUri.libraryFragment,
     );
   }
 
diff --git a/pkg/analyzer/lib/src/dart/element/display_string_builder.dart b/pkg/analyzer/lib/src/dart/element/display_string_builder.dart
index 155cf2c..34775fe 100644
--- a/pkg/analyzer/lib/src/dart/element/display_string_builder.dart
+++ b/pkg/analyzer/lib/src/dart/element/display_string_builder.dart
@@ -433,7 +433,7 @@
 
   void _writeDirectiveUri(DirectiveUri uri) {
     if (uri is DirectiveUriWithUnitImpl) {
-      _write('unit ${uri.unit.source.uri}');
+      _write('unit ${uri.libraryFragment.source.uri}');
     } else if (uri is DirectiveUriWithSourceImpl) {
       _write('source ${uri.source}');
     } else {
diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart
index 6d0349c..fdd953e 100644
--- a/pkg/analyzer/lib/src/dart/element/element.dart
+++ b/pkg/analyzer/lib/src/dart/element/element.dart
@@ -688,10 +688,7 @@
 
 /// A concrete implementation of [LibraryFragment].
 class CompilationUnitElementImpl extends UriReferencedElementImpl
-    implements
-        // ignore:deprecated_member_use_from_same_package,analyzer_use_new_elements
-        CompilationUnitElement,
-        LibraryFragment {
+    implements LibraryFragment {
   /// The source that corresponds to this compilation unit.
   @override
   final Source source;
@@ -914,7 +911,7 @@
   @override
   ElementKind get kind => ElementKind.COMPILATION_UNIT;
 
-  @override
+  /// The libraries exported by this unit.
   List<LibraryExportElementImpl> get libraryExports {
     linkedData?.read(this);
     return _libraryExports;
@@ -938,12 +935,14 @@
   @override
   LibraryFragment get libraryFragment => this;
 
-  @override
+  /// The prefixes used by [libraryImports].
+  ///
+  /// Each prefix can be used in more than one `import` directive.
   List<PrefixElementImpl> get libraryImportPrefixes {
     return _libraryImportPrefixes ??= _buildLibraryImportPrefixes();
   }
 
-  @override
+  /// The libraries imported by this unit.
   List<LibraryImportElementImpl> get libraryImports {
     linkedData?.read(this);
     return _libraryImports;
@@ -1018,7 +1017,7 @@
   @override
   List<PartInclude> get partIncludes => parts.cast<PartInclude>();
 
-  @override
+  /// The parts included by this unit.
   List<PartElementImpl> get parts => _parts;
 
   set parts(List<PartElementImpl> parts) {
@@ -1026,7 +1025,7 @@
       part.enclosingElement3 = this;
       var uri = part.uri;
       if (uri is DirectiveUriWithUnitImpl) {
-        uri.unit.enclosingElement3 = this;
+        uri.libraryFragment.enclosingElement3 = this;
       }
     }
     _parts = parts;
@@ -1907,19 +1906,16 @@
 class DirectiveUriWithUnitImpl extends DirectiveUriWithRelativeUriImpl
     implements DirectiveUriWithUnit {
   @override
-  final CompilationUnitElementImpl unit;
+  final CompilationUnitElementImpl libraryFragment;
 
   DirectiveUriWithUnitImpl({
     required super.relativeUriString,
     required super.relativeUri,
-    required this.unit,
+    required this.libraryFragment,
   });
 
   @override
-  CompilationUnitElementImpl get libraryFragment => unit;
-
-  @override
-  Source get source => unit.source;
+  Source get source => libraryFragment.source;
 }
 
 /// The synthetic element representing the declaration of the type `dynamic`.
@@ -6568,7 +6564,7 @@
   AnalysisSessionImpl session;
 
   /// The compilation unit that defines this library.
-  late CompilationUnitElementImpl _definingCompilationUnit;
+  late CompilationUnitElementImpl definingCompilationUnit;
 
   /// The language version for the library.
   LibraryLanguageVersion? _languageVersion;
@@ -6673,16 +6669,6 @@
   }
 
   @override
-  CompilationUnitElementImpl get definingCompilationUnit =>
-      _definingCompilationUnit;
-
-  /// Set the compilation unit that defines this library to the given
-  ///  compilation[unit].
-  set definingCompilationUnit(CompilationUnitElementImpl unit) {
-    _definingCompilationUnit = unit;
-  }
-
-  @override
   Null get enclosingElement2 => null;
 
   @override
@@ -6690,7 +6676,7 @@
 
   @override
   CompilationUnitElementImpl get enclosingUnit {
-    return _definingCompilationUnit;
+    return definingCompilationUnit;
   }
 
   @override
@@ -6765,7 +6751,7 @@
   @override
   List<CompilationUnitElementImpl> get fragments {
     return [
-      _definingCompilationUnit,
+      definingCompilationUnit,
       ..._partUnits,
     ];
   }
@@ -6790,7 +6776,7 @@
   }
 
   @override
-  String get identifier => '${_definingCompilationUnit.source.uri}';
+  String get identifier => '${definingCompilationUnit.source.uri}';
 
   @override
   List<LibraryElementImpl> get importedLibraries {
@@ -6890,7 +6876,7 @@
 
   @override
   Source get source {
-    return _definingCompilationUnit.source;
+    return definingCompilationUnit.source;
   }
 
   @override
@@ -6908,10 +6894,13 @@
     }
   }
 
-  @override
+  /// The compilation units this library consists of.
+  ///
+  /// This includes the defining compilation unit and units included using the
+  /// `part` directive.
   List<CompilationUnitElementImpl> get units {
     return [
-      _definingCompilationUnit,
+      definingCompilationUnit,
       ..._partUnits,
     ];
   }
@@ -6925,7 +6914,7 @@
     void visitParts(CompilationUnitElementImpl unit) {
       for (var part in unit.parts) {
         if (part.uri case DirectiveUriWithUnitImpl uri) {
-          var unit = uri.unit;
+          var unit = uri.libraryFragment;
           result.add(unit);
           visitParts(unit);
         }
diff --git a/pkg/analyzer/lib/src/summary2/bundle_reader.dart b/pkg/analyzer/lib/src/summary2/bundle_reader.dart
index a735a25..c6249c8 100644
--- a/pkg/analyzer/lib/src/summary2/bundle_reader.dart
+++ b/pkg/analyzer/lib/src/summary2/bundle_reader.dart
@@ -842,7 +842,7 @@
         return DirectiveUriWithUnitImpl(
           relativeUriString: parent.relativeUriString,
           relativeUri: parent.relativeUri,
-          unit: unitElement,
+          libraryFragment: unitElement,
         );
       case DirectiveUriKind.withSource:
         return readWithSource();
diff --git a/pkg/analyzer/lib/src/summary2/bundle_writer.dart b/pkg/analyzer/lib/src/summary2/bundle_writer.dart
index c1e841f..430ae87 100644
--- a/pkg/analyzer/lib/src/summary2/bundle_writer.dart
+++ b/pkg/analyzer/lib/src/summary2/bundle_writer.dart
@@ -196,7 +196,7 @@
     } else if (element is DirectiveUriWithUnitImpl) {
       _sink.writeByte(DirectiveUriKind.withUnit.index);
       writeWithSource(element);
-      _writeUnitElement(element.unit);
+      _writeUnitElement(element.libraryFragment);
     } else if (element is DirectiveUriWithSource) {
       _sink.writeByte(DirectiveUriKind.withSource.index);
       writeWithSource(element);
diff --git a/pkg/analyzer/lib/src/summary2/library_builder.dart b/pkg/analyzer/lib/src/summary2/library_builder.dart
index 56ef4f7..f27e89a 100644
--- a/pkg/analyzer/lib/src/summary2/library_builder.dart
+++ b/pkg/analyzer/lib/src/summary2/library_builder.dart
@@ -693,7 +693,7 @@
           directiveUri = DirectiveUriWithUnitImpl(
             relativeUriString: state.selectedUri.relativeUriStr,
             relativeUri: state.selectedUri.relativeUri,
-            unit: unitElement,
+            libraryFragment: unitElement,
           );
         } else {
           directiveUri = DirectiveUriWithSourceImpl(