-
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
Make const panic!("..") work in Rust 2021. #86998
Conversation
A small problem with this is that the panic is now reported inside
Maybe const eval should use #[track_caller] too. |
Hm, looks like that should already work:
Trying to figure out what goes wrong now. |
Ah, that's just used for the message, which is correct:
But maybe it should be used for the span to point the error at too. |
This comment has been minimized.
This comment has been minimized.
Fixed in #87000 Marking this as blocked on that. |
0dc0cf7
to
5a8d2a8
Compare
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we have existing tests that ensure that format_args!
cannot be called in const contexts? Just to make sure we don't accidentally allow things due to the various allow_internal_unstable on these macros.
The test is failing now, but will pass once #87000 is merged. |
This change will indeed allow |
How does this MR relate to #86830? They seem to try to solve the same problem...? |
Oh, didn't see that one yet. Looking now. |
fixes the same problem, but the solution is more maintainable I believe.
I think it should work by adding |
Nope :( |
Looks like #86830 does the exact same, but in a different way. It also has the same problem of making |
Oh... and we can't fix that, because any fix would then also allow user-expressions in the macro arguments to use the unstable things. So I guess we'd need @eddyb's solution and have a separate-but-equal forever-unstable const_format_args that panic uses internally. |
Yeah. Did that now. |
This comment has been minimized.
This comment has been minimized.
#![feature(const_panic)] | ||
#![crate_type = "lib"] | ||
|
||
const A: () = std::panic!("blåhaj"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So... if my understanding is right, std::panic!(SOME_CONST)
does not work anymore in 2021, does your change permit panic!("{}", some_str)
to work? If so, that should have a test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oof, our test suite is really subpar. We have zero tests for
const FOO: () = {
let mut x = [4, 2];
for i in 0..x.len() {
x[i] = x[i] + b'0';
}
let z = unsafe { std::str::from_utf8_unchecked(&x) };
panic!("{}", z);
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So... if my understanding is right,
std::panic!(SOME_CONST)
does not work anymore in 2021, does your change permitpanic!("{}", some_str)
to work?
Nope. I want to make a next PR that makes panic!("{}", "literal")
work. panic!("{}", some_str_var)
will be a step further into the future.
This comment has been minimized.
This comment has been minimized.
7caa172
to
3d260ff
Compare
This comment has been minimized.
This comment has been minimized.
Why that? As I suggested in the other PR, the const checking code could accept That seems like a less ugly alternative than a 2nd macro.^^ |
Or we could just do an FCP to start allowing |
☔ The latest upstream changes (presumably #86857) made this pull request unmergeable. Please resolve the merge conflicts. |
During const eval, this replaces calls to core::panicking::panic_fmt and std::panicking::being_panic_fmt with a call to a new const fn: core::panicking::const_panic_fmt. That function uses fmt::Arguments::as_str() to get the str and calls panic_str with that instead. panic!() invocations with formatting arguments are still not accepted, as the creation of such a fmt::Arguments cannot be done in constant functions right now.
Co-authored-by: Ralf Jung <[email protected]>
239ae27
to
312bf8e
Compare
📌 Commit 312bf8e has been approved by |
☀️ Test successful - checks-actions |
During const eval, this replaces calls to core::panicking::panic_fmt and std::panicking::being_panic_fmt with a call to a new const fn: core::panicking::const_panic_fmt. That function uses fmt::Arguments::as_str() to get the str and calls panic_str with that instead.
panic!() invocations with formatting arguments are still not accepted, as the creation of such a fmt::Arguments cannot be done in constant functions right now.
r? @RalfJung