Skip to content

Commit 7d8acef

Browse files
authored
[docs] Update SIL.rst for the new verifier checks on enum instructions (#18450)
1 parent 3bec21a commit 7d8acef

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

docs/SIL.rst

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3874,12 +3874,12 @@ arrays or if the element-types do not match.
38743874
Enums
38753875
~~~~~
38763876

3877-
These instructions construct values of enum type. Loadable enum values are
3878-
created with the `enum`_ instruction. Address-only enums require two-step
3879-
initialization. First, if the case requires data, that data is stored into
3880-
the enum at the address projected by `init_enum_data_addr`_. This step is
3881-
skipped for cases without data. Finally, the tag for
3882-
the enum is injected with an `inject_enum_addr`_ instruction::
3877+
These instructions construct and manipulate values of enum type. Loadable enum
3878+
values are created with the `enum`_ instruction. Address-only enums require
3879+
two-step initialization. First, if the case requires data, that data is stored
3880+
into the enum at the address projected by `init_enum_data_addr`_. This step is
3881+
skipped for cases without data. Finally, the tag for the enum is injected with
3882+
an `inject_enum_addr`_ instruction::
38833883

38843884
enum AddressOnlyEnum {
38853885
case HasData(AddressOnlyType)
@@ -3938,6 +3938,21 @@ projecting the enum value with `unchecked_take_enum_data_addr`_::
39383938
/* use %b */
39393939
}
39403940

3941+
Both `switch_enum`_ and `switch_enum_addr`_ must include a ``default`` case
3942+
unless the enum can be exhaustively switched in the current function, i.e. when
3943+
the compiler can be sure that it knows all possible present and future values
3944+
of the enum in question. This is generally true for enums defined in Swift, but
3945+
there are two exceptions: *non-frozen enums* declared in libraries compiled
3946+
with the ``-enable-resilience`` flag, which may grow new cases in the future in
3947+
an ABI-compatible way; and enums marked with the ``objc`` attribute, for which
3948+
other bit patterns are permitted for compatibility with C. All enums imported
3949+
from C are treated as "non-exhaustive" for the same reason, regardless of the
3950+
presence or value of the ``enum_extensibility`` Clang attribute.
3951+
3952+
(See `SE-0192`__ for more information about non-frozen enums.)
3953+
3954+
__ https://github.com/apple/swift-evolution/blob/master/proposals/0192-non-exhaustive-enums.md
3955+
39413956
enum
39423957
````
39433958
::
@@ -4075,6 +4090,9 @@ but turns the control flow dependency into a data flow dependency.
40754090
For address-only enums, `select_enum_addr`_ offers the same functionality for
40764091
an indirectly referenced enum value in memory.
40774092

4093+
Like `switch_enum`_, ``select_enum`` must have a ``default`` case unless the
4094+
enum can be exhaustively switched in the current function.
4095+
40784096
select_enum_addr
40794097
````````````````
40804098
::
@@ -4097,6 +4115,9 @@ Selects one of the "case" or "default" operands based on the case of the
40974115
referenced enum value. This is the address-only counterpart to
40984116
`select_enum`_.
40994117

4118+
Like `switch_enum_addr`_, ``select_enum_addr`` must have a ``default`` case
4119+
unless the enum can be exhaustively switched in the current function.
4120+
41004121
Protocol and Protocol Composition Types
41014122
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41024123

@@ -5207,13 +5228,13 @@ switch_enum
52075228
Conditionally branches to one of several destination basic blocks based on the
52085229
discriminator in a loadable ``enum`` value. Unlike ``switch_int``,
52095230
``switch_enum`` requires coverage of the operand type: If the ``enum`` type
5210-
is resilient, the ``default`` branch is required; if the ``enum`` type is
5211-
fragile, the ``default`` branch is required unless a destination is assigned to
5212-
every ``case`` of the ``enum``. The destination basic block for a ``case`` may
5213-
take an argument of the corresponding ``enum`` ``case``'s data type (or of the
5214-
address type, if the operand is an address). If the branch is taken, the
5215-
destination's argument will be bound to the associated data inside the
5216-
original enum value. For example::
5231+
cannot be switched exhaustively in the current function, the ``default`` branch
5232+
is required; otherwise, the ``default`` branch is required unless a destination
5233+
is assigned to every ``case`` of the ``enum``. The destination basic block for
5234+
a ``case`` may take an argument of the corresponding ``enum`` ``case``'s data
5235+
type (or of the address type, if the operand is an address). If the branch is
5236+
taken, the destination's argument will be bound to the associated data inside
5237+
the original enum value. For example::
52175238

52185239
enum Foo {
52195240
case Nothing
@@ -5280,11 +5301,12 @@ Conditionally branches to one of several destination basic blocks based on
52805301
the discriminator in the enum value referenced by the address operand.
52815302

52825303
Unlike ``switch_int``, ``switch_enum`` requires coverage of the operand type:
5283-
If the ``enum`` type is resilient, the ``default`` branch is required; if the
5284-
``enum`` type is fragile, the ``default`` branch is required unless a
5285-
destination is assigned to every ``case`` of the ``enum``.
5304+
If the ``enum`` type cannot be switched exhaustively in the current function,
5305+
the ``default`` branch is required; otherwise, the ``default`` branch is
5306+
required unless a destination is assigned to every ``case`` of the ``enum``.
52865307
Unlike ``switch_enum``, the payload value is not passed to the destination
5287-
basic blocks; it must be projected out separately with `unchecked_take_enum_data_addr`_.
5308+
basic blocks; it must be projected out separately with
5309+
`unchecked_take_enum_data_addr`_.
52885310

52895311
dynamic_method_br
52905312
`````````````````

0 commit comments

Comments
 (0)