Skip to content

Commit d77e4eb

Browse files
alexmarkovcommit-bot@chromium.org
authored andcommitted
[vm/aot/nnbd] Fix class range 'is' tests for nullable types
Issue: flutter/flutter#63819 Change-Id: I7d3fe2a3f6c40b4a5bae9b1ba3ab1dd36fab8f5c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/158800 Reviewed-by: Vyacheslav Egorov <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent d39cb30 commit d77e4eb

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
// Verify that 'is' test of a nullable type accepts null.
6+
// Regression test for https://github.com/flutter/flutter/issues/63819.
7+
8+
import 'package:expect/expect.dart';
9+
10+
abstract class A {}
11+
12+
class B extends A {}
13+
14+
class C extends A {}
15+
16+
@pragma('vm:never-inline')
17+
bool foo(A? x) {
18+
if (x is C?) {
19+
print('$x is C?');
20+
return true;
21+
}
22+
print('$x is not C?');
23+
return false;
24+
}
25+
26+
void main() {
27+
B();
28+
C();
29+
Expect.isFalse(foo(B()));
30+
Expect.isTrue(foo(null));
31+
}

runtime/vm/compiler/backend/il.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,12 @@ bool HierarchyInfo::InstanceOfHasClassRange(const AbstractType& type,
453453
intptr_t* lower_limit,
454454
intptr_t* upper_limit) {
455455
ASSERT(CompilerState::Current().is_aot());
456+
if (type.IsNullable()) {
457+
// 'is' test for nullable types should accept null cid in addition to the
458+
// class range. In most cases it is not possible to extend class range to
459+
// include kNullCid.
460+
return false;
461+
}
456462
if (CanUseSubtypeRangeCheckFor(type)) {
457463
const Class& type_class =
458464
Class::Handle(thread()->zone(), type.type_class());

0 commit comments

Comments
 (0)