Skip to content

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

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/IRGen/GenKeyPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

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

Copy link
Contributor

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.

auto propertyBaseDecl = property->getDeclContext()->getSelfClassDecl();
auto currentBaseTy = loweredBaseTy.getASTType();
while (currentBaseTy->getClassOrBoundGenericClass() != propertyBaseDecl &&
Copy link
Contributor

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()

currentBaseTy->getClassOrBoundGenericClass()->hasSuperclass()) {
currentBaseTy = currentBaseTy->getClassOrBoundGenericClass()
->getSuperclass()
->getCanonicalType();
}
assert(currentBaseTy->getClassOrBoundGenericClass() == propertyBaseDecl);
loweredBaseTy = IGM.getLoweredType(AbstractionPattern::getOpaque(),
currentBaseTy->getWithoutSpecifierType());
Copy link
Contributor

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.


switch (getClassFieldAccess(IGM, loweredBaseTy, property)) {
case FieldAccess::ConstantDirect: {
// Known constant fixed offset.
Expand Down
14 changes: 13 additions & 1 deletion test/IRGen/keypaths.sil
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ sil_vtable C2 {}
// CHECK-32-SAME: <i32 0x0380_0008> }>
// CHECK-64-SAME: <i32 0x0380_0010> }>

// -- %d1: C1.x
// CHECK: [[KP_D1:@keypath(\..*)?]] = private global <{ {{.*}} }> <{
// CHECK-SAME: [[WORD]]* @keypath_once
// CHECK-SAME: @"symbolic
// CHECK-SAME: @"symbolic
// -- instantiable in-line, size 4
// CHECK-SAME: <i32 0x8000_0004>,
// -- 0x0300_0000 (class) + mutable + offset of C.x
// CHECK-32-SAME: <i32 0x0380_0008> }>
// CHECK-64-SAME: <i32 0x0380_0010> }>

// -- %e: C.y
// CHECK: [[KP_E:@keypath(\..*)?]] = private global <{ {{.*}} }> <{
// CHECK-SAME: [[WORD]]* @keypath_once
Expand Down Expand Up @@ -218,9 +229,10 @@ entry:
%b = keypath $KeyPath<S, String>, (root $S; stored_property #S.y : $String)
// CHECK: call %swift.refcounted* @swift_getKeyPath(i8* bitcast ({{.*}} [[KP_C]] to i8*), i8* undef)
%c = keypath $KeyPath<S, C>, (root $S; stored_property #S.z : $C)

// CHECK: call %swift.refcounted* @swift_getKeyPath(i8* bitcast ({{.*}} [[KP_D]] to i8*), i8* undef)
%d = keypath $KeyPath<C, Int>, (root $C; stored_property #C.x : $Int)
// CHECK: call %swift.refcounted* @swift_getKeyPath(i8* bitcast ({{.*}} [[KP_D1]] to i8*), i8* undef)
%d1 = keypath $KeyPath<C1, Int>, (root $C1; stored_property #C.x : $Int)
// CHECK: call %swift.refcounted* @swift_getKeyPath(i8* bitcast ({{.*}} [[KP_E]] to i8*), i8* undef)
%e = keypath $KeyPath<C, String>, (root $C; stored_property #C.y : $String)
// CHECK: call %swift.refcounted* @swift_getKeyPath(i8* bitcast ({{.*}} [[KP_F]] to i8*), i8* undef)
Expand Down