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

@ -1,3 +1,25 @@
errors_delayed_at_with_newline =
delayed at {$emitted_at}
{$note}
errors_delayed_at_without_newline =
delayed at {$emitted_at} - {$note}
errors_expected_lifetime_parameter =
expected lifetime {$count ->
[1] parameter
*[other] parameters
}
errors_indicate_anonymous_lifetime =
indicate the anonymous {$count ->
[1] lifetime
*[other] lifetimes
}
errors_invalid_flushed_delayed_diagnostic_level =
`flushed_delayed` got diagnostic with level {$level}, instead of the expected `DelayedBug`
errors_target_inconsistent_architecture = errors_target_inconsistent_architecture =
inconsistent target specification: "data-layout" claims architecture is {$dl}-endian, while "target-endian" is `{$target}` inconsistent target specification: "data-layout" claims architecture is {$dl}-endian, while "target-endian" is `{$target}`

View File

@ -1,15 +1,18 @@
use crate::{fluent_generated as fluent, AddToDiagnostic}; use crate::diagnostic::DiagnosticLocation;
use crate::{fluent_generated as fluent, AddToDiagnostic, Diagnostic};
use crate::{DiagnosticArgValue, DiagnosticBuilder, Handler, IntoDiagnostic, IntoDiagnosticArg}; use crate::{DiagnosticArgValue, DiagnosticBuilder, Handler, IntoDiagnostic, IntoDiagnosticArg};
use rustc_ast as ast; use rustc_ast as ast;
use rustc_ast_pretty::pprust; use rustc_ast_pretty::pprust;
use rustc_error_messages::SubdiagnosticMessage;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_lint_defs::Level; use rustc_lint_defs::{Applicability, Level};
use rustc_span::edition::Edition; use rustc_span::edition::Edition;
use rustc_span::symbol::{Ident, MacroRulesNormalizedIdent, Symbol}; use rustc_span::symbol::{Ident, MacroRulesNormalizedIdent, Symbol};
use rustc_span::Span; use rustc_span::Span;
use rustc_target::abi::TargetDataLayoutErrors; use rustc_target::abi::TargetDataLayoutErrors;
use rustc_target::spec::{PanicStrategy, SplitDebuginfo, StackProtector, TargetTriple}; use rustc_target::spec::{PanicStrategy, SplitDebuginfo, StackProtector, TargetTriple};
use rustc_type_ir as type_ir; use rustc_type_ir as type_ir;
use std::backtrace::Backtrace;
use std::borrow::Cow; use std::borrow::Cow;
use std::fmt; use std::fmt;
use std::num::ParseIntError; use std::num::ParseIntError;
@ -311,3 +314,73 @@ pub enum LabelKind {
Label, Label,
Help, Help,
} }
#[derive(Subdiagnostic)]
#[label(errors_expected_lifetime_parameter)]
pub struct ExpectedLifetimeParameter {
#[primary_span]
pub span: Span,
pub count: usize,
}
#[derive(Subdiagnostic)]
#[note(errors_delayed_at_with_newline)]
pub struct DelayedAtWithNewline {
#[primary_span]
pub span: Span,
pub emitted_at: DiagnosticLocation,
pub note: Backtrace,
}
#[derive(Subdiagnostic)]
#[note(errors_delayed_at_without_newline)]
pub struct DelayedAtWithoutNewline {
#[primary_span]
pub span: Span,
pub emitted_at: DiagnosticLocation,
pub note: Backtrace,
}
impl IntoDiagnosticArg for DiagnosticLocation {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::from(self.to_string()))
}
}
impl IntoDiagnosticArg for Backtrace {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::from(self.to_string()))
}
}
#[derive(Subdiagnostic)]
#[note(errors_invalid_flushed_delayed_diagnostic_level)]
pub struct InvalidFlushedDelayedDiagnosticLevel {
#[primary_span]
pub span: Span,
pub level: rustc_errors::Level,
}
impl IntoDiagnosticArg for rustc_errors::Level {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::from(self.to_string()))
}
}
pub struct IndicateAnonymousLifetime {
pub span: Span,
pub count: usize,
pub suggestion: String,
}
impl AddToDiagnostic for IndicateAnonymousLifetime {
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
where
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
{
diag.span_suggestion_verbose(
self.span,
fluent::errors_indicate_anonymous_lifetime,
self.suggestion,
Applicability::MachineApplicable,
);
}
}

View File

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