leptos-rs/leptos · error
expected named struct fields
Error message
expected named struct fields
What it means
The #[server] (params extraction) macro's params_impl iterates over struct fields and calls field.ident.as_ref().expect("expected named struct fields"). The macro only supports structs with named fields; a unit struct or tuple struct passed to the params derive causes this panic at macro-expansion time.
Source
Thrown at leptos_macro/src/params.rs:19
use quote::{quote, quote_spanned};
use syn::spanned::Spanned;
pub fn params_impl(ast: &syn::DeriveInput) -> proc_macro::TokenStream {
let name = &ast.ident;
let fields = if let syn::Data::Struct(syn::DataStruct {
fields: syn::Fields::Named(ref fields),
..
}) = ast.data
{
fields
.named
.iter()
.map(|field| {
let field_name_string = &field
.ident
.as_ref()
.expect("expected named struct fields")
.to_string()
.trim_start_matches("r#")
.to_owned();
let ident = &field.ident;
let ty = &field.ty;
let span = field.span();
quote_spanned! {
span=> #ident: ::leptos_router::params::macro_helpers::Wrapper::<#ty>::__into_param(
map.get_str(#field_name_string),
#field_name_string
)?
}
})
.collect()
} else {
vec![]
};View on GitHub (pinned to 32d20f6c9d)
Solutions
- Convert the struct to use named fields: struct Login { username: String, password: String }
- If you need a newtype wrapper, extract its inner value into a named struct before passing to the macro
- Use a different deserialization approach (manual FromRequestParts) if tuple structs are required
Example fix
// before
struct Login(String, String);
// after
struct Login {
username: String,
password: String,
} Defensive patterns
Strategy: validation
Validate before calling
// Compile-time guard: named-field structs satisfy the macro; audit types before deriving
const _: () = {
// unit/tuple structs will fail macro expansion — keep server param structs named-field only
}; Prevention
- Use only named-field structs with the server params derive
- Avoid newtype/tuple structs in server function argument positions
- Review refactors that change struct field shape used by server functions
- Let the compiler catch it: the panic occurs at macro expansion, so building immediately reveals the offending struct
When it happens
Trigger: Applying the params extraction derive/macro to a tuple struct (struct Login(String, String);) or unit struct (struct Login;) instead of a named-field struct.
Common situations: Refactoring a named struct into a newtype/tuple struct for convenience; copy-pasting a tuple struct into a server function signature; auto-generated types from schema tools that emit tuple structs.
Related errors
- List of slots must not be empty
- TEMPLATE FAILURE
- Unsupported server function HTTP method: {method:?}
- keyword argument repeated: `encoding`
- keyword argument repeated: `endpoint`
AI-assisted analysis of leptos-rs/leptos@32d20f6c9d (2026-09-01).
Data as JSON: /api/errors/347061131dc28213.
Report an issue: GitHub.