Skip to content

View escape through a mut fn: self.v.push(&x) and self.r = &x store a local's view into the receiver unchecked (wider class of #1778) #1783

Closed

Description

@ehartford

Found fixing #1778 (which covered Mutex.set/RwLock.write/guard write-back). The same escape through an ordinary mut fn still compiles:

type Holder = ephemeral { v: Vec[&i32], r: Option[&i32] }
extend Holder:
    mut fn keep(x: &i32):
        self.v.push(x)      // stores a view of the caller's local into self
        self.r = Some(x)
fn main:
    var h = Holder { v: Vec.new(), r: None }
    let n = 5
    h.keep(&n)
    // h outlives n's scope in other shapes; nothing checks

Expected (§21.1 / D22): a store of a view into a place owned by the receiver requires the view's origin to outlive the receiver; the checker must treat a mut fn's stores into self like #1778 treats Mutex.set. Second gap in the same class: Rule 7 (an implicit drop is a use) is not enforced here — a Drop holder given a view of a local declared after it compiles.

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