【咬文嚼字】2024S文档ch1中对于panic_handler的描述 #207
Replies: 2 comments 2 replies
-
我倾向于认为更好的说法是“ panic_handler 提供语义项 (panic_impl)”。lang-items extern "Rust" {
#[lang = "panic_impl"]
fn panic_impl(pi: &PanicInfo<'_>) -> !;
} 它首先是一个 builtin attribute, 用于定义 Rust 程序运行期间的 panic 的行为:
然后,从 rustc 内部实现来看(也就是你找的那段代码),它其实可以被视为一个宽泛意义上的语义项:
|
Beta Was this translation helpful? Give feedback.
-
又稍微找了一下资料:
既然
其中:
error: the feature `lang_items` is internal to the compiler or standard library
--> src/main.rs:24:12
|
24 | #![feature(lang_items)]
| ^^^^^^^^^^
|
= note: using it is strongly discouraged 从而也就能理解 core lib 只声明,但不能定义 panicking 的原因了 :) core/panicking.rs
//! The core library cannot define panicking, but it does *declare* panicking. This
//! means that the functions inside of core are allowed to panic, but to be
//! useful an upstream crate must define panicking for core to use. The current
//! interface for panicking is: fn panic_impl(pi: &core::panic::PanicInfo<'_>) -> !
|
Beta Was this translation helpful? Give feedback.
-
这里对panic_handler描述为一个语义项lang_item,但实际并不算语义项吧,在死灵书中描述" #[panic_handler] 用于定义 panic! 在 #![no_std] 程序中的行为",并没有lang="xxx"的标识(如之后的会报错提示的eh_personality和start lang_item),应该只算依赖吧
翻看原v3文档对panic_handler解释的比较清楚:
好玩儿的是在 https://github.com/rust-lang/rust/blob/master/compiler/rustc_hir/src/lang_items.rs 中的extract方法(extract lang_item方法连带着panic_handler一起extract了)
下面也有lang_item_table的定义,不过对于原v3文档的解释推敲一下感觉#[panic_handler]属性标记的就是panic_impl?
这就引出下面的core::panicking了
NOTE: FFI(外部函数接口) boundary是指Rust代码和用另一种编程语言编写的代码之间的边界
英吹斯汀,"resolved to the #[panic_handler] function",所以panic_handler其实应该是包含一个语义项实现还是就是解释为一个语义项呢?大家怎么看
Beta Was this translation helpful? Give feedback.
All reactions