tracel-ai/burn · error
Only struct can be derived
Error message
Only struct can be derived
What it means
Derive-macro input guard in the Module codegen: `parse_module_fields` only handles struct ASTs, so applying the Module derive to an enum or union reaches the non-struct arm and fails at compile time. The offending input is the non-struct item carrying the derive attribute.
Source
Thrown at crates/burn-derive/src/module/codegen_struct.rs:340
generics: &mut ModuleGenerics,
) -> syn::Result<Vec<ModuleField>> {
let mut fields = Vec::new();
let mut module_generics = HashSet::new();
let mut skip_generics = HashSet::new();
match &ast.data {
syn::Data::Struct(struct_data) => {
for field in struct_data.fields.iter() {
let field_type = parse_module_field_type(field, generics)?;
if field_type.is_module {
module_generics.extend(field_type.generic_idents.iter().cloned());
} else if matches!(field_type.attr, Some(ModuleFieldAttribute::Skip)) {
skip_generics.extend(field_type.generic_idents.iter().cloned());
}
fields.push(ModuleField::new(field.clone(), field_type));
}
}
syn::Data::Enum(_) => panic!("Only struct can be derived"),
syn::Data::Union(_) => panic!("Only struct can be derived"),
};
for ident in module_generics.intersection(&skip_generics) {
if let Some(param) = ast.generics.params.iter().find_map(|p| match p {
syn::GenericParam::Type(tp) if tp.ident == *ident => Some(tp),
_ => None,
}) {
return Err(syn::Error::new(
param.ident.span(),
format!(
"Generic type `{ident}` should not be used on both a module field and a skipped field. \
Consider removing `#[module(skip)]` or using a different type for one of the fields.",
),
));
}
}
View on GitHub (pinned to d16f7ba2ed)
Solutions
- Apply the Module derive only to structs; model alternatives with nested module structs or an enum of separate module types.
- Remove the derive from the enum and implement `Module` manually if enum-like behavior is needed.
- Wrap each variant's data in its own struct and dispatch outside the derive system.
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/burn-derive/src/module/codegen_struct.rs:340 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/0b1fc399f52c306c.
Report an issue: GitHub.