-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pattern type inference error #52373
Comments
Number 1 is reproducible on Dart Number 2 is only reproducible on Dart |
Here are shorter repros. Case 1 Reproduction class A {}
class B extends A {}
class C extends A {
E1 get foo => new E1();
}
class D extends A {
E2? get bar => new E2();
E3? get baz => new E3();
}
class E {}
class E1 extends E {}
class E2 extends E1 {}
class E3 extends E1 {}
test(A x) {
switch (x) {
case B _:
return 1;
case D(bar: final foo?) ||
D(baz: final foo?) ||
C(:final foo):
return 2;
case D(bar: null, baz: null):
return 3;
}
} Case 2 Reproduction class A {}
class B extends A {}
class C extends A {
E1 get foo => new E1();
}
class D extends A {
E2? get bar => new E2();
E3? get baz => new E3();
}
class E {}
class E1 extends E {}
class E2 extends E1 {}
class E3 extends E1 {}
test(A x) {
switch (x) {
case B _:
return 1;
case D(bar: final E1 foo) ||
D(baz: final E1 foo) ||
C(:final E1 foo):
return 2;
case D(bar: null, baz: null):
return 3;
}
} Currently both the CFE and the Analyzer report the mismatching type or finality on the first one and allow the second one. @stereotype441, since the behavior of the tools is similar, could this issue be a part of the shared analysis? |
@chloestefantsova thanks for the shorter repros! In the main development branch, both the analyzer and CFE report an error for case 1 and no error for case 2. This is correct behaviour, because all the declarations of In the stable branch, the analyzer behaviour is correct, however the CFE reports an error for both case 1 and case 2. So it looks like the correct behaviour has been implemented in the CFE, but it needs to be cherry-picked to stable. Bisecting, I found that the correct behaviour was implemented in b9b1fdf. I will submit a cherry-pick request. |
Cherry-pick request is here: #52488 |
Thank you for finding the root of it, @stereotype441! I've +1'd the cherry pick CL. |
Fixes: #52488 Cherry-pick: https://dart-review.googlesource.com/c/sdk/+/297640 Change-Id: Ib46129a45599ab2e7090e6666bfe07837555eeba Bug: #52373 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/305042 Commit-Queue: Paul Berry <paulberry@google.com> Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
As of 10b909a, the cherry-pick has landed on the stable branch. Assuming no problems arise, it should go out as part of the next stable release, probably next Wednesday. |
Thank you @stereotype441 @chloestefantsova! 🙏 |
I'm facing an error with inference of pattern types when using sealed classes.
Here is the full reproduction: https://github.com/dnys1/sealed_repro
Given the following class hierarchy:
In
base_exception.dart
we define an exception hiearchy for which all classes inheritBaseException
. From this, we deriveAuthException
which is sealed and has discrete subtypes, namelyUnknownException
andAuthServiceException
.In
auth_result.dart
, we define the result of an Auth operation. The result takes one of three discrete states: a successful result (AuthSuccessResult
); a partial success (AuthPartialSuccessResult
) where either the first or second step of the operation failed with anAuthException
; and a complete failure (AuthFailureResult
).Given a method which performs an Auth operation:
We expect the following to work:
1. Inference on exception type
In the second branch,
step1Exception
,step2Exception
, andexception
are all subclasses ofAuthException
, yet its type cannot be inferred. The Analyzer presents the error:2. Explicit exception type
By explicitly specifying the exception types as
AuthException
, the analyzer stops complaining.However, upon compilation, we get a similar error:
The text was updated successfully, but these errors were encountered: