Minor NNBD fixes for type algebra and DartType.toString.

Change-Id: Iafb43852d5fad65a37a58b132a066b6aa3492da1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/119062
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
This commit is contained in:
Paul Berry 2019-09-26 21:51:12 +00:00 committed by commit-bot@chromium.org
parent 403c4af720
commit 1375e262be
3 changed files with 73 additions and 22 deletions

View file

@ -765,8 +765,9 @@ abstract class FunctionTypeImpl extends TypeImpl implements FunctionType {
name = e.name + subscript;
counter++;
}
TypeParameterTypeImpl t =
new TypeParameterTypeImpl(new TypeParameterElementImpl(name, -1));
TypeParameterTypeImpl t = new TypeParameterTypeImpl(
new TypeParameterElementImpl(name, -1),
nullabilitySuffix: NullabilitySuffix.none);
t.appendTo(typeParametersBuffer, visitedTypes,
withNullability: withNullability);
instantiateTypeArgs.add(t);

View file

@ -434,7 +434,8 @@ abstract class _TypeSubstitutor extends DartTypeVisitor<DartType> {
return type;
}
return new InterfaceTypeImpl.explicit(type.element, typeArguments);
return new InterfaceTypeImpl.explicit(type.element, typeArguments,
nullabilitySuffix: (type as TypeImpl).nullabilitySuffix);
}
@override

View file

@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/dart/element/type_algebra.dart';
@ -20,6 +21,7 @@ main() {
defineReflectiveTests(SubstituteFromPairsTest);
defineReflectiveTests(SubstituteFromUpperAndLowerBoundsTest);
defineReflectiveTests(SubstituteTest);
defineReflectiveTests(SubstituteWithNullabilityTest);
});
}
@ -276,6 +278,72 @@ class SubstituteTest extends _Base {
var result = substitute(type, substitution);
expect(result, same(type));
}
}
@reflectiveTest
class SubstituteWithNullabilityTest extends _Base {
SubstituteWithNullabilityTest() : super(useNnbd: true);
test_interface_none() async {
// class A<T> {}
var T = typeParameter('T');
var A = class_(name: 'A', typeParameters: [T]);
var U = typeParameter('U');
var type = interfaceType(A,
typeArguments: [typeParameterType(U)],
nullabilitySuffix: NullabilitySuffix.none);
_assertSubstitution(type, {U: intType}, 'A<int>');
}
test_interface_question() async {
// class A<T> {}
var T = typeParameter('T');
var A = class_(name: 'A', typeParameters: [T]);
var U = typeParameter('U');
var type = interfaceType(A,
typeArguments: [typeParameterType(U)],
nullabilitySuffix: NullabilitySuffix.question);
_assertSubstitution(type, {U: intType}, 'A<int>?');
}
test_interface_star() async {
// class A<T> {}
var T = typeParameter('T');
var A = class_(name: 'A', typeParameters: [T]);
var U = typeParameter('U');
var type = interfaceType(A,
typeArguments: [typeParameterType(U)],
nullabilitySuffix: NullabilitySuffix.star);
_assertSubstitution(type, {U: intType}, 'A<int>*');
}
}
class _Base with ElementsTypesMixin {
final TestTypeProvider typeProvider;
final bool useNnbd;
_Base({this.useNnbd = false})
: typeProvider = TestTypeProvider(null, null,
useNnbd ? NullabilitySuffix.none : NullabilitySuffix.question);
InterfaceType get boolType => typeProvider.boolType;
InterfaceType get doubleType => typeProvider.doubleType;
InterfaceType get intType => typeProvider.intType;
/// Whether `DartType.toString()` with nullability should be asked.
bool get typeToStringWithNullability => useNnbd;
void assertElementTypeString(DartType type, String expected) {
TypeImpl typeImpl = type;
expect(typeImpl.toString(withNullability: typeToStringWithNullability),
expected);
}
void _assertSubstitution(
DartType type,
@ -286,22 +354,3 @@ class SubstituteTest extends _Base {
assertElementTypeString(result, expected);
}
}
class _Base with ElementsTypesMixin {
final typeProvider = TestTypeProvider();
InterfaceType get boolType => typeProvider.boolType;
InterfaceType get doubleType => typeProvider.doubleType;
InterfaceType get intType => typeProvider.intType;
/// Whether `DartType.toString()` with nullability should be asked.
bool get typeToStringWithNullability => false;
void assertElementTypeString(DartType type, String expected) {
TypeImpl typeImpl = type;
expect(typeImpl.toString(withNullability: typeToStringWithNullability),
expected);
}
}