Skip to content

Commit f239bb6

Browse files
committed
Auto merge of #113281 - dayo05:master, r=davidtwco
Implement diagnostic translation for rustc-errors This is my first PR to rustc yeah~ I'm going to implement diagnostic translation on rustc-errors crate. This PR is WIP, the reason of opening this as draft, I want to show my code to prevent the issue caused by misunderstanding and also I have few questions. Some error messages are processed by `pluralize!` macro which determines to use plural word or not. From now, I make two kinds of keys and combine with enum but I'm not sure is this best method to do it. Is there any prefered method to do this? => This resolved on conversation on PR. I'll remain to perform force-push until my first implementation looks good to me
2 parents 2efa46d + 05c856a commit f239bb6

File tree

3 files changed

+111
-14
lines changed

3 files changed

+111
-14
lines changed

Diff for: 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

Diff for: compiler/rustc_errors/src/diagnostic_impls.rs

+61
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::diagnostic::DiagnosticLocation;
12
use crate::{fluent_generated as fluent, AddToDiagnostic};
23
use crate::{DiagnosticArgValue, DiagnosticBuilder, Handler, IntoDiagnostic, IntoDiagnosticArg};
34
use rustc_ast as ast;
@@ -10,6 +11,7 @@ use rustc_span::Span;
1011
use rustc_target::abi::TargetDataLayoutErrors;
1112
use rustc_target::spec::{PanicStrategy, SplitDebuginfo, StackProtector, TargetTriple};
1213
use rustc_type_ir as type_ir;
14+
use std::backtrace::Backtrace;
1315
use std::borrow::Cow;
1416
use std::fmt;
1517
use std::num::ParseIntError;
@@ -317,3 +319,62 @@ pub enum LabelKind {
317319
Label,
318320
Help,
319321
}
322+
323+
#[derive(Subdiagnostic)]
324+
#[label(errors_expected_lifetime_parameter)]
325+
pub struct ExpectedLifetimeParameter {
326+
#[primary_span]
327+
pub span: Span,
328+
pub count: usize,
329+
}
330+
331+
#[derive(Subdiagnostic)]
332+
#[note(errors_delayed_at_with_newline)]
333+
pub struct DelayedAtWithNewline {
334+
#[primary_span]
335+
pub span: Span,
336+
pub emitted_at: DiagnosticLocation,
337+
pub note: Backtrace,
338+
}
339+
#[derive(Subdiagnostic)]
340+
#[note(errors_delayed_at_without_newline)]
341+
pub struct DelayedAtWithoutNewline {
342+
#[primary_span]
343+
pub span: Span,
344+
pub emitted_at: DiagnosticLocation,
345+
pub note: Backtrace,
346+
}
347+
348+
impl IntoDiagnosticArg for DiagnosticLocation {
349+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
350+
DiagnosticArgValue::Str(Cow::from(self.to_string()))
351+
}
352+
}
353+
354+
impl IntoDiagnosticArg for Backtrace {
355+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
356+
DiagnosticArgValue::Str(Cow::from(self.to_string()))
357+
}
358+
}
359+
360+
#[derive(Subdiagnostic)]
361+
#[note(errors_invalid_flushed_delayed_diagnostic_level)]
362+
pub struct InvalidFlushedDelayedDiagnosticLevel {
363+
#[primary_span]
364+
pub span: Span,
365+
pub level: rustc_errors::Level,
366+
}
367+
impl IntoDiagnosticArg for rustc_errors::Level {
368+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
369+
DiagnosticArgValue::Str(Cow::from(self.to_string()))
370+
}
371+
}
372+
373+
#[derive(Subdiagnostic)]
374+
#[suggestion(errors_indicate_anonymous_lifetime, code = "{suggestion}", style = "verbose")]
375+
pub struct IndicateAnonymousLifetime {
376+
#[primary_span]
377+
pub span: Span,
378+
pub count: usize,
379+
pub suggestion: String,
380+
}

Diff for: 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;
@@ -377,13 +379,16 @@ pub struct ExplicitBug;
377379
/// rather than a failed assertion, etc.
378380
pub struct DelayedBugPanic;
379381

382+
use crate::diagnostic_impls::{DelayedAtWithNewline, DelayedAtWithoutNewline};
380383
pub use diagnostic::{
381384
AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgValue, DiagnosticId,
382385
DiagnosticStyledString, IntoDiagnosticArg, SubDiagnostic,
383386
};
384387
pub use diagnostic_builder::{DiagnosticBuilder, EmissionGuarantee, Noted};
385388
pub use diagnostic_impls::{
386-
DiagnosticArgFromDisplay, DiagnosticSymbolList, LabelKind, SingleLabelManySpans,
389+
DiagnosticArgFromDisplay, DiagnosticSymbolList, ExpectedLifetimeParameter,
390+
IndicateAnonymousLifetime, InvalidFlushedDelayedDiagnosticLevel, LabelKind,
391+
SingleLabelManySpans,
387392
};
388393
use std::backtrace::{Backtrace, BacktraceStatus};
389394

@@ -1673,11 +1678,10 @@ impl HandlerInner {
16731678
if bug.level != Level::DelayedBug {
16741679
// NOTE(eddyb) not panicking here because we're already producing
16751680
// an ICE, and the more information the merrier.
1676-
bug.note(format!(
1677-
"`flushed_delayed` got diagnostic with level {:?}, \
1678-
instead of the expected `DelayedBug`",
1679-
bug.level,
1680-
));
1681+
bug.subdiagnostic(InvalidFlushedDelayedDiagnosticLevel {
1682+
span: bug.span.primary_span().unwrap(),
1683+
level: bug.level,
1684+
});
16811685
}
16821686
bug.level = Level::Bug;
16831687

@@ -1744,12 +1748,22 @@ impl DelayedDiagnostic {
17441748
fn decorate(mut self) -> Diagnostic {
17451749
match self.note.status() {
17461750
BacktraceStatus::Captured => {
1747-
self.inner.note(format!("delayed at {}\n{}", self.inner.emitted_at, self.note));
1751+
let inner = &self.inner;
1752+
self.inner.subdiagnostic(DelayedAtWithNewline {
1753+
span: inner.span.primary_span().unwrap(),
1754+
emitted_at: inner.emitted_at.clone(),
1755+
note: self.note,
1756+
});
17481757
}
17491758
// Avoid the needless newline when no backtrace has been captured,
17501759
// the display impl should just be a single line.
17511760
_ => {
1752-
self.inner.note(format!("delayed at {} - {}", self.inner.emitted_at, self.note));
1761+
let inner = &self.inner;
1762+
self.inner.subdiagnostic(DelayedAtWithoutNewline {
1763+
span: inner.span.primary_span().unwrap(),
1764+
emitted_at: inner.emitted_at.clone(),
1765+
note: self.note,
1766+
});
17531767
}
17541768
}
17551769

@@ -1841,20 +1855,20 @@ pub fn add_elided_lifetime_in_path_suggestion(
18411855
incl_angl_brckt: bool,
18421856
insertion_span: Span,
18431857
) {
1844-
diag.span_label(path_span, format!("expected lifetime parameter{}", pluralize!(n)));
1858+
diag.subdiagnostic(ExpectedLifetimeParameter { span: path_span, count: n });
18451859
if !source_map.is_span_accessible(insertion_span) {
18461860
// Do not try to suggest anything if generated by a proc-macro.
18471861
return;
18481862
}
18491863
let anon_lts = vec!["'_"; n].join(", ");
18501864
let suggestion =
18511865
if incl_angl_brckt { format!("<{}>", anon_lts) } else { format!("{}, ", anon_lts) };
1852-
diag.span_suggestion_verbose(
1853-
insertion_span.shrink_to_hi(),
1854-
format!("indicate the anonymous lifetime{}", pluralize!(n)),
1866+
1867+
diag.subdiagnostic(IndicateAnonymousLifetime {
1868+
span: insertion_span.shrink_to_hi(),
1869+
count: n,
18551870
suggestion,
1856-
Applicability::MachineApplicable,
1857-
);
1871+
});
18581872
}
18591873

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

0 commit comments

Comments
 (0)