Skip to content

if v[i]: over a Vec[bool] element is rejected: 'if condition must be bool' (D22 contextual Copy not applied to conditions) #1472

Closed

Description

@ehartford

Spec: §21.1 Rule 10 / D22 contextual Copy / D27 ("xs[i] denotes the element place"). A bool condition is an owned-value demand of a Copy type, so a bool element used as a condition should materialize, the same way let b: bool = v[0] does.

Repro (seed 7c3ed983, and stage1 at ba2d5d1):

type D { flags: Vec[bool] }
fn f(d: &D, i: i32) -> str: if d.flags[i]: "a" else: "b"
fn main:
    let v: Vec[bool] = Vec.new()
    v.push(true)
    let x = if v[0]: 1 else: 2
    let d = D { flags: v }
    print(f"{x} {f(d, 0)}")

Expected: prints 1 a.
Actual:

error: if condition must be bool
 --> bv.w:2:32
2 | fn f(d: &D, i: i32) -> str: if d.flags[i]: "a" else: "b"
error: if condition must be bool
 --> bv.w:6:16
6 |     let x = if v[0]: 1 else: 2

Workaround used (modeled-C stage 6, src/compiler/FacadeRender.w): bind the element to a typed local first (let optional: bool = deps.slot_optional[si]). Remove it when this is fixed.

Found while implementing modeled-C stage 6; not fixed there (out of scope).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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