slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
17.54k stars 600 forks source link

Mismatch type for percent when compiling for `thumbv7em-none-eabihf` #1837

Closed xgroleau closed 2 years ago

xgroleau commented 2 years ago

When compiling a particular slint code on thumbv7em-none-eabihf and including it with the include_modules!() macros, compilation fails.

cargo.toml dependency

slint = { version = "0.3.1", default-features = false, features = ["compat-0-3-0", "unsafe-single-threaded", "libm"] }

Minimum slint code to reproduce.

Inner := Rectangle { 
    property <percent> val;
    background: val < 50% ? red : blue;
}

// It seems the indirection is required
Outer := VerticalLayout {
    property <percent> val;
    Inner { val: parent.val;    } // Commenting this line does not produce a compiler error

    Rectangle { background: val < 50% ? green : yellow; }

} 

export AppWindow := Window {
    width: 240px;
    height: 240px;
    Outer { val: 50%; }
}

Rust error output

error[E0308]: mismatched types
  --> /home/xgroleau/Documents/wearable/target/thumbv7em-none-eabihf/debug/build/firmware-app-f43eb8afe1782cb3/out/main.rs:61:55
   |
61 |                     ) . apply_pin (_self) . get ()) < (50f64)) {
   |                                                       ^^^^^^^ expected `f32`, found `f64`

error[E0277]: can't compare `f32` with `f64`
  --> /home/xgroleau/Documents/wearable/target/thumbv7em-none-eabihf/debug/build/firmware-app-f43eb8afe1782cb3/out/main.rs:61:53
   |
61 |                     ) . apply_pin (_self) . get ()) < (50f64)) {
   |                                                     ^ no implementation for `f32 < f64` and `f32 > f64`
   |
   = help: the trait `PartialOrd<f64>` is not implemented for `f32`
   = help: the following other types implement trait `PartialOrd<Rhs>`:
             <f32 as PartialOrd<fixed::FixedI128<Frac>>>
             <f32 as PartialOrd<fixed::FixedI16<Frac>>>
             <f32 as PartialOrd<fixed::FixedI32<Frac>>>
             <f32 as PartialOrd<fixed::FixedI64<Frac>>>
             <f32 as PartialOrd<fixed::FixedI8<Frac>>>
             <f32 as PartialOrd<fixed::FixedU128<Frac>>>
             <f32 as PartialOrd<fixed::FixedU16<Frac>>>
             <f32 as PartialOrd<fixed::FixedU32<Frac>>>
           and 146 others

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
❯ cargo --version
cargo 1.66.0-nightly (071eeaf21 2022-10-22)
tronical commented 2 years ago

Hmm, in the IR we have the 50% as NumberLiteral with the percentage unit, and we use f32 as type of storage for percent (in rust_primitive_type). However when lowering the IR to the LLW, we have only the llr NumberLiteral left, without the unit. Should we keep the unit around perhaps and use that to create the number literal with the corresponding unit type?

ogoffart commented 2 years ago

We use f32 for everything, so i don't think we need to keep the unit. Normally we do as _ in the generated code, but for some reason we do not do it there.

tronical commented 2 years ago

We translate the LLR that uses NumberLiteral(50) to if (val_prop.get() < (50f64)) { ... }. Do you mean that we should generate if (val_prop.get() < (50f64 as _)) { ... } ?

ogoffart commented 2 years ago

Fix in https://github.com/slint-ui/slint/pull/1844