Saltar al contenido
TE124 · Tipos · error

El bloque `let … else` debe divergir

El bloque `else` puede caer fuera. Si lo hiciera, la ejecución seguiría hacia los bindings del patrón con un valor que NO coincidió, por lo que todo camino por `else` debe retornar, hacer panic, romper o continuar.

Why this fires

A let PATTERN = expr else { ... } block ran to completion without returning, panicking, breaking, or continuing. If the else block can fall through, execution would continue into the pattern's bindings using a value that did NOT match the pattern — so Zolo requires every path through else to diverge.

struct Point { x: int, y: int }

fn f(p) {
    let Point { x, .. } = p else { print("nope") }
    //                          ^^^^^^^^^^^^^^^^^^ error[TE124]: `else` block of
    //                          `let ... else` must diverge (return, panic, break, or continue)
    print(x)
}

Fix it

Make every path through the else block diverge:

fn f(p) -> int {
    let Point { x, .. } = p else { return 0 }   // ok
    return x
}
fn f(p) {
    let Point { x, .. } = p else { panic("not a point") }   // ok
    print(x)
}

See also

Véase también

Buscar en Zolo

9 resultados

en