-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
[const_panic] Report const_panic diagnostics identically to compiler_error invocations #79695
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
3ecd5e0
to
dc820e0
Compare
cc @rust-lang/wg-const-eval r? @oli-obk |
// `ConstEvalErr` (in `librustc_middle/mir/interpret/error.rs`) knows to | ||
// handle these. | ||
impl<'tcx> Into<InterpErrorInfo<'tcx>> for ConstEvalErrKind { | ||
fn into(self) -> InterpErrorInfo<'tcx> { | ||
err_machine_stop!(self.to_string()).into() | ||
match self { | ||
ConstEvalErrKind::Panic { msg, .. } => InterpError::Panic(msg).into(), |
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.
I think we can mirror what miri does here. Instead of having an impl MachineStopType
for String
, we create an
enum TerminationInfo {
Generic(String),
Panic(String),
}
Then you can downcast to TerminationInfo
below and handle it just there.
@@ -193,7 +197,13 @@ impl<'tcx> ConstEvalErr<'tcx> { | |||
rustc_session::lint::builtin::CONST_ERR, | |||
hir_id, | |||
tcx.span, | |||
|lint| finish(lint.build(message), Some(err_msg)), | |||
|lint| { | |||
if is_panic { |
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.
I think something similar needs to be done for the Report as hard error
case below. Maybe the match &self.error
above could produce a message
and an Option<String>
for the note, and then you can set the appropriatly once?
@@ -439,6 +439,8 @@ pub enum InterpError<'tcx> { | |||
/// The program exhausted the interpreter's resources (stack/heap too big, | |||
/// execution takes too long, ...). | |||
ResourceExhaustion(ResourceExhaustionInfo), | |||
/// The program terminated immediately from a panic. | |||
Panic(Symbol), |
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.
This is a CTFE-only error, I don't think it should be added here. This is more of a case for the MachineStopType
.
While I agree that the old rendering of panics is not great, I wonder -- what is the justification for rendering panics so differently from all the other kinds of CTFE errors? Why should everything have "any use of this value will cause an error" but only panics do not? |
That's just because we still haven't made
The main reason I see to render panics during CTFE differently is to make them essentially be the way in which users can define their own errors. |
That looks like a bug though if the same error is rendered very differently for statics vs consts.
I'd say these are still user-defined CTFE errors and hence should be recognized as such. It doesn't have to say "the evaluated program panicked", but I think it should say something about const-evaluation somewhere the same way other CTFE errors do. For example. I would be completely on-board with
Though this does look somewhat redundant since the same text is also shown in the source, but whatever.^^ |
In my perspective, the useful point of this is to enable macro authors to produce errors that look like compiler errors and are able to be evaluated in the regular context of the language within the type system. As a couple of concrete examples (taken from SQLx) that I want to see possible:
struct Foo { foo: String }
For context, we (SQLx) can achieve the above today in nightly (with the recent awesome ability to do @RalfJung would you perhaps be okay with uniformly inverting the diagnostic? From your example:
And a non-panic example:
|
The second one is rather odd:
The "this value" points at the division, but the "value" it references is
I see, so you are also relying on macros to hide the underlying source of the error? Isn't this rather fragile, where if you move the What would be so bad about
I am not sure what our usual policy is for the error label vs the note-at-the-span, but it seems to me that the label should be the "general kind of error" and the note-at-the-span should be more specific than that. |
Does it even make sense to fine-tune this if it will all change again when const_err becomes a hard error? Maybe we should first have a PR that makes const_err a future-incompatibility lint and that makes it look the way we'd want the hard error to look later. Right now it all seems to be a bit too much up in the air. |
cc @rust-lang/wg-diagnostics
Yes. I still believe this is a special situation, just like fn main() {
compile_error!("foomp");
} will produce
without any additional information. To me it feels like panicking during CTFE is just a more controllable variant of |
From my experience, newer developers will stop reading as soon as possible when looking at an error message. In that lens, the more important or relevant information to them in order to fix the error, should be directly next to Additionally, consider how this would look with
It's not possible to guess what's going on from that. |
I don't understand why one would rather want to control the label than the note-at-the-span -- honestly, between the two
But if everyone else thinks that's fine, then whatever. However, the rendering should definitely be the same no matter whether the error arises in a
That sounds like it is quite the problem in general since we usually put very useful specific information in the note. I am not convinced hacking around this in individual diagnostics is a good fix.
Interesting, I didn't know about this flag. Are error labels usually useful enough to make that flag any good? If so, then again this seems like something we should fix for all CTFE errors, not just for panics. |
It seems that when |
Definitely... we can't really do this in general until |
Why can't we make the wording of the error already be the same as what it will be when it becomes a hard error? In particular for future incompat lints (which this one really should be), isn't that what we usually do? |
With the current style, I'd make it something like Then I'd propose to make the short title something like |
Triage: |
Given
Before
After
Ref: #51999 (comment)
If this is accepted, I believe that along with the recently merged #78069, the
const_panic
feature should be unblocked and could be stabilized.I'm not super happy with how the implementation of this was done; any pointers on how to clean that up would be appreciated.