Skip to content

Impl method arity is not checked against the trait (types are, D140): an extra parameter fails late #988

Closed

Description

@eas4ai

Restated 2026-10-25. An impl method's return type and parameter types are now checked against the trait (D140). What is left: arity is not. An impl method with an extra parameter is accepted at the impl, and the mismatch surfaces only at a call.


Original report

Classification

Conformance-checking gap, low severity: fail-late, never fail-silent (audit finding TRAIT-001, downgraded after verification).

Source chain

select_trait_impl (src/SemaCheck.w:16899) is name-level only: an impl block naming the trait satisfies the bound regardless of method signatures. Probe .audit/probes/readonly_pass_20260903/trait_probe.w declares impl Equal for Box2 with a wrong-arity eq2 — accepted at the impl site; the error surfaces only at a call, against the impl's own signature. Concrete-impl dispatch governs everywhere, which is also why impl Eq for str's other: &str works despite the trait's by-value Self. Companion fear disproven: by-value other: Self does NOT consume at method call sites even for Drop types (trait_probe2/3.w pass with post-call use).

Repair boundary

Emit a declaration-site diagnostic when an impl method's arity/parameter types diverge from the trait declaration; keep concrete dispatch as the call-site rule. Test matrix: arity mismatch, param-type mismatch, missing method, plus the passing by-value/drop cases as negative controls.

Activity

  1. ehartford commented on Sep 16, 2026

    @ehartford
    Contributor

    Review: reproduced, with one case that is worse than "fail-late"

    Verdict. Established on current main. An impl Trait for Type block is accepted regardless of whether its method signatures match the trait's declarations, and regardless of whether every trait method is present. Errors, when they come, come from the call site against the impl's own signature. For a parameter-type mismatch that is a reasonable but misattributed diagnostic. For an arity mismatch it is not "fail-late" but "fail at LLVM": with check passes, the typed MIR validator passes, MIR emits a two-argument call to a three-parameter method, and the LLVM verifier aborts the build with an Incorrect number of arguments passed to called function dump and a bare error: code generation failed. A missing method is never diagnosed at all (that is #1002's territory for dyn; for static dispatch it simply compiles).

    Tested revision and compiler

    item value
    source main @ 5809ac1 (2026-09-14)
    compiler with build of 5809ac1 in a detached worktree, self-reports with v0.15.2.1
    platform macOS Darwin 25.6.0, arm64

    Probe matrix

    Common prelude: trait Equal: fn eq2(self: &Self, other: &Self) -> bool, type Box2 { n: i32 }, fn same[T: Equal](a: &T, b: &T) -> bool: a.eq2(b).

    impl body with check with run
    arity: fn eq2(self: &Box2, other: &Box2, extra: i32), called via same(&x, &y) ok Incorrect number of arguments passed to called function! ... INVALID LLVM FUNCTION same__sema__... then error: code generation failed, rc 1
    same arity mismatch, no call anywhere ok prints, rc 0
    missing method: trait declares eq2 and ne2, impl provides only eq2; same(&x, &x) ok true, rc 0
    param type: fn eq2(self: &Box2, other: i32); same(&x, &x) rejected: wrong argument type in call to 'Box2.eq2' at a.eq2(b) inside same, not at the impl n/a
    control: conforming impl ok true

    Tools on the arity case: --validate-all: ok; with analyze a_arity.w audit:all: violations=2 FAILED (the analyzer's caller-marshalling audit does catch it, which is the right tool to promote into check).

    Source

    • src/SemaCheck.w:16993 select_trait_impl and :16957 select_trait_impl_tiered: impl selection by (type, trait) name only.
    • The only trait-related diagnostics in SemaCheck.w are "does not implement trait ... required for dyn parameter" (:16284) and "... required by bound" (:16857), both keyed on the existence of an impl block, never on its contents. grep for a declaration-site signature check ("missing trait method", "trait method signature") finds nothing in src/Sema*.w.
    • The MIR for same in the arity case: call const fn sym747(copy _1.*, copy _2) against Box2.eq2 with locals _1, _2, _3: ty3 (three parameters).

    Spec

    §11.1 (line 5013) shows the impl form with the same signature as the trait, and §11.2 says instantiation-time checking reports "the concrete type, the unsupported operation, and the instantiation". Nothing in §11 states in so many words that an impl must provide every declared method with a matching signature; the language assumes it. This is a place where the spec should say the rule so the declaration-site diagnostic has a normative home (a one-sentence addition under §11.1 is enough, but only the maintainer authors it).

    Related PRs

    None since 450733e touch select_trait_impl. #1002 (open, same author) covers the dyn consequence of a missing method (null vtable slot); this issue is the static-dispatch and declaration-site side of the same gap.

    Would I champion a fix? Yes.

    Acceptance criteria:

    1. At the impl declaration, for each trait method: report a missing method, an arity mismatch, and a parameter/return type mismatch (after substituting Self), with the diagnostic anchored on the impl method (or the impl header for a missing one) and naming the trait's expected signature.
    2. Receiver-mode divergence (&self vs mut self vs move self) is either checked under the same rule or explicitly exempted by ruling; the report's observation that impl Eq for str uses other: &str against a by-value trait parameter suggests the stdlib itself relies on the current laxity, so the sweep must run over lib/std first.
    3. The arity case can no longer reach LLVM: either the declaration-site check rejects it, or validate_mir_body rejects a call whose argument count differs from the callee's parameter count (the same typed-call validator await in fn-param defaults checks green but is invisible to the suspend artifact #986 needs).
    4. test/compile_errors fixtures for the three mismatch kinds plus the passing by-value/Drop controls the report lists as negative cases.

    Untested

    Trait methods with generic parameters and associated-type mismatches were not probed. Linux and Windows not executed.

  2. changed the title [-]Impl method signatures are never checked against trait declarations (fail-late)[/-] [+]Impl method arity is not checked against the trait (types are, D140): an extra parameter fails late[/+] on Oct 25, 2026
  3. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced for arity only.

    $ cat arity.w
    trait Equal:
        fn eq2(self: &Self, other: &Self) -> bool
    type Box2:
        n: i32
    impl Equal for Box2:
        fn eq2(other: &Self, extra: i32) -> bool: true
    fn main:
        print("impl accepted")
    $ with check arity.w
    ok
    # a wrong return type: error: impl method 'eq2' return type does not match trait 'Equal'
    # a wrong parameter type: error: parameter `other` is `i32`; it must match the trait's `&Box2` (D140)
    

    pre-release: a misleading late diagnostic on ordinary trait code; the fix is one count comparison beside the D140 check.

  4. ehartford commented on Oct 26, 2026

    @ehartford
    Contributor

    Fixed on main by 105285c (#2693), merged through stack 6 whose seed-driven battery was green on its top (gate, uat and survey all green). Regression test: test/compile_errors/err_988_impl_method_arity.w.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions