Observatory strong mode fixes: make callback parameter types more general.

This CL addresses a code pattern where a method expects its parameter
to have a certain type, but that method is torn off and passed as a
callback to another method expecting its parameter type to be more
general.  For example:

    void f(int i) { ... }
    void g(void callback(Object o)) { ... }
    void h() {
      g(f); // Error: () -> int is not a subtype of () -> Object
    }

This is a strong mode error because the type system cannot guarantee
that the value pased to f will be an int.  The solution is to broaden
the type of the callback parameter so that it matches the type
expected for the callback.  In most cases, we insert an implicit
downcast (by reassigning the parameter to a local variable with the
expected type), which in Dart 2.0 semantics will result in a runtime
check (similar to what happens in Dart 1.0 checked mode).

Since the downcasts are implicit, the Dart 1.0 semantics are
unchanged, so this should be a safe change.

Change-Id: I9583ea194343b89b39305c9796cfad299a47943f
Reviewed-on: https://dart-review.googlesource.com/55907
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Ryan Macnak <rmacnak@google.com>
diff --git a/runtime/observatory/bin/shell.dart b/runtime/observatory/bin/shell.dart
index ae4e7ff..a0cb274 100644
--- a/runtime/observatory/bin/shell.dart
+++ b/runtime/observatory/bin/shell.dart
@@ -29,7 +29,8 @@
 
 void main() {
   String addr = 'ws://localhost:8181/ws';
-  new WebSocketVM(new WebSocketVMTarget(addr)).load().then((VM vm) {
+  new WebSocketVM(new WebSocketVMTarget(addr)).load().then((serviceObject) {
+    VM vm = serviceObject;
     Isolate isolate = vm.isolates.first;
     repl(vm, isolate, 'isolate ${isolate.id}');
   });
diff --git a/runtime/observatory/lib/src/app/application.dart b/runtime/observatory/lib/src/app/application.dart
index 8ea5b0a..79e85ef 100644
--- a/runtime/observatory/lib/src/app/application.dart
+++ b/runtime/observatory/lib/src/app/application.dart
@@ -20,7 +20,7 @@
   VM _vm;
   VM get vm => _vm;
 
-  static bool isConnectedVMTarget(WebSocketVMTarget target) {
+  static bool isConnectedVMTarget(M.Target target) {
     if (app._vm is CommonWebSocketVM) {
       if ((app._vm as CommonWebSocketVM).target == target) {
         return app._vm.isConnected;
diff --git a/runtime/observatory/lib/src/app/page.dart b/runtime/observatory/lib/src/app/page.dart
index 67fcc89..248cacc 100644
--- a/runtime/observatory/lib/src/app/page.dart
+++ b/runtime/observatory/lib/src/app/page.dart
@@ -170,7 +170,8 @@
       app.locationManager.go(Uris.vmConnect());
       return;
     }
-    app.vm.reload().then((VM vm) {
+    app.vm.reload().then((serviceObject) {
+      VM vm = serviceObject;
       container.children = [
         new VMViewElement(vm, _vmrepository, app.events, app.notifications,
             new IsolateRepository(app.vm), _scriptRepository,
@@ -706,7 +707,8 @@
       return;
     }
     final editor = getEditor(uri);
-    app.vm.reload().then((VM vm) async {
+    app.vm.reload().then((serviceObject) async {
+      VM vm = serviceObject;
       // Preload all isolates to avoid sorting problems.
       await Future.wait(vm.isolates.map((i) => i.load()));
       container.children = [
diff --git a/runtime/observatory/lib/src/elements/allocation_profile.dart b/runtime/observatory/lib/src/elements/allocation_profile.dart
index 5fb89b4..f11e63a 100644
--- a/runtime/observatory/lib/src/elements/allocation_profile.dart
+++ b/runtime/observatory/lib/src/elements/allocation_profile.dart
@@ -455,7 +455,8 @@
     _r.dirty();
   }
 
-  void _updateCollectionLine(Element e, M.ClassHeapStats item, index) {
+  void _updateCollectionLine(Element e, itemDynamic, index) {
+    M.ClassHeapStats item = itemDynamic;
     e.children[0].text = Utils.formatSize(_getAccumulatedSize(item));
     e.children[1].text = '${_getAccumulatedInstances(item)}';
     e.children[2].text = Utils.formatSize(_getCurrentSize(item));
diff --git a/runtime/observatory/lib/src/elements/class_tree.dart b/runtime/observatory/lib/src/elements/class_tree.dart
index 2a74a06..904014c 100644
--- a/runtime/observatory/lib/src/elements/class_tree.dart
+++ b/runtime/observatory/lib/src/elements/class_tree.dart
@@ -188,7 +188,8 @@
     return children;
   }
 
-  Iterable<M.Class> _children(M.Class cls) {
+  Iterable<M.Class> _children(classDynamic) {
+    M.Class cls = classDynamic;
     return _subclasses[cls.id];
   }
 }
diff --git a/runtime/observatory/lib/src/elements/containers/virtual_collection.dart b/runtime/observatory/lib/src/elements/containers/virtual_collection.dart
index b9bea3e..810de3a 100644
--- a/runtime/observatory/lib/src/elements/containers/virtual_collection.dart
+++ b/runtime/observatory/lib/src/elements/containers/virtual_collection.dart
@@ -246,7 +246,7 @@
     }
   }
 
-  Iterable<dynamic> _doSearch(String search) {
+  Iterable<dynamic> _doSearch(Pattern search) {
     return _items.where((item) => _search(search, item));
   }
 }
diff --git a/runtime/observatory/lib/src/elements/cpu_profile/virtual_tree.dart b/runtime/observatory/lib/src/elements/cpu_profile/virtual_tree.dart
index 559a8988..b324ace 100644
--- a/runtime/observatory/lib/src/elements/cpu_profile/virtual_tree.dart
+++ b/runtime/observatory/lib/src/elements/cpu_profile/virtual_tree.dart
@@ -188,7 +188,10 @@
       ];
   }
 
-  static _getChildren(M.CallTreeNode node) => node.children;
+  static _getChildren(nodeDynamic) {
+    M.CallTreeNode node = nodeDynamic;
+    return node.children;
+  }
 
   static const String _expandedIcon = '▼';
   static const String _collapsedIcon = '►';
diff --git a/runtime/observatory/lib/src/elements/cpu_profile_table.dart b/runtime/observatory/lib/src/elements/cpu_profile_table.dart
index d140e3c..3e141a1 100644
--- a/runtime/observatory/lib/src/elements/cpu_profile_table.dart
+++ b/runtime/observatory/lib/src/elements/cpu_profile_table.dart
@@ -222,7 +222,8 @@
     return element;
   }
 
-  void _updateFunction(Element e, M.ProfileFunction item, int index) {
+  void _updateFunction(Element e, itemDynamic, int index) {
+    M.ProfileFunction item = itemDynamic;
     if (item == _selected) {
       e.classes = ['function-item', 'selected'];
     } else {
diff --git a/runtime/observatory/lib/src/elements/heap_map.dart b/runtime/observatory/lib/src/elements/heap_map.dart
index 6e313e8..6d57f77 100644
--- a/runtime/observatory/lib/src/elements/heap_map.dart
+++ b/runtime/observatory/lib/src/elements/heap_map.dart
@@ -289,9 +289,8 @@
     if (gc != null) {
       params['gc'] = gc;
     }
-    return isolate
-        .invokeRpc('_getHeapMap', params)
-        .then((S.ServiceMap response) {
+    return isolate.invokeRpc('_getHeapMap', params).then((serviceObject) {
+      S.ServiceMap response = serviceObject;
       assert(response['type'] == 'HeapMap');
       _fragmentation = response;
       _updateFragmentationData();
diff --git a/runtime/observatory/lib/src/elements/heap_snapshot.dart b/runtime/observatory/lib/src/elements/heap_snapshot.dart
index 59296ec..52c53a8 100644
--- a/runtime/observatory/lib/src/elements/heap_snapshot.dart
+++ b/runtime/observatory/lib/src/elements/heap_snapshot.dart
@@ -397,7 +397,8 @@
   static const int kMaxChildren = 100;
   static const int kMinRetainedSize = 4096;
 
-  static Iterable _getChildrenDominator(M.HeapSnapshotDominatorNode node) {
+  static Iterable _getChildrenDominator(nodeDynamic) {
+    M.HeapSnapshotDominatorNode node = nodeDynamic;
     final list = node.children.toList();
     list.sort((a, b) => b.retainedSize - a.retainedSize);
     return list
@@ -405,8 +406,8 @@
         .take(kMaxChildren);
   }
 
-  static Iterable _getChildrenMergedDominator(
-      M.HeapSnapshotMergedDominatorNode node) {
+  static Iterable _getChildrenMergedDominator(nodeDynamic) {
+    M.HeapSnapshotMergedDominatorNode node = nodeDynamic;
     final list = node.children.toList();
     list.sort((a, b) => b.retainedSize - a.retainedSize);
     return list
diff --git a/runtime/observatory/lib/src/elements/memory/allocations.dart b/runtime/observatory/lib/src/elements/memory/allocations.dart
index 329e6ba..64f9595 100644
--- a/runtime/observatory/lib/src/elements/memory/allocations.dart
+++ b/runtime/observatory/lib/src/elements/memory/allocations.dart
@@ -221,7 +221,8 @@
     _r.dirty();
   }
 
-  void _updateCollectionLine(Element e, M.ClassHeapStats item, index) {
+  void _updateCollectionLine(Element e, itemDynamic, index) {
+    M.ClassHeapStats item = itemDynamic;
     e.children[0].text = Utils.formatSize(_getAccumulatedSize(item));
     e.children[1].text = '${_getAccumulatedInstances(item)}';
     e.children[2].text = Utils.formatSize(_getCurrentSize(item));
diff --git a/runtime/observatory/lib/src/elements/memory/snapshot.dart b/runtime/observatory/lib/src/elements/memory/snapshot.dart
index fd91ebc..d6adcf3e 100644
--- a/runtime/observatory/lib/src/elements/memory/snapshot.dart
+++ b/runtime/observatory/lib/src/elements/memory/snapshot.dart
@@ -186,7 +186,8 @@
   static const int kMaxChildren = 100;
   static const int kMinRetainedSize = 4096;
 
-  static Iterable _getChildrenDominator(M.HeapSnapshotDominatorNode node) {
+  static Iterable _getChildrenDominator(nodeDynamic) {
+    M.HeapSnapshotDominatorNode node = nodeDynamic;
     final list = node.children.toList();
     list.sort((a, b) => b.retainedSize - a.retainedSize);
     return list
diff --git a/runtime/observatory/lib/utils.dart b/runtime/observatory/lib/utils.dart
index 4957014..4167fb5 100644
--- a/runtime/observatory/lib/utils.dart
+++ b/runtime/observatory/lib/utils.dart
@@ -96,7 +96,8 @@
     }
   }
 
-  static String formatSize(int bytes) {
+  static String formatSize(bytesDynamic) {
+    int bytes = bytesDynamic;
     const int digits = 1;
     const int bytesPerKB = 1024;
     const int bytesPerMB = 1024 * bytesPerKB;