Skip to content

Commit dd7f757

Browse files
authored
Merge pull request #19785 from harlanhaskins/static-shock
2 parents 6998e68 + 5f9ff02 commit dd7f757

File tree

4 files changed

+38
-40
lines changed

4 files changed

+38
-40
lines changed

lib/AST/Decl.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,14 +1302,9 @@ VarDecl *PatternBindingDecl::getSingleVar() const {
13021302
bool VarDecl::isInitExposedToClients() const {
13031303
auto parent = dyn_cast<NominalTypeDecl>(getDeclContext());
13041304
if (!parent) return false;
1305-
if (!hasInitialValue())
1306-
return false;
1307-
if (isStatic())
1308-
return false;
1309-
if (!parent->getAttrs().hasAttribute<FixedLayoutAttr>())
1310-
return false;
1311-
auto *module = parent->getModuleContext();
1312-
return module->getResilienceStrategy() == ResilienceStrategy::Resilient;
1305+
if (!hasInitialValue()) return false;
1306+
if (isStatic()) return false;
1307+
return parent->getAttrs().hasAttribute<FixedLayoutAttr>();
13131308
}
13141309

13151310
/// Check whether the given type representation will be

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ bool TypeChecker::diagnoseInlinableDeclRef(SourceLoc loc,
120120
return false;
121121
}
122122

123+
// Property initializers that are not exposed to clients are OK.
124+
if (auto pattern = dyn_cast<PatternBindingInitializer>(DC)) {
125+
auto bindingIndex = pattern->getBindingIndex();
126+
auto &patternEntry = pattern->getBinding()->getPatternList()[bindingIndex];
127+
auto varDecl = patternEntry.getAnchoringVarDecl();
128+
if (!varDecl->isInitExposedToClients())
129+
return false;
130+
}
131+
123132
DowngradeToWarning downgradeToWarning = DowngradeToWarning::No;
124133

125134
// Swift 4.2 did not perform any checks for type aliases.
Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,65 @@
11
// RUN: %empty-directory(%t)
22

33
// RUN: %target-swift-frontend -typecheck -emit-parseable-module-interface-path %t.swiftinterface %s
4-
// RUN: %FileCheck %s < %t.swiftinterface --check-prefix CHECK --check-prefix COMMON
4+
// RUN: %FileCheck %s < %t.swiftinterface
55

66
// RUN: %target-swift-frontend -typecheck -emit-parseable-module-interface-path %t-resilient.swiftinterface -enable-resilience %s
7-
// RUN: %FileCheck %s --check-prefix RESILIENT --check-prefix COMMON < %t-resilient.swiftinterface
7+
// RUN: %FileCheck %s < %t-resilient.swiftinterface
88

9-
// FIXME(rdar44993525): %target-swift-frontend -emit-module -o %t/Test.swiftmodule %t.swiftinterface -disable-objc-attr-requires-foundation-module
10-
// FIXME(rdar44993525): %target-swift-frontend -emit-module -o /dev/null -merge-modules %t/Test.swiftmodule -module-name Test -emit-parseable-module-interface-path - | %FileCheck %s --check-prefix CHECK --check-prefix COMMON
9+
// RUN: %target-swift-frontend -emit-module -o %t/Test.swiftmodule %t.swiftinterface -disable-objc-attr-requires-foundation-module
10+
// RUN: %target-swift-frontend -emit-module -o /dev/null -merge-modules %t/Test.swiftmodule -module-name Test -emit-parseable-module-interface-path - | %FileCheck %s
1111

1212
// RUN: %target-swift-frontend -emit-module -o %t/TestResilient.swiftmodule -enable-resilience %t-resilient.swiftinterface -disable-objc-attr-requires-foundation-module
13-
// RUN: %target-swift-frontend -emit-module -o /dev/null -merge-modules %t/TestResilient.swiftmodule -module-name TestResilient -enable-resilience -emit-parseable-module-interface-path - | %FileCheck %s --check-prefix RESILIENT --check-prefix COMMON
13+
// RUN: %target-swift-frontend -emit-module -o /dev/null -merge-modules %t/TestResilient.swiftmodule -module-name TestResilient -enable-resilience -emit-parseable-module-interface-path - | %FileCheck %s
1414

15-
// COMMON: @_fixed_layout public struct MyStruct {
15+
// CHECK: @_fixed_layout public struct MyStruct {
1616
@_fixed_layout
1717
public struct MyStruct {
18-
// CHECK: @_hasInitialValue public var publicVar: [[BOOL:(Swift\.)?Bool]]{{$}}
19-
// RESILIENT: public var publicVar: [[BOOL:(Swift\.)?Bool]] = false
18+
// CHECK: public var publicVar: [[BOOL:(Swift\.)?Bool]] = false
2019
public var publicVar: Bool = false
2120

22-
// CHECK: @_hasInitialValue internal var internalVar: ([[BOOL]], [[BOOL]]){{$}}
23-
// RESILIENT: internal var internalVar: ([[BOOL]], [[BOOL]]) = (false, true)
21+
// CHECK: internal var internalVar: ([[BOOL]], [[BOOL]]) = (false, true)
2422
internal var internalVar: (Bool, Bool) = (false, true)
2523

26-
// CHECK: @_hasInitialValue private var privateVar: [[BOOL]]{{$}}
27-
// RESILIENT: private var privateVar: [[BOOL]] = Bool(4 < 10)
24+
// CHECK: private var privateVar: [[BOOL]] = Bool(4 < 10)
2825
private var privateVar: Bool = Bool(4 < 10)
2926

3027
// CHECK: @usableFromInline
31-
// CHECK-NEXT: internal var ufiVar: [[BOOL]]{{$}}
32-
// RESILIENT: @usableFromInline
33-
// RESILIENT-NEXT: internal var ufiVar: [[BOOL]] = true
28+
// CHECK-NEXT: internal var ufiVar: [[BOOL]] = true
3429
@usableFromInline internal var ufiVar: Bool = true
3530

36-
// CHECK: @_hasInitialValue public var multiVar1: [[BOOL]], (multiVar2, multiVar3): ([[BOOL]], [[BOOL]])
37-
// RESILIENT: public var multiVar1: [[BOOL]] = Bool(false), (multiVar2, multiVar3): ([[BOOL]], [[BOOL]]) = (true, 3 == 0)
31+
// CHECK: public var multiVar1: [[BOOL]] = Bool(false), (multiVar2, multiVar3): ([[BOOL]], [[BOOL]]) = (true, 3 == 0)
3832
public var multiVar1: Bool = Bool(false), (multiVar2, multiVar3): (Bool, Bool) = (true, 3 == 0)
3933

40-
// COMMON: @_hasInitialValue public static var staticVar: [[BOOL]]
34+
// CHECK: @_hasInitialValue public static var staticVar: [[BOOL]]
4135
public static var staticVar: Bool = Bool(true && false)
4236

43-
// COMMON: @inlinable internal init() {}
37+
// CHECK: @inlinable internal init() {}
4438
@inlinable init() {}
4539
}
4640

47-
// COMMON: @_fixed_layout public class MyClass {
41+
// CHECK: @_fixed_layout public class MyClass {
4842
@_fixed_layout
4943
public class MyClass {
50-
// CHECK: @_hasInitialValue public var publicVar: [[BOOL]]{{$}}
51-
// RESILIENT: public var publicVar: [[BOOL]] = false
44+
// CHECK: public var publicVar: [[BOOL]] = false
5245
public var publicVar: Bool = false
5346

54-
// CHECK: @_hasInitialValue internal var internalVar: [[BOOL]]{{$}}
55-
// RESILIENT: internal var internalVar: [[BOOL]] = false
47+
// CHECK: internal var internalVar: [[BOOL]] = false
5648
internal var internalVar: Bool = false
5749

58-
// CHECK: @_hasInitialValue private var privateVar: {{(Swift\.)?}}UInt8{{$}}
59-
// RESILIENT: private var privateVar: {{(Swift\.)?}}UInt8 = UInt8(2)
50+
// CHECK: private var privateVar: {{(Swift\.)?}}UInt8 = UInt8(2)
6051
private var privateVar: UInt8 = UInt8(2)
6152

6253
// CHECK: @usableFromInline
63-
// CHECK-NEXT: internal var ufiVar: [[BOOL]]{{$}}
64-
// RESILIENT: @usableFromInline
65-
// RESILIENT: internal var ufiVar: [[BOOL]] = true
54+
// CHECK-NEXT: internal var ufiVar: [[BOOL]] = true
6655
@usableFromInline internal var ufiVar: Bool = true
6756

68-
// COMMON: @_hasInitialValue public static var staticVar: [[BOOL]]
57+
// CHECK: @_hasInitialValue public static var staticVar: [[BOOL]]
6958
public static var staticVar: Bool = Bool(true && false)
7059

71-
// COMMON: @inlinable internal init() {}
60+
// CHECK: @inlinable internal init() {}
7261
@inlinable init() {}
7362
}
7463

75-
// COMMON: @_hasInitialValue public var topLevelVar: [[BOOL]]
64+
// CHECK: @_hasInitialValue public var topLevelVar: [[BOOL]]
7665
public var topLevelVar: Bool = Bool(false && !true)

test/attr/attr_inlinable.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,15 @@ public struct PublicResilientStructWithInit {
247247
var y = publicGlobal // OK
248248
}
249249

250+
private func privateIntReturningFunc() -> Int { return 0 }
251+
internal func internalIntReturningFunc() -> Int { return 0 }
252+
250253
@_fixed_layout
251254
public struct PublicFixedStructWithInit {
252255
var x = internalGlobal // expected-error {{let 'internalGlobal' is internal and cannot be referenced from a property initializer in a '@_fixed_layout' type}}
253256
var y = publicGlobal // OK
257+
static var z = privateIntReturningFunc() // OK
258+
static var a = internalIntReturningFunc() // OK
254259
}
255260

256261
public struct KeypathStruct {

0 commit comments

Comments
 (0)