-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Tracking issue for RFC 2345, "Allow panicking in constants" (const_panic) #51999
Comments
Don't forget |
I suppose |
I think they would be "automatically", since macros don’t have a separation similar to |
I'll add some tests to #52011, but yes, it just works. |
…anics_constantly, r=eddyb Allow panicking with string literal messages inside constants r? @eddyb cc #51999 we can't implement things like `panic!("foo: {}", x)` right now because we can't call trait methods (most notably `Display::fmt`) inside constants. Also most of these impls probably have loops and conditions, so it's messy anyway. But hey `panic!("foo")` works at least. cc @japaric got any test ideas for `#![no_std]`?
Triage: @oli-obk so this is implemented now, right? |
Yes |
Now this is implemented, is there anything stopping |
@IsaacWoods to my knowledge there shouldn't be anything in the way of that. |
@Centril great! I'll start work on a PR in that case |
The thing in the way of that is running …oh, right, I was thinking in terms of |
On second thoughts, even |
Yea, we need conditions in constants first |
Right; bummer. @oli-obk Any thoughts about the first unresolved question? |
I'm for basically reporting it verbatim, but having the same prefix ( |
@oli-obk I like that; (and it's a diagnostics issue anyways, not part of the spec, so it is up to T-compiler to decide this...). |
Now that #86998 is merged, this should be good to stabilize, correct? |
I'm still unhappy that in 2018 we can do |
👍 as long as this is enforced properly(i.e. everything accepted in 2018 on stable also works in 2021). |
it is not yet. Not sure how doable that is, it may be easier to just figure out how to do |
For our planned usage of this feature in SQLx we would really prefer either support for formatting or being able to compute panic strings. We can sorta work around it if the panic strings need to be literals but it won't be the greatest. |
One thing that I noticed which kind of sucks: this being unstable means that you can't use |
Allow `panic!("{}", computed_str)` in const fn. Special-case `panic!("{}", arg)` and translate it to `panic_display(&arg)`. `panic_display` will behave like `panic_any` in cosnt eval and behave like `panic!(format_args!("{}", arg))` in runtime. This should bring Rust 2015 and 2021 to feature parity in terms of `const_panic`; and hopefully would unblock the stabilisation of rust-lang#51999. `@rustbot` modify labels: +T-compiler +T-libs +A-const-eval +A-const-fn r? `@oli-obk`
…shtriplett Stabilize `const_panic` Closes rust-lang#51999 FCP completed in rust-lang#89006 `@rustbot` label +A-const-eval +A-const-fn +T-lang cc `@oli-obk` for review (not `r?`'ing as not on lang team)
…shtriplett Stabilize `const_panic` Closes rust-lang#51999 FCP completed in rust-lang#89006 ``@rustbot`` label +A-const-eval +A-const-fn +T-lang cc ``@oli-obk`` for review (not `r?`'ing as not on lang team)
I've noticed that you can use a non-static const _: () = {
let arr = [b'b', b'y', b'e'];
let msg: &str = unsafe{ std::str::from_utf8_unchecked(&arr) };
panic!("{}", msg)
}; if it is intentional, I have an idea for how to implement const panics with bounded-length formatted arguments in a library. |
Yes this is intentional. Getting that to work was actually a blocker for stabilization. |
Since const panics are now [stabilized], I figure it would be good to document some of its gotchas. I couldn't really find documents for the specifics I ran into. I am no const eval expert, and what I wrote is basically a paraphrase of Ralf's comments[^1][^2], but I hope to get the ball rolling for adding some docs. I deliberately chose to not mention the guarantee about syntactically referenced constants in functions that require code generation as it seems hard to explain completely without some ambiguity. It also seems to me that the rules are not completely stable[^3][^4] yet. [stabilized]: rust-lang/rust#89006 [^1]: rust-lang/rust#91877 (comment) [^2]: rust-lang/rust#91877 (comment) [^3]: rust-lang/rust#71800 [^4]: rust-lang/rust#51999 (comment)
Since const panics are now [stabilized], I figure it would be good to document some of its gotchas. I couldn't really find documents for the specifics I ran into. I am no const eval expert, and what I wrote is basically a paraphrase of Ralf's comments[^1][^2], but I hope to get the ball rolling for adding some docs. I deliberately chose to not mention the guarantee about syntactically referenced constants in functions that require code generation as it seems hard to explain completely without some ambiguity. It also seems to me that the rules are not completely stable[^3][^4] yet. [stabilized]: rust-lang/rust#89006 [^1]: rust-lang/rust#91877 (comment) [^2]: rust-lang/rust#91877 (comment) [^3]: rust-lang/rust#71800 [^4]: rust-lang/rust#51999 (comment)
Is there an issue tracking support for non- Right now the compiler just gives an error (implemented in #80734) "argument to The RFC doesn't mention this limitation. |
There's no special tracking for it. It's essentially just blocked on the fact that you can't implement Display or Debug in a way that is callable in a const context. |
Damn, I just stumbled upon a situation where a I need a fallible enum construction, like this one: const fn parse_bool(s: &str) -> bool {
match s {
"true" => true,
"false" => false,
other => panic!("`{}` is not a valid bool", other), // CAN'T DO THAT!
}
} Time to refactor a huge workaround 😞 I really hope this issue can be fixed, I have so many other situations where it would make my design correct. |
@rodrigocfd You can already panic in const fns, you just can't use string formatting machinery yet. Remove the Also a side note, panicing is a bit suspect strategy if you are expecting the matching to fail with some inputs. This is a closed tracking issue for panics in const and not meant for general discussion, if you need help within the current capabilities of the compiler, try https://users.rust-lang.org or https://www.reddit.com/r/rust/ |
@golddranks thank you. |
This is a tracking issue for the RFC "Allow panicking in constants" (rust-lang/rfcs#2345).
Steps:
Unresolved questions:
Should there be some additional message in the error about this being a panic turned error?
Or do we just produce the exact message the panic would produce?
This change becomes really useful if
Result::unwrap
andOption::unwrap
becomeconst fn
, doing both in one go might be a good idea.Blockers:
The text was updated successfully, but these errors were encountered: