Skip to content

Commit fd4ae19

Browse files
authored
Rollup merge of rust-lang#145390 - joshtriplett:rustc-diag-value-earlier, r=lcnr
Shorten some dependency chains in the compiler (I recommend reviewing this commit by commit.) One of the long dependency chains in the compiler is: - Many things depend on `rustc_errors`. - `rustc_errors` depended on many things prior to this PR, including `rustc_target`, `rustc_type_ir`, `rustc_hir`, and `rustc_lint_defs`. - `rustc_lint_defs` depended on `rustc_hir` prior to this PR. - `rustc_hir` depends on `rustc_target`. - `rustc_target` is large and takes a while. This PR breaks that chain, through a few steps: - The `IntoDiagArgs` trait, from `rustc_errors`, moves earlier in the dependency chain. This allows `rustc_errors` to stop depending on a pile of crates just to implement `IntoDiagArgs` for their types. - Split `rustc_hir_id` out of `rustc_hir`, so crates that just need `HirId` and similar don't depend on all of `rust_hir` (and thus `rustc_target`). - Make `rustc_lint_defs` stop depending on `rustc_hir`.
2 parents 1f812db + 2ea7e7d commit fd4ae19

File tree

39 files changed

+508
-456
lines changed

39 files changed

+508
-456
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",
@@ -3779,6 +3780,8 @@ dependencies = [
37793780
"icu_locid",
37803781
"icu_provider_adapters",
37813782
"intl-memoizer",
3783+
"rustc_ast",
3784+
"rustc_ast_pretty",
37823785
"rustc_baked_icu_data",
37833786
"rustc_data_structures",
37843787
"rustc_macros",
@@ -3795,22 +3798,18 @@ dependencies = [
37953798
"annotate-snippets 0.11.5",
37963799
"derive_setters",
37973800
"rustc_abi",
3798-
"rustc_ast",
3799-
"rustc_ast_pretty",
38003801
"rustc_data_structures",
38013802
"rustc_error_codes",
38023803
"rustc_error_messages",
38033804
"rustc_fluent_macro",
38043805
"rustc_hashes",
3805-
"rustc_hir",
3806+
"rustc_hir_id",
38063807
"rustc_index",
38073808
"rustc_lexer",
38083809
"rustc_lint_defs",
38093810
"rustc_macros",
38103811
"rustc_serialize",
38113812
"rustc_span",
3812-
"rustc_target",
3813-
"rustc_type_ir",
38143813
"serde",
38153814
"serde_json",
38163815
"termcolor",
@@ -3898,7 +3897,9 @@ dependencies = [
38983897
"rustc_ast",
38993898
"rustc_ast_pretty",
39003899
"rustc_data_structures",
3900+
"rustc_error_messages",
39013901
"rustc_hashes",
3902+
"rustc_hir_id",
39023903
"rustc_index",
39033904
"rustc_macros",
39043905
"rustc_serialize",
@@ -3936,6 +3937,17 @@ dependencies = [
39363937
"tracing",
39373938
]
39383939

3940+
[[package]]
3941+
name = "rustc_hir_id"
3942+
version = "0.0.0"
3943+
dependencies = [
3944+
"rustc_data_structures",
3945+
"rustc_index",
3946+
"rustc_macros",
3947+
"rustc_serialize",
3948+
"rustc_span",
3949+
]
3950+
39393951
[[package]]
39403952
name = "rustc_hir_pretty"
39413953
version = "0.0.0"
@@ -4127,7 +4139,7 @@ dependencies = [
41274139
"rustc_ast",
41284140
"rustc_data_structures",
41294141
"rustc_error_messages",
4130-
"rustc_hir",
4142+
"rustc_hir_id",
41314143
"rustc_macros",
41324144
"rustc_serialize",
41334145
"rustc_span",
@@ -4268,7 +4280,6 @@ dependencies = [
42684280
"rustc_errors",
42694281
"rustc_fluent_macro",
42704282
"rustc_graphviz",
4271-
"rustc_hir",
42724283
"rustc_index",
42734284
"rustc_macros",
42744285
"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)