Skip to content

Commit ae8da9d

Browse files
authored
Fix implied Future and isBottom check for types (#2565)
* Fix implied dynamic and isBottom check for types * dartfmt and test_package error correction * dartfmt 2
1 parent fbd999e commit ae8da9d

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

lib/src/element_type.dart

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,26 @@ class UndefinedElementType extends ElementType {
115115

116116
@override
117117
String get name {
118-
if (type.isDynamic) {
119-
if (returnedFrom != null &&
120-
(returnedFrom is DefinedElementType &&
121-
(returnedFrom as DefinedElementType).element.isAsynchronous)) {
122-
return 'Future';
123-
} else {
124-
return 'dynamic';
125-
}
126-
}
118+
if (isImpliedFuture) return 'Future';
127119
if (type.isVoid) return 'void';
128-
if (type.isBottom) return 'Never';
129-
assert(false,
120+
assert({'Never', 'void', 'dynamic'}.contains(type.element.name),
130121
'Unrecognized type for UndefinedElementType: ${type.toString()}');
131-
return '';
122+
return type.element.name;
132123
}
133124

125+
/// Returns true if this type is an implied `Future`.
126+
bool get isImpliedFuture => (type.isDynamic &&
127+
returnedFrom != null &&
128+
returnedFrom is DefinedElementType &&
129+
(returnedFrom as DefinedElementType).element.isAsynchronous);
130+
134131
@override
135132
String get nameWithGenerics => '$name$nullabilitySuffix';
136133

134+
@override
135+
String get nullabilitySuffix =>
136+
isImpliedFuture && library.isNullSafety ? '?' : super.nullabilitySuffix;
137+
137138
/// Assume that undefined elements don't have useful bounds.
138139
@override
139140
DartType get instantiatedType => type;

test/end2end/model_test.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ void main() {
9595
nullableElements;
9696
Class b;
9797
Class c;
98+
ModelFunction oddAsyncFunction, anotherOddFunction;
99+
ModelFunction neverReturns, almostNeverReturns;
98100

99101
setUpAll(() async {
100102
lateFinalWithoutInitializer = packageGraph.libraries
@@ -109,6 +111,31 @@ void main() {
109111
.firstWhere((c) => c.name == 'B');
110112
c = nullSafetyClassMemberDeclarations.allClasses
111113
.firstWhere((c) => c.name == 'C');
114+
oddAsyncFunction = nullableElements.publicFunctions
115+
.firstWhere((f) => f.name == 'oddAsyncFunction');
116+
anotherOddFunction = nullableElements.publicFunctions
117+
.firstWhere((f) => f.name == 'oddAsyncFunction');
118+
neverReturns = nullableElements.publicFunctions
119+
.firstWhere((f) => f.name == 'neverReturns');
120+
almostNeverReturns = nullableElements.publicFunctions
121+
.firstWhere((f) => f.name == 'almostNeverReturns');
122+
});
123+
124+
test('Never types are allowed to have nullability markers', () {
125+
expect(neverReturns.modelType.returnType.name, equals('Never'));
126+
expect(neverReturns.modelType.returnType.nullabilitySuffix, equals(''));
127+
expect(almostNeverReturns.modelType.returnType.name, equals('Never'));
128+
expect(almostNeverReturns.modelType.returnType.nullabilitySuffix,
129+
equals('?'));
130+
});
131+
132+
test('implied Future types have correct nullability', () {
133+
expect(oddAsyncFunction.modelType.returnType.name, equals('Future'));
134+
expect(
135+
oddAsyncFunction.modelType.returnType.nullabilitySuffix, equals('?'));
136+
expect(anotherOddFunction.modelType.returnType.name, equals('Future'));
137+
expect(anotherOddFunction.modelType.returnType.nullabilitySuffix,
138+
equals('?'));
112139
});
113140

114141
test('isNullSafety is set correctly for libraries', () {

testing/test_package/lib/features/nnbd_class_member_declarations.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ class C {
3030

3131
List<Map<String, num?>>? method1() => null;
3232

33-
void m3(void listen(int t)?, {void onDone()?});
33+
void m3(void listen(int t)?, {void onDone()?}) {}
3434
}

testing/test_package/lib/features/nullable_elements.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ void set nullableSetter(String? value) {
1414
_nullableSetter = _nullableSetter ??= value;
1515
}
1616

17+
/// This should have return type of `Future?`.
18+
dynamic? oddAsyncFunction() async {}
19+
20+
/// This should also have return type of `Future?`.
21+
dynamic anotherOddFunction() async {}
22+
23+
Never neverReturns() {
24+
throw Exception();
25+
}
26+
27+
Never? almostNeverReturns() {}
28+
1729
void some(int? nullable, String? parameters) {}
1830

1931
class NullableMembers {

0 commit comments

Comments
 (0)