-
Notifications
You must be signed in to change notification settings - Fork 10.5k
IRGen: Don't assert on keypaths that use a subclass as root #21592
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
IRGen: Don't assert on keypaths that use a subclass as root #21592
Conversation
class A { var x : Int = 0 } class B : A {} _ = \B.x rdar://46562046
@swift-ci Please test |
@jckarter Is that the right way to fix this? |
getClassFieldAccess expects the class type that contains the field - it does not handle subclasses |
lib/IRGen/GenKeyPath.cpp
Outdated
} | ||
assert(currentBaseTy->getClassOrBoundGenericClass() == propertyBaseDecl); | ||
loweredBaseTy = IGM.getLoweredType(AbstractionPattern::getOpaque(), | ||
currentBaseTy->getWithoutSpecifierType()); |
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.
Does it ever have a specifier type? (inout or lvalue here)? Might be better to assert.
lib/IRGen/GenKeyPath.cpp
Outdated
@@ -837,6 +837,21 @@ emitKeyPathComponent(IRGenModule &IGM, | |||
// ObjC-ness and resilience of the class hierarchy, there might be a few | |||
// different ways we need to go about this. | |||
if (loweredBaseTy.getClassOrBoundGenericClass()) { | |||
|
|||
// Walk up the class hierarchy to find the class decl that matches the | |||
// property's class decl and use this type to determin the field access. |
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 feels like something SILGen should have already done? CC @jckarter
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.
That might be cleaner, yeah. Then we can assert the condition in the SILVerifier too.
lib/IRGen/GenKeyPath.cpp
Outdated
// property's class decl and use this type to determin the field access. | ||
auto propertyBaseDecl = property->getDeclContext()->getSelfClassDecl(); | ||
auto currentBaseTy = loweredBaseTy.getASTType(); | ||
while (currentBaseTy->getClassOrBoundGenericClass() != propertyBaseDecl && |
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 loop is already encapsulated in TypeBase::getSuperclassForDecl()
This SGTM, though I agree with Slava that having SIL always reference the property by its originating superclass is a little bit cleaner. |
Consider the following example:
Today the key path is
If we change SILGen this would be:
Which would seem odd to me that the type for the var decl: |
@swift-ci Please test |
Build failed |
Build failed |
Ah, I misunderstood, I thought that the |
@aschwaighofer Thanks for the cleanup! If this same pattern comes up in more places, we could add a SILType::getSuperclassDecl() variant too, but it's not too bad as-is. |
class A {
var x : Int = 0
}
class B : A {}
_ = \B.x
rdar://46562046