Skip to content

An all-field if/match join is rejected; §3.8 join rule 3 says it binds a view (elements already do) #1408

Closed

Description

@ehartford

Spec §3.8 (Contextual Copy materialization):

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.

and the join rule:

  1. If all reaching expressions are compatible shared references, the result remains a shared reference and carries the union of their possible origins.
type S { p: str, q: str }
impl S:
    mut fn f(c: bool):
        let x = if c: self.p else: self.q
        print(x)
fn main:
    var s = S { p: "a" ++ "", q: "b" ++ "" }
    s.f(true)
    print(s.p)

With #1395's fix (PR for fix-1395-field-move-through-branch) this is rejected at both arms with the D32 implicit-field-move error (fix-its: move self.p / self.p.clone()). Before the fix it compiled and vacated self.p (for a Vec field it also leaked; through a read fn receiver it double-freed).

By §3.8 the join of two field places with no owned anchor should bind a view, exactly as the element spelling already does: let x = if c: v[0] else: v[1] binds &str (MIR: ref(shared, _2[_6])). Field projections are typed as the owned field type in Sema (str), not as a view, so the join sees two owned candidates and the arms are owned demands. The rejection is the safe direction (it removes a capability; if c: &self.p else: &self.q spells the view today and works), but it is non-compliant with rule 3.

What conforming needs: a field arm of a join with no owned anchor is typed/lowered as a view of its place (MirLower emits ref(shared, place) for it, as lower_expr's NK_INDEX path does for &T element reads), and the binding records the union of the arms' origins — which the element join does not do either (#1406), so both should land together.

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