slint-ui / slint

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

Reaching `unreachable!` code in `lower_to_item_tree.rs` when declaring globals #2005

Closed tecc closed 1 year ago

tecc commented 1 year ago

When trying to compile my project, I constantly get a thread 'main' panicked at 'internal error: entered unreachable code' error. For reference, I'm using Slint version 0.3.3 with the log, and slint-build of the same version as well.

This happens when I try to compile the following code:

export struct SurfaceTheme := {
    background: color,
    foreground: color
}

export global ShellTheme := {
    property <SurfaceTheme> primary;
    property <SurfaceTheme> secondary;
    property <SurfaceTheme> accent-primary; // Normal
    property <SurfaceTheme> accent-secondary; // Highlighted
}

It works if I exclude the ShellTheme declaration, or if I exclude everything but the ShellTheme declaration, but as soon as I add any other statement, it gives me an error.

Cargo tells me this about the error:

Caused by:
  process didn't exit successfully: (path to built build script) (exit status: 101)
  --- stderr
  thread 'main' panicked at 'internal error: entered unreachable code', /home/tecc/.cargo/registry/src/github.com-1ecc6299db9ec823/i-slint-compiler-0.3.3/llr/lower_to_item_tree.rs:300:18
  stack backtrace:
     0: rust_begin_unwind
               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/panicking.rs:575:5
     1: core::panicking::panic_fmt
               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/panicking.rs:65:14
     2: core::panicking::panic
               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/panicking.rs:115:5
     3: i_slint_compiler::llr::lower_to_item_tree::lower_sub_component::{{closure}}
               at /home/tecc/.cargo/registry/src/github.com-1ecc6299db9ec823/i-slint-compiler-0.3.3/llr/lower_to_item_tree.rs:300:18
     4: i_slint_compiler::object_tree::recurse_elem
               at /home/tecc/.cargo/registry/src/github.com-1ecc6299db9ec823/i-slint-compiler-0.3.3/object_tree.rs:1747:17
     5: i_slint_compiler::llr::lower_to_item_tree::lower_sub_component
               at /home/tecc/.cargo/registry/src/github.com-1ecc6299db9ec823/i-slint-compiler-0.3.3/llr/lower_to_item_tree.rs:224:5
     6: i_slint_compiler::llr::lower_to_item_tree::lower_to_item_tree
               at /home/tecc/.cargo/registry/src/github.com-1ecc6299db9ec823/i-slint-compiler-0.3.3/llr/lower_to_item_tree.rs:29:14
     7: i_slint_compiler::generator::rust::generate
               at /home/tecc/.cargo/registry/src/github.com-1ecc6299db9ec823/i-slint-compiler-0.3.3/generator/rust.rs:155:15
     8: slint_build::compile_with_config
               at /home/tecc/.cargo/registry/src/github.com-1ecc6299db9ec823/slint-build-0.3.3/lib.rs:341:21
     9: build_script_build::main::add
               at ./build.rs:28:9
    10: build_script_build::main
               at ./build.rs:37:17
    11: core::ops::function::FnOnce::call_once
               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/ops/function.rs:251:5

The line where the error occurs is the following: https://github.com/slint-ui/slint/blob/69df22e2fb54a071f8baa4554e28fb1fe1f3ffe5/internal/compiler/llr/lower_to_item_tree.rs#L300 At a guess it's making an incorrect assumption that matching to a certain element base type is impossible?

ogoffart commented 1 year ago

Thanks for the bug report!

The problem here is that there is a global, but no component were declared. In that case there is nothing to generate. Your code should always contains at least component that would be generated.

Of course, we shouldn't panic, so the panic will be fixed with PR #2006 Thanks for reporting.