Skip to content

Commit faec586

Browse files
authored
Merge pull request swiftlang#30489 from jckarter/swift-52-opaque-type-mangling
IRGen: Use mangled names to access opaque type associated types in Swift >=5.2.
2 parents b4abd44 + b636a07 commit faec586

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

lib/IRGen/MetadataRequest.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,17 +2217,19 @@ static bool shouldAccessByMangledName(IRGenModule &IGM, CanType type) {
22172217
}
22182218

22192219
// The Swift 5.1 runtime fails to demangle associated types of opaque types.
2220-
auto hasNestedOpaqueArchetype = type.findIf([](CanType sub) -> bool {
2221-
if (auto archetype = dyn_cast<NestedArchetypeType>(sub)) {
2222-
if (isa<OpaqueTypeArchetypeType>(archetype->getRoot())) {
2223-
return true;
2220+
if (!IGM.getAvailabilityContext().isContainedIn(IGM.Context.getSwift52Availability())) {
2221+
auto hasNestedOpaqueArchetype = type.findIf([](CanType sub) -> bool {
2222+
if (auto archetype = dyn_cast<NestedArchetypeType>(sub)) {
2223+
if (isa<OpaqueTypeArchetypeType>(archetype->getRoot())) {
2224+
return true;
2225+
}
22242226
}
2225-
}
2226-
return false;
2227-
});
2228-
2229-
if (hasNestedOpaqueArchetype)
2230-
return false;
2227+
return false;
2228+
});
2229+
2230+
if (hasNestedOpaqueArchetype)
2231+
return false;
2232+
}
22312233

22322234
return true;
22332235

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %target-swift-frontend -target x86_64-apple-macosx10.99 -parse-stdlib -emit-ir %s | %FileCheck %s --check-prefix=CHECK --check-prefix=SWIFT_52
2+
// RUN: %target-swift-frontend -target x86_64-apple-macosx10.15 -parse-stdlib -emit-ir %s | %FileCheck %s --check-prefix=CHECK --check-prefix=SWIFT_PRE_52
3+
4+
protocol P {
5+
associatedtype A
6+
var a: A { get }
7+
}
8+
9+
@_silgen_name("useMetadata") func useMetadata<T>(_: T)
10+
11+
struct X: P { var a: X { return self } }
12+
13+
dynamic func foo() -> some P { return X() }
14+
15+
// CHECK-LABEL: define{{.*}}3bar
16+
public func bar() {
17+
// SWIFT_52: @__swift_instantiateConcreteTypeFromMangledName({{.*}} @"$s39access_type_metadata_by_mangled_name_513fooQryFQOyQo_1AAA1PPQxMD")
18+
// SWIFT_PRE_52: @"$s39access_type_metadata_by_mangled_name_513fooQryFQOyQo_1AAA1PPQxMa"(
19+
useMetadata(foo().a)
20+
}

0 commit comments

Comments
 (0)