Skip to content

mut-self on primitive/str owners passes self by value — in-place mutation cannot reach the caller #644

Closed

Description

@ehartford

Summary

declare_function_at (src/Codegen.w:3847-3866) lowers struct/enum self params as pointers, but primitive and str owners pass self BY VALUE — so a mut self method on a primitive/str extension mutates a callee copy and the caller never sees it. Found during the #641a receiver-lowering design pass (out of scope there: the fix is an ABI change for primitive receivers).

Spec

§9.5: mut self: Self "mutates x in place" (requirements 9.5.1.4) — currently untrue for primitive/str owners.

Fix direction

Either lower flagged mut-self primitive receivers by pointer too, or reject mut self on by-value-ABI owners at check time with a clear message. Needs a small ruling on which.

Activity

  1. added 2 commits that reference this issue on Jul 4, 2026
  2. ehartford commented on Jul 15, 2026

    @ehartford
    ContributorAuthor

    Re-triaged during the v0.16.0 release-blocker sweep — no longer a silent-wrongness blocker

    The silent bug this issue reported (a mut self primitive/str method mutates a callee copy and the caller never sees it) no longer reproduces. The D7 receiver-mode enforcement, which landed after this issue was filed, already converted it to a loud compile error. Every mutation form on a primitive/str receiver is now rejected at check time:

    extend i32:
        mut fn bump():
            self += 1        // error: cannot assign to immutable variable
    

    Verified against the current compiler for self = …, self += …, and mutation via a nested mut fn — all loud, none silent. The codebase contains zero mut fn methods on primitive/str owners, so neither resolution direction risks a self-host flip.

    Consequence for the release: the stability bar ("never silently wrong") is met, so this is removed from the v0.16.0 blocker set.

    What remains (the ruling this issue asked for) — post-release

    The premise is still true: in-place mutation of a primitive/str receiver cannot reach the caller — it's just loud now instead of silent. The original fix direction still needs a maintainer ruling:

    • A. Make it work — lower flagged mut fn primitive/str receivers by pointer (share-place / IndirectPlace), so mut self mutates in place per §9.5. This is an ABI change to the D5 share-place model (fn_param_uses_value_ref_abi, SemaDecl.w:~1086) and should be weighed against docs/completed/mutability.md + decisions.md D5/D6/D7.
    • B. Keep rejecting — but replace the misleading "cannot assign to immutable variable" with a message that names the real reason (primitive/str receivers use by-value ABI; mutate a caller variable directly instead).

    Recommendation: A is the mission-aligned answer (ergonomics-first, §9.5 says mut self mutates in place), but it's an ABI change that wants an explicit D-log entry, so it's out of scope for the packaging-focused release. B's message improvement is a safe, small follow-up regardless of the A/B call.

  3. ehartford commented on Jul 17, 2026

    @ehartford
    ContributorAuthor

    Ruling: Option A — canonized as D12 (BDFL, 2026-07-17)

    mut fn mutates the caller's place on every owner type — primitives and str included. Not deferred: in scope for v0.16.0 ("we do it right, now").

    The governing principle (D12): the receiver mode decides share-place, the owner's type does not. i32 is Copy, but mut fn still borrows in place — mode wins over Copy-ness, exactly as it already does for Copy structs. f(x) copies; x.bump() borrows. No new mechanism: it's D5 share-place / PassMode::IndirectPlace (D6), the same ABI aggregates already use. The previous restriction to STRUCT/GENERIC_INST/ENUM owners (SemaDecl.w:1086 fn_param_uses_value_ref_abi, lines ~1089/1093) was an incompleteness, not a design.

    Reference filter (verified in .reference/): Swift ships mutating func negate() on Int (Integers.swift:327); Rust ships &mut self make_ascii_uppercase on a primitive (core/num/mod.rs:720). Effectively unanimous that the mutation must reach the caller. Mission: §9.5 already promises "mut self mutates in place" with no primitive carve-out — spec-leads-compiler; the spec is right, the impl was incomplete.

    Idiom (spec examples lead with this): domain verbs on distinct/newtypes — distinct type Health = i32; extend Health: mut fn damage(n) → hp.damage(30). Bare i32.bump() is legal but prefer the operator when there's no domain meaning.

    Option B rejected: it bent the spec to a current ABI limitation (backwards from spec-leads-compiler) and denied an ergonomic the two closest references ship in their own stdlibs.

    Implementation (this issue = umbrella)

    Two ABI shapes, split by genuine risk/shape difference:

    Both gated on the /drop-audit matrix. This issue closes when both land and §9.5's promise holds for every owner type. The misleading cannot assign to immutable variable diagnostic disappears as a consequence (the feature works; no honest-error half-measure needed).

  4. ehartford commented on Jul 19, 2026

    @ehartford
    ContributorAuthor

    D12 is COMPLETE across every owner type: scalars/distinct-over-scalar (#677, 566bc58), aggregates (uniform mut-self binding, e101e7e), and str/fat-pointer (#678, e823026). The receiver MODE decides share-place, uniformly — 'extend i32: mut fn bump()' mutates, 'self = Health(...)' mutates, 'self = W{...}' mutates, 'self = self.slice(...)' mutates. Spec §9.5's canonized surface is fully implemented, battery-gated, allocator-lane-verified, and reseeded. With D11 (#630) also live, both canonized-ahead rulings from the 2026-07-17 handoff are now law AND implementation.

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