Skip to content

Commit

Permalink
Extension type. Issue 54548. Don't consider Never? to be a bottom type.
Browse files Browse the repository at this point in the history
Bug: #54548
Change-Id: I56b734c24a84c8188983605eca69de28869b6de0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/345440
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Jan 9, 2024
1 parent 2ff30ee commit 4946618
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
3 changes: 3 additions & 0 deletions pkg/analyzer/lib/src/dart/element/type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,9 @@ class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
Set<TypeParameterElement> seenTypes = {};
TypeParameterType type = this;
while (seenTypes.add(type.element)) {
if (type.nullabilitySuffix == NullabilitySuffix.question) {
return false;
}
var bound = type.bound;
if (bound is TypeParameterType) {
type = bound;
Expand Down
10 changes: 7 additions & 3 deletions pkg/analyzer/lib/src/dart/element/type_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -982,11 +982,15 @@ class TypeSystemImpl implements TypeSystem {

/// Return `true` for things in the equivalence class of `Never`.
bool isBottom(DartType type) {
if (type.nullabilitySuffix == NullabilitySuffix.question) {
assert(!type.isBottom);
return false;
}

// BOTTOM(Never) is true
if (type is NeverType) {
var result = type.nullabilitySuffix != NullabilitySuffix.question;
assert(type.isBottom == result);
return result;
assert(type.isBottom);
return true;
}

// BOTTOM(X&T) is true iff BOTTOM(T)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class BoundsHelperPredicatesTest extends _BoundsTestBase {
T = typeParameter('T', bound: objectQuestion);

isBottom(promotedTypeParameterTypeNone(T, neverNone));
isBottom(promotedTypeParameterTypeQuestion(T, neverNone));
isNotBottom(promotedTypeParameterTypeQuestion(T, neverNone));
isBottom(promotedTypeParameterTypeStar(T, neverNone));

isNotBottom(promotedTypeParameterTypeNone(T, neverQuestion));
Expand All @@ -124,7 +124,7 @@ class BoundsHelperPredicatesTest extends _BoundsTestBase {
// BOTTOM(X extends T) is true iff BOTTOM(T)
T = typeParameter('T', bound: neverNone);
isBottom(typeParameterTypeNone(T));
isBottom(typeParameterTypeQuestion(T));
isNotBottom(typeParameterTypeQuestion(T));
isBottom(typeParameterTypeStar(T));

T = typeParameter('T', bound: neverQuestion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,37 @@ extension type A(Never it) {}
5),
]);
}

test_neverQuestion() async {
await assertNoErrorsInCode('''
extension type A(Never? it) {}
''');
}

test_typeParameter_never_none() async {
await assertErrorsInCode('''
extension type A<T extends Never>(T it) {}
''', [
error(CompileTimeErrorCode.EXTENSION_TYPE_REPRESENTATION_TYPE_BOTTOM, 34,
1),
]);
}

test_typeParameter_never_question() async {
await assertNoErrorsInCode('''
extension type A<T extends Never>(T? it) {}
''');
}

test_typeParameter_never_question2() async {
await assertNoErrorsInCode('''
extension type A<T extends Never, S extends T>(S? it) {}
''');
}

test_typeParameter_never_question3() async {
await assertNoErrorsInCode('''
extension type A<T extends Never, S extends T?>(S it) {}
''');
}
}

0 comments on commit 4946618

Please sign in to comment.