Implement diagnostic translation for expected lifetime parameter message

This commit is contained in:
Dayo 2023-07-03 18:08:49 +09:00
parent 839e9a6e12
commit d6c93b33d0
3 changed files with 125 additions and 16 deletions

View file

@ -22,6 +22,8 @@ extern crate rustc_macros;
#[macro_use]
extern crate tracing;
extern crate self as rustc_errors;
pub use emitter::ColorConfig;
use rustc_lint_defs::LintExpectationId;
@ -375,13 +377,16 @@ pub struct ExplicitBug;
/// rather than a failed assertion, etc.
pub struct DelayedBugPanic;
use crate::diagnostic_impls::{DelayedAtWithNewline, DelayedAtWithoutNewline};
pub use diagnostic::{
AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgValue, DiagnosticId,
DiagnosticStyledString, IntoDiagnosticArg, SubDiagnostic,
};
pub use diagnostic_builder::{DiagnosticBuilder, EmissionGuarantee, Noted};
pub use diagnostic_impls::{
DiagnosticArgFromDisplay, DiagnosticSymbolList, LabelKind, SingleLabelManySpans,
DiagnosticArgFromDisplay, DiagnosticSymbolList, ExpectedLifetimeParameter,
IndicateAnonymousLifetime, InvalidFlushedDelayedDiagnosticLevel, LabelKind,
SingleLabelManySpans,
};
use std::backtrace::{Backtrace, BacktraceStatus};
@ -1670,11 +1675,10 @@ impl HandlerInner {
if bug.level != Level::DelayedBug {
// NOTE(eddyb) not panicking here because we're already producing
// an ICE, and the more information the merrier.
bug.note(format!(
"`flushed_delayed` got diagnostic with level {:?}, \
instead of the expected `DelayedBug`",
bug.level,
));
bug.subdiagnostic(InvalidFlushedDelayedDiagnosticLevel {
span: bug.span.primary_span().unwrap(),
level: bug.level,
});
}
bug.level = Level::Bug;
@ -1741,12 +1745,22 @@ impl DelayedDiagnostic {
fn decorate(mut self) -> Diagnostic {
match self.note.status() {
BacktraceStatus::Captured => {
self.inner.note(format!("delayed at {}\n{}", self.inner.emitted_at, self.note));
let inner = &self.inner;
self.inner.subdiagnostic(DelayedAtWithNewline {
span: inner.span.primary_span().unwrap(),
emitted_at: inner.emitted_at.clone(),
note: self.note,
});
}
// Avoid the needless newline when no backtrace has been captured,
// the display impl should just be a single line.
_ => {
self.inner.note(format!("delayed at {} - {}", self.inner.emitted_at, self.note));
let inner = &self.inner;
self.inner.subdiagnostic(DelayedAtWithoutNewline {
span: inner.span.primary_span().unwrap(),
emitted_at: inner.emitted_at.clone(),
note: self.note,
});
}
}
@ -1838,7 +1852,7 @@ pub fn add_elided_lifetime_in_path_suggestion(
incl_angl_brckt: bool,
insertion_span: Span,
) {
diag.span_label(path_span, format!("expected lifetime parameter{}", pluralize!(n)));
diag.subdiagnostic(ExpectedLifetimeParameter { span: path_span, count: n });
if !source_map.is_span_accessible(insertion_span) {
// Do not try to suggest anything if generated by a proc-macro.
return;
@ -1846,12 +1860,12 @@ pub fn add_elided_lifetime_in_path_suggestion(
let anon_lts = vec!["'_"; n].join(", ");
let suggestion =
if incl_angl_brckt { format!("<{}>", anon_lts) } else { format!("{}, ", anon_lts) };
diag.span_suggestion_verbose(
insertion_span.shrink_to_hi(),
format!("indicate the anonymous lifetime{}", pluralize!(n)),
diag.subdiagnostic(IndicateAnonymousLifetime {
span: insertion_span.shrink_to_hi(),
count: n,
suggestion,
Applicability::MachineApplicable,
);
});
}
#[derive(Clone, Copy, PartialEq, Hash, Debug)]