Skip to content

Commit 4303fdf

Browse files
committed
Fix #[rustc_const_panic_str] functions not actually being hooked
1 parent 5df690e commit 4303fdf

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

crates/hir-ty/src/mir/eval/shim.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ impl Evaluator<'_> {
4949
if self.not_special_fn_cache.borrow().contains(&def) {
5050
return Ok(false);
5151
}
52+
5253
let function_data = self.db.function_data(def);
5354
let is_intrinsic = match &function_data.abi {
5455
Some(abi) => *abi == Interned::new_str("rust-intrinsic"),
@@ -311,16 +312,20 @@ impl Evaluator<'_> {
311312

312313
fn detect_lang_function(&self, def: FunctionId) -> Option<LangItem> {
313314
use LangItem::*;
314-
let candidate = self.db.lang_attr(def.into())?;
315+
let attrs = self.db.attrs(def.into());
316+
317+
if attrs.by_key("rustc_const_panic_str").exists() {
318+
// `#[rustc_const_panic_str]` is treated like `lang = "begin_panic"` by rustc CTFE.
319+
return Some(LangItem::BeginPanic);
320+
}
321+
322+
let candidate = attrs.by_key("lang").string_value().and_then(LangItem::from_str)?;
315323
// We want to execute these functions with special logic
316324
// `PanicFmt` is not detected here as it's redirected later.
317325
if [BeginPanic, SliceLen, DropInPlace].contains(&candidate) {
318326
return Some(candidate);
319327
}
320-
if self.db.attrs(def.into()).by_key("rustc_const_panic_str").exists() {
321-
// `#[rustc_const_panic_str]` is treated like `lang = "begin_panic"` by rustc CTFE.
322-
return Some(LangItem::BeginPanic);
323-
}
328+
324329
None
325330
}
326331

crates/test-utils/src/minicore.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1378,8 +1378,9 @@ mod panic {
13781378
// Special-case the single-argument case for const_panic.
13791379
("{}", $arg:expr $(,)?) => ({
13801380
#[rustc_const_panic_str] // enforce a &&str argument in const-check and hook this by const-eval
1381+
#[rustc_do_not_const_check] // hooked by const-eval
13811382
const fn panic_cold_display<T: $crate::fmt::Display>(arg: &T) -> ! {
1382-
loop {}
1383+
$crate::panicking::panic_display(arg)
13831384
}
13841385
panic_cold_display(&$arg);
13851386
}),

0 commit comments

Comments
 (0)