Skip to content

Commit ff2bbb2

Browse files
committed
De-underscore @Frozen, apply it to structs (swiftlang#24185)
* De-underscore @Frozen for enums * Add @Frozen for structs, deprecate @_fixed_layout for them * Switch usage from _fixed_layout to frozen
1 parent 7dc4478 commit ff2bbb2

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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5017,7 +5017,7 @@ class VarDecl : public AbstractStorageDecl {
50175017
/// exposed to clients.
50185018
/// There's a very narrow case when we would: if the decl is an instance
50195019
/// member with an initializer expression and the parent type is
5020-
/// @_fixed_layout and resides in a resilient module.
5020+
/// @frozen and resides in a resilient module.
50215021
bool isInitExposedToClients() const;
50225022

50235023
/// Is this a special debugger variable?

include/swift/AST/DiagnosticsSema.def

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,8 +1387,8 @@ WARNING(pattern_type_not_usable_from_inline_warn,none,
13871387
"%select{%select{variable|constant}0|property}1 "
13881388
"should be '@usableFromInline' or public",
13891389
(bool, bool))
1390-
ERROR(pattern_type_not_usable_from_inline_fixed_layout,none,
1391-
"type referenced from a stored property in a '@_fixed_layout' struct must "
1390+
ERROR(pattern_type_not_usable_from_inline_frozen,none,
1391+
"type referenced from a stored property in a '@frozen' struct must "
13921392
"be '@usableFromInline' or public",
13931393
(/*ignored*/bool, /*ignored*/bool))
13941394
ERROR(pattern_type_not_usable_from_inline_inferred,none,
@@ -1403,9 +1403,9 @@ WARNING(pattern_type_not_usable_from_inline_inferred_warn,none,
14031403
"with inferred type %2 "
14041404
"should be '@usableFromInline' or public",
14051405
(bool, bool, Type))
1406-
ERROR(pattern_type_not_usable_from_inline_inferred_fixed_layout,none,
1406+
ERROR(pattern_type_not_usable_from_inline_inferred_frozen,none,
14071407
"type referenced from a stored property with inferred type %2 in a "
1408-
"'@_fixed_layout' struct must be '@usableFromInline' or public",
1408+
"'@frozen' struct must be '@usableFromInline' or public",
14091409
(/*ignored*/bool, /*ignored*/bool, Type))
14101410

14111411
ERROR(pattern_binds_no_variables,none,
@@ -4133,6 +4133,15 @@ ERROR(fixed_layout_attr_on_internal_type,
41334133
"%select{private|fileprivate|internal|%error|%error}1",
41344134
(DeclName, AccessLevel))
41354135

4136+
WARNING(fixed_layout_struct,
4137+
none, "'@frozen' attribute is now used for fixed-layout structs", ())
4138+
4139+
ERROR(frozen_attr_on_internal_type,
4140+
none, "'@frozen' attribute can only be applied to '@usableFromInline' "
4141+
"or public declarations, but %0 is "
4142+
"%select{private|fileprivate|internal|%error|%error}1",
4143+
(DeclName, AccessLevel))
4144+
41364145
ERROR(usable_from_inline_attr_with_explicit_access,
41374146
none, "'@usableFromInline' attribute can only be applied to internal "
41384147
"declarations, but %0 is %select{private|fileprivate|%error|public|open}1",
@@ -4149,7 +4158,7 @@ ERROR(usable_from_inline_attr_in_protocol,none,
41494158
"an '@inlinable' function|" \
41504159
"an '@_alwaysEmitIntoClient' function|" \
41514160
"a default argument value|" \
4152-
"a property initializer in a '@_fixed_layout' type}"
4161+
"a property initializer in a '@frozen' type}"
41534162

41544163
#define DECL_OR_ACCESSOR "%select{%0|%0 for}"
41554164

lib/AST/Decl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,8 @@ bool VarDecl::isInitExposedToClients() const {
14881488
if (!parent) return false;
14891489
if (!hasInitialValue()) return false;
14901490
if (isStatic()) return false;
1491-
return parent->getAttrs().hasAttribute<FixedLayoutAttr>();
1491+
return parent->getAttrs().hasAttribute<FrozenAttr>() ||
1492+
parent->getAttrs().hasAttribute<FixedLayoutAttr>();
14921493
}
14931494

14941495
/// Check whether the given type representation will be
@@ -3109,7 +3110,7 @@ bool NominalTypeDecl::isFormallyResilient() const {
31093110
/*treatUsableFromInlineAsPublic=*/true).isPublic())
31103111
return false;
31113112

3112-
// Check for an explicit @_fixed_layout or @_frozen attribute.
3113+
// Check for an explicit @_fixed_layout or @frozen attribute.
31133114
if (getAttrs().hasAttribute<FixedLayoutAttr>() ||
31143115
getAttrs().hasAttribute<FrozenAttr>()) {
31153116
return false;

lib/ClangImporter/ImportDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2614,7 +2614,7 @@ namespace {
26142614
errorWrapper->setAddedImplicitInitializers();
26152615
errorWrapper->setAccess(AccessLevel::Public);
26162616
errorWrapper->getAttrs().add(
2617-
new (Impl.SwiftContext) FixedLayoutAttr(/*IsImplicit*/true));
2617+
new (Impl.SwiftContext) FrozenAttr(/*IsImplicit*/true));
26182618

26192619
StringRef nameForMangling;
26202620
ClangImporterSynthesizedTypeAttr::Kind relatedEntityKind;

lib/IRGen/StructLayout.cpp

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 6 deletions
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.
@@ -497,7 +498,7 @@ IsSerialized_t SILDeclRef::isSerialized() const {
497498
}
498499

499500
// Stored property initializers are inlinable if the type is explicitly
500-
// marked as @_fixed_layout.
501+
// marked as @frozen.
501502
if (isStoredPropertyInitializer()) {
502503
auto *nominal = cast<NominalTypeDecl>(d->getDeclContext());
503504
auto scope =

lib/Sema/TypeCheckAccess.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,8 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
10321032
auto *parentStruct = dyn_cast<StructDecl>(PBD->getDeclContext());
10331033
if (!parentStruct)
10341034
return nullptr;
1035-
if (!parentStruct->getAttrs().hasAttribute<FixedLayoutAttr>() ||
1035+
if (!(parentStruct->getAttrs().hasAttribute<FrozenAttr>() ||
1036+
parentStruct->getAttrs().hasAttribute<FixedLayoutAttr>()) ||
10361037
PBD->isStatic() || !PBD->hasStorage()) {
10371038
return nullptr;
10381039
}
@@ -1064,7 +1065,7 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
10641065
auto diagID = diag::pattern_type_not_usable_from_inline_inferred;
10651066
if (fixedLayoutStructContext) {
10661067
diagID =
1067-
diag::pattern_type_not_usable_from_inline_inferred_fixed_layout;
1068+
diag::pattern_type_not_usable_from_inline_inferred_frozen;
10681069
} else if (!TC.Context.isSwiftVersionAtLeast(5)) {
10691070
diagID = diag::pattern_type_not_usable_from_inline_inferred_warn;
10701071
}
@@ -1100,7 +1101,7 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
11001101
DowngradeToWarning downgradeToWarning) {
11011102
auto diagID = diag::pattern_type_not_usable_from_inline;
11021103
if (fixedLayoutStructContext)
1103-
diagID = diag::pattern_type_not_usable_from_inline_fixed_layout;
1104+
diagID = diag::pattern_type_not_usable_from_inline_frozen;
11041105
else if (!TC.Context.isSwiftVersionAtLeast(5))
11051106
diagID = diag::pattern_type_not_usable_from_inline_warn;
11061107
auto diag = TC.diagnose(TP->getLoc(), diagID, anyVar->isLet(),

lib/Sema/TypeCheckAttr.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,6 +1931,11 @@ void AttributeChecker::visitSpecializeAttr(SpecializeAttr *attr) {
19311931
}
19321932

19331933
void AttributeChecker::visitFixedLayoutAttr(FixedLayoutAttr *attr) {
1934+
if (isa<StructDecl>(D)) {
1935+
TC.diagnose(attr->getLocation(), diag::fixed_layout_struct)
1936+
.fixItReplace(attr->getRange(), "@frozen");
1937+
}
1938+
19341939
auto *VD = cast<ValueDecl>(D);
19351940

19361941
if (VD->getFormalAccess() < AccessLevel::Public &&
@@ -2436,16 +2441,25 @@ void AttributeChecker::visitImplementsAttr(ImplementsAttr *attr) {
24362441
}
24372442

24382443
void AttributeChecker::visitFrozenAttr(FrozenAttr *attr) {
2439-
auto *ED = cast<EnumDecl>(D);
2444+
if (auto *ED = dyn_cast<EnumDecl>(D)) {
2445+
if (!ED->getModuleContext()->isResilient()) {
2446+
diagnoseAndRemoveAttr(attr, diag::enum_frozen_nonresilient, attr);
2447+
return;
2448+
}
24402449

2441-
if (!ED->getModuleContext()->isResilient()) {
2442-
diagnoseAndRemoveAttr(attr, diag::enum_frozen_nonresilient, attr);
2443-
return;
2450+
if (ED->getFormalAccess() < AccessLevel::Public &&
2451+
!ED->getAttrs().hasAttribute<UsableFromInlineAttr>()) {
2452+
diagnoseAndRemoveAttr(attr, diag::enum_frozen_nonpublic, attr);
2453+
return;
2454+
}
24442455
}
24452456

2446-
if (ED->getFormalAccess() < AccessLevel::Public &&
2447-
!ED->getAttrs().hasAttribute<UsableFromInlineAttr>()) {
2448-
diagnoseAndRemoveAttr(attr, diag::enum_frozen_nonpublic, attr);
2457+
auto *VD = cast<ValueDecl>(D);
2458+
2459+
if (VD->getFormalAccess() < AccessLevel::Public &&
2460+
!VD->getAttrs().hasAttribute<UsableFromInlineAttr>()) {
2461+
diagnoseAndRemoveAttr(attr, diag::frozen_attr_on_internal_type,
2462+
VD->getFullName(), VD->getFormalAccess());
24492463
}
24502464
}
24512465

stdlib/private/StdlibUnittest/StdlibCoreExtras.swift

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 3 additions & 3 deletions
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".
@@ -95,7 +95,7 @@ func _convertObjCBoolToBool(_ x: ObjCBool) -> Bool {
9595
/// convert between C strings and selectors.
9696
///
9797
/// The compiler has special knowledge of this type.
98-
@_fixed_layout
98+
@frozen
9999
public struct Selector : ExpressibleByStringLiteral {
100100
var ptr: OpaquePointer
101101

@@ -144,7 +144,7 @@ extension Selector : CustomReflectable {
144144
// NSZone
145145
//===----------------------------------------------------------------------===//
146146

147-
@_fixed_layout
147+
@frozen
148148
public struct NSZone {
149149
var pointer: OpaquePointer
150150
}

stdlib/public/Platform/Platform.swift

Lines changed: 1 addition & 1 deletion
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
var _value: UInt8
3434

stdlib/public/core/ASCII.swift

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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)