2022-10-12 16:55:28 -04:00
|
|
|
use crate::{
|
|
|
|
fluent, DiagnosticArgValue, DiagnosticBuilder, Handler, IntoDiagnostic, IntoDiagnosticArg,
|
|
|
|
};
|
|
|
|
use rustc_ast as ast;
|
|
|
|
use rustc_ast_pretty::pprust;
|
|
|
|
use rustc_hir as hir;
|
2022-10-14 13:25:12 +01:00
|
|
|
use rustc_lint_defs::Level;
|
2022-10-12 16:55:28 -04:00
|
|
|
use rustc_span::edition::Edition;
|
|
|
|
use rustc_span::symbol::{Ident, MacroRulesNormalizedIdent, Symbol};
|
2022-10-14 13:25:12 +01:00
|
|
|
use rustc_target::abi::TargetDataLayoutErrors;
|
|
|
|
use rustc_target::spec::{PanicStrategy, SplitDebuginfo, StackProtector, TargetTriple};
|
2022-11-17 08:53:14 -05:00
|
|
|
use rustc_type_ir as type_ir;
|
2022-10-12 16:55:28 -04:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::fmt;
|
|
|
|
use std::num::ParseIntError;
|
|
|
|
use std::path::{Path, PathBuf};
|
2022-10-30 15:38:37 -04:00
|
|
|
use std::process::ExitStatus;
|
2022-10-12 16:55:28 -04:00
|
|
|
|
|
|
|
pub struct DiagnosticArgFromDisplay<'a>(pub &'a dyn fmt::Display);
|
|
|
|
|
|
|
|
impl IntoDiagnosticArg for DiagnosticArgFromDisplay<'_> {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
self.0.to_string().into_diagnostic_arg()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a dyn fmt::Display> for DiagnosticArgFromDisplay<'a> {
|
|
|
|
fn from(t: &'a dyn fmt::Display) -> Self {
|
|
|
|
DiagnosticArgFromDisplay(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: fmt::Display> From<&'a T> for DiagnosticArgFromDisplay<'a> {
|
|
|
|
fn from(t: &'a T) -> Self {
|
|
|
|
DiagnosticArgFromDisplay(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! into_diagnostic_arg_using_display {
|
|
|
|
($( $ty:ty ),+ $(,)?) => {
|
|
|
|
$(
|
|
|
|
impl IntoDiagnosticArg for $ty {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
self.to_string().into_diagnostic_arg()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)+
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
into_diagnostic_arg_using_display!(
|
|
|
|
i8,
|
|
|
|
u8,
|
|
|
|
i16,
|
|
|
|
u16,
|
|
|
|
i32,
|
|
|
|
u32,
|
|
|
|
i64,
|
|
|
|
u64,
|
|
|
|
i128,
|
|
|
|
u128,
|
|
|
|
std::io::Error,
|
2022-08-09 02:14:43 +02:00
|
|
|
Box<dyn std::error::Error>,
|
2022-10-12 16:55:28 -04:00
|
|
|
std::num::NonZeroU32,
|
|
|
|
hir::Target,
|
|
|
|
Edition,
|
|
|
|
Ident,
|
|
|
|
MacroRulesNormalizedIdent,
|
|
|
|
ParseIntError,
|
|
|
|
StackProtector,
|
|
|
|
&TargetTriple,
|
2022-10-30 15:38:37 -04:00
|
|
|
SplitDebuginfo,
|
|
|
|
ExitStatus,
|
2022-10-12 16:55:28 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
impl IntoDiagnosticArg for bool {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
if self {
|
|
|
|
DiagnosticArgValue::Str(Cow::Borrowed("true"))
|
|
|
|
} else {
|
|
|
|
DiagnosticArgValue::Str(Cow::Borrowed("false"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoDiagnosticArg for char {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
DiagnosticArgValue::Str(Cow::Owned(format!("{:?}", self)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoDiagnosticArg for Symbol {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
self.to_ident_string().into_diagnostic_arg()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IntoDiagnosticArg for &'a str {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
self.to_string().into_diagnostic_arg()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoDiagnosticArg for String {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
DiagnosticArgValue::Str(Cow::Owned(self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 19:01:22 +02:00
|
|
|
impl<'a> IntoDiagnosticArg for Cow<'a, str> {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
DiagnosticArgValue::Str(Cow::Owned(self.into_owned()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 16:55:28 -04:00
|
|
|
impl<'a> IntoDiagnosticArg for &'a Path {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
DiagnosticArgValue::Str(Cow::Owned(self.display().to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoDiagnosticArg for PathBuf {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
DiagnosticArgValue::Str(Cow::Owned(self.display().to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoDiagnosticArg for usize {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
DiagnosticArgValue::Number(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoDiagnosticArg for PanicStrategy {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
DiagnosticArgValue::Str(Cow::Owned(self.desc().to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoDiagnosticArg for hir::ConstContext {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
DiagnosticArgValue::Str(Cow::Borrowed(match self {
|
|
|
|
hir::ConstContext::ConstFn => "constant function",
|
|
|
|
hir::ConstContext::Static(_) => "static",
|
|
|
|
hir::ConstContext::Const => "constant",
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoDiagnosticArg for ast::Path {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
DiagnosticArgValue::Str(Cow::Owned(pprust::path_to_string(&self)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-15 14:24:33 +01:00
|
|
|
impl IntoDiagnosticArg for &ast::Path {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
DiagnosticArgValue::Str(Cow::Owned(pprust::path_to_string(self)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 16:55:28 -04:00
|
|
|
impl IntoDiagnosticArg for ast::token::Token {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
DiagnosticArgValue::Str(pprust::token_to_string(&self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoDiagnosticArg for ast::token::TokenKind {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
DiagnosticArgValue::Str(pprust::token_kind_to_string(&self))
|
|
|
|
}
|
|
|
|
}
|
2022-10-04 20:56:05 -04:00
|
|
|
|
2022-11-17 08:53:14 -05:00
|
|
|
impl IntoDiagnosticArg for type_ir::FloatTy {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
DiagnosticArgValue::Str(Cow::Borrowed(self.name_str()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-14 13:25:12 +01:00
|
|
|
impl IntoDiagnosticArg for Level {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
2023-01-10 10:56:17 +03:00
|
|
|
DiagnosticArgValue::Str(Cow::Borrowed(self.to_cmd_flag()))
|
2022-10-14 13:25:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-22 18:48:20 +08:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct DiagnosticSymbolList(Vec<Symbol>);
|
|
|
|
|
|
|
|
impl From<Vec<Symbol>> for DiagnosticSymbolList {
|
|
|
|
fn from(v: Vec<Symbol>) -> Self {
|
|
|
|
DiagnosticSymbolList(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoDiagnosticArg for DiagnosticSymbolList {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
2022-11-06 14:43:25 +08:00
|
|
|
DiagnosticArgValue::StrListSepByAnd(
|
|
|
|
self.0.into_iter().map(|sym| Cow::Owned(format!("`{sym}`"))).collect(),
|
|
|
|
)
|
2022-10-22 18:48:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 10:34:38 +05:30
|
|
|
impl<Id> IntoDiagnosticArg for hir::def::Res<Id> {
|
|
|
|
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
|
|
|
|
DiagnosticArgValue::Str(Cow::Borrowed(self.descr()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 20:56:05 -04:00
|
|
|
impl IntoDiagnostic<'_, !> for TargetDataLayoutErrors<'_> {
|
|
|
|
fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder<'_, !> {
|
|
|
|
let mut diag;
|
|
|
|
match self {
|
|
|
|
TargetDataLayoutErrors::InvalidAddressSpace { addr_space, err, cause } => {
|
2022-10-22 11:07:54 +02:00
|
|
|
diag = handler.struct_fatal(fluent::errors_target_invalid_address_space);
|
2022-10-04 20:56:05 -04:00
|
|
|
diag.set_arg("addr_space", addr_space);
|
|
|
|
diag.set_arg("cause", cause);
|
|
|
|
diag.set_arg("err", err);
|
|
|
|
diag
|
|
|
|
}
|
|
|
|
TargetDataLayoutErrors::InvalidBits { kind, bit, cause, err } => {
|
2022-10-22 11:07:54 +02:00
|
|
|
diag = handler.struct_fatal(fluent::errors_target_invalid_bits);
|
2022-10-04 20:56:05 -04:00
|
|
|
diag.set_arg("kind", kind);
|
|
|
|
diag.set_arg("bit", bit);
|
|
|
|
diag.set_arg("cause", cause);
|
|
|
|
diag.set_arg("err", err);
|
|
|
|
diag
|
|
|
|
}
|
|
|
|
TargetDataLayoutErrors::MissingAlignment { cause } => {
|
2022-10-22 11:07:54 +02:00
|
|
|
diag = handler.struct_fatal(fluent::errors_target_missing_alignment);
|
2022-10-04 20:56:05 -04:00
|
|
|
diag.set_arg("cause", cause);
|
|
|
|
diag
|
|
|
|
}
|
|
|
|
TargetDataLayoutErrors::InvalidAlignment { cause, err } => {
|
2022-10-22 11:07:54 +02:00
|
|
|
diag = handler.struct_fatal(fluent::errors_target_invalid_alignment);
|
2022-10-04 20:56:05 -04:00
|
|
|
diag.set_arg("cause", cause);
|
|
|
|
diag.set_arg("err", err);
|
|
|
|
diag
|
|
|
|
}
|
|
|
|
TargetDataLayoutErrors::InconsistentTargetArchitecture { dl, target } => {
|
2022-10-22 11:07:54 +02:00
|
|
|
diag = handler.struct_fatal(fluent::errors_target_inconsistent_architecture);
|
2022-10-04 20:56:05 -04:00
|
|
|
diag.set_arg("dl", dl);
|
|
|
|
diag.set_arg("target", target);
|
|
|
|
diag
|
|
|
|
}
|
|
|
|
TargetDataLayoutErrors::InconsistentTargetPointerWidth { pointer_size, target } => {
|
2022-10-22 11:07:54 +02:00
|
|
|
diag = handler.struct_fatal(fluent::errors_target_inconsistent_pointer_width);
|
2022-10-04 20:56:05 -04:00
|
|
|
diag.set_arg("pointer_size", pointer_size);
|
|
|
|
diag.set_arg("target", target);
|
|
|
|
diag
|
|
|
|
}
|
|
|
|
TargetDataLayoutErrors::InvalidBitsSize { err } => {
|
2022-10-22 11:07:54 +02:00
|
|
|
diag = handler.struct_fatal(fluent::errors_target_invalid_bits_size);
|
2022-10-04 20:56:05 -04:00
|
|
|
diag.set_arg("err", err);
|
|
|
|
diag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|