Skip to content

Parameter rebinding rejected despite mutability.md declaring parameters implicitly rebindable #690

Closed

Description

@ehartford

Spec-vs-implementation contradiction found during #677.

docs/completed/mutability.md (canonical, Eric-authored): "Function parameters are implicitly rebindable inside the function body. There is no mut x: T parameter modifier." — with the worked example fn f(x: i32): x = x + 1 // OK, and a whole "Parameter Rebinding" section defining local-rebind-contributes-no-effect semantics.

Implementation: parameters are bound with mut=0 (SemaCheck.check_fn_body_with_sig_at param loop), so x = x + 1 on any parameter is rejected with cannot assign to immutable variable.

Repro:

fn f(x: i32) -> i32:
    x = x + 1
    x

→ error: cannot assign to immutable variable.

Per the doc the fix is binding params mut=1 and keeping the effect-summary rule that the rebinding operation itself contributes nothing (the RHS still contributes normally). Interaction to watch: D12's scalar let-receiver guard treats immutable bindings as non-mutatable receivers — once params become rebindable, x.bump() on a by-value scalar param becomes legal callee-local mutation (consistent with mutability.md's model). Also note non-Copy share-place params: mutability.md says their rebinding is a LOCAL rebind (caller unaffected) — distinct from bare-self writes in mut fn, which are place writes (D12); the two must not be conflated in MIR lowering.

Not wired into #677 to keep that change scalar-scoped; needs its own cycle with effect/borrow fixtures.

Activity

  1. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): needs a ruling. Parameter rebinding is refused:

    fn f(x: i32) -> i32:
        x = x + 1
        x
    
    error: cannot assign to immutable variable
    

    The only text that says parameters are rebindable is mutability.md, now under docs/completed/ (history, not doctrine), and the comment of test/compile_errors/err_mut_nonself_param.w. The current spec does not state it either way: §9 and §29 say nothing about assigning to a parameter. The question: are parameters immutable bindings (today's behavior, which the spec would then state), or implicitly rebindable as the old document said (the compiler would change; an owned parameter rebound is a local rebind with no effect)? D5/D110 settle the mode of a parameter, not its rebindability.

  2. added this to the post-v0.16.0 milestone on Oct 28, 2026
  3. ehartford commented on Nov 19, 2026

    @ehartford
    ContributorAuthor

    Ruled, 2026-11-18 (D220): parameters are immutable bindings, with an error that names both fixes. var x = x should already work: §29.8's consuming-rebind exception covers it, and refusing it for a parameter is an inconsistency to fix.

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions