Skip to content

Commit 565f3ec

Browse files
authored
Merge pull request #66584 from tshortli/usable-from-inline-opaque-type-decl-linkage-5.9
[5.9] AST: Inherit access level of opaque type decls from naming decl
2 parents 0df6e4d + b2fb233 commit 565f3ec

7 files changed

+49
-3
lines changed

lib/AST/AccessRequests.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ AccessLevelRequest::evaluate(Evaluator &evaluator, ValueDecl *D) const {
6767
}
6868
}
6969

70+
// Special case for opaque type decls, which inherit the access of their
71+
// naming decls.
72+
if (auto *opaqueType = dyn_cast<OpaqueTypeDecl>(D)) {
73+
if (auto *namingDecl = opaqueType->getNamingDecl())
74+
return namingDecl->getFormalAccess();
75+
}
76+
7077
DeclContext *DC = D->getDeclContext();
7178

7279
// Special case for generic parameters; we just give them a dummy

lib/AST/Decl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3737,6 +3737,15 @@ bool ValueDecl::isUsableFromInline() const {
37373737
return true;
37383738
}
37393739

3740+
if (auto *opaqueType = dyn_cast<OpaqueTypeDecl>(this)) {
3741+
if (auto *namingDecl = opaqueType->getNamingDecl()) {
3742+
if (namingDecl->getAttrs().hasAttribute<UsableFromInlineAttr>() ||
3743+
namingDecl->getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>() ||
3744+
namingDecl->getAttrs().hasAttribute<InlinableAttr>())
3745+
return true;
3746+
}
3747+
}
3748+
37403749
if (auto *EED = dyn_cast<EnumElementDecl>(this))
37413750
if (EED->getParentEnum()->getAttrs().hasAttribute<UsableFromInlineAttr>())
37423751
return true;

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ OpaqueResultTypeRequest::evaluate(Evaluator &evaluator,
213213
auto opaqueDecl = OpaqueTypeDecl::get(
214214
originatingDecl, genericParams, parentDC, interfaceSignature,
215215
opaqueReprs);
216-
opaqueDecl->copyFormalAccessFrom(originatingDecl);
217216
if (auto originatingSig = originatingDC->getGenericSignatureOfContext()) {
218217
opaqueDecl->setGenericSignature(originatingSig);
219218
} else {

test/IRGen/Inputs/AlwaysInlineIntoWithOpaque.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,16 @@ public func testInlineWithOpaque() -> some P {
1414
}
1515
return 2.0
1616
}
17+
18+
@_alwaysEmitIntoClient
19+
public func testInlineWithOpaqueUsableFromInline() -> some P {
20+
if #available(macOS 9.0, *) {
21+
return usableFromInline()
22+
}
23+
return 4.0
24+
}
25+
26+
@usableFromInline
27+
func usableFromInline() -> some P {
28+
return 3
29+
}

test/IRGen/Inputs/AlwaysInlineIntoWithOpaqueReplacement.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ extension Int : P {
66

77
extension Double : P {
88
}
9+
10+
@usableFromInline
11+
func usableFromInline() -> some P {
12+
return 3
13+
}

test/IRGen/opaque_result_alwaysInlineIntoClient.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-build-swift -target %target-cpu-apple-macosx10.15 -parse-as-library -emit-library -emit-module-path %t/AlwaysInlineIntoWithOpaque.swiftmodule -module-name AlwaysInlineIntoWithOpaque %S/Inputs/AlwaysInlineIntoWithOpaque.swift -o %t/%target-library-name(AlwaysInlineIntoWithOpaque)
2+
// RUN: %target-build-swift -target %target-cpu-apple-macosx10.15 -parse-as-library -emit-library -emit-module-path %t/AlwaysInlineIntoWithOpaque.swiftmodule -module-name AlwaysInlineIntoWithOpaque -enable-library-evolution %S/Inputs/AlwaysInlineIntoWithOpaque.swift -o %t/%target-library-name(AlwaysInlineIntoWithOpaque)
33
// RUN: %target-codesign %t/%target-library-name(AlwaysInlineIntoWithOpaque)
44
// RUN: %target-build-swift -target %target-cpu-apple-macosx10.15 -lAlwaysInlineIntoWithOpaque -module-name main -I %t -L %t %s -o %t/a.out
55
// RUN: %target-codesign %t/a.out
66
// RUN: %target-run %t/a.out | %FileCheck %s
77

8-
// RUN: %target-build-swift -target %target-cpu-apple-macosx10.15 -parse-as-library -emit-library -emit-module-path %t/AlwaysInlineIntoWithOpaque.swiftmodule -module-name AlwaysInlineIntoWithOpaque %S/Inputs/AlwaysInlineIntoWithOpaqueReplacement.swift -o %t/%target-library-name(AlwaysInlineIntoWithOpaque)
8+
// RUN: %target-build-swift -target %target-cpu-apple-macosx10.15 -parse-as-library -emit-library -emit-module-path %t/AlwaysInlineIntoWithOpaque.swiftmodule -module-name AlwaysInlineIntoWithOpaque -enable-library-evolution %S/Inputs/AlwaysInlineIntoWithOpaqueReplacement.swift -o %t/%target-library-name(AlwaysInlineIntoWithOpaque)
99
// RUN: %target-codesign %t/a.out
1010
// RUN: %target-run %t/a.out | %FileCheck %s
1111

@@ -27,3 +27,11 @@ public func test() {
2727

2828
test()
2929
// CHECK: 1
30+
31+
public func testUsableFromInline() {
32+
let p = testInlineWithOpaqueUsableFromInline()
33+
print(p)
34+
}
35+
36+
testUsableFromInline()
37+
// CHECK: 3

test/TBD/opaque_result_type.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public func dynReplacement(x: String) -> some P {
7777
return "replaced"
7878
}
7979

80+
@usableFromInline
81+
func ufi() -> some O {
82+
return 1
83+
}
84+
8085
extension String: P {
8186
public func poo() -> some O {
8287
return 0

0 commit comments

Comments
 (0)