E4111

E4111#

The usage of a break statement is invalid.

This error happens when you use a break statement in the initialization, condition, or update statement of a loop.

Erroneous example#

///|
pub fn f(x : Int, y : Int) -> Unit {
  for i = 0; i < x; i = i + 1 {
    for j = ({
            break
          })
        j < y
        j = j + 1 {
      //            ^^^^^ Error: The usage of break statement is invalid.
      println(i + j)
    }
  }
}

Suggestion#

Do not write a break statement in the initialization, condition, or update statement of a loop. Put it in the loop body instead.

///|
pub fn f(x : Int, y : Int) -> Unit {
  for i = 0; i < x; i = i + 1 {
    for j = 0; j < y; j = j + 1 {
      if i + j > 10 {
        break
      }
      println(i + j)
    }
  }
}

The error also occurs when an unlabelled break is directly inside a labelled block. Specify the block's label when exiting it:

///|
pub fn value_or_zero(value : Int?) -> Int {
  result~: {
    guard value is Some(result) else { break }
    result
  }
}
///|
pub fn value_or_zero(value : Int?) -> Int {
  result~: {
    guard value is Some(result) else { break result~ 0 }
    result
  }
}