You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is intended to clearly define two concepts, the runtime versus the
strategy. Although there is significant overlap between them (they are
chosen using the same CLI flag after all), I think it is helpful to
define them as separate concepts.
Copy file name to clipboardExpand all lines: src/panic.md
+19-3Lines changed: 19 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -19,24 +19,39 @@ There are also language features that provide a level of control over panic beha
19
19
r[panic.runtime]
20
20
## Panic runtimes
21
21
22
-
The actual behavior and implementation of `panic!` is controlled by the _panic runtime_.
22
+
The actual behavior and implementation of a panic is controlled by the _panic runtime_. The panic runtime is a handler linked into the output which provides the necessary implementation for panicking.
23
23
24
24
> [!NOTE]
25
25
> The Rust standard library provides two panic runtimes: `panic_unwind` (which unwinds the stack and is potentially recoverable) and `panic_abort` (which aborts the process and is non-recoverable). The default runtime depends on the target platform, but is generally `panic_unwind` on platforms with native support for C++ exceptions.
26
26
27
+
> [!NOTE]
28
+
> The panic runtime can be chosen in `rustc` with the [`-C panic`] CLI flag when building any crate type except an rlib.
29
+
27
30
> [!NOTE]
28
31
> When compiling code that is guaranteed to be linked to a non-recoverable panic runtime, the optimizer may assume that unwinding across Rust frames is impossible, which can result in both code-size and runtime speed improvements.
29
32
30
33
See also the [`panic_handler` attribute](runtime.md#the-panic_handler-attribute) which can be used to change the behavior of panics.
31
34
35
+
r[panic.strategy]
36
+
## Panic strategy
37
+
38
+
r[panic.strategy.intro]
39
+
The _panic strategy_ defines the kind of panic runtime that a crate is built to support.
40
+
41
+
> [!NOTE]
42
+
> The panic strategy can be chosen in `rustc` with the [`-C panic`] CLI flag.
43
+
44
+
r[panic.strategy.mixed]
45
+
When linking with the `unwind` runtime, all crates must be built with the `unwind` strategy.
46
+
32
47
r[panic.unwind]
33
48
## Unwinding
34
49
35
50
r[panic.unwind.intro]
36
-
Panicking may either be recoverable or non-recoverable, though it can be configured (via `panic=abort`) to always be non-recoverable. (The converse is not true: `panic=unwind` does not guarantee that all panics are recoverable, only that panicking via the `panic!` macro and similar standard library mechanisms is recoverable.)
51
+
Panicking may either be recoverable or non-recoverable, though it can be configured (by choosing the `abort` panic runtime) to always be non-recoverable. (The converse is not true: the `unwind` runtime does not guarantee that all panics are recoverable, only that panicking via the `panic!` macro and similar standard library mechanisms is recoverable.)
37
52
38
53
r[panic.unwind.destruction]
39
-
When panic recovery occurs, the runtime "unwinds" Rust frames, just as C++'s `throw` unwinds C++ frames, until the panic reaches the point of recovery (for instance at a thread boundary). This means that as the panic traverses Rust frames, live objects in those frames that [implement `Drop`][destructors] will have their `drop` methods called. Thus, when normal execution resumes, no-longer-accessible objects will have been "cleaned up" just as if they had gone out of scope normally.
54
+
When panic recovery occurs, the `unwind`runtime "unwinds" Rust frames, just as C++'s `throw` unwinds C++ frames, until the panic reaches the point of recovery (for instance at a thread boundary). This means that as the panic traverses Rust frames, live objects in those frames that [implement `Drop`][destructors] will have their `drop` methods called. Thus, when normal execution resumes, no-longer-accessible objects will have been "cleaned up" just as if they had gone out of scope normally.
40
55
41
56
> [!NOTE]
42
57
> As long as this guarantee of resource-cleanup is preserved, "unwinding" may be implemented without actually using the mechanism used by C++ for the target platform.
@@ -72,3 +87,4 @@ There are currently no guarantees about the behavior that occurs when a foreign
0 commit comments