TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
@frozen attribute is a declaration modifier applied to structures and enumerations to guarantee that their internal layout or set of cases will never change in future versions of a compiled module. By explicitly fixing the type’s Application Binary Interface (ABI), it allows the Swift compiler to perform aggressive memory and control-flow optimizations across module boundaries.
Syntax and Declaration Requirements
The attribute is placed directly before thestruct or enum keyword. Because freezing a type exposes its internal layout to the public ABI, the compiler enforces strict access control rules:
- Type Visibility: The
@frozenattribute can only be applied to types declared aspublicor marked with the@usableFromInlineattribute. Applying it to standardinternal,fileprivate, orprivatetypes results in a compiler error. - Property Visibility: Stored properties within a
@frozenstructure cannot haveprivateorfileprivateaccess control. Furthermore, standardinternalstored properties are invalid on their own; anyinternalstored property must be explicitly marked with the@usableFromInlineattribute so its layout is exposed to the ABI.
Compiler Mechanics and ABI Implications
When a module is compiled with library evolution enabled (-enable-library-evolution), the Swift compiler normally treats public structs and enums as resilient. Resilient types require indirect memory access and dynamic allocation because their size and layout might change in future dynamic library updates.
Applying @frozen strips this resilience, enforcing the following mechanical changes:
For Enumerations:
- Strict Exhaustivity: The compiler guarantees that no new cases will ever be added. Consequently,
switchstatements evaluating a frozen enum from an external module are strictly exhaustive and do not require an@unknown defaultclause to handle future, uncompiled cases. - Payload Layout: The memory footprint required to store the enum’s cases and associated values is permanently locked.
- Fixed Memory Layout: The compiler knows the exact size and alignment of the struct at compile time.
- Direct Access: Clients of the module can allocate the struct directly on the stack and access its stored properties via direct memory offsets, bypassing the overhead of dynamic getter/setter indirection.
- Property Type Restrictions: All stored properties within a frozen struct must have a fixed layout. This includes primitive types, other frozen types, and types with an inherently fixed memory footprint (represented as pointers), such as reference types (classes), existential types (protocols), and function types (closures). Adding, removing, or reordering stored properties in future versions will break ABI compatibility.
Compilation Context
The@frozen attribute is strictly an ABI-level contract. If a Swift target is not compiled with library evolution enabled (which is the default for standard executable applications), all structs and enums are implicitly treated as frozen by the compiler. In these non-evolution contexts, explicitly applying the @frozen attribute is permitted but has no effect. The compiler intentionally does not emit a warning for this redundancy, ensuring that source code remains compatible across different compilation environments and package configurations.
Master Swift with Deep Grasping Methodology!Learn More





