A Rust derive emits `impl #name { ... }` and fails with E0107 on `struct Labeled<T>`; why, and how does syn's split_for_impl fix it?
answer
- impl Labeled, without its parameters
- three pieces for an impl header
- bounds after impl, bare names after the type
- make_where_clause for extra bounds
basics
~20 sThe generated header names Labeled without its parameters, so rustc sees impl Labeled and reports missing generics. Generics::split_for_impl returns the declared parameters with bounds, the bare argument list, and the where clause, to place as impl #impl_generics #name #ty_generics #where_clause.
solid answer
~40 sA derive's output is written against a token-level name. For `struct Labeled<T>`, `impl #name` expands to `impl Labeled`, and rustc rejects it with E0107, "missing generics for struct `Labeled`". The derive must copy the struct's generics into every impl and type it emits. `input.generics.split_for_impl()` returns three printable pieces: `impl_generics` (`<'a, T: Clone, const N: usize>`, with inline bounds) to put after `impl`, `ty_generics` (`<'a, T, N>`, names only) to put after the type, and an optional where clause to put last. That one header works for lifetimes, type and const parameters. When the generated code needs an extra bound, such as `T: Default`, push it into `generics.make_where_clause()` on a copy of the generics before splitting.
code
rust · 28 linesuse proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};
#[proc_macro_derive(DescribeBroken)]
pub fn derive_describe_broken(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
quote! {
impl #name { // no generics: wrong for `struct Labeled<T>`
pub fn type_name() -> &'static str { stringify!(#name) }
}
}
.into()
}
#[proc_macro_derive(Describe)]
pub fn derive_describe(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
quote! {
impl #impl_generics #name #ty_generics #where_clause {
pub fn type_name() -> &'static str { stringify!(#name) }
}
}
.into()
}go deeper
Know that a derive has to repeat a type's generic parameters in the code it generates, or it fails on generic types.
Explain the impl header's two forms of the parameter list, with bounds after impl and names after the type, and how split_for_impl provides both plus the where clause.
Diagnose E0107 and E0261 on a user's struct as derive bugs, carry generics into companion types, and add needed bounds through make_where_clause rather than assuming them.
Insist a derive's test suite covers lifetimes, const parameters and where clauses, because generics bugs surface only when a user first derives on such a type.
## Why the naive derive breaks A derive sees **tokens**. `input.ident` is just `Labeled`; the parameters live separately in `input.generics`. A first draft usually emits: `impl #name { pub fn type_name() -> &'static str { ... } }` On `struct Point` that is fine. On `struct Labeled<T>` it becomes `impl Labeled { ... }`, and rustc stops with **E0107**, "missing generics for struct `Labeled`", underlining the struct's name. The bug is in the derive, yet the user sees it on their own declaration, and it surfaces only when someone first derives on a generic type. ## What an impl header needs A generic impl repeats the parameters twice, in two different forms: `impl<'a, T: Clone> Labeled<'a, T> where T: Send { ... }` | Piece | Example | Where it goes | |---|---|---| | `impl_generics` | `<'a, T: Clone, const N: usize>` | after `impl`: declares the parameters with their inline bounds | | `ty_generics` | `<'a, T, N>` | after the type name: names only | | `where_clause` | `where T: Send` (an `Option`) | after the type, before the body | `syn::Generics::split_for_impl()` returns exactly these three values, each printable with `quote!`: `impl #impl_generics #name #ty_generics #where_clause { ... }` When the struct has no generics, all three print as nothing, so one template covers every case. Printing the pieces for `struct Labeled<'a, T: Clone, const N: usize> where T: Send` gives `<'a, T: Clone, const N: usize>`, `<'a, T, N>` and `where T: Send`. ## Every generated type needs them too A builder derive emits more than an impl. `ServerConfigBuilder` holds `Option<#ty>` fields, and when a field's type mentions `T`, the builder struct must declare `T` as well. The usual approach is: - declare the companion struct with `#impl_generics` and `#where_clause`, so it has the same parameters and bounds; - refer to it everywhere else as `#builder #ty_generics`; - give a `build()` method the return type `#name #ty_generics`. ## Adding bounds of your own Sometimes the generated code needs more than the user declared, for example a `defaults()` method calling `T::default()`. Rewriting the struct is impossible, because a derive cannot change its input, but the impl can carry extra predicates: 1. Clone or take `input.generics` mutably. 2. Collect the type parameter names from `generics.type_params()`. 3. Push `T: ::core::default::Default` predicates into `generics.make_where_clause().predicates`, building each with `syn::parse_quote!`. 4. Call `split_for_impl()` on the modified generics. The bound then lives on the impl, so the method exists only for `T` that satisfy it. Whether to bound every parameter or only the field types is a design choice; standard-library derives make their own choice, which is covered with the standard traits. ## Diagnosing it in review - **E0107 on the user's struct name** after adding a derive: the derive dropped generics. - **E0261, undeclared lifetime**, on a struct with `'a`: the derive wrote `#ty_generics` but forgot `#impl_generics`. - **Bounds missing** in the generated code (E0277 inside the expansion): the derive needs `make_where_clause`, or the user's type does not meet the requirement. - A derive test suite should include a generic struct, a lifetime, a const parameter and a where clause.
- What breaks if the derive writes `impl #ty_generics #name #ty_generics`?For `struct Labeled<T: Clone>` the header becomes `impl<T> Labeled<T>`, which drops the inline bound the struct requires, so rustc reports E0277, "the trait bound `T: Clone` is not satisfied", on the user's struct. `impl_generics` exists to carry the declarations with their bounds, and it drops defaults such as `T = u8`, which an impl may not repeat; `ty_generics` is only the argument list.
- Why must the derive copy the user's where clause onto its impl?A bound on the struct definition, such as `where T: Send`, is a well-formedness requirement: every use of `Labeled<T>` must satisfy it. An impl for `Labeled<T>` without the where clause names a type that may not be well-formed, and rustc reports a missing bound (E0277). Emitting `#where_clause` keeps the impl's assumptions identical to the type's.
- input.ident already includes the struct's generic parameters.
- ty_generics carries the bounds, so impl_generics is optional.
- A derive can add a missing bound by rewriting the struct definition.
- Generics only matter for impl blocks, not for generated helper types.
- split_for_impl ignores lifetimes and const parameters.