Skip to content

D140: a generic trait impl's method with the return omitted gets no return demand (contract ret_type = 0) and ICEs in MIR #2531

Closed

Description

@ehartford

D140 (impl methods take their signature from the trait) works for a concrete impl, and the return half worked before the ruling for concrete impls (trait_impl_method_contract, #652). For a generic trait impl it does not: on the D103/D140 layer (worktree d103-option, 2026-10-21)

impl[T] Iter[&T] for ListIter[T]:
    mut fn next():                       # return omitted: the trait says -> Option[&T]
        if self.idx >= self.list.len(): return None
        let i = self.idx
        self.idx += 1
        &self.list[i]

compiles its body with no return demand (trait_impl_method_contract yields ret_type = 0 because the trait's Option[&T] cannot be instantiated for the impl at check time), so the tail is typed &T, no D103 conversion is recorded, the signature's return is nevertheless the trait's Option[&T], and MIR fails:

error: internal compiler error: invalid MIR before codegen … use rvalue type is incompatible with assign destination (dest ty=1947 kind=19, src ty=356 kind=14) in `ListIter.next__receiver__1946_3`

Expected (D140, D103): the contract instantiates the trait's return for a generic impl as it does for a concrete one (the impl's own type parameters stand for themselves), the body is demanded at it, and the tail converts. Until then ListIter.next keeps -> Option[&T] written. A silent ret_type = 0 that lets the signature and the body disagree is also a no-silent-fallback violation: the contract should refuse, not yield 0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions