Sisu
E0032

Value of the wrong type

Error

Built from the repository at commit a8614dc.

A value must have the type its position expects: the declared type of a let, the parameter of a function, the return type of a function, the operand of an operator.

Why

Sisu does not convert between number types by itself. A u8, a char, an i64 and an f64 are different types even when the number would fit, so a program says where one becomes another with u8(...), i64(...), f64(...) or char(...). A u8 and a char convert through i64: char(i64(b)), and f64(i64(b)). A conversion takes one type: i64(...) takes a u8, a char or an f64, and u8(...), char(...) and f64(...) take an i64. An i64 beside an f64 in arithmetic or a comparison, as in n * x, is the one reported, with the unsafe fix to convert it: f64(n) * x. Where the context wants an i64, as n += x does, the f64 is the one reported, and no fix converts it. A value that does not fit makes u8(...) or char(...) fail, and an f64 that is not a whole number in i64's range makes i64(...) fail, so each gives an optional, a u8?, a char? or an i64?: give a default with ??, or unwrap it with if let. The fix that converts is offered only where that optional is expected.

A value converts to an optional through one ? only: let x: i64? = 5 is valid, but let x: i64?? = 5 is not. Wrap the value in Some(...) for each further layer: let x: i64?? = Some(5).

A panic or todo message is a string. An i64, u8, f64, bool or char written on one line becomes one with interpolation: panic(n) is panic("{n}"), and todo(n) is todo("{n}").

Broken

fn main() {
    let n: i64 = 5
    let b: u8 = n
    print(b)
}

What sisu says

error[E0032]: expected `u8`, found `i64`
 --> E0032.sisu:3:17
  |
3 |     let b: u8 = n
  |                 ^

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

Fixed

fn main() {
    let n: i64 = 5
    let b: u8 = u8(n) ?? 0
    print(b)
}