Skip to content

Bug: by-ref generic trait method dispatch passes receiver with wrong ABI #584

Closed

Description

@ehartford

Summary

A generic function constrained by a trait can call a trait method with move self: Self, but the same path mis-lowers a self: &Self trait method for a concrete generic instantiation. The call returns pointer-like garbage instead of reading the receiver value.

This was found while implementing #391. The generic-inst trait selection fix can prove exact/blanket selection, but behavior tests must currently avoid by-ref trait receiver dispatch.

Minimal reproduction

trait Tag:
    fn tag(self: &Self) -> i32

type Box[T] { value: T }

impl Tag for Box[i32]:
    fn tag(self: &Self) -> i32:
        self.value

fn need_tag[T: Tag](x: T) -> i32:
    x.tag()

fn main:
    let b: Box[i32] = Box { value: 7 }
    print(f"{need_tag(b)}")

Expected

Prints 7.

Actual

Prints a pointer-like integer, for example 76185616, or otherwise reads the receiver with the wrong ABI.

Likely cause

The impl method is emitted under the concrete owner name, e.g. Box__i32.tag. Generic-call fallback can resolve that symbol, but the by-ref receiver ABI metadata is not applied for the concrete generic impl method call, so the receiver is passed/read as the wrong shape.

Test coverage needed

Add positive behavior coverage for a generic trait-bound call to a self: &Self method on a concrete generic instantiation, plus a regression that the same call works through an exact generic impl and through a blanket generic impl.

Activity

  1. ehartford commented on Jul 2, 2026

    @ehartford
    ContributorAuthor

    Status update (skepticism pass, 2026-07-02): the failure has MUTATED on HEAD. The original symptom (runtime pointer garbage from a wrong-ABI &Self receiver) is gone; the repro now fails at CHECK: unknown field 'value' for type 'Box' inside impl Tag for Box[i32] — Self isn't bound to the generic INSTANTIATION in impl-for-generic-inst bodies (a control with explicit self: &Box[i32] runs and prints 7). So this is now a sema Self-binding bug, not codegen ABI. Retargeting accordingly.

  2. ehartford commented on Jul 3, 2026

    @ehartford
    ContributorAuthor

    Partially fixed by the commit above — the filed shape and the worst consequence are gone: self.value through &Self in impl Tag for Box[i32] works (Self now binds the INSTANTIATION in SemaDecl.collect_fn_decl, not the dot-split base), and the silent ABI-garbage miscompile for mixed concrete/generic fields is fixed and pinned with exact-value asserts (behav_impl_generic_inst_self). Self { … } literals also stopped being silently untyped (check_struct_literal returned 0 for Self — resolved via named_types now).

    Remaining shapes, all verified broken BEFORE these fixes too (pre-fix seed): (1) move self: Self by value → codegen arg-type mismatch actual=Box__i32 expected=i32; (2) -> Self return → codegen failure + spurious trait-contract mismatch; (3) field access through a let-bound Self {…} result → MIR lowering failure (untyped field read). Retargeting this issue to those three; they share the Self-through-signature/local substitution seam rather than the receiver path.

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