是否可以在库编译时检查 `panic` 是否设置为 `abort`?

Is it possible to check if `panic` is set to `abort` while a library is compiling?

这可能不是一个好主意或不符合习惯,但我们假设出于某种原因,图书馆的业务逻辑依赖于 catch_unwind

如果 they set panic = "abort" 在 "terminal" 箱子的 Cargo.toml 中,我能否以某种方式警告(通过编译失败并显示错误消息?)该库的用户?

我正在考虑检查 build.rs 中的一些环境变量,但是 can't find any variables with this information

当指定 -C panic=abort 时,您可以在二进制文件或库中使用此不稳定代码导致错误:

#![feature(panic_unwind)]
extern crate panic_unwind;

当使用错误的恐慌策略时会导致这个有用的错误:

error: the linked panic runtime `panic_unwind` is not compiled with this crate's panic strategy `abort`

当恐慌策略正确时,extern crate声明是多余的,但什么都不做。当 panic 策略错误时,它会导致链接错误,因为您不能在同一个二进制文件中有两个不同的 panic strategy crate。由于此检查发生在 crate 链接时,请注意,如果顶层 crate 从未真正使用过库,则不会进行检查 运行。 (但这是一件好事:如果您的图书馆未被使用,那么无论如何都不需要进行此检查!)

此外,这个错误发生在编译过程的后期,所以虽然 cargo build 会出错,但 cargo check 不会抱怨,因为 cargo check 不检查链接错误出于性能原因。

不幸的是,似乎没有办法在稳定频道上执行此操作。