Skip to content

Optional chain resets its receiver: a second o?.field on the same Option reads Some(<empty>) (struct and tuple payloads) #1710

Closed

Description

@ehartford

Found while fixing #1503 (tuple element optional chains). Pre-existing: reproduced on the installed compiler (~/.local/bin/with, 2026-09-26) and on the sema-cluster-a stage1.

type P { a: str, b: i32 }
fn main:
    let o: Option[P] = Some(P { a: "xyz".clone(), b: 4 })
    print(f"{o?.b}")
    print(f"{o?.a}")

Prints Some(4) then Some(). Expected Some(4) then Some(xyz): o?.b reads a Copy field through the Option (§10.3, o.map(v => v.b)), which must not consume or reset o. The same with a tuple payload (t?.1 then t?.0). Binding one chain to a local (let s = t?.0) and printing it works, so the read of the FIRST chain is what blanks the receiver, and no "use of moved value" diagnostic reports it: silent wrong value.

Suspect: MirLower.lower_optional_chain moves the whole payload (or schedules the receiver's drop/reset) instead of projecting the field out of a borrowed downcast; --dump-mir of main shows the field reads as copy _43.0/copy _43.1 after a move of the Option into _43.

Expected: the chain observes the receiver; a Copy field materializes, a non-Copy field is a partial move that the move checker reports on the next use.

Activity

  1. ehartford commented on Sep 27, 2026

    @ehartford
    ContributorAuthor

    Fixed on main by the batch-2 stack (#1790/#1791, squash 0e36508): MIR: an optional chain that takes nothing out reads its receiver in place + Sema: an optional chain's read-or-move verdict is Sema's. Evidence with the reseeded compiler (~/.local/bin/with, release digest f1e2e471…, built from 5b6830b by the pinned seed):

    Some(4)
    Some(xyz)
    

    (Before: Some(4) then Some().)

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