-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Sema: Arrange TypeCheckType to open unbound generics on the fly #32116
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
Sema: Arrange TypeCheckType to open unbound generics on the fly #32116
Conversation
@davidungar This change uncovered what looks like an ASTScope expansion bug: class Outer {
class Inner {}
}
extension Outer.Inner {
func foo(arg: Inner) {} // Could not find `Inner` in scope
} My prints are telling me that name lookup is hopping straight to file scope from the extension. Could you offer me some guidance here? Where exactly is the parent scope decided upon? |
lib/Sema/CSGen.cpp
Outdated
Type parentType = | ||
resolveTypeReferenceInExpression(enumPattern->getParentType()); | ||
Type parentType = resolveTypeReferenceInExpression( | ||
enumPattern->getParentType(), [](auto unboundTy) { |
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.
We oughta make a functor for the no-op case a default argument to both functions.
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.
The no-op is a temporary hack. I figured writing them out explicitly with FIXMEs is a good idea, rather than defaulting the parameter.
if (isSILMode) { | ||
options |= TypeResolutionFlags::SILMode; | ||
} | ||
if (isSILType) | ||
options |= TypeResolutionFlags::SILType; | ||
|
||
auto resolution = TypeResolution::forContextual(DC, GenericEnv, options); | ||
const auto resolution = | ||
TypeResolution::forContextual(DC, GenericEnv, options, |
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.
Another candidate for a default
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.
The approach here looks fine, just picking around the edges mostly. We can take the simpler changes to TypeCheckType (removal of dead parameters and such) now if you would like to split them.
You don't have to worry about that - it's a pre-existing limitation of name lookup in extensions of nested types. |
Thanks for taking a look! I was able to remove the |
ecf74a5
to
33fe6d0
Compare
9b37bcc
to
2ace225
Compare
@swift-ci please smoke test |
@swift-ci please test source compatibility release |
Alright, this is now ready for review. |
@swift-ci please smoke test macOS |
2ace225
to
334de64
Compare
334de64
to
0c351d0
Compare
@swift-ci please smoke test |
@swift-ci please test source compatibility release |
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.
This looks a lot better, thanks! Let’s see what Slava and Pavel have to say.
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.
I think for the underlying type of a type alias, the right way to handle it is to desugar the typealias into a generic one. Eg,
typealias Foo = Array
is really the same as
typealias Foo<Element> = Array<Element>
lib/Sema/CSSolver.cpp
Outdated
TypeResolution::forContextual( | ||
CS.DC, None, | ||
// FIXME: Should we really unconditionally complain | ||
// about unbound generics here? |
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.
if they're truly invalid, then instead of complaining, we should be ensuring that the TypeResolution's context kind and so on doesn't allow them in the first place.
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.
Perhaps the sentence is flawed. What I meant to convey is that we may want to someday reconsider complaining about unbound generics here (we do complain today). For example:
let foo: [Array<Float>] = [[3], [4], [5]] as [Array] // error
0c351d0
to
da3c2eb
Compare
@xedin ping |
@AnthonyLatsis IMO this is ready to merge. |
@slavapestov recd. Needs another rebase though. |
da3c2eb
to
78251b1
Compare
@swift-ci please smoke test and merge |
78251b1
to
2133fe2
Compare
@swift-ci please smoke test and merge |
@swift-ci please smoke test |
Lays the groundwork for sinking
UnboundGenericType
to unblock #31895. The idea was put forth by Slava over here. NFC expected, because for now the "opener" is used as intended only where unbound generics were already being handled in-place; the remaining type resolution sites that allow unbound generics to escape are now marked withFIXME
s. With this much we are safe to nukeTypeResolutionFlags::AllowUnboundGenerics
and start addressing theseFIXME
s incrementally.