Skip to content

Commit 9b651a4

Browse files
authored
Unrolled build for rust-lang#131942
Rollup merge of rust-lang#131942 - workingjubilee:reduce-haruspicy, r=lukas-code,lnicola compiler: Adopt rust-analyzer impls for `LayoutCalculatorError` We're about to massively churn the internals of `rustc_abi`. To minimize the immediate and future impact on rust-analyzer, as a subtree that depends on this crate, grow some API on `LayoutCalculatorError` that reflects their uses of it. This way we can nest the type in theirs, and they can just call functions on it without having to inspect and flatten-out its innards.
2 parents 54791ef + e1d6164 commit 9b651a4

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

compiler/rustc_abi/src/layout.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ enum NicheBias {
3939
End,
4040
}
4141

42-
#[derive(Copy, Clone, Debug)]
42+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
4343
pub enum LayoutCalculatorError<F> {
4444
/// An unsized type was found in a location where a sized type was expected.
4545
///
@@ -56,6 +56,31 @@ pub enum LayoutCalculatorError<F> {
5656
EmptyUnion,
5757
}
5858

59+
impl<F> LayoutCalculatorError<F> {
60+
pub fn without_payload(&self) -> LayoutCalculatorError<()> {
61+
match self {
62+
LayoutCalculatorError::UnexpectedUnsized(_) => {
63+
LayoutCalculatorError::UnexpectedUnsized(())
64+
}
65+
LayoutCalculatorError::SizeOverflow => LayoutCalculatorError::SizeOverflow,
66+
LayoutCalculatorError::EmptyUnion => LayoutCalculatorError::EmptyUnion,
67+
}
68+
}
69+
70+
/// Format an untranslated diagnostic for this type
71+
///
72+
/// Intended for use by rust-analyzer, as neither it nor `rustc_abi` depend on fluent infra.
73+
pub fn fallback_fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74+
f.write_str(match self {
75+
LayoutCalculatorError::UnexpectedUnsized(_) => {
76+
"an unsized type was found where a sized type was expected"
77+
}
78+
LayoutCalculatorError::SizeOverflow => "size overflow",
79+
LayoutCalculatorError::EmptyUnion => "type is a union with no fields",
80+
})
81+
}
82+
}
83+
5984
type LayoutCalculatorResult<FieldIdx, VariantIdx, F> =
6085
Result<LayoutS<FieldIdx, VariantIdx>, LayoutCalculatorError<F>>;
6186

src/tools/rust-analyzer/crates/hir-ty/src/layout.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ pub type Variants = hir_def::layout::Variants<RustcFieldIdx, RustcEnumVariantIdx
7272

7373
#[derive(Debug, PartialEq, Eq, Clone)]
7474
pub enum LayoutError {
75+
// FIXME: Remove variants that duplicate LayoutCalculatorError's variants after sync
76+
BadCalc(LayoutCalculatorError<()>),
7577
EmptyUnion,
7678
HasErrorConst,
7779
HasErrorType,
@@ -90,6 +92,7 @@ impl std::error::Error for LayoutError {}
9092
impl fmt::Display for LayoutError {
9193
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9294
match self {
95+
LayoutError::BadCalc(err) => err.fallback_fmt(f),
9396
LayoutError::EmptyUnion => write!(f, "type is an union with no fields"),
9497
LayoutError::HasErrorConst => write!(f, "type contains an unevaluatable const"),
9598
LayoutError::HasErrorType => write!(f, "type contains an error"),
@@ -114,11 +117,7 @@ impl fmt::Display for LayoutError {
114117

115118
impl<F> From<LayoutCalculatorError<F>> for LayoutError {
116119
fn from(err: LayoutCalculatorError<F>) -> Self {
117-
match err {
118-
LayoutCalculatorError::EmptyUnion => LayoutError::EmptyUnion,
119-
LayoutCalculatorError::UnexpectedUnsized(_) => LayoutError::UnexpectedUnsized,
120-
LayoutCalculatorError::SizeOverflow => LayoutError::SizeOverflow,
121-
}
120+
LayoutError::BadCalc(err.without_payload())
122121
}
123122
}
124123

0 commit comments

Comments
 (0)