-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Description
Currently we have dedicated error variants for many interpreter errors in our InterpError
enum, but for some errors we use throw_ub_custom!
to directly pick a translatable diagnostic instead of a variant.
The original purpose of these variants was to
- make sure the same error looks the same when raised from multiple locations
- avoid expensive operations such as string formatting when constructing an error, if it is never actually shown to the user (because it is caught again later, e.g. during value validation)
The first point doesn't really apply any more with translatable diagnostics. And the overhead of these error variants is quite significant; they each need an arm in diagnostic_message
and add_args
. (And these two things need to be carefully synced, since diagnostic_message
decides which arguments need to be added later! Would be nice if these could be syntactically together somehow. Right now not only are we using an entirely untyped system here without any checks whether the right arguments are being added, we also have setting the arguments quite far removed from the only place that could potentially tell us which arguments are the right ones. That's pretty bad for maintenance.) That makes it tempting to convert errors to throw_ub_custom!
and reduce this boilerplate, but I don't know if that is a good idea -- @fee1-dead suggested we should avoid throw_ub_custom!
.
I also see some throw_ub_custom!
do expensive work on error creation (such as this), though under the hood the macro puts this into a thunk so -- if the error is never rendered, does the format!
ever happen?
Either way, we should come up with some kind of consistent policy here. We have a lot of throw_ub_custom!
currently (34 to be precise), I don't quite see the point of turning them all into variants -- that would be a lot of work, for which gain?
Activity
RalfJung commentedon Nov 15, 2023
There's something else about this that I do not understand... part of the issue here was that we needed a diagnostics type that includes a Span, and so we have this entire duplication of error types in compiler/rustc_const_eval/src/errors.rs.
However, when emitting a translatable lint, we are using diagnostics types without a span! Here are some examples. Only when emitting an error do we need a diagnostic type with span. This makes it a bunch of work to transition between lint and hard error, and also causes a lot of the pain in const-eval.
Why can't we emit hard errors the same way we emit lints, i.e. with these two separate arguments (that's from
emit_spanned_lint
)?Instead,
emit_err
takes anerr: impl IntoDiagnostic<'a>
.Cc @davidtwco
davidtwco commentedon Nov 19, 2023
I'm aware of this specific issue, and addressing the various paper cuts around the diagnostic structs and translatable diagnostics is near the top of my to-do list (I'm also unhappy with the impact of the translatable diagnostic structs on our ergonomics), so I'll assign myself to this and make sure to loop back to it when I've got a little bit more time to write something that would useful.
However, I can quickly answer this:
Lints are handled differently from diagnostics, because the lint machinery uses the
Span
to decide on whether a lint should be silenced entirely, a warning or an error (theSpan
lets it look for#[allow(..)]
or#[deny(..)]
, etc). We probably could do hard errors in the same way, I just hadn't thought to do that at the time. Including theSpan
in the error itself is useful though, because then you can easily add additional warnings/notes/labels to that sameSpan
by just annotating the field.We have an implementation of
IntoDiagnostic
onSpanned
and that might let you separate out theSpan
from the type, and ideally re-use the type part as a lint - though we would need unifying theDecorateLint
andIntoDiagnostic
traits for that, which might be achievable.RalfJung commentedon Nov 19, 2023
Ah I see, makes sense.
So yeah, having the option of giving the span and diagnostics info separately for hard errors might be useful, both for porting lints to hard errors and for making lints have less overhead in const-eval.
RalfJung commentedon Nov 26, 2023
Hm, looking at the API again I am actually getting doubts about this. The lint-emitting functions take both a
hir_id
and aspan
. I thought it is thehir_id
that is used to determine which#[allow(..)]
etc apply? There are various parts in the compiler where alint_root
function is used to compute said HIR ID.RalfJung commentedon Feb 14, 2024
This feels somewhat separate from the const-eval issue, so I have opened a separate issue to track this: #121077.
RalfJung commentedon Feb 14, 2024
I wonder if we can use the
derive(LintDiagnostic)
machinery in const-eval somehow to get automatic code generation for diagnostics without a span, and then have an "adapter" that adds the span so this can be fed into the hard-error APIs? Or would that be too much of a hack?^^Like, I am imagining something like
Not sure if that is even remotely realistic though.
davidtwco commentedon Feb 16, 2024
I'd like to be able to do something like that eventually, from my earlier comment:
I'm working towards this at the moment.