Skip to content

IRGen: Fix cast of existential type with anyobject to super class constraint existential #27622

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
6 changes: 5 additions & 1 deletion lib/IRGen/GenCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,12 @@ void irgen::emitScalarExistentialDowncast(IRGenFunction &IGF,
bool checkSuperclassConstraint = false;
if (hasSuperclassConstraint) {
Type srcSuperclassType = srcInstanceType;
if (srcSuperclassType->isExistentialType())
if (srcSuperclassType->isExistentialType()) {
srcSuperclassType = srcSuperclassType->getSuperclass();
// Look for an AnyObject superclass (getSuperclass() returns nil).
if (!srcSuperclassType && srcInstanceType->isClassExistentialType())
checkSuperclassConstraint = true;
}
if (srcSuperclassType) {
checkSuperclassConstraint =
!destInstanceType->getSuperclass()->isExactSuperclassOf(
Expand Down
48 changes: 48 additions & 0 deletions test/IRGen/casts.sil
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,51 @@ entry(%a : $OA):
%b = unconditional_checked_cast %a : $OA to $OB
return %b : $OB
}

protocol P {}
protocol PAnyObject: AnyObject {}
class C {}
sil_vtable C {}

// CHECK-LABEL: define{{.*}} @cast_protocol_composition_with_anyobject
// CHECK: [[C:%.*]] = call swiftcc %swift.metadata_response @"$s5casts1CCMa"
// CHECK: [[C_META:%.*]] = extractvalue %swift.metadata_response [[C]], 0
// CHECK: call { i8*, i8** } @dynamic_cast_existential_1_superclass_conditional({{.*}}, %swift.type* [[C_META]], %swift.protocol* {{.*}}@"$s5casts1PMp"

sil @cast_protocol_composition_with_anyobject : $@convention(thin) (@owned P & AnyObject ) -> @owned Optional<C & P> {
bb0(%0: $P & AnyObject):
checked_cast_br %0 : $P & AnyObject to $C & P, bb1, bb2

bb1(%2 : $C & P):
%3 = enum $Optional<C & P>, #Optional.some!enumelt.1, %2 : $C & P
br bb3(%3 : $Optional<C & P>)

bb2:
strong_release %0 : $P & AnyObject
%6 = enum $Optional<C & P>, #Optional.none!enumelt
br bb3(%6 : $Optional<C & P>)

bb3(%11 : $Optional<C & P>):
return %11 : $Optional<C & P>
}

// CHECK-LABEL: define{{.*}} @cast_protocol_with_anyobject
// CHECK: [[C:%.*]] = call swiftcc %swift.metadata_response @"$s5casts1CCMa"
// CHECK: [[C_META:%.*]] = extractvalue %swift.metadata_response [[C]], 0
// CHECK: call { i8*, i8** } @dynamic_cast_existential_1_superclass_conditional({{.*}}, %swift.type* [[C_META]], %swift.protocol* {{.*}}@"$s5casts10PAnyObjectMp"
sil @cast_protocol_with_anyobject : $@convention(thin) (@owned PAnyObject ) -> @owned Optional<C & PAnyObject> {
bb0(%0: $PAnyObject):
checked_cast_br %0 : $PAnyObject to $C & PAnyObject, bb1, bb2

bb1(%2 : $C & PAnyObject):
%3 = enum $Optional<C & PAnyObject>, #Optional.some!enumelt.1, %2 : $C & PAnyObject
br bb3(%3 : $Optional<C & PAnyObject>)

bb2:
strong_release %0 : $PAnyObject
%6 = enum $Optional<C & PAnyObject>, #Optional.none!enumelt
br bb3(%6 : $Optional<C & PAnyObject>)

bb3(%11 : $Optional<C & PAnyObject>):
return %11 : $Optional<C & PAnyObject>
}
20 changes: 20 additions & 0 deletions test/Interpreter/casts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,23 @@ Casts.test("testConditionalBridgedCastFromSwiftToNSObjectDerivedClass") {
}
expectEqual(0, LifetimeTracked.instances)
}

protocol Q {}
class K { }
class D: Q {}
typealias AnyQ = Q & AnyObject
typealias KQ = K & Q

Casts.test("testCastProtocolCompoWithAnyObjectToProtocolCompoTypeSuperclass") {
let shouldBeNil = (D() as AnyQ) as? KQ
expectNil(shouldBeNil)
}

protocol QAny: AnyObject {}
typealias KQAny = K & QAny
class F: QAny {}

Casts.test("testCastProtocolWithAnyObjectToProtocolCompoTypeSuperclass") {
let shouldBeNil = (F() as QAny) as? KQAny
expectNil(shouldBeNil)
}