E0128 Forward Declared Identifier in Default Fix
Written December 3, 2026 · Tested with Rust 1.99.1 · 19 min read
- The rule underneath: defaults are evaluated in declaration order
- How to read the diagnostic
- The first failure: the index example
- The same error on other items
Written December 3, 2026
Tested with Rust 1.99.1
What this guide is
Error E0128 means a type parameter with a default value is using a forward declared identifier - in the index's words, a default may only use parameters that occur before it. The error codes index example declares struct Foo<T = U, U = ()>, where the default for T names U before U is declared, and its advice is to reorder the parameters so the referenced one comes first, and to check that the forward name is not a name clash and rename the parameter if it is.
In the shapes probed in this guide, the index example verbatim drew exactly one E0128 and no other code - this premise is clean, with no ride-along to separate. The same header standing alone outside the index's wrapper drew the same single report. The count follows the forward references, in the probed shapes: a list with two forward defaults, T = U and U = V, drew exactly two E0128 reports, one per forward reference. The code is not specific to structs: the same forward default on an enum, a trait, a type alias, and a const generic parameter each drew exactly one E0128 in the probed shapes, and on a union it drew one E0128 plus a separable E0740 that belongs to the union's fields, not to the default. A default that names a type that does not exist anywhere is a different code - E0425, in the probed shape - and a forward default shared a file with an unresolved name drew E0128 and E0425 side by side. At the boundary, a default referencing an earlier parameter compiles and runs, and so do a chain of defaults each referencing the one before it, a default referencing an earlier parameter nested inside Vec, a default naming a concrete type, and a parameter list with no defaults at all. On a function and on an impl the forward default still drew E0128, but those positions also drew their own separate errors, because parameter defaults are not allowed there at all. This guide shows the E0128 shapes, the neighboring shapes, the boundary, and the repairs.
Build every program in this guide with rustc --edition 2021. In the compiler transcripts, the file names are the bare names shown on the command line.
The rule underneath: defaults are evaluated in declaration order
A type parameter default is the type used when a caller does not supply that parameter. The parameters in a list are evaluated in order, so a default may name the parameters declared before it, concrete types, and other items already in scope - but not a parameter declared after it, and not itself. That ordering is the whole rule E0128 enforces, and it is why the repair is usually a reordering rather than a redesign: in the index example, U is a perfectly good default for T, it is simply declared one position too late.
Two different defects hide behind a default that names something odd, and the probed shapes give each its own code. If the name is a parameter of the same list that is declared later - or the parameter itself - the code is E0128, and the diagnostic says so in those words. If the name is not declared anywhere at all, the code is E0425, "cannot find type", because there is no forward declaration to point at - the name is simply missing. Read which one you have before repairing: reordering fixes the first, and does nothing for the second.
The index adds one more check, and it matters in real code: verify the forward name was not a name clash. If a parameter was meant to be named something else and accidentally shares a name with a later parameter or another item, reordering would entrench the wrong name. Renaming the clashed parameter - so the name the default uses is the name of a parameter declared before it - is the repair for that case, and it is shown running below.
How to read the diagnostic
The E0128 header is fixed wording: "generic parameter defaults cannot reference parameters before they are declared". The span is a single caret under the forward identifier inside the default - in the index example, under the U in T = U - and the label repeats the point from the other side: "cannot reference U before it is declared". There is no note and no suggested fix in the report itself; the reordering advice lives in the index entry.
The count follows the forward references, in the probed shapes - not the parameters, and not the items. One forward default drew one report. A list carrying two forward defaults drew two reports, one under each forward identifier, ending with "aborting due to 2 previous errors":
error[E0128]: generic parameter defaults cannot reference parameters before they are declared
--> fail_two_forward.rs:1:16
|
1 | struct Foo<T = U, U = V, V = ()> {
| ^ cannot reference `U` before it is declared
error[E0128]: generic parameter defaults cannot reference parameters before they are declared
--> fail_two_forward.rs:1:23
|
1 | struct Foo<T = U, U = V, V = ()> {
| ^ cannot reference `V` before it is declared
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0128`.
Fix every forward reference in the output before recompiling; a second one is already reported, not hidden behind the first. A default that references the parameter it belongs to is the limiting case of the same rule - struct Foo<T = T> drew exactly one E0128 in the probed shape, its label reading "cannot reference T before it is declared", because at the point of its own default the parameter is not yet declared either. Skipping one position changes nothing: T = V with V declared two positions later drew the same single E0128, in the file fail_skip_one.rs.
The first failure: the index example
The index's erroneous example, verbatim - the default for T names U, which is declared after it:
#![allow(unused)]
fn main() {
struct Foo<T = U, U = ()> {
field1: T,
field2: U,
}
// error: generic parameters with a default cannot use forward declared
// identifiers
}
error[E0128]: generic parameter defaults cannot reference parameters before they are declared
--> fail_index.rs:3:16
|
3 | struct Foo<T = U, U = ()> {
| ^ cannot reference `U` before it is declared
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0128`.
Exactly one report, and it is the whole story: no second code rides along in this example. The separation checks confirm the default is the only defect. The identical struct standing alone, outside the index's #![allow(unused)] fn main() wrapper, drew exactly one E0128 in the file sep_standalone.rs. Removing the default from T - leaving struct Foo<T, U = ()> with the same fields - compiled and ran, printing 7. And the forward reference does not need the fields to fire: the same header over fields of concrete type drew one E0128 plus two E0392 reports, in the file sep_forward_no_field_use.rs - the E0392 pair is a separate matter, the two parameters being unused once no field mentions them, and it says nothing about the default.
The forward name may sit inside a larger type and the report is unchanged in kind. A default of Vec<U> with U declared later drew exactly one E0128, its caret under the U inside the Vec, in the file fail_nested_forward.rs. A default of (U, u32) - a tuple whose first element is the forward parameter - drew exactly one E0128 the same way, in the file fail_tuple_forward.rs. Nesting hides the name from a quick read of the parameter list, not from the rule.
The same error on other items
The rule binds the parameter list, not the struct. An enum carrying the same list drew exactly one E0128 in the probed shape:
error[E0128]: generic parameter defaults cannot reference parameters before they are declared
--> fail_enum_forward.rs:1:14
|
1 | enum Bar<T = U, U = ()> {
| ^ cannot reference `U` before it is declared
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0128`.
A trait drew the same single report, in the file fail_trait_forward.rs, and so did a type alias, in the file fail_type_alias_forward.rs. A const generic parameter is under the same ordering: a const default naming a later const parameter drew exactly one E0128 in the probed shape:
error[E0128]: generic parameter defaults cannot reference parameters before they are declared
--> fail_const_forward.rs:1:29
|
1 | struct Foo<const N: usize = M, const M: usize = 3> {
| ^ cannot reference `M` before it is declared
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0128`.
On a union the forward default drew E0128 plus a second code, and that second code is separable. The union shape drew one E0128 and one E0740 together:
error[E0128]: generic parameter defaults cannot reference parameters before they are declared
--> fail_union_forward.rs:1:15
|
1 | union Bar<T = U, U = u32> {
| ^ cannot reference `U` before it is declared
error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> fail_union_forward.rs:2:5
|
2 | a: T,
| ^^^^
|
= note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
help: wrap the field type in `ManuallyDrop<...>`
|
2 | a: std::mem::ManuallyDrop<T>,
| +++++++++++++++++++++++ +
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0128, E0740.
For more information about an error, try `rustc --explain E0128`.
The E0740 belongs to the union's field, not to the default: the same union with its parameters reordered - union Bar<U = u32, T = U> - drew exactly one E0740 and no E0128, in the file sep_union_e0740_alone.rs. A generic field in a union must be Copy or wrapped in ManuallyDrop, whatever order the parameters are in, so fix the two reports on their own terms.
Two positions add a further defect of their own. Parameter defaults are not allowed on functions or on impls at all, and a forward default in those positions drew E0128 alongside that separate error. The function shape drew one E0128 plus two unnumbered "defaults for generic parameters are not allowed here" errors, ending with "aborting due to 3 previous errors":
error[E0128]: generic parameter defaults cannot reference parameters before they are declared
--> fail_fn_forward.rs:1:10
|
1 | fn f<T = U, U = ()>() {}
| ^ cannot reference `U` before it is declared
error: defaults for generic parameters are not allowed here
--> fail_fn_forward.rs:1:6
|
1 | fn f<T = U, U = ()>() {}
| ^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #36887 <https://github.com/rust-lang/rust/issues/36887>
= note: `#[deny(invalid_type_param_default)]` (part of `#[deny(future_incompatible)]`) on by default
error: defaults for generic parameters are not allowed here
--> fail_fn_forward.rs:1:13
|
1 | fn f<T = U, U = ()>() {}
| ^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #36887 <https://github.com/rust-lang/rust/issues/36887>
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0128`.
The impl shape, in the file fail_impl_forward.rs, drew one E0128, the same two not-allowed-here errors, and two E0207 reports - the impl's parameters being unconstrained - for five errors in all. Reordering would cure the E0128 in either position and leave the rest standing: on a function or an impl, the defaults themselves have to go.
The shapes that are not this error
A default that names a type which is not declared anywhere drew a different code. struct Foo<T = Missing> drew exactly one E0425 in the probed shape - not E0128:
error[E0425]: cannot find type `Missing` in this scope
--> neighbor_undeclared_default.rs:1:16
|
1 | struct Foo<T = Missing> {
| ^^^^^^^ not found in this scope
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0425`.
The split, in the probed shapes, is whether the name is declared later in the same list. U in the index example is declared - just too late - so the compiler can point at the ordering and report E0128. Missing is declared nowhere, so there is no ordering to fix, and the report is the ordinary unresolved-name code. The two appear together when both defects are present: struct Foo<T = U, U = Missing> drew one E0128 for the forward U and one E0425 for the Missing default of U itself, in the file neighbor_undeclared_forward.rs - each default judged by its own name.
The shape where a second code rides along
Other errors in the same program are reported alongside E0128. A program whose forward-default struct shared the file with an unresolved name in the entry point drew E0128 and E0425 together in the probed shape, ending with "aborting due to 2 previous errors":
error[E0128]: generic parameter defaults cannot reference parameters before they are declared
--> ride_unresolved.rs:1:16
|
1 | struct Foo<T = U, U = ()> {
| ^ cannot reference `U` before it is declared
error[E0425]: cannot find value `missing_name` in this scope
--> ride_unresolved.rs:5:28
|
5 | fn main() { println!("{}", missing_name); }
| ^^^^^^^^^^^^ not found in this scope
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0128, E0425.
For more information about an error, try `rustc --explain E0128`.
Fixing the parameter order clears the E0128 in that shape and leaves the E0425 standing - the unresolved name is a separate problem that happens to share the output, the same separation the union's E0740 needed.
The boundary: defaults that reference earlier parameters
The line is position in the list. On the other side of it, the same default shape is ordinary code. The index's own reordering - struct Foo<U = (), T = U> - compiled and ran in the probed shape, printing backward:
struct Foo<U = (), T = U> {
field1: T,
field2: U,
}
fn main() { let f: Foo = Foo { field1: (), field2: () }; println!("backward"); }
backward
A chain of defaults, each referencing the parameter before it, compiles and runs the same way: struct Foo<A = (), B = A, C = B> printed chain in the probed shape. So does a default that uses an earlier parameter inside a larger type: struct Foo<U = u32, T = Vec<U>> compiled and ran in the probed shape, printing 9, the element read back out of the defaulted Vec. Nesting is allowed in both directions - it is the declaration order that decides, not the shape of the default.
The simpler boundary shapes behave as expected. A default naming a concrete type - struct Foo<T = u32> - compiled and ran in the probed shape, printing 5. A later parameter whose own default references an earlier parameter that itself has a concrete default - struct Foo<T = u32, U = T> - compiled and ran, printing 7, the sum of the two fields. And a list with no defaults at all, struct Foo<T, U>, compiled and ran, printing 3.
Related guides
For a default or any other code naming a type that does not exist, the neighboring code is E0425. For a parameter that no field or use ever mentions, the neighboring code is E0392. For a generic field in a union that is not Copy or wrapped in ManuallyDrop, the neighboring code is E0740. For a type parameter on an impl that nothing constrains, the neighboring code is E0207.
The repairs
Every repair below starts from the same observation: the default names a parameter that is not yet declared at that point in the list. Either move the declaration earlier, stop naming a later parameter, or - where the name itself is the mistake - rename so the name in the default is a parameter declared before it.
Reorder the parameters. This is the index's own repair: declare the referenced parameter first, and let the default follow it. The index's reordered struct compiled and ran in the probed shape, printing 7 - the value stored through the defaulted parameter:
struct Foo<U = (), T = U> {
field1: T,
field2: U,
}
fn main() { let f = Foo { field1: 7u32, field2: () }; println!("{}", f.field1); }
7
Callers who supplied the parameters positionally are affected by a reorder, so check call sites that supply the parameters in order; callers who relied on the defaults are unaffected in the probed shape - the reordered struct above is used with no parameter list at all.
Drop the forward default for a concrete one. Where the later parameter was only ever a placeholder for a known type, giving the first parameter that type - or the unit type - as its own default removes the forward reference entirely. The shape with both defaults concrete compiled and ran in the probed shape, printing 5, the sum of the two fields supplied explicitly:
struct Foo<T = (), U = ()> {
field1: T,
field2: U,
}
fn main() { let f: Foo<u32, u32> = Foo { field1: 2, field2: 3 }; println!("{}", f.field1 + f.field2); }
5
Rename a clashed parameter. This is the index's second check: if the forward name came from a clash - a parameter named so that it collides with a later one - rename it so the default references a parameter declared before it, under a name that says what it holds. The renamed shape compiled and ran in the probed shape, printing 11:
struct Foo<Value = (), T = Value> {
field1: T,
field2: Value,
}
fn main() { let f = Foo { field1: 11u32, field2: () }; println!("{}", f.field1); }
11
Fixes in reach order
First, read the name under the caret. If it is a parameter declared later in the same list, the fix is ordering. If it is declared nowhere, the report to chase is E0425, and no reordering will help.
Second, reorder the list so every parameter a default names is declared before that default - the index's repair, and the one that preserves every default's meaning.
Third, before reordering, take the index's clash check seriously: if the forward name is a parameter that was misnamed, rename it instead of moving it, so the list does not bake the wrong name in.
Fourth, if the forward default was only a placeholder, replace it with the concrete type it stood for, or drop the default and let callers supply the parameter.
Fifth, if the shape sits on a function or an impl, remove the defaults altogether - those positions do not accept parameter defaults, and the E0128 there is one report among several that all point at the defaults.
Sixth, if the output also carries a second code, separate it before judging the program: a generic union field is an E0740 of its own, an unused parameter is an E0392 of its own, and an unresolved name elsewhere is an E0425 of its own - each survives the ordering fix until it is fixed on its own terms.
Decision summary
E0128 fired in the probed shapes for the index example verbatim (exactly one E0128, no other code), for the same struct standing alone (exactly one), for two forward defaults in one list (exactly two, one per forward reference), for a default referencing the parameter itself and for a default skipping one position to a later parameter (exactly one each), for a forward name nested inside Vec and inside a tuple (exactly one each), for the same list on an enum, a trait, and a type alias (exactly one each), and for a const generic default naming a later const parameter (exactly one). It rode along, in the probed shapes, with E0740 on a union (separable - the reordered union drew E0740 alone), with E0425 for an unresolved name elsewhere in the program and for a second default naming an undeclared type, with E0392 for unused parameters when no field mentioned them, and on a function and an impl with the separate not-allowed-here errors for defaults in those positions (the impl shape also drawing E0207). It did not fire, in the probed shapes, for the reordered list (compiled and ran, printing backward, and 7 in the repair form), for a chain of defaults each referencing the previous parameter (printing chain), for a default referencing an earlier parameter nested inside Vec (printing 9), for a concrete default (printing 5), for a later default referencing an earlier defaulted parameter (printing 7), or for a list with no defaults (printing 3). The neighboring shape drew another code instead in the probed shapes: a default naming a type declared nowhere drew exactly one E0425. The repairs that compiled and ran in the probed shapes were reordering the parameters as the index suggests (printing 7), replacing the forward default with concrete defaults and supplying the parameters explicitly (printing 5), and renaming a clashed parameter so the referenced name is declared first (printing 11).
Checklist
- The test is position, in the probed shapes: a default may name only parameters declared before it in the same list - later parameters and the parameter itself are both forward references.
- The count is per forward reference, in the probed shapes: one forward default drew one report, and two forward defaults drew two.
- Nesting changes nothing, in the probed shapes: a forward name inside
Vecor inside a tuple drew the same E0128, and an earlier name nested the same way compiled and ran. - The item changes nothing either, in the probed shapes: a struct, an enum, a trait, a type alias, and a const generic parameter all drew E0128 for the same forward default.
- A missing name is a different error, in the probed shapes: a default naming a type declared nowhere drew E0425, not E0128 - check which name the caret sits under before reordering.
- On the other side of the line there is no error, in the probed shapes: a reordered list, a chain of backward references, a concrete default, and a list with no defaults all compile and run.
Sources
- Rust error codes index, E0128 - a type parameter with a default value is using a forward declared identifier; type parameter defaults can only use parameters that occur before them, the erroneous example is
struct Foo<T = U, U = ()>, the fix is to reorder tostruct Foo<U = (), T = U>, and the entry adds to verify the forward name was not a name clash and rename the parameter if so. - Rust error codes index, E0425 - an unresolved name was used; the neighboring code drawn in the probed shapes by a default naming a type declared nowhere, and by an unresolved name elsewhere in a program that also carried a forward default.
- Rust error codes index, E0392 - a type parameter is unused; the neighboring code drawn in the probed shape alongside E0128 when the forward-default struct's fields mentioned neither parameter.
- Rust error codes index, E0740 - a union field must implement
Copyor be wrapped inManuallyDrop; the neighboring code drawn in the probed shape alongside E0128 by a generic union field, and drawn alone by the same union with its parameters reordered. - Rust error codes index, E0207 - a type parameter on an impl is not constrained by the impl trait, the self type, or predicates; the neighboring code drawn in the probed shape by the impl carrying a forward default, alongside E0128 and the not-allowed-here errors for defaults in that position.
- The Rust Reference, Generics - the generic parameter lists on items: functions, type aliases, structs, enumerations, unions, traits, and implementations may be parameterized by types, constants, and lifetimes.
Keep Exploring
The full Rust hub \u2014 ownership, structs and enums, async, and measured comparisons.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.