Skip to content

Copy field bindings copy while Copy element bindings view — §3.8 says the rule is uniform #1531

Closed

Description

@ehartford

Question for a ruling (spec text vs. implementation; not changed by #1408).

§3.8: "A binding names what's there; an annotation demands what it says: an unannotated let binds the view unchanged, while a typed binding is an owned-value demand (D22 §6.2, D27). This is uniform across field projections and collection element access." D27 ruling 3 says the same.

For a Copy element the implementation binds a view; for a Copy field it binds a copy:

fn main:
    var v: Vec[i32] = Vec.new()
    v.push(1)
    let x = v[0]
    v[0] = 9          // error: cannot mutate `v` while `x` is a live view into it
    print(x)
type S { n: i32 }
fn main:
    var s = S { n: 1 }
    let x = s.n
    s.n = 5           // accepted
    print(x)          // prints 1

(stage1 of origin/main ba2d5d1). Joins follow the same split: if c: v[0] else: v[1] is &i32; if c: s.n else: s.m is i32. #1408 makes a join of non-Copy field places a view (the alternative there was the D32 rejection) and deliberately leaves Copy field joins as values, matching the plain Copy field binding.

The question: is the Copy field binding (and join) non-compliant with "uniform across field projections and collection element access" — should let x = s.n view s.n like let x = v[0] views v[0], with the let x: i32 = ... spelling for the copy — or is a Copy field read meant to stay a value?

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