Skip to content
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
6 changes: 6 additions & 0 deletions crates/oxc_span/src/generated/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const _: () = {

assert!(size_of::<SourceType>() == 3);
assert!(align_of::<SourceType>() == 1);
assert!(offset_of!(SourceType, language) == 0);
assert!(offset_of!(SourceType, module_kind) == 1);
assert!(offset_of!(SourceType, variant) == 2);

assert!(size_of::<Language>() == 1);
assert!(align_of::<Language>() == 1);
Expand All @@ -36,6 +39,9 @@ const _: () = {

assert!(size_of::<SourceType>() == 3);
assert!(align_of::<SourceType>() == 1);
assert!(offset_of!(SourceType, language) == 0);
assert!(offset_of!(SourceType, module_kind) == 1);
assert!(offset_of!(SourceType, variant) == 2);

assert!(size_of::<Language>() == 1);
assert!(align_of::<Language>() == 1);
Expand Down
6 changes: 3 additions & 3 deletions tasks/ast_tools/src/generators/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,9 @@ fn generate_layout_assertions_for_struct<'s>(
let size_align_assertions = generate_size_align_assertions(layout, struct_ident);

let offset_asserts = struct_def.fields.iter().filter_map(|field| {
if field.visibility != Visibility::Public {
// Cannot create assertions for fields which are not public, as assertions
// are generated in `oxc_ast` crate, and those types are in other crates
if struct_def.is_foreign || field.visibility == Visibility::Private {
// Cannot create assertions for private fields (cant access them)
// or foreign types (we don't know what fields they have)
return None;
}

Expand Down
22 changes: 13 additions & 9 deletions tasks/ast_tools/src/parse/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ pub fn load_file(
}

fn parse_struct(item: ItemStruct, file_id: FileId) -> Option<(StructSkeleton, /* is_meta */ bool)> {
let (name, is_meta) = get_type_name(&item.attrs, &item.ident)?;
Some((StructSkeleton { name, file_id, item }, is_meta))
let (name, is_foreign, is_meta) = get_type_name(&item.attrs, &item.ident)?;
Some((StructSkeleton { name, is_foreign, file_id, item }, is_meta))
}

fn parse_enum(item: ItemEnum, file_id: FileId) -> Option<(EnumSkeleton, /* is_meta */ bool)> {
let (name, is_meta) = get_type_name(&item.attrs, &item.ident)?;
Some((EnumSkeleton { name, file_id, item, inherits: vec![] }, is_meta))
let (name, is_foreign, is_meta) = get_type_name(&item.attrs, &item.ident)?;
Some((EnumSkeleton { name, is_foreign, file_id, item, inherits: vec![] }, is_meta))
}

fn parse_macro(item: &ItemMacro, file_id: FileId) -> Option<EnumSkeleton> {
Expand All @@ -96,7 +96,7 @@ fn parse_macro(item: &ItemMacro, file_id: FileId) -> Option<EnumSkeleton> {
let where_clause = input.parse::<Option<WhereClause>>()?;
assert!(where_clause.is_none(), "Types with `where` clauses are not supported");

let Some((name, false)) = get_type_name(&attrs, &ident) else {
let Some((name, false, false)) = get_type_name(&attrs, &ident) else {
panic!("Enum in `inherit_variants!` macro must have `#[ast]` attr: {ident}");
};

Expand Down Expand Up @@ -126,7 +126,7 @@ fn parse_macro(item: &ItemMacro, file_id: FileId) -> Option<EnumSkeleton> {
}

let item = ItemEnum { attrs, vis, enum_token, ident, generics, brace_token, variants };
Ok(EnumSkeleton { name, file_id, item, inherits })
Ok(EnumSkeleton { name, is_foreign: false, file_id, item, inherits })
})
.expect("Failed to parse contents of `inherit_variants!` macro");

Expand All @@ -147,7 +147,7 @@ fn parse_macro(item: &ItemMacro, file_id: FileId) -> Option<EnumSkeleton> {
fn get_type_name(
attrs: &[Attribute],
ident: &Ident,
) -> Option<(/* type name */ String, /* is_meta */ bool)> {
) -> Option<(/* type name */ String, /* is_foreign */ bool, /* is_meta */ bool)> {
let mut has_ast_attr = false;
let mut has_meta_attr = false;
let mut foreign_name = None;
Expand Down Expand Up @@ -178,9 +178,13 @@ fn get_type_name(
!has_ast_attr,
"Type cannot be tagged with both `#[ast]` and `#[ast_meta]`: `{ident}`"
);
Some((ident_name(ident), true))
Some((ident_name(ident), false, true))
} else if has_ast_attr {
Some((foreign_name.unwrap_or_else(|| ident_name(ident)), false))
if let Some(foreign_name) = foreign_name {
Some((foreign_name, true, false))
} else {
Some((ident_name(ident), false, false))
}
} else {
None
}
Expand Down
6 changes: 4 additions & 2 deletions tasks/ast_tools/src/parse/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl<'c> Parser<'c> {

/// Parse [`StructSkeleton`] to yield a [`TypeDef`].
fn parse_struct(&mut self, type_id: TypeId, skeleton: StructSkeleton) -> TypeDef {
let StructSkeleton { name, item, file_id } = skeleton;
let StructSkeleton { name, item, is_foreign, file_id } = skeleton;
let has_lifetime = check_generics(&item.generics, &name);
let fields = self.parse_fields(&item.fields);
let (generated_derives, plural_name) =
Expand All @@ -214,6 +214,7 @@ impl<'c> Parser<'c> {
name,
plural_name,
has_lifetime,
is_foreign,
file_id,
generated_derives,
fields,
Expand Down Expand Up @@ -277,7 +278,7 @@ impl<'c> Parser<'c> {

/// Parse [`EnumSkeleton`] to yield a [`TypeDef`].
fn parse_enum(&mut self, type_id: TypeId, skeleton: EnumSkeleton) -> TypeDef {
let EnumSkeleton { name, item, inherits, file_id } = skeleton;
let EnumSkeleton { name, item, inherits, is_foreign, file_id } = skeleton;
let has_lifetime = check_generics(&item.generics, &name);
let variants = item.variants.iter().map(|variant| self.parse_variant(variant)).collect();
let inherits = inherits.into_iter().map(|name| self.type_id(&name)).collect();
Expand All @@ -288,6 +289,7 @@ impl<'c> Parser<'c> {
name,
plural_name,
has_lifetime,
is_foreign,
file_id,
generated_derives,
variants,
Expand Down
2 changes: 2 additions & 0 deletions tasks/ast_tools/src/parse/skeleton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ pub enum Skeleton {
#[derive(Debug)]
pub struct StructSkeleton {
pub name: String,
pub is_foreign: bool,
pub file_id: FileId,
pub item: ItemStruct,
}

#[derive(Debug)]
pub struct EnumSkeleton {
pub name: String,
pub is_foreign: bool,
pub file_id: FileId,
pub item: ItemEnum,
pub inherits: Vec<String>,
Expand Down
4 changes: 4 additions & 0 deletions tasks/ast_tools/src/schema/defs/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub struct EnumDef {
pub name: String,
pub plural_name: Option<String>,
pub has_lifetime: bool,
#[expect(unused)]
pub is_foreign: bool,
pub file_id: FileId,
pub generated_derives: Derives,
pub variants: Vec<VariantDef>,
Expand All @@ -51,6 +53,7 @@ impl EnumDef {
name: String,
plural_name: Option<String>,
has_lifetime: bool,
is_foreign: bool,
file_id: FileId,
generated_derives: Derives,
variants: Vec<VariantDef>,
Expand All @@ -61,6 +64,7 @@ impl EnumDef {
name,
plural_name,
has_lifetime,
is_foreign,
file_id,
generated_derives,
variants,
Expand Down
4 changes: 4 additions & 0 deletions tasks/ast_tools/src/schema/defs/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct StructDef {
pub name: String,
pub plural_name: Option<String>,
pub has_lifetime: bool,
pub is_foreign: bool,
pub file_id: FileId,
pub generated_derives: Derives,
pub fields: Vec<FieldDef>,
Expand All @@ -42,11 +43,13 @@ pub struct StructDef {

impl StructDef {
/// Create new [`StructDef`].
#[expect(clippy::too_many_arguments)]
pub fn new(
id: TypeId,
name: String,
plural_name: Option<String>,
has_lifetime: bool,
is_foreign: bool,
file_id: FileId,
generated_derives: Derives,
fields: Vec<FieldDef>,
Expand All @@ -56,6 +59,7 @@ impl StructDef {
name,
plural_name,
has_lifetime,
is_foreign,
file_id,
generated_derives,
fields,
Expand Down
Loading