[vm/aot/tfa] Fix crash while getting static type context for signature shaking

Fixes https://github.com/flutter/flutter/issues/56479

Change-Id: I3e308382bf64b1b1aa39174c2095ee7a465907a4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/146980
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Commit-Queue: Alexander Markov <alexmarkov@google.com>
diff --git a/pkg/vm/lib/transformations/type_flow/transformer.dart b/pkg/vm/lib/transformations/type_flow/transformer.dart
index 65d3abc..b16f5c1 100644
--- a/pkg/vm/lib/transformations/type_flow/transformer.dart
+++ b/pkg/vm/lib/transformations/type_flow/transformer.dart
@@ -1001,12 +1001,12 @@
 
   @override
   TreeNode defaultMember(Member node) {
+    currentMember = node;
     if (shaker.isMemberBodyReachable(node)) {
       if (kPrintTrace) {
         tracePrint("Visiting $node");
       }
       shaker.addUsedMember(node);
-      currentMember = node;
       node.transformChildren(this);
     } else if (shaker.isMemberReferencedFromNativeCode(node)) {
       // Preserve members referenced from native code to satisfy lookups, even
@@ -1015,11 +1015,13 @@
       // its enclosing class are allocated.
       shaker.addUsedMember(node);
     }
+    currentMember = null;
     return node;
   }
 
   @override
   TreeNode visitField(Field node) {
+    currentMember = node;
     if (shaker.retainField(node)) {
       if (kPrintTrace) {
         tracePrint("Visiting $node");
@@ -1037,6 +1039,7 @@
       // used as an instance call target.
       fieldMorpher.adjustInstanceCallTarget(node, isSetter: true);
     }
+    currentMember = null;
     return node;
   }
 
diff --git a/tests/language_2/vm/regress_flutter_56479_test.dart b/tests/language_2/vm/regress_flutter_56479_test.dart
new file mode 100644
index 0000000..87e3a70
--- /dev/null
+++ b/tests/language_2/vm/regress_flutter_56479_test.dart
@@ -0,0 +1,19 @@
+// Copyright (c) 2020, the Dart project authors.  Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+// Verifies that optional parameters can be transformed in field initializers.
+// Regression test for https://github.com/flutter/flutter/issues/56479.
+
+import "package:expect/expect.dart";
+
+int bar({int x = 2}) => x + 1;
+
+class A {
+  static int foo = bar(x: 42);
+}
+
+main() {
+  Expect.equals(5, bar(x: 4));
+  Expect.equals(43, A.foo);
+}