Stop storing contextual information on VariableDeclaration nodes.

Instead of using InferenceContext.getContext and
InferenceContext.setType to associate type inference information with
VariableDeclaration nodes, we directly look up the variable type when
we need it.  This should be more efficient (since it avoids a map
lookup), and it should make the code easier to reason about (since it
makes the flow of data in the type inference process more explicit).

This is part of a larger effort to elimiate the use of
InferenceContext.getContext and InferenceContext.setType entirely.

Change-Id: I01262fb3c3e6789ee9da924e731d86d99ce3f4de
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/230946
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
diff --git a/pkg/analyzer/lib/src/dart/resolver/variable_declaration_resolver.dart b/pkg/analyzer/lib/src/dart/resolver/variable_declaration_resolver.dart
index ca56632..5cbf8db 100644
--- a/pkg/analyzer/lib/src/dart/resolver/variable_declaration_resolver.dart
+++ b/pkg/analyzer/lib/src/dart/resolver/variable_declaration_resolver.dart
@@ -43,13 +43,13 @@
     var isTopLevel =
         element is FieldElement || element is TopLevelVariableElement;
 
-    InferenceContext.setTypeFromNode(initializer, node);
     if (isTopLevel) {
       _resolver.flowAnalysis.topLevelDeclaration_enter(node, null);
     } else if (element.isLate) {
       _resolver.flowAnalysis.flow?.lateInitializer_begin(node);
     }
 
+    InferenceContext.setType(initializer, element.type);
     initializer.accept(_resolver);
     initializer = node.initializer!;
     var whyNotPromoted =
diff --git a/pkg/analyzer/lib/src/generated/resolver.dart b/pkg/analyzer/lib/src/generated/resolver.dart
index 3b38316..8cc62ed 100644
--- a/pkg/analyzer/lib/src/generated/resolver.dart
+++ b/pkg/analyzer/lib/src/generated/resolver.dart
@@ -143,7 +143,7 @@
   /// [TypeSystemImpl.lowerBoundForType] if you would prefer a known type
   /// that represents the bound of the context type.
   static DartType? getContext(AstNode? node) {
-    if (node is ArgumentList) {
+    if (node is ArgumentList || node is VariableDeclaration) {
       assert(false, 'Nodes of type ${node.runtimeType} should use context');
     }
     return node?.getProperty(_typeProperty);
@@ -152,7 +152,7 @@
   /// Attach contextual type information [type] to [node] for use during
   /// inference.
   static void setType(AstNode? node, DartType? type) {
-    if (node is ArgumentList) {
+    if (node is ArgumentList || node is VariableDeclaration) {
       assert(false, 'Nodes of type ${node.runtimeType} should use context');
     }
     if (type == null || type.isDynamic) {
@@ -2543,10 +2543,6 @@
   @override
   void visitVariableDeclarationList(VariableDeclarationList node) {
     flowAnalysis.variableDeclarationList(node);
-    for (VariableDeclaration decl in node.variables) {
-      VariableElement variableElement = decl.declaredElement!;
-      InferenceContext.setType(decl, variableElement.type);
-    }
     checkUnreachableNode(node);
     node.visitChildren(this);
     elementResolver.visitVariableDeclarationList(node);