diff options
| author | Stuart Cook <Zalathar@users.noreply.github.com> | 2024-10-20 14:06:04 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-20 14:06:04 +1100 |
| commit | 63fccf0c2fb5b155ed72a98a106bdbcf60144cfb (patch) | |
| tree | 4b34953765419f5a7222075a346a228be317ae51 | |
| parent | 28a5ef91756a6d1c3aa59751ef6d2a63291925ec (diff) | |
| parent | e1d6164607d63df552ce2adf900c2c4cbf7f5ce3 (diff) | |
| download | rust-63fccf0c2fb5b155ed72a98a106bdbcf60144cfb.tar.gz rust-63fccf0c2fb5b155ed72a98a106bdbcf60144cfb.zip | |
Rollup merge of #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.
| -rw-r--r-- | compiler/rustc_abi/src/layout.rs | 27 | ||||
| -rw-r--r-- | src/tools/rust-analyzer/crates/hir-ty/src/layout.rs | 9 |
2 files changed, 30 insertions, 6 deletions
diff --git a/compiler/rustc_abi/src/layout.rs b/compiler/rustc_abi/src/layout.rs index 6e1299944a0..5ce5f14ce57 100644 --- a/compiler/rustc_abi/src/layout.rs +++ b/compiler/rustc_abi/src/layout.rs @@ -39,7 +39,7 @@ enum NicheBias { End, } -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum LayoutCalculatorError<F> { /// An unsized type was found in a location where a sized type was expected. /// @@ -56,6 +56,31 @@ pub enum LayoutCalculatorError<F> { EmptyUnion, } +impl<F> LayoutCalculatorError<F> { + pub fn without_payload(&self) -> LayoutCalculatorError<()> { + match self { + LayoutCalculatorError::UnexpectedUnsized(_) => { + LayoutCalculatorError::UnexpectedUnsized(()) + } + LayoutCalculatorError::SizeOverflow => LayoutCalculatorError::SizeOverflow, + LayoutCalculatorError::EmptyUnion => LayoutCalculatorError::EmptyUnion, + } + } + + /// Format an untranslated diagnostic for this type + /// + /// Intended for use by rust-analyzer, as neither it nor `rustc_abi` depend on fluent infra. + pub fn fallback_fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(match self { + LayoutCalculatorError::UnexpectedUnsized(_) => { + "an unsized type was found where a sized type was expected" + } + LayoutCalculatorError::SizeOverflow => "size overflow", + LayoutCalculatorError::EmptyUnion => "type is a union with no fields", + }) + } +} + type LayoutCalculatorResult<FieldIdx, VariantIdx, F> = Result<LayoutS<FieldIdx, VariantIdx>, LayoutCalculatorError<F>>; diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/layout.rs b/src/tools/rust-analyzer/crates/hir-ty/src/layout.rs index bfbae2941da..2c68d50013e 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/layout.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/layout.rs @@ -72,6 +72,8 @@ pub type Variants = hir_def::layout::Variants<RustcFieldIdx, RustcEnumVariantIdx #[derive(Debug, PartialEq, Eq, Clone)] pub enum LayoutError { + // FIXME: Remove variants that duplicate LayoutCalculatorError's variants after sync + BadCalc(LayoutCalculatorError<()>), EmptyUnion, HasErrorConst, HasErrorType, @@ -90,6 +92,7 @@ impl std::error::Error for LayoutError {} impl fmt::Display for LayoutError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { + LayoutError::BadCalc(err) => err.fallback_fmt(f), LayoutError::EmptyUnion => write!(f, "type is an union with no fields"), LayoutError::HasErrorConst => write!(f, "type contains an unevaluatable const"), LayoutError::HasErrorType => write!(f, "type contains an error"), @@ -114,11 +117,7 @@ impl fmt::Display for LayoutError { impl<F> From<LayoutCalculatorError<F>> for LayoutError { fn from(err: LayoutCalculatorError<F>) -> Self { - match err { - LayoutCalculatorError::EmptyUnion => LayoutError::EmptyUnion, - LayoutCalculatorError::UnexpectedUnsized(_) => LayoutError::UnexpectedUnsized, - LayoutCalculatorError::SizeOverflow => LayoutError::SizeOverflow, - } + LayoutError::BadCalc(err.without_payload()) } } |
