Skip to content

Sema accepts &T method argument where T is expected; codegen rejects it #567

Closed

Description

@ehartford

Problem

Passing a &T value to a method parameter declared as plain T passes with check but fails during code generation:

error: wrong argument type actual=ptr expected=MirBody fn=Codegen.mir_emit_call_term
error: code generation failed

For free functions the same mismatch is caught at sema with a proper diagnostic ("wrong argument type in call to '...': argument expression 'body' has type &MirBody ... parameter expects MirBody"). The method-call argument path lacks the equivalent ref/value compatibility check, so the error surfaces only as a codegen failure naming the enclosing function — far from the offending call, with no source location.

Repro shape

Found during the #564 migration (C4): Codegen.mir_emit_call_term(body: &MirBody) in src/CodegenDispatch.w passed its borrowed body to Codegen.mir_call_context(body: MirBody) (src/Codegen.w:1713, by-value at the time). with-stage2 check src/main.w reported zero errors; with build :stage1 failed in codegen.

Expected

Sema's method-call argument checking should enforce the same &T/T compatibility as the free-function path (check_call's call_arg_type_compatible), with a source-located diagnostic.

Context

Same sema-accepts/codegen-rejects family as #566. This one actively hampers the #564 signature migration: every missed method-arg crossing costs a 5-minute build to discover instead of a 15-second check.

Activity

  1. ehartford commented on Jun 12, 2026

    @ehartford
    ContributorAuthor

    Scope expansion — this is much bigger than &T/T coercion: the registered-sig method path for plain (non-generic-inst) receivers performs no argument type checking whatsoever. Probe against 9803cd47:

    type Acc { n: i32 }
    fn Acc.take(self: &Self, x: i32) -> i32:
        self.n + x
    fn main:
        let a = Acc { n: 1 }
        let r = a.take("oops")   // str where i32 expected
    

    with check → ok.

    Only TY_GENERIC_INST receivers get arg checking (the substitution loop in check_method_call_parts), and dyn-trait calls have their own loop. The fix is the same loop as check_call's known-function path — sig_param_type + call_arg_type_compatible + emit_argument_type_mismatch + note_auto_ref_call_arg — in the plain registered-sig branch. Expect fallout: any pre-existing wrong-typed method args in the compiler, stdlib, and tests have been invisibly accepted until now.

  2. ehartford commented on Jun 13, 2026

    @ehartford
    ContributorAuthor

    Residual (the distinct-type carve-out) scoped. Recommendation: de-distinct the AstPool/Sema id-param surface and delete the carve-out — restoring full §4.5 method-arg enforcement for user code at ~1 day's cost, versus a multi-week structural migration that would buy nothing.

    Why tightening is structural, not mechanical: the dominant producers of node ids are get_data0/1/2/get_extra, which return i32 by necessity — the d-fields are polymorphic (the same accessor yields child nodes, name syms, ops, counts, extra offsets). "Wrap once, unwrap never" needs per-kind typed accessors; the cheap alternative is ~3.6k unchecked as NodeId casts that prove nothing. Surface: 71 NodeId params in Ast.w, ~3,616 accessor call sites (SemaCheck 1,796; MirLower 994; …), 814 node: i32 fn params threading ids; TypeId repeats the pattern bigger (552 existing casts).

    What distinctness protects today: essentially nothing — methods accept base (the carve-out), distinct→base auto-unwraps even for free fns (SemaCheck.w:199-204), storage flattens to i32. Enforcement survives only at a handful of free fns serviced by unchecked casts.

    The carve-out's real cost is user-facing: probed — type UserId = distinct i32 + a method take(id: UserId) accepts a raw i32 (free-fn equivalent correctly rejects). That's a live §4.5 violation in user code.

    Plan: (1) flip Ast.w's 71 NodeId params to i32 (returns can stay; distinct→base already unwraps); (2) same decision for Sema's TypeId param surface; (3) keep BlockId and the CiIR ids distinct — they genuinely follow wrap-once discipline; (4) delete the carve-out at SemaCheck.w:12070-12078 + add a compile-error test for method-arg distinct enforcement; (5) separable follow-up: whether §4.5 intends the implicit distinct→base erasure on the actual side (SemaCheck.w:199-204) at all.

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