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.52k stars 600 forks source link

TextInput.focus in PopupWindow causes crash in i-slint-compiler #5852

Closed R3alCl0ud closed 2 months ago

R3alCl0ud commented 2 months ago

Hello, I'm experiencing a crash in the compiler itself when I try to compile my application with this component

Platform: Archlinux AMD64 Wayland (COSMIC Epoch) Programming Language: Rust

Steps to reproduce

Try to compile app with a component like the following

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

export component TextEntryDialog {
    popup-window := PopupWindow {
        text-input := TextInput {
            text: "Test Text";
        }
    }

    public function show() {
        popup-window.show();
        text-input.focus(); // this will cause the compiler to crash
    }

    public function close() {
        popup-window.close();
    }
}

export component App inherits Window {
    VerticalLayout {
        txt-dia := TextEntryDialog {

        }
        Button {
            clicked => {
                txt-dia.show();
            }
        }
    }
}

Expected behavior:

App compiles properly and text input is focused when the popup window is shown. or an error is shown showing why this is invalid code.

Console log from compiler crash

error: failed to run custom build command for `rust4diva v0.2.0 (/home/neptune/Documents/git/Rust4Diva)`

Caused by:
  process didn't exit successfully: `/home/neptune/Documents/git/Rust4Diva/target/debug/build/rust4diva-77449c8929d1e2ee/build-script-build` (exit status: 101)
  --- stderr
  thread 'main' panicked at /home/neptune/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.7.2/llr/lower_expression.rs:35:34:
  called `Option::unwrap()` on a `None` value
  stack backtrace:
     0: rust_begin_unwind
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/panicking.rs:652:5
     1: core::panicking::panic_fmt
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/panicking.rs:72:14
     2: core::panicking::panic
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/panicking.rs:146:5
     3: core::option::unwrap_failed
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/option.rs:1985:5
     4: core::option::Option<T>::unwrap
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/option.rs:935:21
     5: i_slint_compiler::llr::lower_expression::ExpressionContext::map_property_reference
               at /home/neptune/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.7.2/llr/lower_expression.rs:35:23
     6: i_slint_compiler::llr::lower_expression::lower_expression
               at /home/neptune/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.7.2/llr/lower_expression.rs:82:17
     7: i_slint_compiler::llr::lower_expression::lower_expression::{{closure}}
               at /home/neptune/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.7.2/llr/lower_expression.rs:128:46
     8: core::iter::adapters::map::map_fold::{{closure}}
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/iter/adapters/map.rs:89:28
     9: <core::slice::iter::Iter<T> as core::iter::traits::iterator::Iterator>::fold
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/slice/iter/macros.rs:230:27
    10: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::fold
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/iter/adapters/map.rs:129:9
    11: core::iter::traits::iterator::Iterator::for_each
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/iter/traits/iterator.rs:818:9
    12: alloc::vec::Vec<T,A>::extend_trusted
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/alloc/src/vec/mod.rs:3096:17
    13: <alloc::vec::Vec<T,A> as alloc::vec::spec_extend::SpecExtend<T,I>>::spec_extend
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/alloc/src/vec/spec_extend.rs:26:9
    14: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/alloc/src/vec/spec_from_iter_nested.rs:62:9
    15: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/alloc/src/vec/spec_from_iter.rs:33:9
    16: <alloc::vec::Vec<T> as core::iter::traits::collect::FromIterator<T>>::from_iter
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/alloc/src/vec/mod.rs:2970:9
    17: core::iter::traits::iterator::Iterator::collect
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/iter/traits/iterator.rs:2005:9
    18: i_slint_compiler::llr::lower_expression::lower_expression
               at /home/neptune/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.7.2/llr/lower_expression.rs:128:21
    19: i_slint_compiler::llr::lower_expression::lower_expression::{{closure}}
               at /home/neptune/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.7.2/llr/lower_expression.rs:113:59
    20: core::iter::adapters::map::map_fold::{{closure}}
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/iter/adapters/map.rs:89:28
    21: <core::slice::iter::Iter<T> as core::iter::traits::iterator::Iterator>::fold
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/slice/iter/macros.rs:230:27
    22: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::fold
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/iter/adapters/map.rs:129:9
    23: core::iter::traits::iterator::Iterator::for_each
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/iter/traits/iterator.rs:818:9
    24: alloc::vec::Vec<T,A>::extend_trusted
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/alloc/src/vec/mod.rs:3096:17
    25: <alloc::vec::Vec<T,A> as alloc::vec::spec_extend::SpecExtend<T,I>>::spec_extend
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/alloc/src/vec/spec_extend.rs:26:9
    26: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/alloc/src/vec/spec_from_iter_nested.rs:62:9
    27: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/alloc/src/vec/spec_from_iter.rs:33:9
    28: <alloc::vec::Vec<T> as core::iter::traits::collect::FromIterator<T>>::from_iter
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/alloc/src/vec/mod.rs:2970:9
    29: core::iter::traits::iterator::Iterator::collect
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/iter/traits/iterator.rs:2005:9
    30: i_slint_compiler::llr::lower_expression::lower_expression
               at /home/neptune/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.7.2/llr/lower_expression.rs:113:39
    31: i_slint_compiler::llr::lower_to_item_tree::lower_sub_component::{{closure}}
               at /home/neptune/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.7.2/llr/lower_to_item_tree.rs:364:21
    32: i_slint_compiler::generator::handle_property_bindings_init::handle_property_inner
               at /home/neptune/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.7.2/generator.rs:433:9
    33: i_slint_compiler::generator::handle_property_bindings_init::{{closure}}
               at /home/neptune/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.7.2/generator.rs:439:13
    34: i_slint_compiler::object_tree::recurse_elem
               at /home/neptune/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.7.2/object_tree.rs:2059:17
    35: i_slint_compiler::generator::handle_property_bindings_init
               at /home/neptune/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.7.2/generator.rs:437:5
    36: i_slint_compiler::llr::lower_to_item_tree::lower_sub_component
               at /home/neptune/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.7.2/llr/lower_to_item_tree.rs:356:5
    37: i_slint_compiler::llr::lower_to_item_tree::lower_to_item_tree
               at /home/neptune/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.7.2/llr/lower_to_item_tree.rs:29:18
    38: i_slint_compiler::generator::rust::generate
               at /home/neptune/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.7.2/generator/rust.rs:163:15
    39: slint_build::compile_with_config
               at /home/neptune/.cargo/registry/src/index.crates.io-6f17d22bba15001f/slint-build-1.7.2/lib.rs:396:21
    40: build_script_build::main
               at ./build.rs:3:5
    41: core::ops::function::FnOnce::call_once
               at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/ops/function.rs:250:5
  note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
warning: build failed, waiting for other jobs to finish...

Other Relevant Stuff

Link to simple slintpad implementation of the crash: Slint Pad Focus Crash The preview will work until you click the button to open the popup window at which point the preview portion of the page hangs completely until you refresh the page.

R3alCl0ud commented 2 months ago

Crash happens with at least Slint >=1.7.1 Haven't tested any older versions than this.

ogoffart commented 2 months ago

Thanks for filling an issue.

This is a problem with the PopupWindow. access to element inside the PopupWindow shouldn't be allowed. Property access already report errors, but we should also report errors for function access.

tronical commented 2 months ago

You can work around this by using an init callback, btw:

popup-window := PopupWindow {
        text-input := TextInput {
            text: "Test Text";
        }
        init => { text-input.focus(); }
    }