[cfe] Remove ConstantEvaluator.libraryOf

Instead of finding the current library through the parents relation
we use the static type context which already had access to the
library.

Change-Id: Ia2116160b24b701ffa0794b9ccc9a59506c3adc0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/288840
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
diff --git a/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart b/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart
index 4f943d0..f991751 100644
--- a/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart
@@ -1129,7 +1129,7 @@
                   patternGuards[error.index].fileOffset,
                   messageUnreachableSwitchCase));
         } else if (error is NonExhaustiveError && !hasDefault) {
-          Library library = constantEvaluator.libraryOf(node);
+          Library library = _staticTypeContext!.enclosingLibrary;
           constantEvaluator.errorReporter.report(
               constantEvaluator.createLocatedMessageWithOffset(
                   node,
@@ -1152,7 +1152,7 @@
       SwitchStatement node, TreeNode? removalSentinel) {
     //return _handleExhaustiveness(node, node, () {
     TreeNode result = super.visitSwitchStatement(node, removalSentinel);
-    Library library = constantEvaluator.libraryOf(node);
+    Library library = _staticTypeContext!.enclosingLibrary;
     // ignore: unnecessary_null_comparison
     if (library != null) {
       for (SwitchCase switchCase in node.cases) {
@@ -4197,8 +4197,9 @@
 
   @override
   Constant visitSymbolLiteral(SymbolLiteral node) {
-    final Reference? libraryReference =
-        node.value.startsWith('_') ? libraryOf(node).reference : null;
+    final Reference? libraryReference = node.value.startsWith('_')
+        ? _staticTypeContext!.enclosingLibrary.reference
+        : null;
     return canonicalize(new SymbolConstant(node.value, libraryReference));
   }
 
@@ -4613,17 +4614,6 @@
         templateNotConstantExpression.withArguments("Binary '$op' operation"));
   }
 
-  // TODO(johnniwinther): Remove the need for this by adding a current library
-  // field.
-  Library libraryOf(TreeNode? node) {
-    // The tree structure of the kernel AST ensures we always have an enclosing
-    // library.
-    while (true) {
-      if (node is Library) return node;
-      node = node!.parent;
-    }
-  }
-
   @override
   Constant defaultBasicLiteral(BasicLiteral node) => defaultExpression(node);
 
diff --git a/pkg/kernel/lib/type_environment.dart b/pkg/kernel/lib/type_environment.dart
index 85df905..caeee30 100644
--- a/pkg/kernel/lib/type_environment.dart
+++ b/pkg/kernel/lib/type_environment.dart
@@ -546,6 +546,9 @@
   /// The static type of a `this` expression.
   InterfaceType? get thisType;
 
+  /// The enclosing library of this context.
+  Library get enclosingLibrary;
+
   /// Creates a static type context for computing static types in the body
   /// of [member].
   factory StaticTypeContext(Member member, TypeEnvironment typeEnvironment,
@@ -625,6 +628,9 @@
       {StaticTypeCache? cache})
       : this.direct(library, typeEnvironment, cache: cache);
 
+  @override
+  Library get enclosingLibrary => _library;
+
   /// The [Nullability] used for non-nullable types.
   ///
   /// For opt out libraries this is [Nullability.legacy].
@@ -729,6 +735,9 @@
   _FlatStatefulStaticTypeContext(TypeEnvironment typeEnvironment)
       : super._internal(typeEnvironment);
 
+  @override
+  Library get enclosingLibrary => _library;
+
   Library get _library {
     Library? library = _currentLibrary ?? _currentMember?.enclosingLibrary;
     assert(library != null,
@@ -834,6 +843,9 @@
   _StackedStatefulStaticTypeContext(TypeEnvironment typeEnvironment)
       : super._internal(typeEnvironment);
 
+  @override
+  Library get enclosingLibrary => _library;
+
   Library get _library {
     assert(_contextStack.isNotEmpty,
         "No library currently associated with StaticTypeContext.");