Make DiagnosticBuilder::emit
consuming.
This works for most of its call sites. This is nice, because `emit` very much makes sense as a consuming operation -- indeed, `DiagnosticBuilderState` exists to ensure no diagnostic is emitted twice, but it uses runtime checks. For the small number of call sites where a consuming emit doesn't work, the commit adds `DiagnosticBuilder::emit_without_consuming`. (This will be removed in subsequent commits.) Likewise, `emit_unless` becomes consuming. And `delay_as_bug` becomes consuming, while `delay_as_bug_without_consuming` is added (which will also be removed in subsequent commits.) All this requires significant changes to `DiagnosticBuilder`'s chaining methods. Currently `DiagnosticBuilder` method chaining uses a non-consuming `&mut self -> &mut Self` style, which allows chaining to be used when the chain ends in `emit()`, like so: ``` struct_err(msg).span(span).emit(); ``` But it doesn't work when producing a `DiagnosticBuilder` value, requiring this: ``` let mut err = self.struct_err(msg); err.span(span); err ``` This style of chaining won't work with consuming `emit` though. For that, we need to use to a `self -> Self` style. That also would allow `DiagnosticBuilder` production to be chained, e.g.: ``` self.struct_err(msg).span(span) ``` However, removing the `&mut self -> &mut Self` style would require that individual modifications of a `DiagnosticBuilder` go from this: ``` err.span(span); ``` to this: ``` err = err.span(span); ``` There are *many* such places. I have a high tolerance for tedious refactorings, but even I gave up after a long time trying to convert them all. Instead, this commit has it both ways: the existing `&mut self -> Self` chaining methods are kept, and new `self -> Self` chaining methods are added, all of which have a `_mv` suffix (short for "move"). Changes to the existing `forward!` macro lets this happen with very little additional boilerplate code. I chose to add the suffix to the new chaining methods rather than the existing ones, because the number of changes required is much smaller that way. This doubled chainging is a bit clumsy, but I think it is worthwhile because it allows a *lot* of good things to subsequently happen. In this commit, there are many `mut` qualifiers removed in places where diagnostics are emitted without being modified. In subsequent commits: - chaining can be used more, making the code more concise; - more use of chaining also permits the removal of redundant diagnostic APIs like `struct_err_with_code`, which can be replaced easily with `struct_err` + `code_mv`; - `emit_without_diagnostic` can be removed, which simplifies a lot of machinery, removing the need for `DiagnosticBuilderState`.
This commit is contained in:
parent
ca2fc426a9
commit
b1b9278851
86 changed files with 329 additions and 312 deletions
|
@ -198,23 +198,34 @@ impl EmissionGuarantee for rustc_span::fatal_error::FatalError {
|
|||
}
|
||||
}
|
||||
|
||||
/// In general, the `DiagnosticBuilder` uses deref to allow access to
|
||||
/// the fields and methods of the embedded `diagnostic` in a
|
||||
/// transparent way. *However,* many of the methods are intended to
|
||||
/// be used in a chained way, and hence ought to return `self`. In
|
||||
/// that case, we can't just naively forward to the method on the
|
||||
/// `diagnostic`, because the return type would be a `&Diagnostic`
|
||||
/// instead of a `&DiagnosticBuilder<'a>`. This `forward!` macro makes
|
||||
/// it easy to declare such methods on the builder.
|
||||
/// `DiagnosticBuilder` impls `DerefMut`, which allows access to the fields and
|
||||
/// methods of the embedded `Diagnostic`. However, that doesn't allow method
|
||||
/// chaining at the `DiagnosticBuilder` level. Each use of this macro defines
|
||||
/// two builder methods at that level, both of which wrap the equivalent method
|
||||
/// in `Diagnostic`.
|
||||
/// - A `&mut self -> &mut Self` method, with the same name as the underlying
|
||||
/// `Diagnostic` method. It is mostly to modify existing diagnostics, either
|
||||
/// in a standalone fashion, e.g. `err.code(code)`, or in a chained fashion
|
||||
/// to make multiple modifications, e.g. `err.code(code).span(span)`.
|
||||
/// - A `self -> Self` method, with `_mv` suffix added (short for "move").
|
||||
/// It is mostly used in a chained fashion when producing a new diagnostic,
|
||||
/// e.g. `let err = struct_err(msg).code_mv(code)`, or when emitting a new
|
||||
/// diagnostic , e.g. `struct_err(msg).code_mv(code).emit()`.
|
||||
///
|
||||
/// Although the latter method can be used to modify an existing diagnostic,
|
||||
/// e.g. `err = err.code_mv(code)`, this should be avoided because the former
|
||||
/// method give shorter code, e.g. `err.code(code)`.
|
||||
macro_rules! forward {
|
||||
// Forward pattern for &mut self -> &mut Self
|
||||
(
|
||||
$(#[$attrs:meta])*
|
||||
pub fn $n:ident(&mut self $(, $name:ident: $ty:ty)* $(,)?) -> &mut Self
|
||||
($n:ident, $n_mv:ident)($($name:ident: $ty:ty),* $(,)?)
|
||||
) => {
|
||||
$(#[$attrs])*
|
||||
#[doc = concat!("See [`Diagnostic::", stringify!($n), "()`].")]
|
||||
pub fn $n(&mut self $(, $name: $ty)*) -> &mut Self {
|
||||
pub fn $n(&mut self, $($name: $ty),*) -> &mut Self {
|
||||
self.diagnostic.$n($($name),*);
|
||||
self
|
||||
}
|
||||
#[doc = concat!("See [`Diagnostic::", stringify!($n), "()`].")]
|
||||
pub fn $n_mv(mut self, $($name: $ty),*) -> Self {
|
||||
self.diagnostic.$n($($name),*);
|
||||
self
|
||||
}
|
||||
|
@ -254,11 +265,15 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Emit the diagnostic. Does not consume `self`, which may be surprising,
|
||||
/// but there are various places that rely on continuing to use `self`
|
||||
/// after calling `emit`.
|
||||
/// Emit and consume the diagnostic.
|
||||
#[track_caller]
|
||||
pub fn emit(&mut self) -> G::EmitResult {
|
||||
pub fn emit(mut self) -> G::EmitResult {
|
||||
G::emit_producing_guarantee(&mut self)
|
||||
}
|
||||
|
||||
/// Emit the diagnostic without consuming it. `emit` should be preferred.
|
||||
#[track_caller]
|
||||
pub fn emit_without_consuming(&mut self) -> G::EmitResult {
|
||||
G::emit_producing_guarantee(self)
|
||||
}
|
||||
|
||||
|
@ -267,7 +282,7 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
|
|||
///
|
||||
/// See `emit` and `delay_as_bug` for details.
|
||||
#[track_caller]
|
||||
pub fn emit_unless(&mut self, delay: bool) -> G::EmitResult {
|
||||
pub fn emit_unless(mut self, delay: bool) -> G::EmitResult {
|
||||
if delay {
|
||||
self.downgrade_to_delayed_bug();
|
||||
}
|
||||
|
@ -354,142 +369,142 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
|
|||
/// In the meantime, though, callsites are required to deal with the "bug"
|
||||
/// locally in whichever way makes the most sense.
|
||||
#[track_caller]
|
||||
pub fn delay_as_bug(&mut self) -> G::EmitResult {
|
||||
pub fn delay_as_bug(mut self) -> G::EmitResult {
|
||||
self.downgrade_to_delayed_bug();
|
||||
self.emit()
|
||||
}
|
||||
|
||||
forward!(pub fn span_label(
|
||||
&mut self,
|
||||
/// Non-consuming variant of `delay_as_bug`.
|
||||
#[track_caller]
|
||||
pub fn delay_as_bug_without_consuming(&mut self) -> G::EmitResult {
|
||||
self.downgrade_to_delayed_bug();
|
||||
self.emit_without_consuming()
|
||||
}
|
||||
|
||||
forward!((span_label, span_label_mv)(
|
||||
span: Span,
|
||||
label: impl Into<SubdiagnosticMessage>
|
||||
) -> &mut Self);
|
||||
forward!(pub fn span_labels(
|
||||
&mut self,
|
||||
label: impl Into<SubdiagnosticMessage>,
|
||||
));
|
||||
forward!((span_labels, span_labels_mv)(
|
||||
spans: impl IntoIterator<Item = Span>,
|
||||
label: &str,
|
||||
) -> &mut Self);
|
||||
forward!(pub fn note_expected_found(
|
||||
&mut self,
|
||||
));
|
||||
forward!((note_expected_found, note_expected_found_mv)(
|
||||
expected_label: &dyn fmt::Display,
|
||||
expected: DiagnosticStyledString,
|
||||
found_label: &dyn fmt::Display,
|
||||
found: DiagnosticStyledString,
|
||||
) -> &mut Self);
|
||||
forward!(pub fn note_expected_found_extra(
|
||||
&mut self,
|
||||
));
|
||||
forward!((note_expected_found_extra, note_expected_found_extra_mv)(
|
||||
expected_label: &dyn fmt::Display,
|
||||
expected: DiagnosticStyledString,
|
||||
found_label: &dyn fmt::Display,
|
||||
found: DiagnosticStyledString,
|
||||
expected_extra: &dyn fmt::Display,
|
||||
found_extra: &dyn fmt::Display,
|
||||
) -> &mut Self);
|
||||
forward!(pub fn note(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
|
||||
forward!(pub fn note_once(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
|
||||
forward!(pub fn span_note(
|
||||
&mut self,
|
||||
));
|
||||
forward!((note, note_mv)(
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
));
|
||||
forward!((note_once, note_once_mv)(
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
));
|
||||
forward!((span_note, span_note_mv)(
|
||||
sp: impl Into<MultiSpan>,
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
) -> &mut Self);
|
||||
forward!(pub fn span_note_once(
|
||||
&mut self,
|
||||
));
|
||||
forward!((span_note_once, span_note_once_mv)(
|
||||
sp: impl Into<MultiSpan>,
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
) -> &mut Self);
|
||||
forward!(pub fn warn(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
|
||||
forward!(pub fn span_warn(
|
||||
&mut self,
|
||||
));
|
||||
forward!((warn, warn_mv)(
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
));
|
||||
forward!((span_warn, span_warn_mv)(
|
||||
sp: impl Into<MultiSpan>,
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
) -> &mut Self);
|
||||
forward!(pub fn help(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
|
||||
forward!(pub fn help_once(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
|
||||
forward!(pub fn span_help(
|
||||
&mut self,
|
||||
));
|
||||
forward!((help, help_mv)(
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
));
|
||||
forward!((help_once, help_once_mv)(
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
));
|
||||
forward!((span_help, span_help_once_mv)(
|
||||
sp: impl Into<MultiSpan>,
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
) -> &mut Self);
|
||||
forward!(pub fn is_lint(&mut self) -> &mut Self);
|
||||
forward!(pub fn disable_suggestions(&mut self) -> &mut Self);
|
||||
forward!(pub fn multipart_suggestion(
|
||||
&mut self,
|
||||
));
|
||||
forward!((multipart_suggestion, multipart_suggestion_mv)(
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
suggestion: Vec<(Span, String)>,
|
||||
applicability: Applicability,
|
||||
) -> &mut Self);
|
||||
forward!(pub fn multipart_suggestion_verbose(
|
||||
&mut self,
|
||||
));
|
||||
forward!((multipart_suggestion_verbose, multipart_suggestion_verbose_mv)(
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
suggestion: Vec<(Span, String)>,
|
||||
applicability: Applicability,
|
||||
) -> &mut Self);
|
||||
forward!(pub fn tool_only_multipart_suggestion(
|
||||
&mut self,
|
||||
));
|
||||
forward!((tool_only_multipart_suggestion, tool_only_multipart_suggestion_mv)(
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
suggestion: Vec<(Span, String)>,
|
||||
applicability: Applicability,
|
||||
) -> &mut Self);
|
||||
forward!(pub fn span_suggestion(
|
||||
&mut self,
|
||||
));
|
||||
forward!((span_suggestion, span_suggestion_mv)(
|
||||
sp: Span,
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
suggestion: impl ToString,
|
||||
applicability: Applicability,
|
||||
) -> &mut Self);
|
||||
forward!(pub fn span_suggestions(
|
||||
&mut self,
|
||||
));
|
||||
forward!((span_suggestions, span_suggestions_mv)(
|
||||
sp: Span,
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
suggestions: impl IntoIterator<Item = String>,
|
||||
applicability: Applicability,
|
||||
) -> &mut Self);
|
||||
forward!(pub fn multipart_suggestions(
|
||||
&mut self,
|
||||
));
|
||||
forward!((multipart_suggestions, multipart_suggestions_mv)(
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
suggestions: impl IntoIterator<Item = Vec<(Span, String)>>,
|
||||
applicability: Applicability,
|
||||
) -> &mut Self);
|
||||
forward!(pub fn span_suggestion_short(
|
||||
&mut self,
|
||||
));
|
||||
forward!((span_suggestion_short, span_suggestion_short_mv)(
|
||||
sp: Span,
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
suggestion: impl ToString,
|
||||
applicability: Applicability,
|
||||
) -> &mut Self);
|
||||
forward!(pub fn span_suggestion_verbose(
|
||||
&mut self,
|
||||
));
|
||||
forward!((span_suggestion_verbose, span_suggestion_verbose_mv)(
|
||||
sp: Span,
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
suggestion: impl ToString,
|
||||
applicability: Applicability,
|
||||
) -> &mut Self);
|
||||
forward!(pub fn span_suggestion_hidden(
|
||||
&mut self,
|
||||
));
|
||||
forward!((span_suggestion_hidden, span_suggestion_hidden_mv)(
|
||||
sp: Span,
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
suggestion: impl ToString,
|
||||
applicability: Applicability,
|
||||
) -> &mut Self);
|
||||
forward!(pub fn tool_only_span_suggestion(
|
||||
&mut self,
|
||||
));
|
||||
forward!((tool_only_span_suggestion, tool_only_span_suggestion_mv)(
|
||||
sp: Span,
|
||||
msg: impl Into<SubdiagnosticMessage>,
|
||||
suggestion: impl ToString,
|
||||
applicability: Applicability,
|
||||
) -> &mut Self);
|
||||
forward!(pub fn primary_message(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self);
|
||||
forward!(pub fn span(&mut self, sp: impl Into<MultiSpan>) -> &mut Self);
|
||||
forward!(pub fn code(&mut self, s: DiagnosticId) -> &mut Self);
|
||||
forward!(pub fn arg(
|
||||
&mut self,
|
||||
name: impl Into<Cow<'static, str>>,
|
||||
arg: impl IntoDiagnosticArg,
|
||||
) -> &mut Self);
|
||||
forward!(pub fn subdiagnostic(
|
||||
&mut self,
|
||||
subdiagnostic: impl crate::AddToDiagnostic
|
||||
) -> &mut Self);
|
||||
));
|
||||
forward!((primary_message, primary_message_mv)(
|
||||
msg: impl Into<DiagnosticMessage>,
|
||||
));
|
||||
forward!((span, span_mv)(
|
||||
sp: impl Into<MultiSpan>,
|
||||
));
|
||||
forward!((code, code_mv)(
|
||||
s: DiagnosticId,
|
||||
));
|
||||
forward!((arg, arg_mv)(
|
||||
name: impl Into<Cow<'static, str>>, arg: impl IntoDiagnosticArg,
|
||||
));
|
||||
forward!((subdiagnostic, subdiagnostic_mv)(
|
||||
subdiagnostic: impl crate::AddToDiagnostic,
|
||||
));
|
||||
}
|
||||
|
||||
impl<G: EmissionGuarantee> Debug for DiagnosticBuilder<'_, G> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue