A trait's type parameter is not substituted into a default method body
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 14
- Forks
- 2
- Avg merge
- 12h 42m
- Merged PRs (30d)
- 61
Description
A trait method's default body cannot name the trait's own type parameter.
trait Iterator<Item> {
fn next(self : &mut Self) -> Option<Item>;
fn last(self : &mut Self) -> Option<Item> {
let mut seen = Option<Item>::None(); // Item is not substituted here
...
}
}
impl Iterator<i64> for Range { ... }
Codegen Error: internal: generic type reached codegen (should have been monomorphized): Generic("Item", None)
A default body is copied into each impl by fill_trait_defaults_in in src/resolver.rs. As of the change that adds ImplBlock::trait_args, the copied signature is substituted -- Option<Item> becomes Option<i64> for impl Iterator<i64> for Range. The body is not, so a type named inside it survives as the letter.
This does not affect an impl's own body: impl<I, Item, U> Iterator<U> for Map<I, Item, U> writes Option<U>::None() and that works, because an impl generic is bound when the impl is monomorphised. It is specific to a default body, which has no such binding.
core::iter works around it: no default body names Item, and every Option<Item> in one comes from next or from a core::option method, so inference supplies the type. That is workable but it constrains how a default can be written -- find had to be expressed through Option::filter rather than by rebuilding the option.
Substituting the body the way the signature is substituted needs a type-level walk over statements, which does not exist yet; substitute_self handles types only.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/resolver.rs at fill_trait_defaults_in and compare how the default signature is substituted with how substitute_self handles types. Trace the copied method body for Option and inspect ImplBlock::trait_args. Done means default method bodies receive the trait-argument substitution without leaving generic types for codegen.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100