Skip to content

Commit 6c20cf9

Browse files
Auto merge of #145390 - joshtriplett:rustc-diag-value-earlier, r=<try>
Shorten some dependency chains in the compiler
2 parents 2c1ac85 + 35d6871 commit 6c20cf9

File tree

37 files changed

+507
-464
lines changed

37 files changed

+507
-464
lines changed

Cargo.lock

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3367,6 +3367,7 @@ dependencies = [
33673367
"rand 0.9.2",
33683368
"rand_xoshiro",
33693369
"rustc_data_structures",
3370+
"rustc_error_messages",
33703371
"rustc_hashes",
33713372
"rustc_index",
33723373
"rustc_macros",
@@ -3777,6 +3778,8 @@ dependencies = [
37773778
"icu_locid",
37783779
"icu_provider_adapters",
37793780
"intl-memoizer",
3781+
"rustc_ast",
3782+
"rustc_ast_pretty",
37803783
"rustc_baked_icu_data",
37813784
"rustc_data_structures",
37823785
"rustc_macros",
@@ -3793,22 +3796,18 @@ dependencies = [
37933796
"annotate-snippets 0.11.5",
37943797
"derive_setters",
37953798
"rustc_abi",
3796-
"rustc_ast",
3797-
"rustc_ast_pretty",
37983799
"rustc_data_structures",
37993800
"rustc_error_codes",
38003801
"rustc_error_messages",
38013802
"rustc_fluent_macro",
38023803
"rustc_hashes",
3803-
"rustc_hir",
3804+
"rustc_hir_id",
38043805
"rustc_index",
38053806
"rustc_lexer",
38063807
"rustc_lint_defs",
38073808
"rustc_macros",
38083809
"rustc_serialize",
38093810
"rustc_span",
3810-
"rustc_target",
3811-
"rustc_type_ir",
38123811
"serde",
38133812
"serde_json",
38143813
"termcolor",
@@ -3896,7 +3895,9 @@ dependencies = [
38963895
"rustc_ast",
38973896
"rustc_ast_pretty",
38983897
"rustc_data_structures",
3898+
"rustc_error_messages",
38993899
"rustc_hashes",
3900+
"rustc_hir_id",
39003901
"rustc_index",
39013902
"rustc_macros",
39023903
"rustc_serialize",
@@ -3934,6 +3935,17 @@ dependencies = [
39343935
"tracing",
39353936
]
39363937

3938+
[[package]]
3939+
name = "rustc_hir_id"
3940+
version = "0.0.0"
3941+
dependencies = [
3942+
"rustc_data_structures",
3943+
"rustc_index",
3944+
"rustc_macros",
3945+
"rustc_serialize",
3946+
"rustc_span",
3947+
]
3948+
39373949
[[package]]
39383950
name = "rustc_hir_pretty"
39393951
version = "0.0.0"
@@ -4125,7 +4137,7 @@ dependencies = [
41254137
"rustc_ast",
41264138
"rustc_data_structures",
41274139
"rustc_error_messages",
4128-
"rustc_hir",
4140+
"rustc_hir_id",
41294141
"rustc_macros",
41304142
"rustc_serialize",
41314143
"rustc_span",
@@ -4266,7 +4278,6 @@ dependencies = [
42664278
"rustc_errors",
42674279
"rustc_fluent_macro",
42684280
"rustc_graphviz",
4269-
"rustc_hir",
42704281
"rustc_index",
42714282
"rustc_macros",
42724283
"rustc_middle",
@@ -4647,6 +4658,7 @@ dependencies = [
46474658
"object 0.37.2",
46484659
"rustc_abi",
46494660
"rustc_data_structures",
4661+
"rustc_error_messages",
46504662
"rustc_fs_util",
46514663
"rustc_macros",
46524664
"rustc_serialize",
@@ -4710,7 +4722,6 @@ name = "rustc_traits"
47104722
version = "0.0.0"
47114723
dependencies = [
47124724
"rustc_data_structures",
4713-
"rustc_hir",
47144725
"rustc_infer",
47154726
"rustc_middle",
47164727
"rustc_span",
@@ -4765,6 +4776,7 @@ dependencies = [
47654776
"rustc-hash 2.1.1",
47664777
"rustc_ast_ir",
47674778
"rustc_data_structures",
4779+
"rustc_error_messages",
47684780
"rustc_index",
47694781
"rustc_macros",
47704782
"rustc_serialize",

compiler/rustc_abi/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ bitflags = "2.4.1"
99
rand = { version = "0.9.0", default-features = false, optional = true }
1010
rand_xoshiro = { version = "0.7.0", optional = true }
1111
rustc_data_structures = { path = "../rustc_data_structures", optional = true }
12+
rustc_error_messages = { path = "../rustc_error_messages", optional = true }
1213
rustc_hashes = { path = "../rustc_hashes" }
1314
rustc_index = { path = "../rustc_index", default-features = false }
1415
rustc_macros = { path = "../rustc_macros", optional = true }
@@ -24,6 +25,7 @@ default = ["nightly", "randomize"]
2425
# without depending on rustc_data_structures, rustc_macros and rustc_serialize
2526
nightly = [
2627
"dep:rustc_data_structures",
28+
"dep:rustc_error_messages",
2729
"dep:rustc_macros",
2830
"dep:rustc_serialize",
2931
"dep:rustc_span",

compiler/rustc_abi/src/extern_abi.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ impl StableOrd for ExternAbi {
223223
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
224224
}
225225

226+
#[cfg(feature = "nightly")]
227+
rustc_error_messages::into_diag_arg_using_display!(ExternAbi);
228+
226229
impl ExternAbi {
227230
/// An ABI "like Rust"
228231
///

compiler/rustc_error_messages/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ icu_list = "1.2"
1111
icu_locid = "1.2"
1212
icu_provider_adapters = "1.2"
1313
intl-memoizer = "0.5.1"
14+
rustc_ast = { path = "../rustc_ast" }
15+
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1416
rustc_baked_icu_data = { path = "../rustc_baked_icu_data" }
1517
rustc_data_structures = { path = "../rustc_data_structures" }
1618
rustc_macros = { path = "../rustc_macros" }
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
use std::backtrace::Backtrace;
2+
use std::borrow::Cow;
3+
use std::fmt;
4+
use std::num::ParseIntError;
5+
use std::path::{Path, PathBuf};
6+
use std::process::ExitStatus;
7+
8+
use rustc_ast as ast;
9+
use rustc_ast_pretty::pprust;
10+
use rustc_span::edition::Edition;
11+
12+
use crate::{DiagArgValue, IntoDiagArg};
13+
14+
pub struct DiagArgFromDisplay<'a>(pub &'a dyn fmt::Display);
15+
16+
impl IntoDiagArg for DiagArgFromDisplay<'_> {
17+
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue {
18+
self.0.to_string().into_diag_arg(path)
19+
}
20+
}
21+
22+
impl<'a> From<&'a dyn fmt::Display> for DiagArgFromDisplay<'a> {
23+
fn from(t: &'a dyn fmt::Display) -> Self {
24+
DiagArgFromDisplay(t)
25+
}
26+
}
27+
28+
impl<'a, T: fmt::Display> From<&'a T> for DiagArgFromDisplay<'a> {
29+
fn from(t: &'a T) -> Self {
30+
DiagArgFromDisplay(t)
31+
}
32+
}
33+
34+
impl<'a, T: Clone + IntoDiagArg> IntoDiagArg for &'a T {
35+
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue {
36+
self.clone().into_diag_arg(path)
37+
}
38+
}
39+
40+
#[macro_export]
41+
macro_rules! into_diag_arg_using_display {
42+
($( $ty:ty ),+ $(,)?) => {
43+
$(
44+
impl $crate::IntoDiagArg for $ty {
45+
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> $crate::DiagArgValue {
46+
self.to_string().into_diag_arg(path)
47+
}
48+
}
49+
)+
50+
}
51+
}
52+
53+
macro_rules! into_diag_arg_for_number {
54+
($( $ty:ty ),+ $(,)?) => {
55+
$(
56+
impl $crate::IntoDiagArg for $ty {
57+
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> $crate::DiagArgValue {
58+
// Convert to a string if it won't fit into `Number`.
59+
#[allow(irrefutable_let_patterns)]
60+
if let Ok(n) = TryInto::<i32>::try_into(self) {
61+
$crate::DiagArgValue::Number(n)
62+
} else {
63+
self.to_string().into_diag_arg(path)
64+
}
65+
}
66+
}
67+
)+
68+
}
69+
}
70+
71+
into_diag_arg_using_display!(
72+
ast::ParamKindOrd,
73+
std::io::Error,
74+
Box<dyn std::error::Error>,
75+
std::num::NonZero<u32>,
76+
Edition,
77+
rustc_span::Ident,
78+
rustc_span::MacroRulesNormalizedIdent,
79+
ParseIntError,
80+
ExitStatus,
81+
);
82+
83+
into_diag_arg_for_number!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize);
84+
85+
impl IntoDiagArg for bool {
86+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
87+
if self {
88+
DiagArgValue::Str(Cow::Borrowed("true"))
89+
} else {
90+
DiagArgValue::Str(Cow::Borrowed("false"))
91+
}
92+
}
93+
}
94+
95+
impl IntoDiagArg for char {
96+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
97+
DiagArgValue::Str(Cow::Owned(format!("{self:?}")))
98+
}
99+
}
100+
101+
impl IntoDiagArg for Vec<char> {
102+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
103+
DiagArgValue::StrListSepByAnd(
104+
self.into_iter().map(|c| Cow::Owned(format!("{c:?}"))).collect(),
105+
)
106+
}
107+
}
108+
109+
impl IntoDiagArg for rustc_span::Symbol {
110+
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue {
111+
self.to_ident_string().into_diag_arg(path)
112+
}
113+
}
114+
115+
impl<'a> IntoDiagArg for &'a str {
116+
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue {
117+
self.to_string().into_diag_arg(path)
118+
}
119+
}
120+
121+
impl IntoDiagArg for String {
122+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
123+
DiagArgValue::Str(Cow::Owned(self))
124+
}
125+
}
126+
127+
impl<'a> IntoDiagArg for Cow<'a, str> {
128+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
129+
DiagArgValue::Str(Cow::Owned(self.into_owned()))
130+
}
131+
}
132+
133+
impl<'a> IntoDiagArg for &'a Path {
134+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
135+
DiagArgValue::Str(Cow::Owned(self.display().to_string()))
136+
}
137+
}
138+
139+
impl IntoDiagArg for PathBuf {
140+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
141+
DiagArgValue::Str(Cow::Owned(self.display().to_string()))
142+
}
143+
}
144+
145+
impl IntoDiagArg for ast::Expr {
146+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
147+
DiagArgValue::Str(Cow::Owned(pprust::expr_to_string(&self)))
148+
}
149+
}
150+
151+
impl IntoDiagArg for ast::Path {
152+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
153+
DiagArgValue::Str(Cow::Owned(pprust::path_to_string(&self)))
154+
}
155+
}
156+
157+
impl IntoDiagArg for ast::token::Token {
158+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
159+
DiagArgValue::Str(pprust::token_to_string(&self))
160+
}
161+
}
162+
163+
impl IntoDiagArg for ast::token::TokenKind {
164+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
165+
DiagArgValue::Str(pprust::token_kind_to_string(&self))
166+
}
167+
}
168+
169+
impl IntoDiagArg for std::ffi::CString {
170+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
171+
DiagArgValue::Str(Cow::Owned(self.to_string_lossy().into_owned()))
172+
}
173+
}
174+
175+
impl IntoDiagArg for rustc_data_structures::small_c_str::SmallCStr {
176+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
177+
DiagArgValue::Str(Cow::Owned(self.to_string_lossy().into_owned()))
178+
}
179+
}
180+
181+
impl IntoDiagArg for ast::Visibility {
182+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
183+
let s = pprust::vis_to_string(&self);
184+
let s = s.trim_end().to_string();
185+
DiagArgValue::Str(Cow::Owned(s))
186+
}
187+
}
188+
189+
impl IntoDiagArg for Backtrace {
190+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
191+
DiagArgValue::Str(Cow::from(self.to_string()))
192+
}
193+
}
194+
195+
impl IntoDiagArg for ast::util::parser::ExprPrecedence {
196+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
197+
DiagArgValue::Number(self as i32)
198+
}
199+
}
200+
201+
impl IntoDiagArg for ast::FloatTy {
202+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
203+
DiagArgValue::Str(Cow::Borrowed(self.name_str()))
204+
}
205+
}

0 commit comments

Comments
 (0)