[dart2js] Treat Object as a top type in weak mode.

We were only treating Object as a top type if NNBD was disabled. This is
incorrect and we should be treating Object as a top type whenever legacy
subtyping is in effect. In particular, Object acts as a top type in weak
mode.

Change-Id: I76acc25e92fda2b9e2b7872fc72d319161318874
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/144689
Reviewed-by: Stephen Adams <sra@google.com>
Commit-Queue: Mayank Patke <fishythefish@google.com>
This commit is contained in:
Mayank Patke 2020-04-28 02:27:26 +00:00 committed by commit-bot@chromium.org
parent 8c96812bec
commit 7ac53f1825
5 changed files with 34 additions and 37 deletions

View file

@ -663,7 +663,6 @@ class Message {
final CompilerOptions _options;
bool get terse => _options?.terseDiagnostics ?? false;
bool get _printLegacyStars => _options?.printLegacyStars ?? false;
bool get _useNullSafety => _options?.useNullSafety ?? true;
bool get _useLegacySubtyping => _options?.useLegacySubtyping ?? false;
String message;
@ -716,7 +715,6 @@ class Message {
if (value is DartType) {
value = value.toStructuredText(
printLegacyStars: _printLegacyStars,
useNullSafety: _useNullSafety,
useLegacySubtyping: _useLegacySubtyping);
} else if (value is ConstantValue) {
value = value.toDartText();

View file

@ -92,15 +92,15 @@ abstract class DartType {
DartType get withoutNullability => this;
/// Is `true` if this type is a top type but not a legacy top type.
bool _isStrongTop(bool useNullSafety) => false;
bool _isStrongTop(bool useLegacySubtyping) => false;
/// Is `true` if this type is a top type.
bool _isTop(bool useNullSafety) => _isStrongTop(useNullSafety);
bool _isTop(bool useLegacySubtyping) => _isStrongTop(useLegacySubtyping);
/// Is `true` if every type argument of this type is a top type.
// TODO(fishythefish): Should we instead check if each type argument is at its
// bound?
bool _treatAsRaw(bool useNullSafety) => true;
bool _treatAsRaw(bool useLegacySubtyping) => true;
/// Whether this type contains a type variable.
bool get containsTypeVariables => false;
@ -131,12 +131,8 @@ abstract class DartType {
String toString() => toStructuredText();
String toStructuredText(
{bool printLegacyStars = true,
bool useNullSafety = true,
bool useLegacySubtyping = false}) =>
_DartTypeToStringVisitor(
printLegacyStars, useNullSafety, useLegacySubtyping)
.run(this);
{bool printLegacyStars = true, bool useLegacySubtyping = false}) =>
_DartTypeToStringVisitor(printLegacyStars, useLegacySubtyping).run(this);
}
/// Pairs of [FunctionTypeVariable]s that are currently assumed to be
@ -237,10 +233,11 @@ class LegacyType extends DartType {
DartType get withoutNullability => baseType;
@override
bool _isTop(bool useNullSafety) => baseType.isObject;
bool _isTop(bool useLegacySubtyping) => baseType.isObject;
@override
bool _treatAsRaw(bool useNullSafety) => baseType._treatAsRaw(useNullSafety);
bool _treatAsRaw(bool useLegacySubtyping) =>
baseType._treatAsRaw(useLegacySubtyping);
@override
bool get containsTypeVariables => baseType.containsTypeVariables;
@ -298,10 +295,11 @@ class NullableType extends DartType {
DartType get withoutNullability => baseType;
@override
bool _isStrongTop(bool isLegacy) => baseType.isObject;
bool _isStrongTop(bool useLegacySubtyping) => baseType.isObject;
@override
bool _treatAsRaw(bool useNullSafety) => baseType._treatAsRaw(useNullSafety);
bool _treatAsRaw(bool useLegacySubtyping) =>
baseType._treatAsRaw(useLegacySubtyping);
@override
bool get containsTypeVariables => baseType.containsTypeVariables;
@ -359,7 +357,7 @@ class InterfaceType extends DartType {
}
@override
bool _isStrongTop(bool useNullSafety) => useNullSafety ? false : isObject;
bool _isTop(bool useLegacySubtyping) => useLegacySubtyping && isObject;
@override
bool get isObject =>
@ -380,9 +378,9 @@ class InterfaceType extends DartType {
}
@override
bool _treatAsRaw(bool useNullSafety) {
bool _treatAsRaw(bool useLegacySubtyping) {
for (DartType type in typeArguments) {
if (!type._isTop(useNullSafety)) return false;
if (!type._isTop(useLegacySubtyping)) return false;
}
return true;
}
@ -567,7 +565,7 @@ class VoidType extends DartType {
}
@override
bool _isStrongTop(bool useNullSafety) => true;
bool _isStrongTop(bool useLegacySubtyping) => true;
@override
R accept<R, A>(DartTypeVisitor<R, A> visitor, A argument) =>
@ -597,7 +595,7 @@ class DynamicType extends DartType {
}
@override
bool _isStrongTop(bool useNullSafety) => true;
bool _isStrongTop(bool useLegacySubtyping) => true;
@override
R accept<R, A>(DartTypeVisitor<R, A> visitor, A argument) =>
@ -627,7 +625,7 @@ class ErasedType extends DartType {
}
@override
bool _isStrongTop(bool useNullSafety) => true;
bool _isStrongTop(bool useLegacySubtyping) => true;
@override
R accept<R, A>(DartTypeVisitor<R, A> visitor, A argument) =>
@ -668,7 +666,7 @@ class AnyType extends DartType {
}
@override
bool _isStrongTop(bool useNullSafety) => true;
bool _isStrongTop(bool useLegacySubtyping) => true;
@override
R accept<R, A>(DartTypeVisitor<R, A> visitor, A argument) =>
@ -1507,7 +1505,6 @@ class _DeferredName {
class _DartTypeToStringVisitor extends DartTypeVisitor<void, void> {
final bool _printLegacyStars;
final bool _useNullSafety;
final bool _useLegacySubtyping;
final List _fragments = []; // Strings and _DeferredNames
bool _lastIsIdentifier = false;
@ -1515,8 +1512,7 @@ class _DartTypeToStringVisitor extends DartTypeVisitor<void, void> {
Map<FunctionTypeVariable, _DeferredName> _variableToName;
Set<FunctionType> _genericFunctions;
_DartTypeToStringVisitor(
this._printLegacyStars, this._useNullSafety, this._useLegacySubtyping);
_DartTypeToStringVisitor(this._printLegacyStars, this._useLegacySubtyping);
String run(DartType type) {
_visit(type);
@ -1668,7 +1664,7 @@ class _DartTypeToStringVisitor extends DartTypeVisitor<void, void> {
needsComma = _comma(needsComma);
_visit(typeVariable);
DartType bound = typeVariable.bound;
if (!bound._isTop(_useNullSafety) &&
if (!bound._isTop(_useLegacySubtyping) &&
(!_useLegacySubtyping || !bound.isObject)) {
_token(' extends ');
_visit(bound);
@ -1749,7 +1745,7 @@ abstract class DartTypes {
DartType legacyType(DartType baseType) {
DartType result;
if (isTopType(baseType) ||
if (isStrongTopType(baseType) ||
baseType.isNull ||
baseType is LegacyType ||
baseType is NullableType) {
@ -1893,12 +1889,12 @@ abstract class DartTypes {
/// Returns `true` if every type argument of [t] is a top type.
// TODO(fishythefish): Should we instead check if each type argument is at its
// bound?
bool treatAsRawType(DartType t) => t._treatAsRaw(useNullSafety);
bool treatAsRawType(DartType t) => t._treatAsRaw(useLegacySubtyping);
/// Returns `true` if [t] is a top type, that is, a supertype of every type.
bool isTopType(DartType t) => t._isTop(useNullSafety);
bool isTopType(DartType t) => t._isTop(useLegacySubtyping);
bool isStrongTopType(DartType t) => t._isStrongTop(useNullSafety);
bool isStrongTopType(DartType t) => t._isStrongTop(useLegacySubtyping);
/// Returns `true` if [s] is a subtype of [t].
bool isSubtype(DartType s, DartType t) => _subtypeHelper(s, t);

View file

@ -1684,7 +1684,7 @@ class _Universe {
universe, Rti baseType, String key, bool normalize) {
if (normalize) {
int baseKind = Rti._getKind(baseType);
if (isTopType(baseType) ||
if (isStrongTopType(baseType) ||
isNullType(baseType) ||
baseKind == Rti.kindQuestion ||
baseKind == Rti.kindStar) {
@ -2884,7 +2884,10 @@ bool isNullable(Rti t) {
kind == Rti.kindFutureOr && isNullable(Rti._getFutureOrArgument(t));
}
bool isTopType(Rti t) => isStrongTopType(t) || isLegacyObjectType(t);
bool isTopType(Rti t) =>
isStrongTopType(t) ||
isLegacyObjectType(t) ||
JS_GET_FLAG('LEGACY') && isObjectType(t);
bool isStrongTopType(Rti t) {
int kind = Rti._getKind(t);
@ -2892,7 +2895,6 @@ bool isStrongTopType(Rti t) {
kind == Rti.kindVoid ||
kind == Rti.kindAny ||
kind == Rti.kindErased ||
!JS_GET_FLAG('NNBD') && isObjectType(t) ||
isNullableObjectType(t);
}

View file

@ -1778,7 +1778,7 @@ class _Universe {
Object? universe, Rti baseType, String key, bool normalize) {
if (normalize) {
int baseKind = Rti._getKind(baseType);
if (isTopType(baseType) ||
if (isStrongTopType(baseType) ||
isNullType(baseType) ||
baseKind == Rti.kindQuestion ||
baseKind == Rti.kindStar) {
@ -2980,7 +2980,10 @@ bool isNullable(Rti t) {
kind == Rti.kindFutureOr && isNullable(Rti._getFutureOrArgument(t));
}
bool isTopType(Rti t) => isStrongTopType(t) || isLegacyObjectType(t);
bool isTopType(Rti t) =>
isStrongTopType(t) ||
isLegacyObjectType(t) ||
JS_GET_FLAG('LEGACY') && isObjectType(t);
bool isStrongTopType(Rti t) {
int kind = Rti._getKind(t);
@ -2988,7 +2991,6 @@ bool isStrongTopType(Rti t) {
kind == Rti.kindVoid ||
kind == Rti.kindAny ||
kind == Rti.kindErased ||
!JS_GET_FLAG('NNBD') && isObjectType(t) ||
isNullableObjectType(t);
}

View file

@ -73,7 +73,6 @@ main() {
String printType(DartType type) => type.toStructuredText(
printLegacyStars: options.printLegacyStars,
useNullSafety: options.useNullSafety,
useLegacySubtyping: options.useLegacySubtyping);
List<String> printTypes(List<DartType> types) =>