Skip to content

Commit 197fb1b

Browse files
Rollup merge of #154449 - nnethercote:rustc_errors-dep-rustc_abi, r=davidtwco
Invert dependency between `rustc_errors` and `rustc_abi`. Currently, `rustc_errors` depends on `rustc_abi`, which depends on `rustc_error_messages`. This is a bit odd. `rustc_errors` depends on `rustc_abi` for a single reason: `rustc_abi` defines a type `TargetDataLayoutErrors` and `rustc_errors` impls `Diagnostic` for that type. We can get a more natural relationship by inverting the dependency, moving the `Diagnostic` trait upstream. Then `rustc_abi` defines `TargetDataLayoutErrors` and also impls `Diagnostic` for it. `rustc_errors` is already pretty far upstream in the crate graph, it doesn't hurt to push it a little further because errors are a very low-level concept. r? @davidtwco
2 parents c729f33 + 0db4e3a commit 197fb1b

8 files changed

Lines changed: 79 additions & 83 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3505,6 +3505,7 @@ dependencies = [
35053505
"rand_xoshiro",
35063506
"rustc_data_structures",
35073507
"rustc_error_messages",
3508+
"rustc_errors",
35083509
"rustc_hashes",
35093510
"rustc_index",
35103511
"rustc_macros",
@@ -3909,7 +3910,6 @@ dependencies = [
39093910
"anstream",
39103911
"anstyle",
39113912
"derive_setters",
3912-
"rustc_abi",
39133913
"rustc_ast",
39143914
"rustc_data_structures",
39153915
"rustc_error_codes",

compiler/rustc_abi/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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 }
1212
rustc_error_messages = { path = "../rustc_error_messages", optional = true }
13+
rustc_errors = { path = "../rustc_errors", optional = true }
1314
rustc_hashes = { path = "../rustc_hashes" }
1415
rustc_index = { path = "../rustc_index", default-features = false }
1516
rustc_macros = { path = "../rustc_macros", optional = true }
@@ -21,11 +22,12 @@ tracing = "0.1"
2122
[features]
2223
# tidy-alphabetical-start
2324
default = ["nightly", "randomize"]
24-
# rust-analyzer depends on this crate and we therefore require it to built on a stable toolchain
25-
# without depending on rustc_data_structures, rustc_macros and rustc_serialize
25+
# rust-analyzer depends on this crate and we therefore require it to build on a stable toolchain
26+
# without depending on the rustc_* crates in the following list.
2627
nightly = [
2728
"dep:rustc_data_structures",
2829
"dep:rustc_error_messages",
30+
"dep:rustc_errors",
2931
"dep:rustc_macros",
3032
"dep:rustc_serialize",
3133
"dep:rustc_span",

compiler/rustc_abi/src/lib.rs

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ use std::str::FromStr;
4646
use bitflags::bitflags;
4747
#[cfg(feature = "nightly")]
4848
use rustc_data_structures::stable_hasher::StableOrd;
49+
#[cfg(feature = "nightly")]
50+
use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, msg};
4951
use rustc_hashes::Hash64;
5052
use rustc_index::{Idx, IndexSlice, IndexVec};
5153
#[cfg(feature = "nightly")]
@@ -332,7 +334,7 @@ impl Default for TargetDataLayout {
332334
}
333335
}
334336

335-
pub enum TargetDataLayoutErrors<'a> {
337+
pub enum TargetDataLayoutError<'a> {
336338
InvalidAddressSpace { addr_space: &'a str, cause: &'a str, err: ParseIntError },
337339
InvalidBits { kind: &'a str, bit: &'a str, cause: &'a str, err: ParseIntError },
338340
MissingAlignment { cause: &'a str },
@@ -343,6 +345,51 @@ pub enum TargetDataLayoutErrors<'a> {
343345
UnknownPointerSpecification { err: String },
344346
}
345347

348+
#[cfg(feature = "nightly")]
349+
impl<G: EmissionGuarantee> Diagnostic<'_, G> for TargetDataLayoutError<'_> {
350+
fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
351+
match self {
352+
TargetDataLayoutError::InvalidAddressSpace { addr_space, err, cause } => {
353+
Diag::new(dcx, level, msg!("invalid address space `{$addr_space}` for `{$cause}` in \"data-layout\": {$err}"))
354+
.with_arg("addr_space", addr_space)
355+
.with_arg("cause", cause)
356+
.with_arg("err", err)
357+
}
358+
TargetDataLayoutError::InvalidBits { kind, bit, cause, err } => {
359+
Diag::new(dcx, level, msg!("invalid {$kind} `{$bit}` for `{$cause}` in \"data-layout\": {$err}"))
360+
.with_arg("kind", kind)
361+
.with_arg("bit", bit)
362+
.with_arg("cause", cause)
363+
.with_arg("err", err)
364+
}
365+
TargetDataLayoutError::MissingAlignment { cause } => {
366+
Diag::new(dcx, level, msg!("missing alignment for `{$cause}` in \"data-layout\""))
367+
.with_arg("cause", cause)
368+
}
369+
TargetDataLayoutError::InvalidAlignment { cause, err } => {
370+
Diag::new(dcx, level, msg!("invalid alignment for `{$cause}` in \"data-layout\": {$err}"))
371+
.with_arg("cause", cause)
372+
.with_arg("err", err.to_string())
373+
}
374+
TargetDataLayoutError::InconsistentTargetArchitecture { dl, target } => {
375+
Diag::new(dcx, level, msg!("inconsistent target specification: \"data-layout\" claims architecture is {$dl}-endian, while \"target-endian\" is `{$target}`"))
376+
.with_arg("dl", dl).with_arg("target", target)
377+
}
378+
TargetDataLayoutError::InconsistentTargetPointerWidth { pointer_size, target } => {
379+
Diag::new(dcx, level, msg!("inconsistent target specification: \"data-layout\" claims pointers are {$pointer_size}-bit, while \"target-pointer-width\" is `{$target}`"))
380+
.with_arg("pointer_size", pointer_size).with_arg("target", target)
381+
}
382+
TargetDataLayoutError::InvalidBitsSize { err } => {
383+
Diag::new(dcx, level, msg!("{$err}")).with_arg("err", err)
384+
}
385+
TargetDataLayoutError::UnknownPointerSpecification { err } => {
386+
Diag::new(dcx, level, msg!("unknown pointer specification `{$err}` in datalayout string"))
387+
.with_arg("err", err)
388+
}
389+
}
390+
}
391+
}
392+
346393
impl TargetDataLayout {
347394
/// Parse data layout from an
348395
/// [llvm data layout string](https://llvm.org/docs/LangRef.html#data-layout)
@@ -352,17 +399,17 @@ impl TargetDataLayout {
352399
pub fn parse_from_llvm_datalayout_string<'a>(
353400
input: &'a str,
354401
default_address_space: AddressSpace,
355-
) -> Result<TargetDataLayout, TargetDataLayoutErrors<'a>> {
402+
) -> Result<TargetDataLayout, TargetDataLayoutError<'a>> {
356403
// Parse an address space index from a string.
357404
let parse_address_space = |s: &'a str, cause: &'a str| {
358405
s.parse::<u32>().map(AddressSpace).map_err(|err| {
359-
TargetDataLayoutErrors::InvalidAddressSpace { addr_space: s, cause, err }
406+
TargetDataLayoutError::InvalidAddressSpace { addr_space: s, cause, err }
360407
})
361408
};
362409

363410
// Parse a bit count from a string.
364411
let parse_bits = |s: &'a str, kind: &'a str, cause: &'a str| {
365-
s.parse::<u64>().map_err(|err| TargetDataLayoutErrors::InvalidBits {
412+
s.parse::<u64>().map_err(|err| TargetDataLayoutError::InvalidBits {
366413
kind,
367414
bit: s,
368415
cause,
@@ -378,7 +425,7 @@ impl TargetDataLayout {
378425
let parse_align_str = |s: &'a str, cause: &'a str| {
379426
let align_from_bits = |bits| {
380427
Align::from_bits(bits)
381-
.map_err(|err| TargetDataLayoutErrors::InvalidAlignment { cause, err })
428+
.map_err(|err| TargetDataLayoutError::InvalidAlignment { cause, err })
382429
};
383430
let abi = parse_bits(s, "alignment", cause)?;
384431
Ok(align_from_bits(abi)?)
@@ -388,7 +435,7 @@ impl TargetDataLayout {
388435
// ignoring the secondary alignment specifications.
389436
let parse_align_seq = |s: &[&'a str], cause: &'a str| {
390437
if s.is_empty() {
391-
return Err(TargetDataLayoutErrors::MissingAlignment { cause });
438+
return Err(TargetDataLayoutError::MissingAlignment { cause });
392439
}
393440
parse_align_str(s[0], cause)
394441
};
@@ -426,7 +473,7 @@ impl TargetDataLayout {
426473
// However, we currently don't take into account further specifications:
427474
// an error is emitted instead.
428475
if p.starts_with(char::is_alphabetic) {
429-
return Err(TargetDataLayoutErrors::UnknownPointerSpecification {
476+
return Err(TargetDataLayoutError::UnknownPointerSpecification {
430477
err: p.to_string(),
431478
});
432479
}
@@ -471,7 +518,7 @@ impl TargetDataLayout {
471518
// However, we currently don't take into account further specifications:
472519
// an error is emitted instead.
473520
if p.starts_with(char::is_alphabetic) {
474-
return Err(TargetDataLayoutErrors::UnknownPointerSpecification {
521+
return Err(TargetDataLayoutError::UnknownPointerSpecification {
475522
err: p.to_string(),
476523
});
477524
}

compiler/rustc_codegen_ssa/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ pub struct CompiledModules {
256256
pub allocator_module: Option<CompiledModule>,
257257
}
258258

259-
pub enum CodegenErrors {
259+
pub enum CodegenError {
260260
WrongFileType,
261261
EmptyVersionNumber,
262262
EncodingVersionMismatch { version_array: String, rlink_version: u32 },
@@ -317,32 +317,32 @@ impl CompiledModules {
317317
pub fn deserialize_rlink(
318318
sess: &Session,
319319
data: Vec<u8>,
320-
) -> Result<(Self, CrateInfo, EncodedMetadata, OutputFilenames), CodegenErrors> {
320+
) -> Result<(Self, CrateInfo, EncodedMetadata, OutputFilenames), CodegenError> {
321321
// The Decodable machinery is not used here because it panics if the input data is invalid
322322
// and because its internal representation may change.
323323
if !data.starts_with(RLINK_MAGIC) {
324-
return Err(CodegenErrors::WrongFileType);
324+
return Err(CodegenError::WrongFileType);
325325
}
326326
let data = &data[RLINK_MAGIC.len()..];
327327
if data.len() < 4 {
328-
return Err(CodegenErrors::EmptyVersionNumber);
328+
return Err(CodegenError::EmptyVersionNumber);
329329
}
330330

331331
let mut version_array: [u8; 4] = Default::default();
332332
version_array.copy_from_slice(&data[..4]);
333333
if u32::from_be_bytes(version_array) != RLINK_VERSION {
334-
return Err(CodegenErrors::EncodingVersionMismatch {
334+
return Err(CodegenError::EncodingVersionMismatch {
335335
version_array: String::from_utf8_lossy(&version_array).to_string(),
336336
rlink_version: RLINK_VERSION,
337337
});
338338
}
339339

340340
let Ok(mut decoder) = MemDecoder::new(&data[4..], 0) else {
341-
return Err(CodegenErrors::CorruptFile);
341+
return Err(CodegenError::CorruptFile);
342342
};
343343
let rustc_version = decoder.read_str();
344344
if rustc_version != sess.cfg_version {
345-
return Err(CodegenErrors::RustcVersionMismatch {
345+
return Err(CodegenError::RustcVersionMismatch {
346346
rustc_version: rustc_version.to_string(),
347347
});
348348
}

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use std::{env, str};
2828

2929
use rustc_ast as ast;
3030
use rustc_codegen_ssa::traits::CodegenBackend;
31-
use rustc_codegen_ssa::{CodegenErrors, CompiledModules};
31+
use rustc_codegen_ssa::{CodegenError, CompiledModules};
3232
use rustc_data_structures::profiling::{
3333
TimePassesFormat, get_resident_set_size, print_time_passes_entry,
3434
};
@@ -567,23 +567,21 @@ fn process_rlink(sess: &Session, compiler: &interface::Compiler) {
567567
}
568568
Err(err) => {
569569
match err {
570-
CodegenErrors::WrongFileType => dcx.emit_fatal(RLinkWrongFileType),
571-
CodegenErrors::EmptyVersionNumber => {
572-
dcx.emit_fatal(RLinkEmptyVersionNumber)
573-
}
574-
CodegenErrors::EncodingVersionMismatch { version_array, rlink_version } => {
570+
CodegenError::WrongFileType => dcx.emit_fatal(RLinkWrongFileType),
571+
CodegenError::EmptyVersionNumber => dcx.emit_fatal(RLinkEmptyVersionNumber),
572+
CodegenError::EncodingVersionMismatch { version_array, rlink_version } => {
575573
dcx.emit_fatal(RLinkEncodingVersionMismatch {
576574
version_array,
577575
rlink_version,
578576
})
579577
}
580-
CodegenErrors::RustcVersionMismatch { rustc_version } => {
578+
CodegenError::RustcVersionMismatch { rustc_version } => {
581579
dcx.emit_fatal(RLinkRustcVersionMismatch {
582580
rustc_version,
583581
current_version: sess.cfg_version,
584582
})
585583
}
586-
CodegenErrors::CorruptFile => {
584+
CodegenError::CorruptFile => {
587585
dcx.emit_fatal(RlinkCorruptFile { file });
588586
}
589587
};

compiler/rustc_errors/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ annotate-snippets = { version = "0.12.15", features = ["simd"] }
99
anstream = "0.6.20"
1010
anstyle = "1.0.13"
1111
derive_setters = "0.1.6"
12-
rustc_abi = { path = "../rustc_abi" }
1312
rustc_ast = { path = "../rustc_ast" }
1413
rustc_data_structures = { path = "../rustc_data_structures" }
1514
rustc_error_codes = { path = "../rustc_error_codes" }

compiler/rustc_errors/src/diagnostic_impls.rs

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use std::borrow::Cow;
22

3-
use rustc_abi::TargetDataLayoutErrors;
43
use rustc_error_messages::{DiagArgValue, IntoDiagArg};
54
use rustc_macros::Subdiagnostic;
65
use rustc_span::{Span, Symbol};
76

87
use crate::diagnostic::DiagLocation;
9-
use crate::{Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, Subdiagnostic, msg};
8+
use crate::{Diag, EmissionGuarantee, Subdiagnostic};
109

1110
impl IntoDiagArg for DiagLocation {
1211
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
@@ -37,55 +36,6 @@ impl<S: std::fmt::Display> IntoDiagArg for DiagSymbolList<S> {
3736
}
3837
}
3938

40-
impl<G: EmissionGuarantee> Diagnostic<'_, G> for TargetDataLayoutErrors<'_> {
41-
fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
42-
match self {
43-
TargetDataLayoutErrors::InvalidAddressSpace { addr_space, err, cause } => {
44-
Diag::new(dcx, level, msg!("invalid address space `{$addr_space}` for `{$cause}` in \"data-layout\": {$err}"))
45-
.with_arg("addr_space", addr_space)
46-
.with_arg("cause", cause)
47-
.with_arg("err", err)
48-
}
49-
TargetDataLayoutErrors::InvalidBits { kind, bit, cause, err } => {
50-
Diag::new(dcx, level, msg!("invalid {$kind} `{$bit}` for `{$cause}` in \"data-layout\": {$err}"))
51-
.with_arg("kind", kind)
52-
.with_arg("bit", bit)
53-
.with_arg("cause", cause)
54-
.with_arg("err", err)
55-
}
56-
TargetDataLayoutErrors::MissingAlignment { cause } => {
57-
Diag::new(dcx, level, msg!("missing alignment for `{$cause}` in \"data-layout\""))
58-
.with_arg("cause", cause)
59-
}
60-
TargetDataLayoutErrors::InvalidAlignment { cause, err } => {
61-
Diag::new(dcx, level, msg!(
62-
"invalid alignment for `{$cause}` in \"data-layout\": {$err}"
63-
))
64-
.with_arg("cause", cause)
65-
.with_arg("err", err.to_string())
66-
}
67-
TargetDataLayoutErrors::InconsistentTargetArchitecture { dl, target } => {
68-
Diag::new(dcx, level, msg!(
69-
"inconsistent target specification: \"data-layout\" claims architecture is {$dl}-endian, while \"target-endian\" is `{$target}`"
70-
))
71-
.with_arg("dl", dl).with_arg("target", target)
72-
}
73-
TargetDataLayoutErrors::InconsistentTargetPointerWidth { pointer_size, target } => {
74-
Diag::new(dcx, level, msg!(
75-
"inconsistent target specification: \"data-layout\" claims pointers are {$pointer_size}-bit, while \"target-pointer-width\" is `{$target}`"
76-
)).with_arg("pointer_size", pointer_size).with_arg("target", target)
77-
}
78-
TargetDataLayoutErrors::InvalidBitsSize { err } => {
79-
Diag::new(dcx, level, msg!("{$err}")).with_arg("err", err)
80-
}
81-
TargetDataLayoutErrors::UnknownPointerSpecification { err } => {
82-
Diag::new(dcx, level, msg!("unknown pointer specification `{$err}` in datalayout string"))
83-
.with_arg("err", err)
84-
}
85-
}
86-
}
87-
}
88-
8939
/// Utility struct used to apply a single label while highlighting multiple spans
9040
pub struct SingleLabelManySpans {
9141
pub spans: Vec<Span>,

compiler/rustc_target/src/spec/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use std::str::FromStr;
4747
use std::{fmt, io};
4848

4949
use rustc_abi::{
50-
Align, CanonAbi, Endian, ExternAbi, Integer, Size, TargetDataLayout, TargetDataLayoutErrors,
50+
Align, CanonAbi, Endian, ExternAbi, Integer, Size, TargetDataLayout, TargetDataLayoutError,
5151
};
5252
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
5353
use rustc_error_messages::{DiagArgValue, IntoDiagArg, into_diag_arg_using_display};
@@ -2166,15 +2166,15 @@ pub struct TargetMetadata {
21662166
}
21672167

21682168
impl Target {
2169-
pub fn parse_data_layout(&self) -> Result<TargetDataLayout, TargetDataLayoutErrors<'_>> {
2169+
pub fn parse_data_layout(&self) -> Result<TargetDataLayout, TargetDataLayoutError<'_>> {
21702170
let mut dl = TargetDataLayout::parse_from_llvm_datalayout_string(
21712171
&self.data_layout,
21722172
self.options.default_address_space,
21732173
)?;
21742174

21752175
// Perform consistency checks against the Target information.
21762176
if dl.endian != self.endian {
2177-
return Err(TargetDataLayoutErrors::InconsistentTargetArchitecture {
2177+
return Err(TargetDataLayoutError::InconsistentTargetArchitecture {
21782178
dl: dl.endian.as_str(),
21792179
target: self.endian.as_str(),
21802180
});
@@ -2183,7 +2183,7 @@ impl Target {
21832183
let target_pointer_width: u64 = self.pointer_width.into();
21842184
let dl_pointer_size: u64 = dl.pointer_size().bits();
21852185
if dl_pointer_size != target_pointer_width {
2186-
return Err(TargetDataLayoutErrors::InconsistentTargetPointerWidth {
2186+
return Err(TargetDataLayoutError::InconsistentTargetPointerWidth {
21872187
pointer_size: dl_pointer_size,
21882188
target: self.pointer_width,
21892189
});
@@ -2192,7 +2192,7 @@ impl Target {
21922192
dl.c_enum_min_size = Integer::from_size(Size::from_bits(
21932193
self.c_enum_min_bits.unwrap_or(self.c_int_width as _),
21942194
))
2195-
.map_err(|err| TargetDataLayoutErrors::InvalidBitsSize { err })?;
2195+
.map_err(|err| TargetDataLayoutError::InvalidBitsSize { err })?;
21962196

21972197
Ok(dl)
21982198
}

0 commit comments

Comments
 (0)