[dart2js] Remove dependency of ir.StaticTypeContext from element map constant query.

Change-Id: I1ef1d6699af7284b921c00c0faa89f2728f8f38e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/348880
Reviewed-by: Mayank Patke <fishythefish@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
diff --git a/pkg/compiler/lib/src/deferred_load/entity_data_info.dart b/pkg/compiler/lib/src/deferred_load/entity_data_info.dart
index ac1d009..6013d5f 100644
--- a/pkg/compiler/lib/src/deferred_load/entity_data_info.dart
+++ b/pkg/compiler/lib/src/deferred_load/entity_data_info.dart
@@ -3,7 +3,6 @@
 // BSD-style license that can be found in the LICENSE file.
 
 import 'package:kernel/ast.dart' as ir;
-import 'package:kernel/type_environment.dart' as ir;
 
 import 'entity_data.dart';
 
@@ -446,9 +445,8 @@
 class ConstantCollector extends ir.RecursiveVisitor {
   final KernelToElementMap elementMap;
   final EntityDataInfoBuilder infoBuilder;
-  final ir.StaticTypeContext staticTypeContext;
 
-  ConstantCollector(this.elementMap, this.staticTypeContext, this.infoBuilder);
+  ConstantCollector(this.elementMap, this.infoBuilder);
 
   CommonElements get commonElements => elementMap.commonElements;
 
@@ -460,8 +458,7 @@
     // Fetch the internal node in order to skip annotations on the member.
     // TODO(sigmund): replace this pattern when the kernel-ast provides a better
     // way to skip annotations (issue 31565).
-    var visitor = ConstantCollector(
-        elementMap, elementMap.getStaticTypeContext(member), infoBuilder);
+    var visitor = ConstantCollector(elementMap, infoBuilder);
     if (node is ir.Field) {
       node.initializer?.accept(visitor);
       return;
@@ -474,9 +471,8 @@
   }
 
   void add(ir.Expression node, {bool requireConstant = true}) {
-    ConstantValue? constant = elementMap.getConstantValue(
-        staticTypeContext, node,
-        requireConstant: requireConstant);
+    ConstantValue? constant =
+        elementMap.getConstantValue(node, requireConstant: requireConstant);
     if (constant != null) {
       infoBuilder.addConstant(constant,
           import: elementMap.getImport(getDeferredImport(node)));
diff --git a/pkg/compiler/lib/src/js_backend/field_analysis.dart b/pkg/compiler/lib/src/js_backend/field_analysis.dart
index 3cabe5e..27f5b56 100644
--- a/pkg/compiler/lib/src/js_backend/field_analysis.dart
+++ b/pkg/compiler/lib/src/js_backend/field_analysis.dart
@@ -3,7 +3,6 @@
 // BSD-style license that can be found in the LICENSE file.
 
 import 'package:kernel/ast.dart' as ir;
-import 'package:kernel/type_environment.dart' as ir;
 
 import '../common.dart';
 import '../constants/values.dart';
@@ -61,8 +60,7 @@
               expression.target, _elementMap.coreTypes.createSentinelMethod)) {
         value = LateSentinelConstantValue();
       } else {
-        value = _elementMap.getConstantValue(
-            _elementMap.getStaticTypeContext(fieldElement), expression,
+        value = _elementMap.getConstantValue(expression,
             requireConstant: false, implicitNull: true);
       }
       if (value != null && value.isConstant) {
@@ -73,8 +71,6 @@
     for (ir.Constructor constructor in classNode.constructors) {
       JConstructor constructorElement =
           _elementMap.getConstructor(constructor) as JConstructor;
-      ir.StaticTypeContext staticTypeContext =
-          _elementMap.getStaticTypeContext(constructorElement);
       constructors.add(constructorElement);
       for (ir.Initializer initializer in constructor.initializers) {
         if (initializer is ir.FieldInitializer) {
@@ -89,8 +85,7 @@
 
           Initializer initializerValue = const Initializer.complex();
           ir.Expression value = initializer.value;
-          ConstantValue? constantValue = _elementMap.getConstantValue(
-              staticTypeContext, value,
+          ConstantValue? constantValue = _elementMap.getConstantValue(value,
               requireConstant: false, implicitNull: true);
           if (constantValue != null && constantValue.isConstant) {
             initializerValue = Initializer.direct(constantValue);
@@ -101,8 +96,9 @@
             if (position != -1) {
               if (position >= constructor.function.requiredParameterCount) {
                 constantValue = _elementMap.getConstantValue(
-                    staticTypeContext, parameter.initializer,
-                    requireConstant: false, implicitNull: true);
+                    parameter.initializer,
+                    requireConstant: false,
+                    implicitNull: true);
                 if (constantValue != null && constantValue.isConstant) {
                   initializerValue =
                       Initializer.positional(position, constantValue);
@@ -113,8 +109,9 @@
                   constructor.function.namedParameters.indexOf(parameter);
               if (position != -1) {
                 constantValue = _elementMap.getConstantValue(
-                    staticTypeContext, parameter.initializer,
-                    requireConstant: false, implicitNull: true);
+                    parameter.initializer,
+                    requireConstant: false,
+                    implicitNull: true);
                 if (constantValue != null && constantValue.isConstant) {
                   initializerValue =
                       Initializer.named(parameter.name, constantValue);
@@ -138,8 +135,7 @@
             expression.target, _elementMap.coreTypes.createSentinelMethod)) {
       value = LateSentinelConstantValue();
     } else {
-      value = _elementMap.getConstantValue(
-          _elementMap.getStaticTypeContext(field), expression,
+      value = _elementMap.getConstantValue(expression,
           requireConstant: node.isConst, implicitNull: true);
     }
     if (value != null && !value.isConstant) {
diff --git a/pkg/compiler/lib/src/kernel/element_map_impl.dart b/pkg/compiler/lib/src/kernel/element_map_impl.dart
index 6d980bf..5a3cb66 100644
--- a/pkg/compiler/lib/src/kernel/element_map_impl.dart
+++ b/pkg/compiler/lib/src/kernel/element_map_impl.dart
@@ -803,12 +803,6 @@
   ir.ClassHierarchy get classHierarchy =>
       _classHierarchy ??= ir.ClassHierarchy(env.mainComponent, coreTypes);
 
-  ir.StaticTypeContext getStaticTypeContext(MemberEntity member) {
-    // TODO(johnniwinther): Cache the static type context.
-    return ir.StaticTypeContext(
-        getMemberNode(member as JMember), typeEnvironment);
-  }
-
   @override
   Name getName(ir.Name name, {bool setter = false}) {
     return Name(name.text, name.isPrivate ? name.library!.importUri : null,
@@ -1076,9 +1070,8 @@
     return null;
   }
 
-  /// Computes the [ConstantValue] for the constant [expression].
-  ConstantValue? getConstantValue(
-      ir.StaticTypeContext staticTypeContext, ir.Expression? node,
+  /// Computes the [ConstantValue] for the constant [node].
+  ConstantValue? getConstantValue(ir.Expression? node,
       {bool requireConstant = true,
       bool implicitNull = false,
       bool checkCasts = true}) {
@@ -1107,15 +1100,13 @@
   }
 
   /// Converts [annotations] into a list of [ConstantValue]s.
-  List<ConstantValue> getMetadata(
-      ir.StaticTypeContext staticTypeContext, List<ir.Expression> annotations) {
+  List<ConstantValue> getMetadata(List<ir.Expression> annotations) {
     if (annotations.isEmpty) return const <ConstantValue>[];
     List<ConstantValue> metadata = <ConstantValue>[];
     annotations.forEach((ir.Expression node) {
       // We skip the implicit cast checks for metadata to avoid circular
       // dependencies in the js-interop class registration.
-      metadata
-          .add(getConstantValue(staticTypeContext, node, checkCasts: false)!);
+      metadata.add(getConstantValue(node, checkCasts: false)!);
     });
     return metadata;
   }
diff --git a/pkg/compiler/lib/src/kernel/env.dart b/pkg/compiler/lib/src/kernel/env.dart
index c008b57..fbeda31 100644
--- a/pkg/compiler/lib/src/kernel/env.dart
+++ b/pkg/compiler/lib/src/kernel/env.dart
@@ -6,7 +6,6 @@
 
 import 'package:js_shared/variance.dart';
 import 'package:kernel/ast.dart' as ir;
-import 'package:kernel/type_environment.dart' as ir;
 import 'package:collection/collection.dart' show mergeSort; // a stable sort.
 
 import '../common.dart';
@@ -165,10 +164,7 @@
   KLibraryData(this.library);
 
   Iterable<ConstantValue> getMetadata(KernelToElementMap elementMap) {
-    return _metadata ??= elementMap.getMetadata(
-        ir.StaticTypeContext.forAnnotations(
-            library, elementMap.typeEnvironment),
-        library.annotations);
+    return _metadata ??= elementMap.getMetadata(library.annotations);
   }
 
   Iterable<ImportEntity> getImports(KernelToElementMap elementMap) {
@@ -436,10 +432,7 @@
   bool isCallTypeComputed = false;
 
   Iterable<ConstantValue> getMetadata(covariant KernelToElementMap elementMap) {
-    return _metadata ??= elementMap.getMetadata(
-        ir.StaticTypeContext.forAnnotations(
-            node.enclosingLibrary, elementMap.typeEnvironment),
-        node.annotations);
+    return _metadata ??= elementMap.getMetadata(node.annotations);
   }
 
   List<Variance> getVariances() =>
@@ -460,9 +453,7 @@
   KMemberData(this.node);
 
   Iterable<ConstantValue> getMetadata(covariant KernelToElementMap elementMap) {
-    return _metadata ??= elementMap.getMetadata(
-        ir.StaticTypeContext(node, elementMap.typeEnvironment),
-        node.annotations);
+    return _metadata ??= elementMap.getMetadata(node.annotations);
   }
 
   InterfaceType? getMemberThisType(JsToElementMap elementMap) {
diff --git a/pkg/compiler/test/model/cfe_constant_test.dart b/pkg/compiler/test/model/cfe_constant_test.dart
index ada5ce6..f54bee4 100644
--- a/pkg/compiler/test/model/cfe_constant_test.dart
+++ b/pkg/compiler/test/model/cfe_constant_test.dart
@@ -82,9 +82,7 @@
   String? computeNodeValue(Id id, ir.TreeNode node) {
     if (node is ir.ConstantExpression) {
       return constantToText(
-          elementMap.types,
-          elementMap.getConstantValue(
-              elementMap.getStaticTypeContext(member), node)!);
+          elementMap.types, elementMap.getConstantValue(node)!);
     }
     return null;
   }