-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Sema] [SR-4347] Improve inference of optional supertypes #8339
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
@rudkx Want to take a look? |
@swift-ci Please smoke test |
A couple comments:
|
Another code path that is relevant in collection element type inference is in lib/AST/TypeJoinMeet.cpp. Does that need generalization too? @rudkx Do you know what the difference is between the code being changed in this PR, and the type join stuff? |
Thanks for your feedback! I've added additional test cases for metatypes, protocol composition types, etc. I also noticed that the existing test cases don't cover the case where BTW, why does it still say Some checks haven't completed yet. Is it normal that the smoke tests take so long? Or are the tests not running yet? |
Executing tests requires privileges. |
@swift-ci Please smoke test |
On the Type::join function - I don't think any changes are needed there as it will already properly handle joining types involving optional. This change is really about using binding T? rather than T if there is a constraint for the type variable involving ExpressibleByNilLiteral. |
@tonisuter Thanks for the fixes! |
If an array literal contains at least one element that is a
nil
literal and at least one other element that is of typeT
, the type checker will infer an array element type ofOptional<T>
:Similarly, there are other situations where the compiler can infer a common optional supertype:
However, this currently only works if
T
is a nominal type (and does not conform toExpressibleByNilLiteral
). IfT
is any other kind of type (e.g., a tuple type, a function type, a generic parameter, Any), the compiler will emit a type error (see discussion on the mailing list).This pull request fixes this to allow inference of an optional supertype for any kind of type
T
.Resolves SR-4347.