Skip to content

Commit 6d88b04

Browse files
leafpetersencommit-bot@chromium.org
authored andcommitted
Fix incorrect instance checks.
Several instance checks added during the null safety core library migration were incorrectly rejecting null values in places where null values can still flow in legacy mode. This eliminates the instance checks in favor of casts which will behave as desired. Bug: #41465 Change-Id: I50fb9e6cef41d645d5312871932c7168f2d80c29 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/143333 Commit-Queue: Leaf Petersen <[email protected]> Reviewed-by: Nate Bosch <[email protected]>
1 parent aa3d4ef commit 6d88b04

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

sdk_nnbd/lib/async/future.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,8 @@ abstract class Future<T> {
223223
if (result is Future<T>) {
224224
return result;
225225
} else {
226-
if (result is! T)
227-
throw "unreachable"; // TODO(lrn): Remove when type promotion works.
228-
return new _Future<T>.value(result);
226+
// TODO(40014): Remove cast when type promotion works.
227+
return new _Future<T>.value(result as dynamic);
229228
}
230229
} catch (error, stackTrace) {
231230
var future = new _Future<T>();

sdk_nnbd/lib/async/stream.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,10 +523,8 @@ abstract class Stream<T> {
523523
subscription.pause();
524524
newValue.then(add, onError: addError).whenComplete(resume);
525525
} else {
526-
if (newValue is! E) {
527-
throw "unreachable"; // TODO(lrn): Remove when type promotion works.
528-
}
529-
controller.add(newValue);
526+
// TODO(40014): Remove cast when type promotion works.
527+
controller.add(newValue as dynamic);
530528
}
531529
});
532530
controller.onCancel = subscription.cancel;

tests/corelib/weak_mode/README

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This directory contains tests specific to null safety weak mode
2+
behavior.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// @dart = 2.7
6+
7+
import "dart:async";
8+
import "package:expect/expect.dart";
9+
10+
// Regression test for https://github.com/dart-lang/sdk/issues/41465
11+
12+
void main() {
13+
var stream = Stream.fromIterable([1, 2, 3, 4]);
14+
var eventsStream =
15+
stream.asyncMap((i) => i % 2 == 0 ? i : null).where((i) => i != null);
16+
eventsStream.listen(Expect.isNotNull);
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// @dart = 2.7
6+
7+
import "dart:async";
8+
import "package:expect/expect.dart";
9+
10+
// Regression test for https://github.com/dart-lang/sdk/issues/41465
11+
12+
void main() async {
13+
var f = Future<int>.sync(() => null);
14+
Expect.isNull(await f);
15+
}

0 commit comments

Comments
 (0)