Skip to content

Commit e0b7841

Browse files
alexmarkovcommit-bot@chromium.org
authored andcommitted
[vm/nnbd] Fix CompileType::IsAssignableTo for nullable types in strong mode
Also, tests/corelib/string_operations_with_null_test.dart is fixed for strong mode: in strong mode TypeError is thrown when null is casted to a non-nullable type; in weak mode null is silently passed and ArgumentError or NoSuchMethodError is thrown. Change-Id: I566a80293ffb03752cc3409e1b79491f0aff6888 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/140779 Reviewed-by: Ryan Macnak <rmacnak@google.com> Commit-Queue: Alexander Markov <alexmarkov@google.com>
1 parent 2323087 commit e0b7841

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

runtime/vm/compiler/backend/type_propagator.cc

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -815,17 +815,13 @@ bool CompileType::IsAssignableTo(const AbstractType& other) {
815815
if (other.IsTopTypeForAssignability()) {
816816
return true;
817817
}
818-
819818
if (IsNone()) {
820819
return false;
821820
}
822-
823-
// Consider the compile type of the value.
824-
const AbstractType& compile_type = *ToAbstractType();
825-
if (compile_type.IsNullType()) {
826-
return Instance::NullIsAssignableTo(other);
821+
if (is_nullable() && !Instance::NullIsAssignableTo(other)) {
822+
return false;
827823
}
828-
return compile_type.IsSubtypeOf(other, Heap::kOld);
824+
return ToAbstractType()->IsSubtypeOf(other, Heap::kOld);
829825
}
830826

831827
bool CompileType::IsInstanceOf(const AbstractType& other) {

tests/corelib/string_operations_with_null_test.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ returnStringOrNull() {
1111
}
1212

1313
main() {
14-
Expect.throwsArgumentError(() => 'foo' + returnStringOrNull());
14+
Expect.throws(() => 'foo' + returnStringOrNull(),
15+
(e) => e is ArgumentError || e is TypeError);
1516
Expect.throws(() => 'foo'.split(returnStringOrNull()),
16-
(e) => e is ArgumentError || e is NoSuchMethodError);
17+
(e) => e is ArgumentError || e is NoSuchMethodError || e is TypeError);
1718
}

0 commit comments

Comments
 (0)