Skip to content

Commit

Permalink
fix(ast_codegen): detect "complex" type wrappers (#4617)
Browse files Browse the repository at this point in the history
Detect types such as `Cell<Option<ScopeId>>` and mark them as such! We didn't used to use this method for these options but now we have to check all types to calculate their layouts which means we need to process them correctly(instead of falling to their inner value).
  • Loading branch information
rzvxa committed Aug 3, 2024
1 parent e2b15ac commit bcfa297
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions tasks/ast_codegen/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub trait StrExt: AsRef<str> {

#[derive(Debug)]
pub enum TypeIdentResult<'a> {
/// We bailed on detecting wrapper
Complex(Box<TypeIdentResult<'a>>),
Ident(&'a Ident),
Vec(Box<TypeIdentResult<'a>>),
Box(Box<TypeIdentResult<'a>>),
Expand All @@ -81,16 +83,22 @@ impl<'a> TypeIdentResult<'a> {
Self::Option(Box::new(inner))
}

fn complex(inner: Self) -> Self {
Self::Complex(Box::new(inner))
}

fn reference(inner: Self) -> Self {
Self::Reference(Box::new(inner))
}

pub fn inner_ident(&self) -> &'a Ident {
match self {
Self::Ident(it) => it,
Self::Vec(it) | Self::Box(it) | Self::Option(it) | Self::Reference(it) => {
it.inner_ident()
}
Self::Complex(it)
| Self::Vec(it)
| Self::Box(it)
| Self::Option(it)
| Self::Reference(it) => it.inner_ident(),
}
}

Expand All @@ -116,6 +124,8 @@ pub enum TypeWrapper {
OptBox,
OptVec,
Ref,
/// We bailed on detecting the type wrapper
Complex,
}

#[derive(Debug)]
Expand Down Expand Up @@ -151,7 +161,7 @@ impl TypeExt for Type {
if seg1.ident == "Option" {
TypeIdentResult::option(inner)
} else {
inner
TypeIdentResult::complex(inner)
}
}
Some(GenericArgument::Lifetime(_)) => {
Expand All @@ -176,6 +186,11 @@ impl TypeExt for Type {
let mut wrapper = TypeWrapper::None;
let ident = match res {
TypeIdentResult::Ident(inner) => inner,
TypeIdentResult::Complex(inner) => {
wrapper = TypeWrapper::Complex;
let (inner, _) = analyze(inner)?;
inner
}
TypeIdentResult::Box(inner) => {
wrapper = TypeWrapper::Box;
let (inner, inner_kind) = analyze(inner)?;
Expand Down

0 comments on commit bcfa297

Please sign in to comment.