Skip to content

Commit d6c93b3

Browse files
committed
Implement diagnostic translation for expected lifetime parameter message
1 parent 839e9a6 commit d6c93b3

File tree

3 files changed

+125
-16
lines changed

3 files changed

+125
-16
lines changed

compiler/rustc_errors/messages.ftl

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
errors_delayed_at_with_newline =
2+
delayed at {$emitted_at}
3+
{$note}
4+
5+
errors_delayed_at_without_newline =
6+
delayed at {$emitted_at} - {$note}
7+
8+
errors_expected_lifetime_parameter =
9+
expected lifetime {$count ->
10+
[1] parameter
11+
*[other] parameters
12+
}
13+
14+
errors_indicate_anonymous_lifetime =
15+
indicate the anonymous {$count ->
16+
[1] lifetime
17+
*[other] lifetimes
18+
}
19+
20+
errors_invalid_flushed_delayed_diagnostic_level =
21+
`flushed_delayed` got diagnostic with level {$level}, instead of the expected `DelayedBug`
22+
123
errors_target_inconsistent_architecture =
224
inconsistent target specification: "data-layout" claims architecture is {$dl}-endian, while "target-endian" is `{$target}`
325

compiler/rustc_errors/src/diagnostic_impls.rs

+75-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
use crate::{fluent_generated as fluent, AddToDiagnostic};
1+
use crate::diagnostic::DiagnosticLocation;
2+
use crate::{fluent_generated as fluent, AddToDiagnostic, Diagnostic};
23
use crate::{DiagnosticArgValue, DiagnosticBuilder, Handler, IntoDiagnostic, IntoDiagnosticArg};
34
use rustc_ast as ast;
45
use rustc_ast_pretty::pprust;
6+
use rustc_error_messages::SubdiagnosticMessage;
57
use rustc_hir as hir;
6-
use rustc_lint_defs::Level;
8+
use rustc_lint_defs::{Applicability, Level};
79
use rustc_span::edition::Edition;
810
use rustc_span::symbol::{Ident, MacroRulesNormalizedIdent, Symbol};
911
use rustc_span::Span;
1012
use rustc_target::abi::TargetDataLayoutErrors;
1113
use rustc_target::spec::{PanicStrategy, SplitDebuginfo, StackProtector, TargetTriple};
1214
use rustc_type_ir as type_ir;
15+
use std::backtrace::Backtrace;
1316
use std::borrow::Cow;
1417
use std::fmt;
1518
use std::num::ParseIntError;
@@ -311,3 +314,73 @@ pub enum LabelKind {
311314
Label,
312315
Help,
313316
}
317+
318+
#[derive(Subdiagnostic)]
319+
#[label(errors_expected_lifetime_parameter)]
320+
pub struct ExpectedLifetimeParameter {
321+
#[primary_span]
322+
pub span: Span,
323+
pub count: usize,
324+
}
325+
326+
#[derive(Subdiagnostic)]
327+
#[note(errors_delayed_at_with_newline)]
328+
pub struct DelayedAtWithNewline {
329+
#[primary_span]
330+
pub span: Span,
331+
pub emitted_at: DiagnosticLocation,
332+
pub note: Backtrace,
333+
}
334+
#[derive(Subdiagnostic)]
335+
#[note(errors_delayed_at_without_newline)]
336+
pub struct DelayedAtWithoutNewline {
337+
#[primary_span]
338+
pub span: Span,
339+
pub emitted_at: DiagnosticLocation,
340+
pub note: Backtrace,
341+
}
342+
343+
impl IntoDiagnosticArg for DiagnosticLocation {
344+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
345+
DiagnosticArgValue::Str(Cow::from(self.to_string()))
346+
}
347+
}
348+
349+
impl IntoDiagnosticArg for Backtrace {
350+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
351+
DiagnosticArgValue::Str(Cow::from(self.to_string()))
352+
}
353+
}
354+
355+
#[derive(Subdiagnostic)]
356+
#[note(errors_invalid_flushed_delayed_diagnostic_level)]
357+
pub struct InvalidFlushedDelayedDiagnosticLevel {
358+
#[primary_span]
359+
pub span: Span,
360+
pub level: rustc_errors::Level,
361+
}
362+
impl IntoDiagnosticArg for rustc_errors::Level {
363+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
364+
DiagnosticArgValue::Str(Cow::from(self.to_string()))
365+
}
366+
}
367+
368+
pub struct IndicateAnonymousLifetime {
369+
pub span: Span,
370+
pub count: usize,
371+
pub suggestion: String,
372+
}
373+
374+
impl AddToDiagnostic for IndicateAnonymousLifetime {
375+
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
376+
where
377+
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
378+
{
379+
diag.span_suggestion_verbose(
380+
self.span,
381+
fluent::errors_indicate_anonymous_lifetime,
382+
self.suggestion,
383+
Applicability::MachineApplicable,
384+
);
385+
}
386+
}

compiler/rustc_errors/src/lib.rs

+28-14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ extern crate rustc_macros;
2222
#[macro_use]
2323
extern crate tracing;
2424

25+
extern crate self as rustc_errors;
26+
2527
pub use emitter::ColorConfig;
2628

2729
use rustc_lint_defs::LintExpectationId;
@@ -375,13 +377,16 @@ pub struct ExplicitBug;
375377
/// rather than a failed assertion, etc.
376378
pub struct DelayedBugPanic;
377379

380+
use crate::diagnostic_impls::{DelayedAtWithNewline, DelayedAtWithoutNewline};
378381
pub use diagnostic::{
379382
AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgValue, DiagnosticId,
380383
DiagnosticStyledString, IntoDiagnosticArg, SubDiagnostic,
381384
};
382385
pub use diagnostic_builder::{DiagnosticBuilder, EmissionGuarantee, Noted};
383386
pub use diagnostic_impls::{
384-
DiagnosticArgFromDisplay, DiagnosticSymbolList, LabelKind, SingleLabelManySpans,
387+
DiagnosticArgFromDisplay, DiagnosticSymbolList, ExpectedLifetimeParameter,
388+
IndicateAnonymousLifetime, InvalidFlushedDelayedDiagnosticLevel, LabelKind,
389+
SingleLabelManySpans,
385390
};
386391
use std::backtrace::{Backtrace, BacktraceStatus};
387392

@@ -1670,11 +1675,10 @@ impl HandlerInner {
16701675
if bug.level != Level::DelayedBug {
16711676
// NOTE(eddyb) not panicking here because we're already producing
16721677
// an ICE, and the more information the merrier.
1673-
bug.note(format!(
1674-
"`flushed_delayed` got diagnostic with level {:?}, \
1675-
instead of the expected `DelayedBug`",
1676-
bug.level,
1677-
));
1678+
bug.subdiagnostic(InvalidFlushedDelayedDiagnosticLevel {
1679+
span: bug.span.primary_span().unwrap(),
1680+
level: bug.level,
1681+
});
16781682
}
16791683
bug.level = Level::Bug;
16801684

@@ -1741,12 +1745,22 @@ impl DelayedDiagnostic {
17411745
fn decorate(mut self) -> Diagnostic {
17421746
match self.note.status() {
17431747
BacktraceStatus::Captured => {
1744-
self.inner.note(format!("delayed at {}\n{}", self.inner.emitted_at, self.note));
1748+
let inner = &self.inner;
1749+
self.inner.subdiagnostic(DelayedAtWithNewline {
1750+
span: inner.span.primary_span().unwrap(),
1751+
emitted_at: inner.emitted_at.clone(),
1752+
note: self.note,
1753+
});
17451754
}
17461755
// Avoid the needless newline when no backtrace has been captured,
17471756
// the display impl should just be a single line.
17481757
_ => {
1749-
self.inner.note(format!("delayed at {} - {}", self.inner.emitted_at, self.note));
1758+
let inner = &self.inner;
1759+
self.inner.subdiagnostic(DelayedAtWithoutNewline {
1760+
span: inner.span.primary_span().unwrap(),
1761+
emitted_at: inner.emitted_at.clone(),
1762+
note: self.note,
1763+
});
17501764
}
17511765
}
17521766

@@ -1838,20 +1852,20 @@ pub fn add_elided_lifetime_in_path_suggestion(
18381852
incl_angl_brckt: bool,
18391853
insertion_span: Span,
18401854
) {
1841-
diag.span_label(path_span, format!("expected lifetime parameter{}", pluralize!(n)));
1855+
diag.subdiagnostic(ExpectedLifetimeParameter { span: path_span, count: n });
18421856
if !source_map.is_span_accessible(insertion_span) {
18431857
// Do not try to suggest anything if generated by a proc-macro.
18441858
return;
18451859
}
18461860
let anon_lts = vec!["'_"; n].join(", ");
18471861
let suggestion =
18481862
if incl_angl_brckt { format!("<{}>", anon_lts) } else { format!("{}, ", anon_lts) };
1849-
diag.span_suggestion_verbose(
1850-
insertion_span.shrink_to_hi(),
1851-
format!("indicate the anonymous lifetime{}", pluralize!(n)),
1863+
1864+
diag.subdiagnostic(IndicateAnonymousLifetime {
1865+
span: insertion_span.shrink_to_hi(),
1866+
count: n,
18521867
suggestion,
1853-
Applicability::MachineApplicable,
1854-
);
1868+
});
18551869
}
18561870

18571871
#[derive(Clone, Copy, PartialEq, Hash, Debug)]

0 commit comments

Comments
 (0)