Skip to content

Commit 28af54a

Browse files
authored
Merge pull request #80913 from stzn/fix-crash-self-keypath-root
[SILGen]Check if baseType is DynamicSelfType
2 parents d10dfb3 + 3a8483e commit 28af54a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3397,7 +3397,11 @@ static ManagedValue emitKeyPathRValueBase(SILGenFunction &subSGF,
33973397

33983398
// Upcast a class instance to the property's declared type if necessary.
33993399
if (auto propertyClass = storage->getDeclContext()->getSelfClassDecl()) {
3400-
if (baseType->getClassOrBoundGenericClass() != propertyClass) {
3400+
if (auto selfType = baseType->getAs<DynamicSelfType>())
3401+
baseType = selfType->getSelfType()->getCanonicalType();
3402+
auto baseClass = baseType->getClassOrBoundGenericClass();
3403+
3404+
if (baseClass != propertyClass) {
34013405
baseType = baseType->getSuperclassForDecl(propertyClass)
34023406
->getCanonicalType();
34033407
paramSubstValue = subSGF.B.createUpcast(loc, paramSubstValue,

test/SILGen/keypaths.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,3 +805,24 @@ protocol HasAlias {
805805
func testHasAlias() {
806806
_ = \HasAlias.id
807807
}
808+
809+
// https://github.com/swiftlang/swift/issues/80669
810+
func type<Root, Value>(at keyPath: KeyPath<Root, Value>) -> Value.Type {
811+
return Value.self
812+
}
813+
814+
class DynamicSelfTypeTestClass {
815+
var other = DynamicSelfTypeTestClass()
816+
817+
static func staticFunc() {
818+
type(at: \Self.other).bar()
819+
}
820+
821+
static func bar() {
822+
print("Hello")
823+
}
824+
}
825+
826+
func testDynamicSelfType() {
827+
DynamicSelfTypeTestClass.staticFunc()
828+
}

0 commit comments

Comments
 (0)