-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Stabilize RFC 2345: Allow panicking in constants #85194
Comments
One additional unresolved question that @RalfJung had brought up previously - do we ever want to allow catching panics in constants? The only issue I can think of would be users expecting |
Is the question really about whether we want to allow catching panics in constants? |
IIRC, the original question was about whether |
const MSG: &str = "custom message";
const _: () = std::panic!(MSG); This specific example triggers the |
@Aaron1011 so in other words, the question is, will someone write a constant that panics and assumes that this panic aborts compilation in some crucial way, but then we introduce a wrapper that captures the panic, and now the safety guarantee is not upheld? |
Personally I'd like to be able to panic in a const context to perform data validation from a macro. This would allow me to eliminate a procedural macro. If a const panic could be caught, that would effectively eliminate the ability to validate data (I think, feel free to correct me). Reference to what I hope will be possible once |
@jhpratt The code in that issue would not be affected by |
@Nemo157 In my comment on the original issue I proposed other options, which might be easier to implement:
|
I'd like an example where there is a panic that should abort compilation but catching it would produce a problem -- it feels like the panic erases the const value being produced, so I'm not clear on what kind of error would be swallowed that would be a problem. |
@Aaron1011 wrote
I am confused -- yes we do want to allow panicking in constants...? EDIT: Oh, I guess you forgot a "catch" somewhere... could you edit the text? Looks like I am not the only one who got confused. ;) |
I guess if a But I don't see how this would be any more problematic at compile-time than it is at run-time. |
Another question that came up is if we should change the error that is emitted when a |
I see. I definitely think it should be a hard error. |
Are |
|
Based on the conversation in the lang team meeting today, we're good to move to @rfcbot fcp merge There was a conversation that potentially allowing the catching of panics is not really a concern, as we could create a more specific uncatchable-in-const error if needed (strawmen: |
Team member @scottmcm has proposed to merge this. The next step is review by the rest of the tagged team members: Concerns:
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
panic in const questions writeupdo we want to allow catching panics in constants?Why not? There may be some library that wishes to unconditionally abort compilation, but a wrapper library could prevent that from happening. However, as Ralf noted here, it's not clear why this should be more problematic at compilation time than runtime. It's also easy to imagine legitimate uses for catch in panics. Niko's conclusion: We should document that panics may eventually be catchable and if folks want unconditional compilation aborts to raise those examples with us. We can add another mechanism to enable it (analogous to abort at runtime). hard error or not?Ralf wrote:
Niko's conclusion: It should be a hard error. Raising a concern for this. @rfcbot concern hard-error unwrap/expectQuestion: can we stabilize unwrap/expect? Ralf's Answer: unwrap yes, but expect requires more machinery Seems fne. |
@rfcbot reviewed I'm generally in favor, once my concern is resolved. |
I think I have vaguely the opposite preference. It's not clear to me why one would want to catch a panic that occurs at compile-time. The usual use-cases I'm aware of for catching panics involve recovery from unexpected runtime errors or building test harnesses, neither of which seem applicable. That said, I don't feel super strongly about this, and I think my preference here is largely a reflection of my usual distaste for catching panics (I can't remember the last time I used a mode other than |
I've opened #85704 to turn const panics into a hard error. |
I don't understand why we would resolve the question of whether a panic can be caught or not at this time. I'd rather wait and gain experience. The |
@nikomatsakis: With #85704 merged, a const panic now results in a hard error. |
Because
Oh, didn't realize that was already fixed. Apparently I hit an edge case by accident, which still ICE's: #85907 |
Ah yeah I see. |
That would be relatively simple to implement as a special case in the builtin macro. But making it work for |
We discussed this in the 2021-06-15 lang meeting and determined it makes sense to close this issue specifically tracking stabilization at this time. There's a clear blocker to stabilizing at this time around the migration story to the next edition. The lang team is still interested in this getting stabilized, but until that issue is resolved doesn't want to track stabilization on this issue. A new one can get filed and FCP re-proposed once there's a new draft. |
…r, r=kennytm Make `fmt::Arguments::as_str` unstably const Motivation: mostly to move "panic!() in const contexts" forward, making use of `as_str` was mentioned in rust-lang#85194 (comment) and seems like the simplest way forward.
Is it possible to special-case Rust 2021 |
@nbdd0121 Those are not the same:
We can make |
So, I see that there are a lot of blockers on the 2021-edition-style panicking, but I don't actually know what the plan is for this. Got lead here from the tracking issue for #53188, which is blocked on this but still open. Is the plan to just never allow panicking in const functions, ever? That seems wrong. Are we blocking it on const trait impls to ensure that arbitrary |
Personally I'd like to see const panic with only string literals supported — this would permit things along the lines of (debug) assertions so long as a custom message is provided. Something is better than nothing. |
@clarfonthey There's multiple ways forward:
In addition, I've heard this idea floating around to make |
Still unstably by the looks of it? playground Just asking for clarity purposes because your wording makes it sound like that subset has been stabilized. |
Yes, gated behind |
@clarfonthey Note that this issue was just for the stabilization report. The tracking issue for |
Oh! That makes a lot of sense now. |
I'd like to see at least |
The issue is that we want all code that works in Rust 2018 to work (or auto-fixed to working code) in Rust 2021. If we stablise
|
Stabilization report
🛑 Issue with Rust 2021 🛑
Stabilization is currently blocked on resolving how this works with Rust 2021
Summary
This feature allows panicking within constants and reporting a custom message as a compile-time error.
The following code will become valid on stable:
The message looks like:
Motivation for this RFC can be found here: https://rust-lang.github.io/rfcs/2345-const-panic.html#motivation
More examples can be found here: https://github.com/rust-lang/rust/blob/673d0db5e393e9c64897005b470bfeb6d5aec61b/src/test/ui/consts/const-eval/const_panic.rs
Edge cases & tests
Some questions which should be resolved in the future
We have some "unresolved questions" but they aren't an actual blocker.
Or do we just produce the exact message the panic would produce?
Result::unwrap
andOption::unwrap
becomeconst fn
, doing both in one go might be a good idea.See the brief summary comment here: #51999 (comment)
Originally posted by @JohnTitor in #51999 (comment)
The text was updated successfully, but these errors were encountered: