[dart2js] Fix handling of type variable locals during inlining.

Even if a function doesn't have an RTI-need for type arguments, locals
for those type variables must still be updated with default values. Some
incorrect control flow caused _setupStateForInlining to skip all type
variable local setup when the function has no RTI-need for type
arguments.

Fixes: #48762
Change-Id: I5d770b6fcd563afa9454026e4b79eb021cf38a37
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/247609
Commit-Queue: Mayank Patke <fishythefish@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
diff --git a/pkg/compiler/lib/src/ssa/builder.dart b/pkg/compiler/lib/src/ssa/builder.dart
index af68bc6..41b6d82 100644
--- a/pkg/compiler/lib/src/ssa/builder.dart
+++ b/pkg/compiler/lib/src/ssa/builder.dart
@@ -669,10 +669,6 @@
 
   /// Extend current method parameters with parameters for the function type
   /// variables.
-  ///
-  /// TODO(johnniwinther): Do we need this?
-  /// If the method has type variables but does not need them, bind to `dynamic`
-  /// (represented as `null`).
   void _addFunctionTypeVariablesIfNeeded(MemberEntity member) {
     if (member is! FunctionEntity) return;
 
@@ -684,8 +680,7 @@
     }
     bool needsTypeArguments = _rtiNeed.methodNeedsTypeArguments(function);
     bool elideTypeParameters = function.parameterStructure.typeParameters == 0;
-    for (TypeVariableType typeVariable
-        in _elementEnvironment.getFunctionTypeVariables(function)) {
+    for (TypeVariableType typeVariable in typeVariables) {
       HInstruction param;
       bool erased = false;
       if (elideTypeParameters) {
@@ -6257,6 +6252,20 @@
       localsHandler.updateLocal(local, argument);
     });
 
+    bool hasTypeParameters = function.parameterStructure.typeParameters > 0;
+    bool needsTypeArguments = _rtiNeed.methodNeedsTypeArguments(function);
+    for (TypeVariableType typeVariable
+        in _elementEnvironment.getFunctionTypeVariables(function)) {
+      HInstruction argument;
+      if (hasTypeParameters && needsTypeArguments) {
+        argument = compiledArguments[argumentIndex++];
+      } else {
+        argument = _computeTypeArgumentDefaultValue(function, typeVariable);
+      }
+      localsHandler.updateLocal(
+          localsHandler.getTypeVariableAsLocal(typeVariable), argument);
+    }
+
     if (forGenerativeConstructorBody && scopeData.requiresContextBox) {
       HInstruction box = compiledArguments[argumentIndex++];
       assert(box is HCreateBox);
@@ -6278,22 +6287,7 @@
             localsHandler.getTypeVariableAsLocal(typeVariable), argument);
       });
     }
-    if (_rtiNeed.methodNeedsTypeArguments(function)) {
-      bool inlineTypeParameters =
-          function.parameterStructure.typeParameters == 0;
-      for (TypeVariableType typeVariable
-          in _elementEnvironment.getFunctionTypeVariables(function)) {
-        HInstruction argument;
-        if (inlineTypeParameters) {
-          // Add inlined type parameters.
-          argument = _computeTypeArgumentDefaultValue(function, typeVariable);
-        } else {
-          argument = compiledArguments[argumentIndex++];
-        }
-        localsHandler.updateLocal(
-            localsHandler.getTypeVariableAsLocal(typeVariable), argument);
-      }
-    }
+
     assert(
         argumentIndex == compiledArguments.length ||
             !_rtiNeed.methodNeedsTypeArguments(function) &&