rust-lang/rust · error

immutable unnamed local

Error message

immutable unnamed local

What it means

mutability_errors.rs:91: while reporting a mutability error on a local, for a local that is not the direct access place, the diagnostic does self.local_name(local).expect("immutable unnamed local"). The invariant: a local reachable here has a user-visible name. Compiler-generated temporaries or unnamed locals are not expected on this path.

Source

Thrown at compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs:91

                access_place={:?}, span={:?}, the_place_err={:?}, error_access={:?}, location={:?},\
            )",
            access_place, span, the_place_err, error_access, location,
        );

        let mut err;
        let item_msg;
        let reason;
        let mut opt_source = None;
        let access_place_desc = self.describe_any_place(access_place.as_ref());
        debug!("report_mutability_error: access_place_desc={:?}", access_place_desc);

        match the_place_err {
            PlaceRef { local, projection: [] } => {
                item_msg = access_place_desc;
                if access_place.as_local().is_some() {
                    reason = ", as it is not declared as mutable".to_string();
                } else {
                    let name = self.local_name(local).expect("immutable unnamed local");
                    reason = format!(", as `{name}` is not declared as mutable");
                }
            }

            PlaceRef {
                local,
                projection: [proj_base @ .., ProjectionElem::Field(upvar_index, _)],
            } => {
                debug_assert!(is_closure_like(
                    Place::ty_from(local, proj_base, self.body, self.infcx.tcx).ty
                ));

                let imm_borrow_derefed = self.upvars[upvar_index.index()]
                    .place
                    .deref_tys()
                    .any(|ty| matches!(ty.kind(), ty::Ref(.., hir::Mutability::Not)));

                // If the place is immutable then:

View on GitHub (pinned to 7088e4b63a)

Solutions

  1. File a rustc ICE with the minimized code that produces the mutability error.
  2. Rewrite the expression so the offending place is a named binding (bind the temporary to a let).
  3. Bisect across nightlies with cargo bisect-rustc.
  4. Try stable vs nightly to confirm a regression.
Defensive patterns

Strategy: fallback

Prevention

When it happens

Trigger: Borrowck builds a mutability diagnostic for an unnamed/compiler-generated local that reaches the non-access-place branch, where local_name() returns None.

Common situations: An ICE inside borrowck's diagnostic emitter, typically a regression when reporting `cannot assign`-style errors for synthetic temporaries; not triggered by ordinary source.

Related errors


AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10). Data as JSON: /api/errors/04f8780ff1443e8b. Report an issue: GitHub.