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 all commits
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
9 changes: 9 additions & 0 deletions lib/IRGen/GenKeyPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,15 @@ 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()) {

// Use the property's class type to determine the field access.
auto propertyBaseDecl = property->getDeclContext()->getSelfClassDecl();
auto currentBaseTy =
loweredBaseTy.getASTType()->getSuperclassForDecl(propertyBaseDecl);
assert(currentBaseTy->getClassOrBoundGenericClass() == propertyBaseDecl);
loweredBaseTy =
IGM.getLoweredType(AbstractionPattern::getOpaque(), currentBaseTy);

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