Skip to content

Commit d20c727

Browse files
Merge pull request #31111 from aschwaighofer/irgen_keypath_context_type
IRGen: Map interface to archetypes in keypath function
2 parents e37e475 + 87820b4 commit d20c727

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

lib/IRGen/GenKeyPath.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,11 @@ emitKeyPathComponent(IRGenModule &IGM,
10381038
// native class resilience. We never directly access ObjC-imported
10391039
// ivars so we can disregard ObjC ivar resilience for this computation
10401040
// and start counting at the Swift native root.
1041+
if (loweredClassTy.getASTType()->hasTypeParameter())
1042+
loweredClassTy = SILType::getPrimitiveObjectType(
1043+
GenericEnvironment::mapTypeIntoContext(
1044+
genericEnv, loweredClassTy.getASTType())
1045+
->getCanonicalType());
10411046
switch (getClassFieldAccess(IGM, loweredClassTy, property)) {
10421047
case FieldAccess::ConstantDirect:
10431048
case FieldAccess::ConstantIndirect:

test/IRGen/Inputs/ResilientBase.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
@propertyWrapper
2+
public struct Wrapper<Value> {
3+
var value: Value
4+
5+
public init(wrappedValue: Value) {
6+
self.init(initialValue: wrappedValue)
7+
}
8+
9+
public init(initialValue: Value) {
10+
value = initialValue
11+
}
12+
13+
public var wrappedValue: Value {
14+
get { fatalError("unreachable") }
15+
set { fatalError("unreachable") }
16+
}
17+
18+
public static subscript<T: AnyObject>(
19+
_enclosingInstance object: T,
20+
wrapped wrappedKeyPath: ReferenceWritableKeyPath<T, Value>,
21+
storage storageKeyPath: ReferenceWritableKeyPath<T, Self>
22+
) -> Value {
23+
get {
24+
object[keyPath: storageKeyPath].value
25+
}
26+
set {
27+
object[keyPath: storageKeyPath].value = newValue
28+
}
29+
}
30+
}
31+
32+
open class ResilientBase {
33+
public init() {}
34+
}

test/IRGen/keypath.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -enable-library-evolution -emit-module-path=%t/ResilientBase.swiftmodule -module-name=ResilientBase %S/Inputs/ResilientBase.swift
3+
// RUN: %target-swift-frontend -primary-file %s -emit-ir -I %t -profile-generate -profile-coverage-mapping
4+
5+
// This test use to crash because we emitted a FieldAccess::NonConstantDirect for _t.
6+
7+
import ResilientBase
8+
9+
class TheTest {
10+
func test_resilientSubclassCrasher() {
11+
final class SubclassOfResilientBase<T> : ResilientBase {
12+
@Wrapper var t: T
13+
14+
init(_ t: T) {
15+
self.t = t
16+
}
17+
}
18+
_ = SubclassOfResilientBase(0)
19+
}
20+
}

0 commit comments

Comments
 (0)