Skip to content

Closure capturing self in a read-mode fn method segfaults when called #2025

Closed

Description

@ehartford

Spec: §12 closures / §21.1 captures; §9.5 receiver modes.

A closure that reads self.field inside a read-mode fn method crashes (SIGSEGV, rc=139) when it is called through a fn(..) parameter or a pipeline map. The same closure in a mut fn or move fn method works. Found while writing the #1930 fixtures; reproduces on the pinned seed and the installed compiler (main c884c1c).

fn apply(f: fn(i32) -> i32, x: i32) -> i32: f(x)

type C {
    count: i32,
}

impl C:
    fn plus(x: i32) -> i32: apply(y => y + self.count, x)

fn main:
    let c = C { count: 5 }
    print(f"{c.plus(3)}")

Expected: 8. Actual: segfault (rc=139), no output. let n = self.count before the closure, then capturing n, prints 8. mut fn plus / move fn plus print 8. self.items.iter() |> map(it + self.count) |> collect[Vec]() in a fn method also segfaults.

Workaround in the #1930 fixture: the closure case is exercised in mut fn / move fn methods; the fn case waits on this fix.

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