Skip to content

Commit 663f068

Browse files
authored
Merge pull request #70141 from apple/es-res
Treat package as resilient as non-frozen public by default
2 parents 47ee847 + c24a003 commit 663f068

10 files changed

+1258
-128
lines changed

include/swift/AST/AccessScope.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class AccessScope {
7575
bool isFileScope() const;
7676
bool isInternal() const;
7777
bool isPackage() const;
78+
bool isPublicOrPackage() const { return isPublic() || isPackage(); }
7879

7980
/// Checks if the DeclContext of this (use site) access scope is more
8081
/// restrictive than that of the argument (decl site) based on the DeclContext

lib/AST/Decl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3003,7 +3003,7 @@ bool AbstractStorageDecl::isFormallyResilient() const {
30033003
// Non-public global and static variables always have a
30043004
// fixed layout.
30053005
if (!getFormalAccessScope(/*useDC=*/nullptr,
3006-
/*treatUsableFromInlineAsPublic=*/true).isPublic())
3006+
/*treatUsableFromInlineAsPublic=*/true).isPublicOrPackage())
30073007
return false;
30083008

30093009
return true;
@@ -5004,10 +5004,10 @@ int TypeDecl::compare(const TypeDecl *type1, const TypeDecl *type2) {
50045004
}
50055005

50065006
bool NominalTypeDecl::isFormallyResilient() const {
5007-
// Private, (unversioned) internal, and package types always have a
5007+
// Private and (unversioned) internal types always have a
50085008
// fixed layout.
50095009
if (!getFormalAccessScope(/*useDC=*/nullptr,
5010-
/*treatUsableFromInlineAsPublic=*/true).isPublic())
5010+
/*treatUsableFromInlineAsPublic=*/true).isPublicOrPackage())
50115011
return false;
50125012

50135013
// Check for an explicit @_fixed_layout or @frozen attribute.

test/IRGen/Inputs/package_types/resilient_class.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ package class ResilientGenericOutsideParent<A> {
6565
}
6666
}
6767

68+
public class PublicGenericOutsideParent<A> {
69+
public var property: A
70+
public init(property: A) {
71+
self.property = property
72+
print("PublicGenericOutsideParent.init()")
73+
}
74+
75+
public func method() {
76+
print("PublicGenericOutsideParent.method()")
77+
}
78+
79+
public class func classMethod() {
80+
print("PublicGenericOutsideParent.classMethod()")
81+
}
82+
}
83+
6884

6985
// Resilient generic subclass
7086

test/IRGen/Inputs/package_types/resilient_enum.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ package enum SimpleShape {
66
case Triangle(Size)
77
}
88

9+
public enum PublicSimpleShape {
10+
case pbKleinBottle
11+
case pbTriangle(PublicSize)
12+
}
13+
914
// Fixed-layout enum with resilient members
1015
package enum Shape {
1116
case Point
@@ -19,6 +24,11 @@ package enum FunnyShape {
1924
indirect case Trapezoid(Size)
2025
}
2126

27+
public enum PublicFunnyShape {
28+
indirect case Parallelogram(PublicSize)
29+
indirect case Trapezoid(PublicSize)
30+
}
31+
2232
package enum FullyFixedLayout {
2333
case noPayload
2434
case hasPayload(Int)
@@ -58,6 +68,18 @@ package enum Medium {
5868
case Canvas // 1
5969
}
6070

71+
public enum PublicMedium {
72+
// Indirect case
73+
indirect case Pamphlet(PublicMedium) // -1
74+
75+
// Case with resilient payload
76+
case Postcard(PublicSize) // -2
77+
78+
// Empty cases
79+
case Paper // 0
80+
case Canvas // 1
81+
}
82+
6183
// Indirect resilient enum
6284
package indirect enum IndirectApproach {
6385
case Angle(Double) // -1

test/IRGen/Inputs/package_types/resilient_struct.swift

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Fixed-layout struct
21
package struct Point {
32
package var x: Int // read-write stored property
43
package let y: Int // read-only stored property
@@ -12,7 +11,6 @@ package struct Point {
1211
package mutating func mutantMethod() {}
1312
}
1413

15-
// Resilient-layout struct
1614
package struct Size {
1715
package var w: Int // should have getter and setter
1816
package let h: Int // getter only
@@ -26,7 +24,6 @@ package struct Size {
2624
package mutating func mutantMethod() {}
2725
}
2826

29-
// Fixed-layout struct with resilient members
3027
package struct Rectangle {
3128
package let p: Point
3229
package let s: Size
@@ -119,3 +116,66 @@ package struct UnavailableResilientInt {
119116
self.i = i
120117
}
121118
}
119+
120+
121+
public struct PublicPoint {
122+
public var x: Int // read-write stored property
123+
public let y: Int // read-only stored property
124+
125+
public init(x: Int, y: Int) {
126+
self.x = x
127+
self.y = y
128+
}
129+
130+
public func method() {}
131+
public mutating func mutantMethod() {}
132+
}
133+
134+
@frozen
135+
public struct FrozenPublicPoint {
136+
public var x: Int // read-write stored property
137+
public let y: Int // read-only stored property
138+
139+
public init(x: Int, y: Int) {
140+
self.x = x
141+
self.y = y
142+
}
143+
144+
public func method() {}
145+
public mutating func mutantMethod() {}
146+
}
147+
148+
public struct PublicSize {
149+
public var w: Int // should have getter and setter
150+
public let h: Int // getter only
151+
152+
public init(w: Int, h: Int) {
153+
self.w = w
154+
self.h = h
155+
}
156+
157+
public func method() {}
158+
public mutating func mutantMethod() {}
159+
}
160+
161+
@frozen
162+
public struct FrozenPublicSize {
163+
public var w: Int // should have getter and setter
164+
public let h: Int // getter only
165+
166+
public init(w: Int, h: Int) {
167+
self.w = w
168+
self.h = h
169+
}
170+
171+
public func method() {}
172+
public mutating func mutantMethod() {}
173+
}
174+
175+
public struct PublicResilientInt {
176+
public let i: Int
177+
178+
public init(i: Int) {
179+
self.i = i
180+
}
181+
}

test/IRGen/package_resilience.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// possible.
66
//
77

8+
// REQUIRES: rdar118947451
9+
810
// RUN: %empty-directory(%t)
911
// RUN: %{python} %utils/chex.py < %s > %t/package_resilience.swift
1012
// RUN: %target-swift-frontend -package-name MyPkg -emit-module -enable-library-evolution -emit-module-path=%t/resilient_struct.swiftmodule -module-name=resilient_struct %S/Inputs/package_types/resilient_struct.swift

0 commit comments

Comments
 (0)