[vm/aot] Set type context for fields in signature shaker.

Fixes https://github.com/dart-lang/sdk/issues/45642

TEST=Regression test added.
Change-Id: Ib14b27674810ac22aad21c19647e68e7ad32ceaa
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/194762
Auto-Submit: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Alexander Markov <alexmarkov@google.com>
diff --git a/pkg/vm/lib/transformations/type_flow/signature_shaking.dart b/pkg/vm/lib/transformations/type_flow/signature_shaking.dart
index 20a0412..c6c7608 100644
--- a/pkg/vm/lib/transformations/type_flow/signature_shaking.dart
+++ b/pkg/vm/lib/transformations/type_flow/signature_shaking.dart
@@ -498,6 +498,12 @@
     super.visitProcedure(node);
   }
 
+  @override
+  void visitField(Field node) {
+    typeContext = StaticTypeContext(node, shaker.typeFlowAnalysis.environment);
+    super.visitField(node);
+  }
+
   static void forEachArgumentRev(Arguments args, _ProcedureInfo info,
       void Function(Expression, _ParameterInfo) fun) {
     for (int i = args.named.length - 1; i >= 0; i--) {
diff --git a/tests/language/regress/regress45642_test.dart b/tests/language/regress/regress45642_test.dart
new file mode 100644
index 0000000..9222e13
--- /dev/null
+++ b/tests/language/regress/regress45642_test.dart
@@ -0,0 +1,15 @@
+// Copyright (c) 2021, 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.
+
+class Utils {
+  static final foo = Foo(() {});
+}
+
+class Foo {
+  Foo(func);
+}
+
+void main() {
+  print(Utils.foo); // should print: Instance of 'Foo'
+}
diff --git a/tests/language_2/regress/regress45642_test.dart b/tests/language_2/regress/regress45642_test.dart
new file mode 100644
index 0000000..9222e13
--- /dev/null
+++ b/tests/language_2/regress/regress45642_test.dart
@@ -0,0 +1,15 @@
+// Copyright (c) 2021, 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.
+
+class Utils {
+  static final foo = Foo(() {});
+}
+
+class Foo {
+  Foo(func);
+}
+
+void main() {
+  print(Utils.foo); // should print: Instance of 'Foo'
+}