Sisu
E0142

Loop label that is also a name in scope

Error

Built from the repository at commit a8614dc.

A loop label may not have the same name as a binding, a function, a constant, a class, an enum or a built-in type that is in scope where a break or continue names it. After break, a loop label may not be followed by a token that only continues an expression, such as + or ., or by a ( or [ that touches it.

Why

After break, a name that is the loop label of a loop around the jump is the loop label, and what follows it is the value. A reader who also knows the name as a value cannot tell which one break search means, and break search(i) would leave the loop labeled search with the value (i) rather than call the function. Rename the loop label. To give a value in parentheses or an array, put a space after the loop label: break outer (x), break outer [1, 2].

Broken

fn search(n: i64) -> i64 {
    n * 2
}

fn main() {
    let found = search: loop {
        for i in 0..10 {
            if i == 3 {
                break search(i)
            }
        }
    }
    print(found)
}

What sisu says

error[E0142]: `search` is a loop label here, so `(` cannot call it
 --> E0142.sisu:9:23
  |
6 |     let found = search: loop {
  |                 ------ loop label declared here
...
9 |                 break search(i)
  |                       ^^^^^^^
  |
  = help: to give a value in parentheses, put a space after the loop label: `break search (i)`; if `search` also names a function, rename the loop label

sisu: 1 error
sisu: for more on a code, run `sisu explain <code>`: E0142

Fixed

fn search(n: i64) -> i64 {
    n * 2
}

fn main() {
    let found = outer: loop {
        for i in 0..10 {
            if i == 3 {
                break outer search(i)
            }
        }
    }
    print(found)
}

Related

  • E0143: a loop label used by an enclosing loop.
  • E0144: break or continue naming no loop label around it.