-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[TypeCheckPattern] Attempt ExprPattern conversion before failing pattern coercion to optional #67479
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
Conversation
…ern coercion to optional
57de0bc
to
d13e997
Compare
@swift-ci please test |
1 similar comment
@swift-ci please test |
@swift-ci please test source compatibility |
@swift-ci please test |
@swift-ci please test windows platform |
1 similar comment
@swift-ci please test windows platform |
@swift-ci please test source compatibility release |
@swift-ci please test windows platform |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! This LGTM
@swift-ci please test windows platform |
@swift-ci please test source compatibility debug |
Thanks @hamishknight! |
See previous PR[1] and previous forum threads[2][3] regarding similar behavior and the intended semantics of expression pattern matching.
Expected behavior is for structural pattern matching and expression pattern matching to be transparent for the user. Currently, when matching an
EnumElementPattern
(of the form.foo
) against an expression of typeT
, if we fail to find an enum elementT.foo
then we will attempt to fall back to matching.foo
as an expression, which can find static members.foo
.However, if
T
is an optional typeU?
, then if lookup ofOptional.foo
fails to find a matching enum element member, we will look into the wrapped type for an enum elementU.foo
. If this fails, we (presently) do not take the fallback path and instead immediately fail with the error "enum case 'foo' not found in type 'U?'" even though lookup forUnresolvedMemberExpr
s would also look through optional types when attempting expression matching.This PR allows us to match against
U.foo
expressions by delaying the failure diagnostic in the optional case until after we've attempted the expression fallback. This will have the effect of allowing us to match the pattern.foo
against values of typeU?
whenU
has a static memberfoo: Self
. It will also potentially change the diagnostic in failure cases from "enum case 'foo' not found in type 'U?'" to "type 'U?' has no member 'foo'," which is IMO more accurate since 'foo' is indeed permitted to be any (static) member, not just an enum case.[1]: #22486
[2]: https://forums.swift.org/t/matching-optionals-in-a-switch-statement/12905
[3]: https://forums.swift.org/t/allow-enum-case-matching-without/20544