Skip to content

Commit e9d4687

Browse files
De-underscore @Frozen, apply it to structs (#24185)
* De-underscore @Frozen for enums * Add @Frozen for structs, deprecate @_fixed_layout for them * Switch usage from _fixed_layout to frozen
1 parent 5aca779 commit e9d4687

File tree

173 files changed

+755
-622
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

173 files changed

+755
-622
lines changed

include/swift/AST/Attr.def

+3-2
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,11 @@ SIMPLE_DECL_ATTR(_weakLinked, WeakLinked,
364364
OnNominalType | OnAssociatedType | OnFunc | OnAccessor | OnVar |
365365
OnSubscript | OnConstructor | OnEnumElement | OnExtension | UserInaccessible,
366366
75)
367-
SIMPLE_DECL_ATTR(_frozen, Frozen,
368-
OnEnum |
367+
SIMPLE_DECL_ATTR(frozen, Frozen,
368+
OnEnum | OnStruct |
369369
UserInaccessible,
370370
76)
371+
DECL_ATTR_ALIAS(_frozen, Frozen)
371372
SIMPLE_DECL_ATTR(_forbidSerializingReference, ForbidSerializingReference,
372373
OnAnyDecl |
373374
LongAttribute | RejectByParser | UserInaccessible | NotSerialized,

include/swift/AST/Decl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -5064,7 +5064,7 @@ class VarDecl : public AbstractStorageDecl {
50645064
/// exposed to clients.
50655065
/// There's a very narrow case when we would: if the decl is an instance
50665066
/// member with an initializer expression and the parent type is
5067-
/// @_fixed_layout and resides in a resilient module.
5067+
/// @frozen and resides in a resilient module.
50685068
bool isInitExposedToClients() const;
50695069

50705070
/// Is this a special debugger variable?

include/swift/AST/DiagnosticsSema.def

+14-5
Original file line numberDiff line numberDiff line change
@@ -1388,8 +1388,8 @@ WARNING(pattern_type_not_usable_from_inline_warn,none,
13881388
"%select{%select{variable|constant}0|property}1 "
13891389
"should be '@usableFromInline' or public",
13901390
(bool, bool))
1391-
ERROR(pattern_type_not_usable_from_inline_fixed_layout,none,
1392-
"type referenced from a stored property in a '@_fixed_layout' struct must "
1391+
ERROR(pattern_type_not_usable_from_inline_frozen,none,
1392+
"type referenced from a stored property in a '@frozen' struct must "
13931393
"be '@usableFromInline' or public",
13941394
(/*ignored*/bool, /*ignored*/bool))
13951395
ERROR(pattern_type_not_usable_from_inline_inferred,none,
@@ -1404,9 +1404,9 @@ WARNING(pattern_type_not_usable_from_inline_inferred_warn,none,
14041404
"with inferred type %2 "
14051405
"should be '@usableFromInline' or public",
14061406
(bool, bool, Type))
1407-
ERROR(pattern_type_not_usable_from_inline_inferred_fixed_layout,none,
1407+
ERROR(pattern_type_not_usable_from_inline_inferred_frozen,none,
14081408
"type referenced from a stored property with inferred type %2 in a "
1409-
"'@_fixed_layout' struct must be '@usableFromInline' or public",
1409+
"'@frozen' struct must be '@usableFromInline' or public",
14101410
(/*ignored*/bool, /*ignored*/bool, Type))
14111411

14121412
ERROR(pattern_binds_no_variables,none,
@@ -4136,6 +4136,15 @@ ERROR(fixed_layout_attr_on_internal_type,
41364136
"%select{private|fileprivate|internal|%error|%error}1",
41374137
(DeclName, AccessLevel))
41384138

4139+
WARNING(fixed_layout_struct,
4140+
none, "'@frozen' attribute is now used for fixed-layout structs", ())
4141+
4142+
ERROR(frozen_attr_on_internal_type,
4143+
none, "'@frozen' attribute can only be applied to '@usableFromInline' "
4144+
"or public declarations, but %0 is "
4145+
"%select{private|fileprivate|internal|%error|%error}1",
4146+
(DeclName, AccessLevel))
4147+
41394148
ERROR(usable_from_inline_attr_with_explicit_access,
41404149
none, "'@usableFromInline' attribute can only be applied to internal "
41414150
"declarations, but %0 is %select{private|fileprivate|%error|public|open}1",
@@ -4152,7 +4161,7 @@ ERROR(usable_from_inline_attr_in_protocol,none,
41524161
"an '@inlinable' function|" \
41534162
"an '@_alwaysEmitIntoClient' function|" \
41544163
"a default argument value|" \
4155-
"a property initializer in a '@_fixed_layout' type}"
4164+
"a property initializer in a '@frozen' type}"
41564165

41574166
#define DECL_OR_ACCESSOR "%select{%0|%0 for}"
41584167

lib/AST/Decl.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,8 @@ bool VarDecl::isInitExposedToClients() const {
15161516
if (!parent) return false;
15171517
if (!hasInitialValue()) return false;
15181518
if (isStatic()) return false;
1519-
return parent->getAttrs().hasAttribute<FixedLayoutAttr>();
1519+
return parent->getAttrs().hasAttribute<FrozenAttr>() ||
1520+
parent->getAttrs().hasAttribute<FixedLayoutAttr>();
15201521
}
15211522

15221523
/// Check whether the given type representation will be
@@ -3188,7 +3189,7 @@ bool NominalTypeDecl::isFormallyResilient() const {
31883189
/*treatUsableFromInlineAsPublic=*/true).isPublic())
31893190
return false;
31903191

3191-
// Check for an explicit @_fixed_layout or @_frozen attribute.
3192+
// Check for an explicit @_fixed_layout or @frozen attribute.
31923193
if (getAttrs().hasAttribute<FixedLayoutAttr>() ||
31933194
getAttrs().hasAttribute<FrozenAttr>()) {
31943195
return false;

lib/ClangImporter/ImportDecl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2712,7 +2712,7 @@ namespace {
27122712
errorWrapper->setAddedImplicitInitializers();
27132713
errorWrapper->setAccess(AccessLevel::Public);
27142714
errorWrapper->getAttrs().add(
2715-
new (Impl.SwiftContext) FixedLayoutAttr(/*IsImplicit*/true));
2715+
new (Impl.SwiftContext) FrozenAttr(/*IsImplicit*/true));
27162716

27172717
StringRef nameForMangling;
27182718
ClangImporterSynthesizedTypeAttr::Kind relatedEntityKind;

lib/IRGen/StructLayout.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ StructLayout::StructLayout(IRGenModule &IGM,
9393

9494
assert(typeToFill == nullptr || Ty == typeToFill);
9595

96-
// If the struct is not @_fixed_layout, it will have a dynamic
96+
// If the struct is not @frozen, it will have a dynamic
9797
// layout outside of its resilience domain.
9898
if (decl) {
9999
if (IGM.isResilient(decl, ResilienceExpansion::Minimal))

lib/SIL/SILDeclRef.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,14 @@ SILLinkage SILDeclRef::getLinkage(ForDefinition_t forDefinition) const {
317317
if (isStoredPropertyInitializer()) {
318318
// Three cases:
319319
//
320-
// 1) Type is formally @_fixed_layout. Root initializers can be declared
321-
// @inlinable. The property initializer must only reference
320+
// 1) Type is formally @_fixed_layout/@frozen. Root initializers can be
321+
// declared @inlinable. The property initializer must only reference
322322
// public symbols, and is serialized, so we give it PublicNonABI linkage.
323323
//
324-
// 2) Type is not formally @_fixed_layout and the module is not resilient.
325-
// Root initializers can be declared @inlinable. This is the annoying
326-
// case. We give the initializer public linkage if the type is public.
324+
// 2) Type is not formally @_fixed_layout/@frozen and the module is not
325+
// resilient. Root initializers can be declared @inlinable. This is the
326+
// annoying case. We give the initializer public linkage if the type is
327+
// public.
327328
//
328329
// 3) Type is resilient. The property initializer is never public because
329330
// root initializers cannot be @inlinable.
@@ -491,7 +492,7 @@ IsSerialized_t SILDeclRef::isSerialized() const {
491492
}
492493

493494
// Stored property initializers are inlinable if the type is explicitly
494-
// marked as @_fixed_layout.
495+
// marked as @frozen.
495496
if (isStoredPropertyInitializer()) {
496497
auto *nominal = cast<NominalTypeDecl>(d->getDeclContext());
497498
auto scope =

lib/Sema/TypeCheckAccess.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,8 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
10591059
auto *parentStruct = dyn_cast<StructDecl>(PBD->getDeclContext());
10601060
if (!parentStruct)
10611061
return nullptr;
1062-
if (!parentStruct->getAttrs().hasAttribute<FixedLayoutAttr>() ||
1062+
if (!(parentStruct->getAttrs().hasAttribute<FrozenAttr>() ||
1063+
parentStruct->getAttrs().hasAttribute<FixedLayoutAttr>()) ||
10631064
PBD->isStatic() || !PBD->hasStorage()) {
10641065
return nullptr;
10651066
}
@@ -1091,7 +1092,7 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
10911092
auto diagID = diag::pattern_type_not_usable_from_inline_inferred;
10921093
if (fixedLayoutStructContext) {
10931094
diagID =
1094-
diag::pattern_type_not_usable_from_inline_inferred_fixed_layout;
1095+
diag::pattern_type_not_usable_from_inline_inferred_frozen;
10951096
} else if (!TC.Context.isSwiftVersionAtLeast(5)) {
10961097
diagID = diag::pattern_type_not_usable_from_inline_inferred_warn;
10971098
}
@@ -1127,7 +1128,7 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
11271128
DowngradeToWarning downgradeToWarning) {
11281129
auto diagID = diag::pattern_type_not_usable_from_inline;
11291130
if (fixedLayoutStructContext)
1130-
diagID = diag::pattern_type_not_usable_from_inline_fixed_layout;
1131+
diagID = diag::pattern_type_not_usable_from_inline_frozen;
11311132
else if (!TC.Context.isSwiftVersionAtLeast(5))
11321133
diagID = diag::pattern_type_not_usable_from_inline_warn;
11331134
auto diag = TC.diagnose(TP->getLoc(), diagID, anyVar->isLet(),

lib/Sema/TypeCheckAttr.cpp

+21-7
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,11 @@ void AttributeChecker::visitSpecializeAttr(SpecializeAttr *attr) {
19571957
}
19581958

19591959
void AttributeChecker::visitFixedLayoutAttr(FixedLayoutAttr *attr) {
1960+
if (isa<StructDecl>(D)) {
1961+
TC.diagnose(attr->getLocation(), diag::fixed_layout_struct)
1962+
.fixItReplace(attr->getRange(), "@frozen");
1963+
}
1964+
19601965
auto *VD = cast<ValueDecl>(D);
19611966

19621967
if (VD->getFormalAccess() < AccessLevel::Public &&
@@ -2462,16 +2467,25 @@ void AttributeChecker::visitImplementsAttr(ImplementsAttr *attr) {
24622467
}
24632468

24642469
void AttributeChecker::visitFrozenAttr(FrozenAttr *attr) {
2465-
auto *ED = cast<EnumDecl>(D);
2470+
if (auto *ED = dyn_cast<EnumDecl>(D)) {
2471+
if (!ED->getModuleContext()->isResilient()) {
2472+
diagnoseAndRemoveAttr(attr, diag::enum_frozen_nonresilient, attr);
2473+
return;
2474+
}
24662475

2467-
if (!ED->getModuleContext()->isResilient()) {
2468-
diagnoseAndRemoveAttr(attr, diag::enum_frozen_nonresilient, attr);
2469-
return;
2476+
if (ED->getFormalAccess() < AccessLevel::Public &&
2477+
!ED->getAttrs().hasAttribute<UsableFromInlineAttr>()) {
2478+
diagnoseAndRemoveAttr(attr, diag::enum_frozen_nonpublic, attr);
2479+
return;
2480+
}
24702481
}
24712482

2472-
if (ED->getFormalAccess() < AccessLevel::Public &&
2473-
!ED->getAttrs().hasAttribute<UsableFromInlineAttr>()) {
2474-
diagnoseAndRemoveAttr(attr, diag::enum_frozen_nonpublic, attr);
2483+
auto *VD = cast<ValueDecl>(D);
2484+
2485+
if (VD->getFormalAccess() < AccessLevel::Public &&
2486+
!VD->getAttrs().hasAttribute<UsableFromInlineAttr>()) {
2487+
diagnoseAndRemoveAttr(attr, diag::frozen_attr_on_internal_type,
2488+
VD->getFullName(), VD->getFormalAccess());
24752489
}
24762490
}
24772491

stdlib/private/StdlibUnittest/StdlibCoreExtras.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public func _isStdlibDebugConfiguration() -> Bool {
268268
#endif
269269
}
270270

271-
@_fixed_layout
271+
@frozen
272272
public struct LinearCongruentialGenerator: RandomNumberGenerator {
273273

274274
@usableFromInline

stdlib/public/Darwin/ARKit/ARKit.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension ARCamera {
1717
/**
1818
A value describing the camera's tracking state.
1919
*/
20-
@_frozen
20+
@frozen
2121
public enum TrackingState {
2222
public enum Reason {
2323
/** Tracking is limited due to initialization in progress. */

stdlib/public/Darwin/CoreGraphics/CGFloat.swift.gyb

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ word_bits = int(CMAKE_SIZEOF_VOID_P) * 8
2222
@_exported import CoreGraphics
2323
import Darwin
2424

25-
@_fixed_layout
25+
@frozen
2626
public struct CGFloat {
2727
#if arch(i386) || arch(arm)
2828
/// The native type used to store the CGFloat, which is Float on

stdlib/public/Darwin/Dispatch/Dispatch.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public struct DispatchQoS : Equatable {
121121
}
122122

123123
///
124-
@_frozen
124+
@frozen
125125
public enum DispatchTimeoutResult {
126126
case success
127127
case timedOut

stdlib/public/Darwin/Foundation/Data.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ internal class __NSSwiftData : NSData {
602602
#endif
603603
}
604604

605-
@_fixed_layout
605+
@frozen
606606
public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessCollection, MutableCollection, RangeReplaceableCollection, MutableDataProtocol, ContiguousBytes {
607607
public typealias ReferenceType = NSData
608608

@@ -618,7 +618,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
618618
// A small inline buffer of bytes suitable for stack-allocation of small data.
619619
// Inlinability strategy: everything here should be inlined for direct operation on the stack wherever possible.
620620
@usableFromInline
621-
@_fixed_layout
621+
@frozen
622622
internal struct InlineData {
623623
#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le)
624624
@usableFromInline typealias Buffer = (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
@@ -839,7 +839,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
839839
// A buffer of bytes too large to fit in an InlineData, but still small enough to fit a storage pointer + range in two words.
840840
// Inlinability strategy: everything here should be easily inlinable as large _DataStorage methods should not inline into here.
841841
@usableFromInline
842-
@_fixed_layout
842+
@frozen
843843
internal struct InlineSlice {
844844
// ***WARNING***
845845
// These ivars are specifically laid out so that they cause the enum _Representation to be 16 bytes on 64 bit platforms. This means we _MUST_ have the class type thing last
@@ -1085,7 +1085,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
10851085
// A buffer of bytes whose range is too large to fit in a signle word. Used alongside a RangeReference to make it fit into _Representation's two-word size.
10861086
// Inlinability strategy: everything here should be easily inlinable as large _DataStorage methods should not inline into here.
10871087
@usableFromInline
1088-
@_fixed_layout
1088+
@frozen
10891089
internal struct LargeSlice {
10901090
// ***WARNING***
10911091
// These ivars are specifically laid out so that they cause the enum _Representation to be 16 bytes on 64 bit platforms. This means we _MUST_ have the class type thing last
@@ -1261,7 +1261,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
12611261
// The actual storage for Data's various representations.
12621262
// Inlinability strategy: almost everything should be inlinable as forwarding the underlying implementations. (Inlining can also help avoid retain-release traffic around pulling values out of enums.)
12631263
@usableFromInline
1264-
@_frozen
1264+
@frozen
12651265
internal enum _Representation {
12661266
case empty
12671267
case inline(InlineData)

stdlib/public/Darwin/ObjectiveC/ObjectiveC.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import _SwiftObjectiveCOverlayShims
2323
/// On 64-bit iOS, the Objective-C BOOL type is a typedef of C/C++
2424
/// bool. Elsewhere, it is "signed char". The Clang importer imports it as
2525
/// ObjCBool.
26-
@_fixed_layout
26+
@frozen
2727
public struct ObjCBool : ExpressibleByBooleanLiteral {
2828
#if os(macOS) || (os(iOS) && (arch(i386) || arch(arm)))
2929
// On OS X and 32-bit iOS, Objective-C's BOOL type is a "signed char".
@@ -101,7 +101,7 @@ func _convertObjCBoolToBool(_ x: ObjCBool) -> Bool {
101101
/// convert between C strings and selectors.
102102
///
103103
/// The compiler has special knowledge of this type.
104-
@_fixed_layout
104+
@frozen
105105
public struct Selector : ExpressibleByStringLiteral {
106106
var ptr: OpaquePointer
107107

@@ -150,7 +150,7 @@ extension Selector : CustomReflectable {
150150
// NSZone
151151
//===----------------------------------------------------------------------===//
152152

153-
@_fixed_layout
153+
@frozen
154154
public struct NSZone {
155155
var pointer: OpaquePointer
156156
}

stdlib/public/Platform/Platform.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public var noErr: OSStatus { return 0 }
2828
/// Foundation.
2929
///
3030
/// The C type is a typedef for `unsigned char`.
31-
@_fixed_layout
31+
@frozen
3232
public struct DarwinBoolean : ExpressibleByBooleanLiteral {
3333
@usableFromInline var _value: UInt8
3434

stdlib/public/core/ASCII.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212
extension Unicode {
13-
@_frozen
13+
@frozen
1414
public enum ASCII {}
1515
}
1616

@@ -68,7 +68,7 @@ extension Unicode.ASCII : Unicode.Encoding {
6868
return encode(FromEncoding.decode(content))
6969
}
7070

71-
@_fixed_layout
71+
@frozen
7272
public struct Parser {
7373
@inlinable
7474
public init() { }

stdlib/public/core/Algorithm.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public func max<T : Comparable>(_ x: T, _ y: T, _ z: T, _ rest: T...) -> T {
9191
/// }
9292
/// // Prints "0: foo"
9393
/// // Prints "1: bar"
94-
@_fixed_layout
94+
@frozen
9595
public struct EnumeratedSequence<Base: Sequence> {
9696
@usableFromInline
9797
internal var _base: Base
@@ -118,7 +118,7 @@ extension EnumeratedSequence {
118118
///
119119
/// To create an instance, call
120120
/// `enumerated().makeIterator()` on a sequence or collection.
121-
@_fixed_layout
121+
@frozen
122122
public struct Iterator {
123123
@usableFromInline
124124
internal var _base: Base.Iterator

stdlib/public/core/AnyHashable.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ internal struct _ConcreteHashableBox<Base : Hashable> : _AnyHashableBox {
123123
/// print(descriptions[AnyHashable(43)]) // prints "nil"
124124
/// print(descriptions[AnyHashable(Int8(43))]!) // prints "an Int8"
125125
/// print(descriptions[AnyHashable(Set(["a", "b"]))]!) // prints "a set of strings"
126-
@_fixed_layout
126+
@frozen
127127
public struct AnyHashable {
128128
internal var _box: _AnyHashableBox
129129

stdlib/public/core/Array.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@
296296
/// - Note: The `ContiguousArray` and `ArraySlice` types are not bridged;
297297
/// instances of those types always have a contiguous block of memory as
298298
/// their storage.
299-
@_fixed_layout
299+
@frozen
300300
public struct Array<Element>: _DestructorSafeContainer {
301301
#if _runtime(_ObjC)
302302
@usableFromInline

stdlib/public/core/ArrayBody.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import SwiftShims
1919

20-
@_fixed_layout
20+
@frozen
2121
@usableFromInline
2222
internal struct _ArrayBody {
2323
@usableFromInline

stdlib/public/core/ArrayBuffer.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal typealias _ArrayBridgeStorage
2323
= _BridgeStorage<__ContiguousArrayStorageBase>
2424

2525
@usableFromInline
26-
@_fixed_layout
26+
@frozen
2727
internal struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
2828

2929
/// Create an empty buffer.

0 commit comments

Comments
 (0)