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

refactor(ast_codegen): disallow implicit enum discriminants on #[ast] types. #4690

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
2 changes: 1 addition & 1 deletion tasks/ast_codegen/src/schema/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl EnumDef {
pub struct VariantDef {
pub name: String,
pub fields: Vec<FieldDef>,
pub discriminant: Option<u8>,
pub discriminant: u8,
pub markers: InnerMarkers,
}

Expand Down
27 changes: 18 additions & 9 deletions tasks/ast_codegen/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ fn lower_ast_enum(it @ rust::Enum { item, meta }: &rust::Enum, ctx: &crate::Earl
.variants
.iter()
.filter(|it| !it.attrs.iter().any(|it| it.path().is_ident("inherit")))
.map(|var| lower_variant(var, ctx))
.map(|var| lower_variant(var, || it.ident().to_string(), ctx))
.collect(),
inherits: meta.inherits.iter().map(|it| lower_inherit(it, ctx)).collect(),
has_lifetime: item.generics.lifetimes().count() > 0,
Expand Down Expand Up @@ -181,15 +181,21 @@ fn lower_ast_struct(
}
}

fn lower_variant(variant: &syn::Variant, ctx: &crate::EarlyCtx) -> VariantDef {
fn lower_variant<F>(variant: &syn::Variant, enum_dbg_name: F, ctx: &crate::EarlyCtx) -> VariantDef
where
F: Fn() -> String,
{
VariantDef {
name: variant.ident.to_string(),
discriminant: variant.discriminant.as_ref().map(|(_, disc)| match disc {
syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(lit), .. }) => {
lit.base10_parse().expect("invalid base10 enum discriminant")
}
_ => panic!("invalid enum discriminant"),
}),
discriminant: variant.discriminant.as_ref().map_or_else(
|| panic!("expected explicit enum discriminants on {}", enum_dbg_name()),
|(_, disc)| match disc {
syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(lit), .. }) => {
lit.base10_parse().expect("invalid base10 enum discriminant")
}
_ => panic!("invalid enum discriminant {:?} on {}", disc, enum_dbg_name()),
},
),
fields: variant.fields.iter().map(|fi| lower_field(fi, ctx)).collect(),
markers: parse_inner_markers(&variant.attrs).unwrap(),
}
Expand All @@ -199,7 +205,10 @@ fn lower_inherit(inherit: &rust::Inherit, ctx: &crate::EarlyCtx) -> InheritDef {
match inherit {
rust::Inherit::Linked { super_, variants } => InheritDef {
super_: create_type_ref(super_, ctx),
variants: variants.iter().map(|var| lower_variant(var, ctx)).collect(),
variants: variants
.iter()
.map(|var| lower_variant(var, || super_.get_ident().inner_ident().to_string(), ctx))
.collect(),
},
rust::Inherit::Unlinked(_) => {
panic!("`Unlinked` inherits can't be converted to a valid `InheritDef`!")
Expand Down