Skip to content

Trait impl method with inferred return type fails conformance against trait's explicit return type #652

Closed

Description

@ehartford

Summary

A trait impl method that omits its return type (relying on inference) is rejected
with impl method '<m>' return type does not match trait, even though the inferred
body type matches the trait's declared return type exactly. This forces the user to
spell a type the compiler can infer — a mission violation ("don't make the user
write anything the compiler already knows").

Spec reference

§4.6 Type Inference (return types are inferred from the body). The trait
conformance check compares the impl method's declared return type against the
trait's, before inference fills the impl's return type from its body.

Minimal reproduction

pub trait Bar:
    fn val(self: &Self) -> i32
type D { m: i32 }
impl Bar for D:
    fn val(self: &Self): self.m     // return type omitted -> inferred i32
fn main:
    let d = D { m: 9 }
    print_i32(d.val())

Expected vs actual

  • Expected: compiles; prints 9 (inferred return i32 matches the trait's -> i32).
  • Actual: error: impl method 'val' return type does not match trait 'Bar'.

Control (proves it is inference-specific, not receivers)

Adding the explicit -> i32 to the impl method compiles and prints 9. The bug
reproduces with the plain explicit self: &Self receiver — it is unrelated to the
D7 keyword-receiver work; it was surfaced while writing D7 trait tests in
least-ceremony style.

Root-cause direction

Trait conformance (SemaDecl / trait-method matching) reads the impl method's
annotated return type and finds none (or a placeholder) instead of using the type
inferred from the method body. The fix is to compare against the inferred return
type (resolve the impl body's return type before, or during, conformance), not the
syntactic annotation.

Workaround

Spell the return type explicitly on the impl method.

Activity

  1. ehartford commented on Jul 14, 2026

    @ehartford
    ContributorAuthor

    Fixed in e7ffc47. The trait-impl method contract compared the impl method's return type against the trait's BEFORE inference ran, so an unannotated method saw the placeholder ty_void and was rejected even when its inferred return matched the trait. Fix: only run the comparison when the impl method carries its own return annotation. Verified: repro prints 9; fixture behav_trait_impl_inferred_return. Full gate green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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