Skip to content

Commit

Permalink
Fix #[rustc_const_panic_str] functions not actually being hooked
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Apr 18, 2024
1 parent 5df690e commit 4303fdf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
15 changes: 10 additions & 5 deletions crates/hir-ty/src/mir/eval/shim.rs
Expand Up @@ -49,6 +49,7 @@ impl Evaluator<'_> {
if self.not_special_fn_cache.borrow().contains(&def) {
return Ok(false);
}

let function_data = self.db.function_data(def);
let is_intrinsic = match &function_data.abi {
Some(abi) => *abi == Interned::new_str("rust-intrinsic"),
Expand Down Expand Up @@ -311,16 +312,20 @@ impl Evaluator<'_> {

fn detect_lang_function(&self, def: FunctionId) -> Option<LangItem> {
use LangItem::*;
let candidate = self.db.lang_attr(def.into())?;
let attrs = self.db.attrs(def.into());

if attrs.by_key("rustc_const_panic_str").exists() {
// `#[rustc_const_panic_str]` is treated like `lang = "begin_panic"` by rustc CTFE.
return Some(LangItem::BeginPanic);
}

let candidate = attrs.by_key("lang").string_value().and_then(LangItem::from_str)?;
// We want to execute these functions with special logic
// `PanicFmt` is not detected here as it's redirected later.
if [BeginPanic, SliceLen, DropInPlace].contains(&candidate) {
return Some(candidate);
}
if self.db.attrs(def.into()).by_key("rustc_const_panic_str").exists() {
// `#[rustc_const_panic_str]` is treated like `lang = "begin_panic"` by rustc CTFE.
return Some(LangItem::BeginPanic);
}

None
}

Expand Down
3 changes: 2 additions & 1 deletion crates/test-utils/src/minicore.rs
Expand Up @@ -1378,8 +1378,9 @@ mod panic {
// Special-case the single-argument case for const_panic.
("{}", $arg:expr $(,)?) => ({
#[rustc_const_panic_str] // enforce a &&str argument in const-check and hook this by const-eval
#[rustc_do_not_const_check] // hooked by const-eval
const fn panic_cold_display<T: $crate::fmt::Display>(arg: &T) -> ! {
loop {}
$crate::panicking::panic_display(arg)
}
panic_cold_display(&$arg);
}),
Expand Down

0 comments on commit 4303fdf

Please sign in to comment.