You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Trait impl method with inferred return type fails conformance against trait's explicit return type #652
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.
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.
Activity
Sema: fix return-type inference for early value returns (#653, #659, #…
ehartford commentedon Jul 14, 2026
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.
D140: an impl method takes its parameter types from the trait; a writ…
D103 at every demand site; D140 impl signatures from the trait; D141 …