Skip to content
TE120 · Type · error

Flag value must be a power of two or a composite

Each `@flags` variant must be either a fresh single bit (auto-incremented or an explicit power of two) or a named composite built only from bits declared above it.

Why this fires

A flag's value is neither a single power-of-two bit nor a combination of flags declared earlier in the same enum. @flags variants must each be either a fresh bit (auto-incremented, or an explicit power of two) or a named composite built from bits already declared above it.

@flags
enum Perm: u8 {
    All = 3,
    //    ^ error[TE120]: flag `Perm::All` (value 3) is neither a power of two
    //      nor a composite of previously-declared flags
    Read,
    Write,
}

3 (0b11) would be a valid composite of Read | Write — but only after Read and Write are declared, since composites can only reference bits declared earlier.

Fix it

1. Declare the bits first, then the composite

@flags
enum Perm: u8 {
    Read,
    Write,
    All = 3,                 // ok — Read (1) | Write (2)
}

2. Use a real power of two

@flags
enum Perm: u8 {
    Read = 1,                 // ok
    Write = 2,
}

See also

See also

Search Zolo

9 results

en