Skip to content

Conversation

Firestar99
Copy link
Member

@eddyb mind if we move that code to declutter call?

Comment on lines +23 to +57
// HACK(eddyb) Rust 2021 `panic!` always uses `format_args!`, even
// in the simple case that used to pass a `&str` constant, which
// would not remain reachable in the SPIR-V - but `format_args!` is
// more complex and neither immediate (`fmt::Arguments` is too big)
// nor simplified in MIR (e.g. promoted to a constant) in any way,
// so we have to try and remove the `fmt::Arguments::new` call here.
#[derive(Default)]
struct DecodedFormatArgs<'tcx> {
/// If fully constant, the `pieces: &'a [&'static str]` input
/// of `fmt::Arguments<'a>` (i.e. the strings between args).
const_pieces: Option<SmallVec<[String; 2]>>,

/// Original references for `fmt::Arguments<'a>` dynamic arguments,
/// i.e. the `&'a T` passed to `fmt::rt::Argument::<'a>::new_*`,
/// tracking the type `T` and `char` formatting specifier.
///
/// E.g. for `format_args!("{a} {b:x}")` they'll be:
/// * `&a` with `typeof a` and ' ',
/// * `&b` with `typeof b` and 'x'
ref_arg_ids_with_ty_and_spec: SmallVec<[(Word, Ty<'tcx>, char); 2]>,

/// If `fmt::Arguments::new_v1_formatted` was used, this holds
/// the length of the `&[fmt::rt::Placeholder]` slice, which
/// currently cannot be directly supported, and therefore even
/// if all of `ref_arg_ids_with_ty_and_spec` are printable,
/// a much jankier fallback still has to be used, as it it were:
///
/// `format!("a{{0}}b{{1}}c\n with {{…}} from: {}, {}", x, y)`
/// (w/ `const_pieces = ["a", "b", "c"]` & `ref_args = [&x, &y]`).
has_unknown_fmt_placeholder_to_args_mapping: Option<usize>,
}
struct FormatArgsNotRecognized(String);

// HACK(eddyb) this is basically a `try` block.
let mut try_decode_and_remove_format_args = || {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make more sense to make these types (and the closure becoming a method) the real interface here, and name the module format_args_decompiler (because that's what this cursedness really is).

In theory we could allow non-panic uses of it, like instead of debug_printf! we could have debug_print! (or even the Rust std dbg! macro), that uses format_args! combined with a function that we treat just like we do panic entry-points.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants