Skip to content

Commit a99716b

Browse files
committed
Visit var/subscript opaque return decls during IRGen/TBDGen.
1 parent b57f352 commit a99716b

File tree

6 files changed

+52
-7
lines changed

6 files changed

+52
-7
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1817,8 +1817,8 @@ void IRGenModule::emitGlobalDecl(Decl *D) {
18171817
DebugInfo->emitImport(cast<ImportDecl>(D));
18181818
return;
18191819

1820-
// We emit these as part of the PatternBindingDecl.
18211820
case DeclKind::Var:
1821+
emitAbstractStorageDecl(cast<AbstractStorageDecl>(D));
18221822
return;
18231823

18241824
case DeclKind::Accessor:
@@ -3724,6 +3724,9 @@ void IRGenModule::emitNestedTypeDecls(DeclRange members) {
37243724

37253725
case DeclKind::Var:
37263726
case DeclKind::Subscript:
3727+
emitAbstractStorageDecl(cast<AbstractStorageDecl>(member));
3728+
continue;
3729+
37273730
case DeclKind::PatternBinding:
37283731
case DeclKind::Accessor:
37293732
case DeclKind::Constructor:

lib/IRGen/GenStruct.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,14 @@ void IRGenModule::emitFuncDecl(FuncDecl *fd) {
887887
}
888888
}
889889

890+
void IRGenModule::emitAbstractStorageDecl(AbstractStorageDecl *fd) {
891+
// If there's an opaque return type for this function, emit its descriptor.
892+
if (auto opaque = fd->getOpaqueResultTypeDecl()) {
893+
if (!IRGen.hasLazyMetadata(opaque))
894+
emitOpaqueTypeDecl(opaque);
895+
}
896+
}
897+
890898
namespace {
891899
/// A type implementation for resilient struct types. This is not a
892900
/// StructTypeInfoBase at all, since we don't know anything about

lib/IRGen/IRGenModule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,7 @@ private: \
12021202
void emitClassDecl(ClassDecl *D);
12031203
void emitExtension(ExtensionDecl *D);
12041204
void emitFuncDecl(FuncDecl *D);
1205+
void emitAbstractStorageDecl(AbstractStorageDecl *D);
12051206
void emitOpaqueTypeDecl(OpaqueTypeDecl *D);
12061207
void emitSILGlobalVariable(SILGlobalVariable *gv);
12071208
void emitCoverageMapping();

lib/TBDGen/TBDGen.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,9 @@ void TBDGenVisitor::visitAbstractFunctionDecl(AbstractFunctionDecl *AFD) {
232232

233233
void TBDGenVisitor::visitFuncDecl(FuncDecl *FD) {
234234
// If there's an opaque return type, its descriptor is exported.
235-
if (auto opaqueResult = FD->getOpaqueResultTypeDecl())
235+
if (auto opaqueResult = FD->getOpaqueResultTypeDecl()) {
236236
addSymbol(LinkEntity::forOpaqueTypeDescriptor(opaqueResult));
237+
}
237238
visitAbstractFunctionDecl(FD);
238239
}
239240

@@ -249,6 +250,11 @@ void TBDGenVisitor::visitAbstractStorageDecl(AbstractStorageDecl *ASD) {
249250
if (ASD->exportsPropertyDescriptor()) {
250251
addSymbol(LinkEntity::forPropertyDescriptor(ASD));
251252
}
253+
254+
// ...and the opaque result decl if it has one.
255+
if (auto opaqueResult = ASD->getOpaqueResultTypeDecl()) {
256+
addSymbol(LinkEntity::forOpaqueTypeDescriptor(opaqueResult));
257+
}
252258

253259
// Explicitly look at each accessor here: see visitAccessorDecl.
254260
for (auto accessor : ASD->getAllAccessors()) {

test/IRGen/opaque_result_type.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// RUN: %{python} %utils/chex.py < %s > %t/opaque_result_type.swift
33
// RUN: %target-swift-frontend -emit-ir %t/opaque_result_type.swift | %FileCheck --check-prefix=CHECK --check-prefix=CHECK-NODEBUG %t/opaque_result_type.swift
44

5-
protocol O {
5+
public protocol O {
66
func bar()
77
}
8-
protocol O2 {
8+
public protocol O2 {
99
func baz()
1010
}
1111

@@ -21,12 +21,12 @@ protocol Q: AnyObject {
2121
}
2222

2323
extension Int: O, O2 {
24-
func bar() {}
25-
func baz() {}
24+
public func bar() {}
25+
public func baz() {}
2626
}
2727

2828
extension String: P {
29-
// CHECK-LABEL: @"$sSS18opaque_result_typeE3pooQryFQOMQ" = {{.*}} constant <{ {{.*}} }> <{
29+
// CHECK-LABEL: @"$sSS18opaque_result_typeE3pooQryFQOMQ" = {{.*}}constant <{ {{.*}} }> <{
3030
// -- header: opaque type context (0x4), generic (0x80), unique (0x40), two entries (0x2_0000)
3131
// CHECK-SAME: <i32 0x2_00c4>,
3232
// -- parent context: module, or anon context for function
@@ -39,6 +39,21 @@ extension String: P {
3939
func poo() -> some O {
4040
return 0
4141
}
42+
43+
// CHECK-LABEL: @"$sSS18opaque_result_typeE4propQrvpQOMQ" = {{.*}}constant
44+
public var prop: some O {
45+
return 0
46+
}
47+
48+
// CHECK-LABEL: @"$sSS18opaque_result_typeEQrycipQOMQ" = {{.*}}constant
49+
public subscript() -> some O {
50+
return 0
51+
}
52+
}
53+
54+
// CHECK-LABEL: @"$s18opaque_result_type10globalPropQrvpQOMQ" = {{.*}}constant
55+
public var globalProp: some O {
56+
return 0
4257
}
4358

4459
public class C: P, Q {

test/TBD/opaque_result_type.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ extension Int: O, O2 {
2424
public func baz() {}
2525
}
2626

27+
public var globalProp: some O {
28+
return 0
29+
}
30+
2731
public class C: P, Q {
2832
public func poo() -> some O {
2933
return 0
@@ -32,6 +36,14 @@ public class C: P, Q {
3236
public func qoo() -> some O & O2 {
3337
return 0
3438
}
39+
40+
public var prop: some O {
41+
return 0
42+
}
43+
44+
public subscript() -> some O {
45+
return 0
46+
}
3547
}
3648

3749
public func foo(x: String) -> some P {

0 commit comments

Comments
 (0)