Skip to content

Commit

Permalink
Extension type. Extension type is always exhaustive if the erasure is…
Browse files Browse the repository at this point in the history
… always exhaustive.

Bug: #54370
Bug: #54421
Change-Id: Iebd64c413f489ac12b1a8d56a987ad44df00534e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/342729
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Dec 20, 2023
1 parent 93c25ff commit a1006f4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/analyzer/lib/src/dart/constant/constant_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
if (value.hasPrimitiveEquality(_currentLibrary.featureSet)) {
var constantType = value.type;
var matchedValueType = node.matchedValueType;
matchedValueType = matchedValueType?.extensionTypeErasure;
if (matchedValueType != null) {
if (!_canBeEqual(constantType, matchedValueType)) {
_errorReporter.reportErrorForNode(
Expand Down
3 changes: 3 additions & 0 deletions pkg/analyzer/lib/src/dart/element/type_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,9 @@ class TypeSystemImpl implements TypeSystem {
if (element is ClassElement && element.isSealed) {
return true;
}
if (element is ExtensionTypeElement) {
return isAlwaysExhaustive(type.extensionTypeErasure);
}
if (type.isDartAsyncFutureOr) {
return isAlwaysExhaustive(type.typeArguments[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ void f(Future f) async {}
''');
}

test_function_switchStatement_exhaustive_extensionType() async {
await assertNoErrorsInCode(r'''
enum E { a, b }
extension type EE(E it) {}
int f(EE e) {
switch (e) {
case E.a:
return 0;
case E.b:
return 1;
}
}
''');
}

test_function_sync_block_dynamic() async {
await assertNoErrorsInCode('''
dynamic f() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,28 @@ const zero = 0;
''');
}

test_int_extensionTypeBool() async {
await assertErrorsInCode('''
extension type E(bool it) {}
void f(E x) {
if (x case (0)) {}
}
''', [
error(WarningCode.CONSTANT_PATTERN_NEVER_MATCHES_VALUE_TYPE, 58, 1),
]);
}

test_int_extensionTypeInt() async {
await assertNoErrorsInCode('''
extension type E(int it) {}
void f(E x) {
if (x case (0)) {}
}
''');
}

test_int_functionType() async {
await assertErrorsInCode('''
void f(void Function() x) {
Expand Down

0 comments on commit a1006f4

Please sign in to comment.