Skip to content

Commit c0beae1

Browse files
committed
codegen: preserve panic_const messages in panic fast path
1 parent 7fc220e commit c0beae1

3 files changed

Lines changed: 59 additions & 5 deletions

File tree

crates/rustc_codegen_spirv/src/builder/builder_methods.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3381,8 +3381,12 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
33813381
}
33823382

33833383
if is_panic_entry_point {
3384-
return DecodedFormatArgs::try_decode_and_remove_format_args(self, args)
3385-
.codegen_panic(self, result_type);
3384+
return DecodedFormatArgs::try_decode_and_remove_format_args(
3385+
self,
3386+
args,
3387+
instance_def_id,
3388+
)
3389+
.codegen_panic(self, result_type);
33863390
}
33873391
if buffer_load_intrinsic {
33883392
return self.codegen_buffer_load_intrinsic(fn_abi, result_type, args);

crates/rustc_codegen_spirv/src/builder/format_args_decompiler.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_abi::BackendRepr;
1212
use rustc_data_structures::fx::FxHashSet;
1313
use rustc_middle::ty::Ty;
1414
use rustc_middle::ty::layout::LayoutOf;
15+
use rustc_span::def_id::DefId;
1516
use smallvec::SmallVec;
1617
use std::borrow::Cow;
1718
use std::cell::Cell;
@@ -56,6 +57,7 @@ impl<'tcx> DecodedFormatArgs<'tcx> {
5657
pub fn try_decode_and_remove_format_args<'a>(
5758
builder: &mut Builder<'a, 'tcx>,
5859
args: &[SpirvValue],
60+
panic_def_id: Option<DefId>,
5961
) -> FormatArgsResult<'tcx> {
6062
let mut decoded_format_args = DecodedFormatArgs::default();
6163

@@ -187,11 +189,59 @@ impl<'tcx> DecodedFormatArgs<'tcx> {
187189
Some((pieces, placeholder_count, placeholders_map_1_to_1))
188190
};
189191

192+
let panic_const_message = || {
193+
let def_id = panic_def_id?;
194+
let def_path = cx.tcx.def_path_str(def_id);
195+
let name = def_path.strip_prefix("core::panicking::panic_const::")?;
196+
match name {
197+
"panic_const_add_overflow" => Some("attempt to add with overflow"),
198+
"panic_const_sub_overflow" => Some("attempt to subtract with overflow"),
199+
"panic_const_mul_overflow" => Some("attempt to multiply with overflow"),
200+
"panic_const_div_overflow" => Some("attempt to divide with overflow"),
201+
"panic_const_rem_overflow" => {
202+
Some("attempt to calculate the remainder with overflow")
203+
}
204+
"panic_const_neg_overflow" => Some("attempt to negate with overflow"),
205+
"panic_const_shr_overflow" => Some("attempt to shift right with overflow"),
206+
"panic_const_shl_overflow" => Some("attempt to shift left with overflow"),
207+
"panic_const_div_by_zero" => Some("attempt to divide by zero"),
208+
"panic_const_rem_by_zero" => {
209+
Some("attempt to calculate the remainder with a divisor of zero")
210+
}
211+
"panic_const_coroutine_resumed" => Some("coroutine resumed after completion"),
212+
"panic_const_async_fn_resumed" => Some("`async fn` resumed after completion"),
213+
"panic_const_async_gen_fn_resumed" => {
214+
Some("`async gen fn` resumed after completion")
215+
}
216+
"panic_const_gen_fn_none" => {
217+
Some("`gen fn` should just keep returning `None` after completion")
218+
}
219+
"panic_const_coroutine_resumed_panic" => Some("coroutine resumed after panicking"),
220+
"panic_const_async_fn_resumed_panic" => Some("`async fn` resumed after panicking"),
221+
"panic_const_async_gen_fn_resumed_panic" => {
222+
Some("`async gen fn` resumed after panicking")
223+
}
224+
"panic_const_gen_fn_none_panic" => {
225+
Some("`gen fn` should just keep returning `None` after panicking")
226+
}
227+
"panic_const_coroutine_resumed_drop" => Some("coroutine resumed after async drop"),
228+
"panic_const_async_fn_resumed_drop" => Some("`async fn` resumed after async drop"),
229+
"panic_const_async_gen_fn_resumed_drop" => {
230+
Some("`async gen fn` resumed after async drop")
231+
}
232+
"panic_const_gen_fn_none_drop" => Some("`gen fn` resumed after async drop"),
233+
_ => None,
234+
}
235+
};
236+
190237
// HACK(eddyb) `panic_explicit` doesn't take any regular arguments,
191238
// only an (implicit) `&'static panic::Location<'static>`.
192239
if args.len() == 1 {
193-
decoded_format_args.const_pieces =
194-
Some(["explicit panic".into()].into_iter().collect());
240+
decoded_format_args.const_pieces = Some(
241+
[panic_const_message().unwrap_or("explicit panic").into()]
242+
.into_iter()
243+
.collect(),
244+
);
195245
return Ok(decoded_format_args);
196246
}
197247

tests/compiletests/ui/dis/panic_sequential_many.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ OpExtension "SPV_KHR_non_semantic_info"
44
OpMemoryModel Logical Simple
55
OpEntryPoint Fragment %2 "main" %3 %4 %5
66
OpExecutionMode %2 OriginUpperLeft
7-
%6 = OpString "/n[Rust panicked at $DIR/panic_sequential_many.rs:31:10]/n explicit panic/n in main()/n"
7+
%6 = OpString "/n[Rust panicked at $DIR/panic_sequential_many.rs:31:10]/n attempt to divide by zero/n in main()/n"
88
%7 = OpString "$DIR/panic_sequential_many.rs"
99
OpName %3 "x"
1010
OpName %4 "y"

0 commit comments

Comments
 (0)