Sisu
E0121

Field access on an enum

Error

Built from the repository at commit a8614dc.

An enum value has no fields to read or assign: s.w is an error when s is an enum, even when one of its variants labels a payload field w.

Why

Which variant a value holds is known only when the program runs, so a field that one variant has may not be there. A match takes the value apart and names each payload in the arm that has it. Enum values do not change, so there is no field to assign either.

Broken

enum Shape {
    Rect(w: i64, h: i64)
}

fn width(s: Shape) -> i64 {
    s.w
}

fn main() {
    print(width(Shape.Rect(w: 2, h: 3)))
}

What sisu says

error[E0121]: `Shape` has no fields; take it apart with `match`
 --> E0121.sisu:6:7
  |
6 |     s.w
  |       ^

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

Fixed

enum Shape {
    Rect(w: i64, h: i64)
}

fn width(s: Shape) -> i64 {
    match s {
        Rect(w, _) => w
    }
}

fn main() {
    print(width(Shape.Rect(w: 2, h: 3)))
}