Skip to content

Commit e71bfd3

Browse files
authored
Merge pull request #25600 from jckarter/opaque-xref-extension-module
Serialization: Deserialize opaque type xrefs from the right extension module.
2 parents de4512d + 70aba4d commit e71bfd3

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

include/swift/AST/Decl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,6 +2853,11 @@ class OpaqueTypeDecl : public GenericTypeDecl {
28532853

28542854
ValueDecl *getNamingDecl() const { return NamingDecl; }
28552855

2856+
void setNamingDecl(ValueDecl *D) {
2857+
assert(!NamingDecl && "already have naming decl");
2858+
NamingDecl = D;
2859+
}
2860+
28562861
GenericSignature *getOpaqueInterfaceGenericSignature() const {
28572862
return OpaqueInterfaceGenericSignature;
28582863
}

lib/Serialization/Deserialization.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,9 +1729,10 @@ ModuleFile::resolveCrossReference(ModuleID MID, uint32_t pathLen) {
17291729

17301730
auto name = getIdentifier(DefiningDeclNameID);
17311731
pathTrace.addOpaqueReturnType(name);
1732-
1733-
if (auto opaqueTy = baseModule->lookupOpaqueResultType(name.str(),
1734-
nullptr)) {
1732+
1733+
auto lookupModule = M ? M : baseModule;
1734+
if (auto opaqueTy = lookupModule->lookupOpaqueResultType(name.str(),
1735+
nullptr)) {
17351736
values.push_back(opaqueTy);
17361737
}
17371738
break;
@@ -3156,7 +3157,6 @@ class swift::DeclDeserializer {
31563157
interfaceTypeID, genericEnvID,
31573158
underlyingTypeID);
31583159

3159-
auto namingDecl = cast<ValueDecl>(MF.getDecl(namingDeclID));
31603160
auto declContext = MF.getDeclContext(contextID);
31613161
auto sig = MF.getGenericSignature(interfaceSigID);
31623162
auto interfaceType = MF.getType(interfaceTypeID)
@@ -3168,10 +3168,13 @@ class swift::DeclDeserializer {
31683168

31693169
// Create the decl.
31703170
auto opaqueDecl =
3171-
new (ctx) OpaqueTypeDecl(namingDecl, nullptr, declContext,
3171+
new (ctx) OpaqueTypeDecl(nullptr, nullptr, declContext,
31723172
sig, interfaceType);
31733173
declOrOffset = opaqueDecl;
31743174

3175+
auto namingDecl = cast<ValueDecl>(MF.getDecl(namingDeclID));
3176+
opaqueDecl->setNamingDecl(namingDecl);
3177+
31753178
if (auto genericParams = MF.maybeReadGenericParams(opaqueDecl))
31763179
opaqueDecl->setGenericParams(genericParams);
31773180

test/SIL/Serialization/Inputs/OpaqueReturnTypeExporter.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@ public protocol Butt {}
22
extension Int: Butt {}
33

44
public func exportsOpaqueReturn() -> some Butt { return 0 }
5+
6+
extension Int {
7+
public func someButt() -> some Butt {
8+
return self
9+
}
10+
}

test/SIL/Serialization/opaque_return_type_serialize.sil

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,27 @@
33
// RUN: %target-sil-opt -I %t %s -emit-sib -module-name test -o %t/test.sib
44
// RUN: %target-swift-frontend -disable-availability-checking -I %t -emit-ir %t/test.sib
55

6+
import Swift
67
import OpaqueReturnTypeExporter
78

89
typealias SomeButt = @_opaqueReturnTypeOf("$s24OpaqueReturnTypeExporter07exportsaB0QryF", 0) opaque
10+
typealias SomeButt2 = @_opaqueReturnTypeOf("$sSi24OpaqueReturnTypeExporterE8someButtQryF", 0) opaque
911

1012
sil @$s24OpaqueReturnTypeExporter07exportsaB0QryF : $@convention(thin) () -> @out SomeButt
13+
sil @$sSi24OpaqueReturnTypeExporterE8someButtQryF : $@convention(thin) (Int) -> @out SomeButt2
1114

12-
sil @use_opaque_type : $@convention(thin) () -> () {
13-
entry:
15+
sil @use_opaque_type : $@convention(thin) (Int) -> () {
16+
entry(%a : $Int):
1417
%f = function_ref @$s24OpaqueReturnTypeExporter07exportsaB0QryF : $@convention(thin) () -> @out SomeButt
1518
%x = alloc_stack $SomeButt
1619
apply %f(%x) : $@convention(thin) () -> @out SomeButt
1720
destroy_addr %x : $*SomeButt
1821
dealloc_stack %x : $*SomeButt
22+
23+
%g = function_ref @$sSi24OpaqueReturnTypeExporterE8someButtQryF : $@convention(thin) (Int) -> @out SomeButt2
24+
%y = alloc_stack $SomeButt2
25+
apply %g(%y, %a) : $@convention(thin) (Int) -> @out SomeButt2
26+
destroy_addr %y : $*SomeButt2
27+
dealloc_stack %y : $*SomeButt2
1928
return undef : $()
2029
}

0 commit comments

Comments
 (0)