@@ -9,7 +9,7 @@ or `std`.
99
1010### core definition of panic!
1111
12- The ` core ` ` panic! ` macro eventually makes the following call (in ` library/core/src/panicking.rs ` ):
12+ The ` core ` ` panic! ` macro eventually makes the following call (in [ ` library/core/src/panicking.rs ` ] ):
1313
1414``` rust
1515// NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
@@ -18,13 +18,18 @@ extern "Rust" {
1818 fn panic_impl (pi : & PanicInfo <'_ >) -> ! ;
1919}
2020
21- let pi = PanicInfo :: internal_constructor (Some (& fmt ), location );
21+ let pi = PanicInfo :: new (
22+ & fmt ,
23+ Location :: caller (),
24+ /* can_unwind */ true ,
25+ /* force_no_backtrace */ false ,
26+ );
2227unsafe { panic_impl (& pi ) }
2328```
2429
2530Actually resolving this goes through several layers of indirection:
2631
27- 1 . In ` compiler/rustc_middle /src/middle/ weak_lang_items.rs ` , ` panic_impl ` is
32+ 1 . In [ ` compiler/rustc_hir /src/weak_lang_items.rs ` ] , ` panic_impl ` is
2833 declared as 'weak lang item', with the symbol ` rust_begin_unwind ` . This is
2934 used in ` rustc_hir_analysis/src/collect.rs ` to set the actual symbol name to
3035 ` rust_begin_unwind ` .
@@ -33,34 +38,33 @@ Actually resolving this goes through several layers of indirection:
3338 which means that core will attempt to call a foreign symbol called ` rust_begin_unwind `
3439 (to be resolved at link time)
3540
36- 2 . In ` library/std/src/panicking.rs ` , we have this definition:
41+ 2 . In [ ` library/std/src/panicking.rs ` ] , we have this definition:
3742
3843``` rust
39- /// Entry point of panic from the core crate.
40- #[cfg(not(test))]
44+ /// Entry point of panics from the core crate (`panic_impl` lang item) .
45+ #[cfg(not(any( test, doctest) ))]
4146#[panic_handler]
42- #[unwind(allowed)]
43- pub fn begin_panic_handler (info : & PanicInfo <'_ >) -> ! {
47+ pub fn panic_handler (info : & core :: panic :: PanicInfo <'_ >) -> ! {
4448 ...
4549}
4650```
4751
48- The special ` panic_handler ` attribute is resolved via ` compiler/rustc_middle /src/middle/ lang_items ` .
49- The ` extract ` function converts the ` panic_handler ` attribute to a ` panic_impl ` lang item.
52+ The special ` panic_handler ` attribute is resolved via [ ` compiler/rustc_passes /src/lang_items.rs ` ] .
53+ The [ ` extract_ast ` ] function converts the ` panic_handler ` attribute to a ` panic_impl ` lang item.
5054
5155Now, we have a matching ` panic_handler ` lang item in the ` std ` . This function goes
5256through the same process as the ` extern { fn panic_impl } ` definition in ` core ` , ending
5357up with a symbol name of ` rust_begin_unwind ` . At link time, the symbol reference in ` core `
54- will be resolved to the definition of ` std ` (the function called ` begin_panic_handler ` in the
58+ will be resolved to the definition of ` std ` (the function called ` panic_handler ` in the
5559Rust source).
5660
5761Thus, control flow will pass from core to std at runtime. This allows panics from ` core `
5862to go through the same infrastructure that other panics use (panic hooks, unwinding, etc)
5963
6064### std implementation of panic!
6165
62- This is where the actual panic-related logic begins. In ` library/std/src/panicking.rs ` ,
63- control passes to ` rust_panic_with_hook ` . This method is responsible
66+ This is where the actual panic-related logic begins. In [ ` library/std/src/panicking.rs ` ] ,
67+ control passes to ` panic_with_hook ` . This method is responsible
6468for invoking the global panic hook, and checking for double panics. Finally,
6569we call ` __rust_start_panic ` , which is provided by the panic runtime.
6670
@@ -111,3 +115,8 @@ the call to the user-provided `main` function is wrapped in `catch_unwind`.
111115
112116
113117[ runtime service ] : https://github.com/rust-lang/rust/blob/HEAD/library/std/src/rt.rs
118+ [ `library/core/src/panicking.rs` ] : https://doc.rust-lang.org/core/panicking/index.html
119+ [ `compiler/rustc_hir/src/weak_lang_items.rs` ] : https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_hir/src/weak_lang_items.rs
120+ [ `library/std/src/panicking.rs` ] : https://github.com/rust-lang/rust/blob/HEAD/library/std/src/panicking.rs
121+ [ `compiler/rustc_passes/src/lang_items.rs` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_passes/lang_items/index.html
122+ [ `extract_ast` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_passes/lang_items/fn.extract_ast.html
0 commit comments