Skip to content

Commit c80f6c5

Browse files
committed
Add isFragile to VarDecl which allows visibility into the decl if
the decl is non-resilient or its defining module allows non-resilient access. This way when package-cmo is enabled, global accessor and other necessary symbols are added to TBD and doesn't throw linker errors. Resolves rdar://127321129
1 parent 1b848ac commit c80f6c5

File tree

6 files changed

+35
-8
lines changed

6 files changed

+35
-8
lines changed

include/swift/AST/Decl.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5964,11 +5964,16 @@ class AbstractStorageDecl : public ValueDecl {
59645964
/// Do we need to use resilient access patterns outside of this
59655965
/// property's resilience domain?
59665966
bool isResilient() const;
5967-
5967+
59685968
/// Do we need to use resilient access patterns when accessing this
59695969
/// property from the given module?
59705970
bool isResilient(ModuleDecl *M, ResilienceExpansion expansion) const;
59715971

5972+
/// True if the decl is non-resilient, or its defining module allows non-resilient
5973+
/// access so its consuming modules within the same package can have
5974+
/// visibility into this decl.
5975+
bool isFragile() const;
5976+
59725977
/// True if the storage can be referenced by a keypath directly.
59735978
/// Otherwise, its override must be referenced.
59745979
bool isValidKeyPathComponent() const;

lib/AST/Decl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3032,6 +3032,10 @@ bool Decl::isOutermostPrivateOrFilePrivateScope() const {
30323032
!isInPrivateOrLocalContext(this);
30333033
}
30343034

3035+
bool AbstractStorageDecl::isFragile() const {
3036+
return !isResilient() || getModuleContext()->allowNonResilientAccess();
3037+
}
3038+
30353039
bool AbstractStorageDecl::isResilient() const {
30363040
// Check for an explicit @_fixed_layout attribute.
30373041
if (getAttrs().hasAttribute<FixedLayoutAttr>())

lib/SIL/IR/SILDeclRef.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ static LinkageLimit getLinkageLimit(SILDeclRef constant) {
461461
return Limit::OnDemand;
462462

463463
case Kind::GlobalAccessor:
464-
return cast<VarDecl>(d)->isResilient() ? Limit::NeverPublic : Limit::None;
464+
return !cast<VarDecl>(d)->isFragile() ? Limit::NeverPublic : Limit::None;
465465

466466
case Kind::DefaultArgGenerator:
467467
// If the default argument is to be serialized, only use non-ABI public

lib/SIL/IR/SILSymbolVisitor.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ class SILSymbolVisitorImpl : public ASTVisitor<SILSymbolVisitorImpl> {
579579

580580
void visitVarDecl(VarDecl *VD) {
581581
// Variables inside non-resilient modules have some additional symbols.
582-
if (!VD->isResilient()) {
582+
if (VD->isFragile()) {
583583
// Non-global variables might have an explicit initializer symbol in
584584
// non-resilient modules.
585585
if (VD->getAttrs().hasAttribute<HasInitialValueAttr>() &&
@@ -589,25 +589,22 @@ class SILSymbolVisitorImpl : public ASTVisitor<SILSymbolVisitorImpl> {
589589
// Stored property initializers for public properties are public.
590590
addFunction(declRef);
591591
}
592-
593592
// Statically/globally stored variables have some special handling.
594593
if (VD->hasStorage() && isGlobalOrStaticVar(VD)) {
595594
if (!shouldSkipVisit(getDeclLinkage(VD))) {
596595
Visitor.addGlobalVar(VD);
597596
}
598-
599597
if (VD->isLazilyInitializedGlobal())
600598
addFunction(SILDeclRef(VD, SILDeclRef::Kind::GlobalAccessor));
601599
}
602-
603600
// Wrapped non-static member properties may have a backing initializer.
604601
auto initInfo = VD->getPropertyWrapperInitializerInfo();
605602
if (initInfo.hasInitFromWrappedValue() && !VD->isStatic()) {
606603
addFunction(SILDeclRef(
607604
VD, SILDeclRef::Kind::PropertyWrapperBackingInitializer));
608605
}
609606
}
610-
607+
611608
visitAbstractStorageDecl(VD);
612609
}
613610

lib/Sema/TypeCheckStorage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2537,7 +2537,7 @@ RequiresOpaqueAccessorsRequest::evaluate(Evaluator &evaluator,
25372537

25382538
} else if (dc->isModuleScopeContext()) {
25392539
// Fixed-layout global variables don't require opaque accessors.
2540-
if (!var->isResilient() && !var->shouldUseNativeDynamicDispatch())
2540+
if (var->isFragile() && !var->shouldUseNativeDynamicDispatch())
25412541
return false;
25422542

25432543
// Stored properties imported from Clang don't require opaque accessors.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-build-swift -module-name=File -package-name Pkg -I%t -emit-sil %s | %FileCheck %s --check-prefix=CHECK-SIL
4+
// RUN: %target-build-swift -module-name=File -package-name Pkg -I%t -emit-sil %s -enable-library-evolution | %FileCheck %s --check-prefix=CHECK-SIL-HID
5+
// RUN: %target-build-swift -module-name=File -package-name Pkg -I%t -emit-sil %s -enable-library-evolution -Xfrontend -experimental-allow-non-resilient-access | %FileCheck %s --check-prefix=CHECK-SIL
6+
// RUN: %target-build-swift -module-name=File -package-name Pkg -I%t -emit-ir %s | %FileCheck %s --check-prefix=CHECK-IR
7+
// RUN: %target-build-swift -module-name=File -package-name Pkg -I%t -emit-ir %s -enable-library-evolution | %FileCheck %s --check-prefix=CHECK-IR-HID
8+
// RUN: %target-build-swift -module-name=File -package-name Pkg -I%t -emit-ir %s -enable-library-evolution -Xfrontend -experimental-allow-non-resilient-access | %FileCheck %s --check-prefix=CHECK-IR
9+
10+
public struct S {
11+
public static var x = "hello world"
12+
}
13+
14+
// S.x.unsafeMutableAddressor
15+
// CHECK-SIL: sil [global_init] @$s4File1SV1xSSvau : $@convention(thin) () -> Builtin.RawPointer {
16+
// S.x.unsafeMutableAddressor
17+
// CHECK-SIL-HID: sil hidden [global_init] @$s4File1SV1xSSvau : $@convention(thin) () -> Builtin.RawPointer {
18+
19+
// CHECK-IR: define swiftcc ptr @"$s4File1SV1xSSvau"()
20+
// CHECK-IR-HID: define hidden swiftcc ptr @"$s4File1SV1xSSvau"()
21+

0 commit comments

Comments
 (0)