Skip to content

Commit 61057b8

Browse files
authored
[ModuleInterface] Print names for @usableFromInline struct properties (#19465)
Because they weren't 'public' we were treating them as layout-only properties and printing `var _`, but they get referenced in inlinable functions. We need the actual name!
1 parent b29f010 commit 61057b8

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ void PrintOptions::clearSynthesizedExtension() {
6464
TransformContext.reset();
6565
}
6666

67+
static bool isPublicOrUsableFromInline(const ValueDecl *VD) {
68+
AccessScope scope =
69+
VD->getFormalAccessScope(/*useDC*/nullptr,
70+
/*treatUsableFromInlineAsPublic*/true);
71+
return scope.isPublic();
72+
}
73+
6774
static bool contributesToParentTypeStorage(const AbstractStorageDecl *ASD) {
6875
auto *DC = ASD->getDeclContext()->getAsDecl();
6976
if (!DC) return false;
@@ -96,10 +103,7 @@ PrintOptions PrintOptions::printTextualInterfaceFile() {
96103
bool shouldPrint(const Decl *D, const PrintOptions &options) override {
97104
// Skip anything that isn't 'public' or '@usableFromInline'.
98105
if (auto *VD = dyn_cast<ValueDecl>(D)) {
99-
AccessScope accessScope =
100-
VD->getFormalAccessScope(/*useDC*/nullptr,
101-
/*treatUsableFromInlineAsPublic*/true);
102-
if (!accessScope.isPublic()) {
106+
if (!isPublicOrUsableFromInline(VD)) {
103107
// We do want to print private stored properties, without their
104108
// original names present.
105109
if (auto *ASD = dyn_cast<AbstractStorageDecl>(VD))
@@ -894,7 +898,7 @@ void PrintAST::printPattern(const Pattern *pattern) {
894898
recordDeclLoc(decl, [&]{
895899
if (Options.OmitNameOfInaccessibleProperties &&
896900
contributesToParentTypeStorage(decl) &&
897-
decl->getFormalAccess() < AccessLevel::Public)
901+
!isPublicOrUsableFromInline(decl))
898902
Printer << "_";
899903
else
900904
Printer.printName(named->getBoundName());

test/ModuleInterface/private-stored-member-type-layout.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
// These two appear out-of-order between run lines
1818

1919
// CHECK-DAG: [[MYCLASS:%T20PrivateStoredMembers7MyClassC]] = type opaque
20-
// CHECK-DAG: [[MYSTRUCT:%T20PrivateStoredMembers8MyStructV]] = type <{ %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V }>
20+
// CHECK-DAG: [[MYSTRUCT:%T20PrivateStoredMembers8MyStructV]] = type <{ %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V }>
2121

22-
// CHECK-MAIN-DAG: [[MYCLASS:%T4main7MyClassC]] = type <{ %swift.refcounted, %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V }>
23-
// CHECK-MAIN-DAG: [[MYSTRUCT:%T4main8MyStructV]] = type <{ %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V }>
22+
// CHECK-MAIN-DAG: [[MYCLASS:%T4main7MyClassC]] = type <{ %swift.refcounted, %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V }>
23+
// CHECK-MAIN-DAG: [[MYSTRUCT:%T4main8MyStructV]] = type <{ %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V, %TSb, [7 x i8], %Ts5Int64V }>
2424

2525
#if SHOULD_IMPORT
2626
import PrivateStoredMembers
@@ -29,7 +29,7 @@ import PrivateStoredMembers
2929
// CHECK-EXEC: swiftcc void @"$s{{[^ ]+}}8makeUseryyF"() #0 {
3030
public func makeUser() {
3131
let ptr = UnsafeMutablePointer<MyStruct>.allocate(capacity: 1)
32-
// CHECK-EXEC: %.publicEndVar = getelementptr inbounds [[MYSTRUCT]], [[MYSTRUCT]]* %{{[0-9]+}}, i32 0, i32 [[PUBLIC_END_VAR_IDX:9]]
32+
// CHECK-EXEC: %.publicEndVar = getelementptr inbounds [[MYSTRUCT]], [[MYSTRUCT]]* %{{[0-9]+}}, i32 0, i32 [[PUBLIC_END_VAR_IDX:12]]
3333
// CHECK-EXEC: %.publicEndVar._value = getelementptr inbounds %Ts5Int64V, %Ts5Int64V* %.publicEndVar, i32 0, i32 0
3434
// CHECK-EXEC: store i64 4, i64* %.publicEndVar._value
3535
ptr.pointee.publicEndVar = 4

test/ModuleInterface/private-stored-members.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ public struct MyStruct {
2929
// RESILIENT-NOT: internal let _: [[BOOL]]{{$}}
3030
let internalLet: Bool
3131

32+
// COMMON-NEXT: @usableFromInline
33+
// COMMON-NEXT: internal var ufiVar: [[INT64]]{{$}}
34+
@usableFromInline var ufiVar: Int64
35+
36+
// COMMON-NEXT: @usableFromInline
37+
// COMMON-NEXT: internal let ufiLet: [[BOOL]]{{$}}
38+
@usableFromInline let ufiLet: Bool
39+
3240
// CHECK-NEXT: private var _: [[INT64]]{{$}}
3341
// RESILIENT-NOT: private var _: [[INT64]]{{$}}
3442
private var privateVar: Int64
@@ -69,6 +77,14 @@ public class MyClass {
6977
// RESILIENT-NOT: internal let _: [[BOOL]]{{$}}
7078
let internalLet: Bool = true
7179

80+
// COMMON-NEXT: @usableFromInline
81+
// COMMON-NEXT: internal var ufiVar: [[INT64]]{{$}}
82+
@usableFromInline var ufiVar: Int64 = 0
83+
84+
// COMMON-NEXT: @usableFromInline
85+
// COMMON-NEXT: internal let ufiLet: [[BOOL]]{{$}}
86+
@usableFromInline let ufiLet: Bool = true
87+
7288
// CHECK-NEXT: private var _: [[INT64]]{{$}}
7389
// RESILIENT-NOT: private var _: [[INT64]]{{$}}
7490
private var privateVar: Int64 = 0

0 commit comments

Comments
 (0)