-
Notifications
You must be signed in to change notification settings - Fork 13.3k
panic!() with formatting arguments sometimes accepted in const #139621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@rust-lang/wg-const-eval Continuing the discussion we started on #139135: What do we do about this? We could 'fix' the situation by not accepting any formatting with arguments in const panic, but that would mean a breaking change for B1 and B3 (and A1 and A2, if we don't we keep the special If we move forward to make But if we allow more, we end up with situations where we panic in const where we cannot format the message in const. |
Ideally,
I'd suggest cratering that. |
I'm no expert on const eval, but it seems the best way out might be to accept all panic!() with any formatting in const, and handle the situation where we cannot format the message in diagnostics. E.g. just print a generic "panic during evaluation" message without the formatted panic message in those cases. Then if we can add support for more formatting in const in the future, we can always add more cases where we add the message, which is now just a diagnostics quality issue rather than a breaking/stable change. |
I guess that would be the other option -- though we should communicate somehow why we ignored the panic message that the user wrote. If t-libs-api can live with const panics having degraded panic messages in a way that is only apparent at "runtime" (i.e., const-eval time, not writing-the-code time), that's fine for me. |
I think the main problem there is that So if we make all We could add a special |
FWIW there have been requests to support formatting numbers in const panics, and the corresponding code would likely actually work in const-eval. So we may not want to tie us to "no arguments", and instead allow arguments with a given allowlist of types. That would involve some crude hacks though to get the heap allocations inside the formatting machinery to work, so the idea of making this best-effort rather than a stable guarantee is enticing. |
When const evaluating a panic, we know we're going to error out anyway, so can we then disable some const checks to allow 'const' evaluating more formatting code? I think @eddyb suggested that a few years ago (something with -Z unleash-the-miri-inside-of-you), but I don't know how feasible/reasonable that is with how things work today. |
We raced saying the same thing. :) One hard constraint I'd like to impose here though is that we do not do this for any user-defined code. Only code we fully control should be run in this "more permissive" const-eval panic formatting mode. |
Just curious: why? It's a proper sandbox, right? |
The previous attempt at this was #90488 (the PR's state was to allow all types to be formatted, and to do dummy formatting for the others, it was closed due to inactivity after being asked to limit it to types we know will be implementable as |
I don't want to risk exposing the unstable parts of the interpreter to whatever creative things people come up with. That's just too many unknowns. |
Don't allow flattened format_args in const. Fixes rust-lang#139136 and rust-lang#139621 by breaking the 'flattened format_args' cases. This is a breaking change. Let's try a crater run.
Turns out that wasn't the error. I did properly mark the |
This issue is a combination of three things:
fn panic_fmt
is marked with#[rustc_do_not_const_check]
and fully relies onformat_args!()
with any arguments to not be accepted in const.Inlining/flattening of format_args!() accidentally exposed as stable through const #139136 -
I accidentally marked this function asEdit: This is actually caused by [generic_assert] Constify methods used by the formatting system #135139const
, meaning that flattened format_args are accepted in const. This went unnoticed until two weeks ago.We have a special case for
panic!("{}", $expr)
that we added at some point to makepanic!("{}", my_string)
work in const, as a workaround forpanic!(my_string)
which no longer works as of Rust 2021.The result is pretty terrible:
Only the four marked lines error. The others compile fine. 🙃
(The
A
constants use the"{}"
special case forpanic!()
and end up invokingpanic_display
for which we have a special const eval check that the argument is a&str
. TheB
constants end up invokingpanic_fmt
(which does not have any const eval checks) andformat_args!()
.)The text was updated successfully, but these errors were encountered: