Skip to content

A second if c: a else: b over the same owned strs reads "" — no diagnostic, no view #1380

Closed

Description

@ehartford

Spec: §3.8 / D27 ("a binding names what is there"). Either the first if moved a/b (then the second read is a use of a possibly-moved value and must be rejected), or it yielded a view (then the second read must see the text). It does neither.

use std.process
fn main:
    let a = "x" ++ "y"
    let b = "u" ++ "v"
    let c = args().len() > 0
    let p = if c: a else: b
    let q = if c: a else: b
    print(f"[{p}] [{q}]")

Actual (installed v0.15.2.1, origin/main 8c859a7): compiles, prints [xy] [], debug-alloc: leak count=0.
Expected: [xy] [xy] (view join) or a use-after-move diagnostic on line 7.

The same shape in a loop (for k in 0..2: let p = if k == 0: a else: b) is also accepted, while the unconditional let p = a in a loop is correctly rejected ("moved inside a loop").

Found while writing the #1363 matrix generator (a let p = if path == "err": missing else: (if small: small_path else: huge_path) inside a loop silently produced "" paths). Workaround there: .clone() each arm.

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