E0136
Alternatives bind different names
ErrorEach alternative of a | pattern binds the same names, with the same types, so the arm can use them whichever alternative matched.
Why
The arm's body runs after any one of the alternatives matches. A name that only some of them bind would have no value when another one matched. Bind the name in every alternative, or use _ in all of them.
Broken
enum Shape {
Square(i64)
Rect(i64, i64)
}
fn width(s: Shape) -> i64 {
match s {
Square(w) | Rect(_, h) => w
}
}
fn main() {
print(width(Shape.Square(2)))
print(width(Shape.Rect(3, 4)))
}
What sisu says
error[E0136]: `w` is bound in one alternative but not the other --> E0136.sisu:8:21 | 8 | Square(w) | Rect(_, h) => w | ^^^^^^^^^^ sisu: 1 error sisu: for more on a code, run `sisu explain <code>`: E0136
Fixed
enum Shape {
Square(i64)
Rect(i64, i64)
}
fn width(s: Shape) -> i64 {
match s {
Square(w) | Rect(w, _) => w
}
}
fn main() {
print(width(Shape.Square(2)))
print(width(Shape.Rect(3, 4)))
}
Sisu