[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>
This commit is contained in:
Alexander Markov 2020-03-25 17:32:02 +00:00 committed by commit-bot@chromium.org
parent 2323087237
commit e0b7841d16
2 changed files with 6 additions and 9 deletions

View file

@ -815,17 +815,13 @@ bool CompileType::IsAssignableTo(const AbstractType& other) {
if (other.IsTopTypeForAssignability()) {
return true;
}
if (IsNone()) {
return false;
}
// Consider the compile type of the value.
const AbstractType& compile_type = *ToAbstractType();
if (compile_type.IsNullType()) {
return Instance::NullIsAssignableTo(other);
if (is_nullable() && !Instance::NullIsAssignableTo(other)) {
return false;
}
return compile_type.IsSubtypeOf(other, Heap::kOld);
return ToAbstractType()->IsSubtypeOf(other, Heap::kOld);
}
bool CompileType::IsInstanceOf(const AbstractType& other) {

View file

@ -11,7 +11,8 @@ returnStringOrNull() {
}
main() {
Expect.throwsArgumentError(() => 'foo' + returnStringOrNull());
Expect.throws(() => 'foo' + returnStringOrNull(),
(e) => e is ArgumentError || e is TypeError);
Expect.throws(() => 'foo'.split(returnStringOrNull()),
(e) => e is ArgumentError || e is NoSuchMethodError);
(e) => e is ArgumentError || e is NoSuchMethodError || e is TypeError);
}