Skip to content

Commit

Permalink
[cfe] Adjust the nullability of UP w.r.t. undetermined RHS
Browse files Browse the repository at this point in the history
Closes #52726

Change-Id: I80c3c219738bb999a1a5939d95d3119d8b63332c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/334441
Reviewed-by: Johnni Winther <[email protected]>
Commit-Queue: Chloe Stefantsova <[email protected]>
  • Loading branch information
chloestefantsova authored and Commit Queue committed Nov 7, 2023
1 parent b15e1bf commit ad298bf
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 11 deletions.
1 change: 1 addition & 0 deletions pkg/front_end/test/spell_checking_list_common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@ desugar
desugared
desugaring
desugars
detached
detail
detailed
details
Expand Down
13 changes: 13 additions & 0 deletions pkg/front_end/testcases/general/issue52726.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

class WeakMap<K extends Object, V> {
WeakMap(List<(K, V)> entries) {
var mapped = entries.map((i) => [i.$1, i.$2]);
var first = mapped.first;
first.add(null);
Object o = first.first; // Error.
}
}

24 changes: 24 additions & 0 deletions pkg/front_end/testcases/general/issue52726.dart.strong.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
library;
//
// Problems in library:
//
// pkg/front_end/testcases/general/issue52726.dart:10:22: Error: A value of type 'Object?' can't be assigned to a variable of type 'Object' because 'Object?' is nullable and 'Object' isn't.
// - 'Object' is from 'dart:core'.
// Object o = first.first; // Error.
// ^
//
import self as self;
import "dart:core" as core;

class WeakMap<K extends core::Object, V extends core::Object? = dynamic> extends core::Object {
constructor •(core::List<(self::WeakMap::K, self::WeakMap::V%)> entries) → self::WeakMap<self::WeakMap::K, self::WeakMap::V%>
: super core::Object::•() {
core::Iterable<core::List<core::Object?>> mapped = entries.{core::Iterable::map}<core::List<core::Object?>>(((self::WeakMap::K, self::WeakMap::V%) i) → core::List<core::Object?> => <core::Object?>[i.$1{self::WeakMap::K}, i.$2{self::WeakMap::V%}]){(((self::WeakMap::K, self::WeakMap::V%)) → core::List<core::Object?>) → core::Iterable<core::List<core::Object?>>};
core::List<core::Object?> first = mapped.{core::Iterable::first}{core::List<core::Object?>};
first.{core::List::add}(null){(core::Object?) → void};
core::Object o = invalid-expression "pkg/front_end/testcases/general/issue52726.dart:10:22: Error: A value of type 'Object?' can't be assigned to a variable of type 'Object' because 'Object?' is nullable and 'Object' isn't.
- 'Object' is from 'dart:core'.
Object o = first.first; // Error.
^" in first.{core::Iterable::first}{core::Object?} as{TypeError} core::Object;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
library;
//
// Problems in library:
//
// pkg/front_end/testcases/general/issue52726.dart:10:22: Error: A value of type 'Object?' can't be assigned to a variable of type 'Object' because 'Object?' is nullable and 'Object' isn't.
// - 'Object' is from 'dart:core'.
// Object o = first.first; // Error.
// ^
//
import self as self;
import "dart:core" as core;

class WeakMap<K extends core::Object, V extends core::Object? = dynamic> extends core::Object {
constructor •(core::List<(self::WeakMap::K, self::WeakMap::V%)> entries) → self::WeakMap<self::WeakMap::K, self::WeakMap::V%>
: super core::Object::•() {
core::Iterable<core::List<core::Object?>> mapped = entries.{core::Iterable::map}<core::List<core::Object?>>(((self::WeakMap::K, self::WeakMap::V%) i) → core::List<core::Object?> => core::_GrowableList::_literal2<core::Object?>(i.$1{self::WeakMap::K}, i.$2{self::WeakMap::V%})){(((self::WeakMap::K, self::WeakMap::V%)) → core::List<core::Object?>) → core::Iterable<core::List<core::Object?>>};
core::List<core::Object?> first = mapped.{core::Iterable::first}{core::List<core::Object?>};
first.{core::List::add}(null){(core::Object?) → void};
core::Object o = invalid-expression "pkg/front_end/testcases/general/issue52726.dart:10:22: Error: A value of type 'Object?' can't be assigned to a variable of type 'Object' because 'Object?' is nullable and 'Object' isn't.
- 'Object' is from 'dart:core'.
Object o = first.first; // Error.
^" in let core::Object? #t1 = first.{core::Iterable::first}{core::Object?} in #t1 == null ?{core::Object} #t1 as{TypeError} core::Object : #t1{core::Object};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class WeakMap<K extends Object, V> {
WeakMap(List<(K, V)> entries) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class WeakMap<K extends Object, V> {
WeakMap(List<(K, V)> entries) {}
}
24 changes: 24 additions & 0 deletions pkg/front_end/testcases/general/issue52726.dart.weak.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
library;
//
// Problems in library:
//
// pkg/front_end/testcases/general/issue52726.dart:10:22: Error: A value of type 'Object?' can't be assigned to a variable of type 'Object' because 'Object?' is nullable and 'Object' isn't.
// - 'Object' is from 'dart:core'.
// Object o = first.first; // Error.
// ^
//
import self as self;
import "dart:core" as core;

class WeakMap<K extends core::Object, V extends core::Object? = dynamic> extends core::Object {
constructor •(core::List<(self::WeakMap::K, self::WeakMap::V%)> entries) → self::WeakMap<self::WeakMap::K, self::WeakMap::V%>
: super core::Object::•() {
core::Iterable<core::List<core::Object?>> mapped = entries.{core::Iterable::map}<core::List<core::Object?>>(((self::WeakMap::K, self::WeakMap::V%) i) → core::List<core::Object?> => <core::Object?>[i.$1{self::WeakMap::K}, i.$2{self::WeakMap::V%}]){(((self::WeakMap::K, self::WeakMap::V%)) → core::List<core::Object?>) → core::Iterable<core::List<core::Object?>>};
core::List<core::Object?> first = mapped.{core::Iterable::first}{core::List<core::Object?>};
first.{core::List::add}(null){(core::Object?) → void};
core::Object o = invalid-expression "pkg/front_end/testcases/general/issue52726.dart:10:22: Error: A value of type 'Object?' can't be assigned to a variable of type 'Object' because 'Object?' is nullable and 'Object' isn't.
- 'Object' is from 'dart:core'.
Object o = first.first; // Error.
^" in first.{core::Iterable::first}{core::Object?} as{TypeError} core::Object;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
library;
//
// Problems in library:
//
// pkg/front_end/testcases/general/issue52726.dart:10:22: Error: A value of type 'Object?' can't be assigned to a variable of type 'Object' because 'Object?' is nullable and 'Object' isn't.
// - 'Object' is from 'dart:core'.
// Object o = first.first; // Error.
// ^
//
import self as self;
import "dart:core" as core;

class WeakMap<K extends core::Object, V extends core::Object? = dynamic> extends core::Object {
constructor •(core::List<(self::WeakMap::K, self::WeakMap::V%)> entries) → self::WeakMap<self::WeakMap::K, self::WeakMap::V%>
: super core::Object::•() {
core::Iterable<core::List<core::Object?>> mapped = entries.{core::Iterable::map}<core::List<core::Object?>>(((self::WeakMap::K, self::WeakMap::V%) i) → core::List<core::Object?> => <core::Object?>[i.$1{self::WeakMap::K}, i.$2{self::WeakMap::V%}]){(((self::WeakMap::K, self::WeakMap::V%)) → core::List<core::Object?>) → core::Iterable<core::List<core::Object?>>};
core::List<core::Object?> first = mapped.{core::Iterable::first}{core::List<core::Object?>};
first.{core::List::add}(null){(core::Object?) → void};
core::Object o = invalid-expression "pkg/front_end/testcases/general/issue52726.dart:10:22: Error: A value of type 'Object?' can't be assigned to a variable of type 'Object' because 'Object?' is nullable and 'Object' isn't.
- 'Object' is from 'dart:core'.
Object o = first.first; // Error.
^" in first.{core::Iterable::first}{core::Object?} as{TypeError} core::Object;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
library;
import self as self;
import "dart:core" as core;

class WeakMap<K extends core::Object, V extends core::Object? = dynamic> extends core::Object {
constructor •(core::List<(self::WeakMap::K, self::WeakMap::V%)> entries) → self::WeakMap<self::WeakMap::K, self::WeakMap::V%>
;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
library;
//
// Problems in library:
//
// pkg/front_end/testcases/general/issue52726.dart:10:22: Error: A value of type 'Object?' can't be assigned to a variable of type 'Object' because 'Object?' is nullable and 'Object' isn't.
// - 'Object' is from 'dart:core'.
// Object o = first.first; // Error.
// ^
//
import self as self;
import "dart:core" as core;

class WeakMap<K extends core::Object, V extends core::Object? = dynamic> extends core::Object {
constructor •(core::List<(self::WeakMap::K, self::WeakMap::V%)> entries) → self::WeakMap<self::WeakMap::K, self::WeakMap::V%>
: super core::Object::•() {
core::Iterable<core::List<core::Object?>> mapped = entries.{core::Iterable::map}<core::List<core::Object?>>(((self::WeakMap::K, self::WeakMap::V%) i) → core::List<core::Object?> => core::_GrowableList::_literal2<core::Object?>(i.$1{self::WeakMap::K}, i.$2{self::WeakMap::V%})){(((self::WeakMap::K, self::WeakMap::V%)) → core::List<core::Object?>) → core::Iterable<core::List<core::Object?>>};
core::List<core::Object?> first = mapped.{core::Iterable::first}{core::List<core::Object?>};
first.{core::List::add}(null){(core::Object?) → void};
core::Object o = invalid-expression "pkg/front_end/testcases/general/issue52726.dart:10:22: Error: A value of type 'Object?' can't be assigned to a variable of type 'Object' because 'Object?' is nullable and 'Object' isn't.
- 'Object' is from 'dart:core'.
Object o = first.first; // Error.
^" in first.{core::Iterable::first}{core::Object?};
}
}
25 changes: 14 additions & 11 deletions pkg/kernel/lib/src/standard_bounds.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1543,13 +1543,16 @@ mixin StandardBounds {
// otherwise UP(B1a, T2)
// where B1a is the greatest closure of B1 with respect to X1,
// as defined in [inference.md].

if (isSubtypeOf(type1, type2, SubtypeCheckMode.withNullabilities)) {
return type2.withDeclaredNullability(
uniteNullabilities(type1.declaredNullability, type2.nullability));
return type2.withDeclaredNullability(combineNullabilitiesForSubstitution(
type2.nullability,
uniteNullabilities(type1.declaredNullability, type2.nullability)));
}
if (isSubtypeOf(type2, type1, SubtypeCheckMode.withNullabilities)) {
return type1.withDeclaredNullability(
uniteNullabilities(type1.declaredNullability, type2.nullability));
return type1.withDeclaredNullability(combineNullabilitiesForSubstitution(
type1.declaredNullability,
uniteNullabilities(type1.declaredNullability, type2.nullability)));
}
NullabilityAwareTypeVariableEliminator eliminator =
new NullabilityAwareTypeVariableEliminator(
Expand All @@ -1558,13 +1561,13 @@ mixin StandardBounds {
topType: coreTypes.objectNullableRawType,
topFunctionType: coreTypes.functionNonNullableRawType,
unhandledTypeHandler: (type, recursor) => false);
return _getNullabilityAwareStandardUpperBound(
eliminator.eliminateToGreatest(type1.parameter.bound), type2,
isNonNullableByDefault: isNonNullableByDefault)
.withDeclaredNullability(uniteNullabilities(
type1.declaredNullability,
uniteNullabilities(
type1.parameter.bound.declaredNullability, type2.nullability)));
DartType result = _getNullabilityAwareStandardUpperBound(
eliminator.eliminateToGreatest(type1.parameter.bound), type2,
isNonNullableByDefault: isNonNullableByDefault);
return result.withDeclaredNullability(combineNullabilitiesForSubstitution(
result.declaredNullability,
uniteNullabilities(
type1.parameter.bound.declaredNullability, type2.nullability)));
}

DartType _getNullabilityAwareIntersectionStandardUpperBound(
Expand Down

0 comments on commit ad298bf

Please sign in to comment.