Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compiler: Adopt rust-analyzer impls for LayoutCalculatorError #131942

Merged
merged 2 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion compiler/rustc_abi/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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>>;

Expand Down
9 changes: 4 additions & 5 deletions src/tools/rust-analyzer/crates/hir-ty/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"),
Expand All @@ -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())
}
}

Expand Down
Loading