Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compilation error non-primitive cast: '(f64, f64)' as '(f32, f32)' in function returning struct #5887

Closed
JakubKoralewski opened this issue Aug 21, 2024 · 1 comment · Fixed by #5889
Labels
a:compiler Slint compiler internal (not the codegen, not the parser) bug Something isn't working

Comments

@JakubKoralewski
Copy link

JakubKoralewski commented Aug 21, 2024

Found this error while using SDrawer from Surrealism UI.

Error

cargo run
   Compiling slint-non-primitive-cast-f64-f64-f32-f32-repro v0.1.0 (D:\...\slint-non-primitive-cast-f64-f64-f32-f32-repro)
error[E0605]: non-primitive cast: `(f64, f64)` as `(f32, f32)`
   --> D:\...\slint-non-primitive-cast-f64-f64-f32-f32-repro\target\debug\build\slint-non-primitive-cast-f64-f64-f32-f32-repro-1d21787b4cb0939a\out\appwindow.rs:466:14
    |
466 | /              ({
467 | |                  (0f64 , 100f64 ,) }
468 | |             ) as _ }
    | |__________________^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object

For more information about this error, try `rustc --explain E0605`.
error: could not compile `slint-non-primitive-cast-f64-f64-f32-f32-repro` (bin "slint-non-primitive-cast-f64-f64-f32-f32-repro") due to 1 previous error

Code

import { VerticalBox } from "std-widgets.slint";

export component AppWindow inherits Window {
    function default-height-width()->{height:percent,width:percent} {
        {height: 0%, width: 100%}
    }
    VerticalBox {
        height: default-height-width().height;
        width: default-height-width().width;
    }
}

Versions

$ rustup toolchain list  
nightly-x86_64-pc-windows-msvc (default)
$ cargo --version
cargo 1.82.0-nightly (2f738d617 2024-08-13)
$ rustc --version
rustc 1.82.0-nightly (506052d49 2024-08-16)
> ver
Microsoft Windows [Version 10.0.19045.4780]

Same on stable

> rustup toolchain list
stable-x86_64-pc-windows-msvc (default)
> rustc --version
rustc 1.80.1 (3f5fd8dd4 2024-08-06)
> cargo --version
cargo 1.80.1 (376290515 2024-07-16)

Same on WSL

$ rustup toolchain list
stable-x86_64-unknown-linux-gnu (default)
$ rustc --version
rustc 1.80.1 (3f5fd8dd4 2024-08-06)
$ cargo --version
cargo 1.80.1 (376290515 2024-07-16)
$ uname -a
Linux DESKTOP-NI12FI7 5.15.133.1-microsoft-standard-WSL2 #1 SMP Thu Oct 5 21:02:42 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Repo

repro repo https://github.com/JakubKoralewski/slint-non-primitive-cast-f64-f64-f32-f32-repro

Notes

Seems to work on slintpad. Same error with return keyword.

Fixed when using named struct:

struct Size {
    width: percent,
    height: percent
}

export component AppWindow inherits Window {
    function default-height-width()->Size {
        return {height: 0%, width: 100%};
    }
    VerticalBox {
        height: default-height-width().height;
        width: default-height-width().width;
    }
}
@JakubKoralewski JakubKoralewski changed the title Compilation error non-primitive cast: (f64, f64) as (f32, f32)`` in function returning struct Compilation error non-primitive cast: \(f64, f64)\ as\ (f32, f32)\`` in function returning struct Aug 21, 2024
@JakubKoralewski JakubKoralewski changed the title Compilation error non-primitive cast: \(f64, f64)\ as\ (f32, f32)\`` in function returning struct Compilation error non-primitive cast: '(f64, f64)' as '(f32, f32)' in function returning struct Aug 21, 2024
ogoffart added a commit that referenced this issue Aug 21, 2024
We wouldn't detect percent as a unit product in
https://github.com/slint-ui/slint/blob/43c7f57b0ff0ee10e7275cd635dc5e6aa0ef4be1/internal/compiler/generator/rust.rs#L2418-L2422

Percent is not strictly of percent unit because that unit doesn't
combine with other unit (as per the comment in `default_unit`)
But since it converts to number, it is then an unitless value. (scalar)

Fixes #5887
@ogoffart
Copy link
Member

Thanks for the bug report.
I can reproduce, and the fix is in #5889

@ogoffart ogoffart added bug Something isn't working a:compiler Slint compiler internal (not the codegen, not the parser) labels Aug 21, 2024
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:compiler Slint compiler internal (not the codegen, not the parser) bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants