Repository: slint-ui/slint (language: rust)

Issue:
Compilation panics when using changed with visible property 
### Bug Description

When trying to use `changed visible` the compiler panics. Maybe it isn't legal, but it shouldn't panic.

```
RUST_BACKTRACE=full cargo run --release
   Compiling nfc-filament v0.1.0 (/Users/yanshay/ProgProj/ESP32/hamster/app_new_ux)
error: failed to run custom build command for `nfc-filament v0.1.0 (/Users/yanshay/ProgProj/ESP32/hamster/app_new_ux)`

Caused by:
  process didn't exit successfully: `/Users/yanshay/ProgProj/ESP32/hamster/app_new_ux/target/release/build/nfc-filament-122677cbca55d046/build-script-build` (exit status: 101)
  --- stderr
  thread 'main' panicked at /Users/yanshay/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.9.1/llr/expression.rs:692:89:
  called `Option::unwrap()` on a `None` value
  stack backtrace:
     0:        0x104dfc178 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h87b0c34ef58e4004
     1:        0x104e13cc4 - core::fmt::write::he53b3198f7c88d94
     2:        0x104dfc004 - std::io::Write::write_fmt::h87543053728887ab
     3:        0x104df086c - std::panicking::default_hook::{{closure}}::h5d60b68f8ca94444
     4:        0x104df04d0 - std::panicking::default_hook::hc061ee16e37508d5
     5:        0x104df0d5c - std::panicking::rust_panic_with_hook::h1684aadd3a0209c7
     6:        0x104dfc64c - std::panicking::begin_panic_handler::{{closure}}::h8bea5110ae4277e3
     7:        0x104dfc3a0 - std::sys::backtrace::__rust_end_short_backtrace::hc436a0735fb3b999
     8:        0x104df0940 - _rust_begin_unwind
     9:        0x104e54650 - core::panicking::panic_fmt::hf73c71445b0758e3
    10:        0x104e546bc - core::panicking::panic::h19761a27b25cbc1c
    11:        0x104e54de4 - core::option::unwrap_failed::hf4cc03bcd523be6e
    12:        0x104440324 - core::option::Option<T>::unwrap::h65633dec77751615
                                 at /Users/espressif/action-runner/esp-rs/_work/rust-build/rust-build/library/core/src/option.rs:965:21
    13:        0x104440324 - <i_slint_compiler::llr::expression::EvaluationContext<T> as i_slint_compiler::llr::expression::TypeResolutionContext>::property_ty::h68802ff5188676c9
                                 at /Users/yanshay/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.9.1/llr/expression.rs:692:17
    14:        0x1045a9184 - i_slint_compiler::generator::rust::compile_expression::h31d3ee3963aebd9b
                                 at /Users/yanshay/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.9.1/generator/rust.rs:2243:29
    15:        0x10458ab98 - i_slint_compiler::generator::rust::generate_sub_component::{{closure}}::h152036d84ffbcf3b
                                 at /Users/yanshay/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.9.1/generator/rust.rs:1057:20
    16:        0x10426b11c - core::iter::adapters::map::map_fold::{{closure}}::h63c314fd4ab5dcfc
                                 at /Users/espressif/action-runner/esp-rs/_work/rust-build/rust-build/library/core/src/iter/adapters/map.rs:88:28
    17:        0x1043a9080 - <core::iter::adapters::enumerate::Enumerate<I> as core::iter::traits::iterator::Iterator>::fold::enumerate::{{closure}}::h55c5979d4d63e219
                                 at /Users/espressif/action-runner/esp-rs/_work/rust-build/rust-build/library/core/src/iter/adapters/enumerate.rs:107:27
    18:        0x1042345c0 - <core::slice::iter::Iter<T> as core::iter::traits::iterator::Iterator>::fold::h6cd39763be8fa4d4
                                 at /Users/espressif/action-runner/esp-rs/_work/rust-build/rust-build/library/core/src/slice/iter/macros.rs:232:27
    19:        0x1043a9048 - <core::iter::adapters::enumerate::Enumerate<I> as core::iter::traits::iterator::Iterator>::fold::h893de115bb1e3224
                                 at /Users/espressif/action-runner/esp-rs/_work/rust-build/rust-build/library/core/src/iter/adapters/enumerate.rs:113:9
    20:        0x10424f4e4 - <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::fold::ha31407c672d2ccc1
                                 at /Users/espressif/action-runner/esp-rs/_work/rust-build/rust-build/library/core/src/iter/adapters/map.rs:128:9
    21:        0x104262f0c - core::iter::traits::iterator::Iterator::for_each::h4fcf8d511efe0852
                                 at /Users/espressif/action-runner/esp-rs/_work/rust-build/rust-build/library/core/src/iter/traits/iterator.rs:813:9
    22:        0x104200738 - alloc::vec::Vec<T,A>::extend_trusted::h81a1e5fd2fb072ab
                                 at /Users/espressif/action-runner/esp-rs/_work/rust-build/rust-build/library/alloc/src/vec/mod.rs:3121:17
    23:        0x104226090 - <alloc::vec::Vec<T,A> as alloc::vec::spec_extend::SpecExtend<T,I>>::spec_extend::hb377db3df91c1f10
                                 at /Users/espressif/action-runner/esp-rs/_work/rust-build/rust-build/library/alloc/src/vec/spec_extend.rs:26:9
    24:        0x104221c74 - <alloc::vec::Vec<T,A> as core::iter::traits::collect::Extend<T>>::extend::hcceea861fea7514e
                                 at /Users/espressif/action-runner/esp-rs/_work/rust-build/rust-build/library/alloc/src/vec/mod.rs:3053:9
    25:        0x1045797ac - i_slint_compiler::generator::rust::generate_sub_component::h77bbc610c0736457
                                 at /Users/yanshay/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.9.1/generator/rust.rs:1055:5
    26:        0x104591fb4 - i_slint_compiler::generator::rust::generate_item_tree::he26dd5a1b9aa945d
                                 at /Users/yanshay/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.9.1/generator/rust.rs:1494:20
    27:        0x10459c8dc - i_slint_compiler::generator::rust::generate_repeated_component::he4ee290c1e965c30
                                 at /Users/yanshay/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.9.1/generator/rust.rs:1753:9
    28:        0x104587198 - i_slint_compiler::generator::rust::generate_sub_component::h77bbc610c0736457
                                 at /Users/yanshay/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.9.1/generator/rust.rs:770:31
    29:        0x104591fb4 - i_slint_compiler::generator::rust::generate_item_tree::he26dd5a1b9aa945d
                                 at /Users/yanshay/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.9.1/generator/rust.rs:1494:20
    30:        0x10456bc54 - i_slint_compiler::generator::rust::generate_public_component::h2ae3bb797dd33d88
                                 at /Users/yanshay/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.9.1/generator/rust.rs:256:21
    31:        0x10456b634 - i_slint_compiler::generator::rust::generate::{{closure}}::h7850f3b8361753b9
                                 at /Users/yanshay/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.9.1/generator/rust.rs:187:46
    32:        0x104561c24 - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &mut F>::call_once::hcab892d5318f83bb
                                 at /Users/espressif/action-runner/esp-rs/_work/rust-build/rust-build/library/core/src/ops/function.rs:305:13
    33:        0x104251e14 - core::option::Option<T>::map::h602346ae159ac346
                                 at /Users/espressif/action-runner/esp-rs/_work/rust-build/rust-build/library/core/src/option.rs:1105:29
    34:        0x104251e14 - <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::next::h616f062cd7e89a3c
                                 at /Users/espressif/action-runner/esp-rs/_work/rust-build/rust-build/library/core/src/iter/adapters/map.rs:107:26
    35:        0x10456a588 - i_slint_compiler::generator::rust::generate::h6e904df2aa626518
                                 at /Users/yanshay/.cargo/registry/src/index.crates.io-6f17d22bba15001f/i-slint-compiler-1.9.1/generator/rust.rs:224:8
    36:        0x104141f30 - slint_build::compile_with_output_path::hbe53397aab428f70
                                 at /Users/yanshay/.cargo/registry/src/index.crates.io-6f17d22bba15001f/slint-build-1.9.1/lib.rs:462:21
    37:        0x104141604 - slint_build::compile_with_config::h29e51febf43e081e
                                 at /Users/yanshay/.cargo/registry/src/index.crates.io-6f17d22bba15001f/slint-build-1.9.1/lib.rs:400:9
    38:        0x104140e80 - build_script_build::main::h542e63e32a8de5e7
                                 at /Users/yanshay/ProgProj/ESP32/hamster/app_new_ux/build.rs:2:5
    39:        0x104124f2c - core::ops::function::FnOnce::call_once::h916aa81d435b214d
                                 at /Users/espressif/action-runner/esp-rs/_work/rust-build/rust-build/library/core/src/ops/function.rs:250:5
    40:        0x1041126c8 - std::sys::backtrace::__rust_begin_short_backtrace::hba759ff590dfe10b
                                 at /Users/espressif/action-runner/esp-rs/_work/rust-build/rust-build/library/std/src/sys/backtrace.rs:154:18
    41:        0x104116dc4 - std::rt::lang_start::{{closure}}::hdcdd2df6be164707
                                 at /Users/espressif/action-runner/esp-rs/_work/rust-build/rust-build/library/std/src/rt.rs:164:18
    42:        0x104dfe858 - std::rt::lang_start_internal::h36fc1d7b78985bfe
    43:        0x104116d90 - std::rt::lang_start::hbce523290eea7d40
                                 at /Users/espressif/action-runner/esp-rs/_work/rust-build/rust-build/library/std/src/rt.rs:163:17
    44:        0x104140f2c - _main
```

### Reproducible Code (if applicable)

```slint
I have this component in my code, when adding the changed visible section, the compiler panics.

export component PostAction inherits ControlPanelBase {
    changed visible => {
    }
}
```


### Environment Details

- Slint Version: 1.9.1
- Platform/OS: Mac
- Programming Language: Rust
- Backend/Renderer: ESP32/SoftwareRenderer


### Product Impact

_No response_


Discussion / hints:

Fix patch:
diff --git a/internal/compiler/passes/materialize_fake_properties.rs b/internal/compiler/passes/materialize_fake_properties.rs
index 5123cad9128..12991cf25c8 100644
--- a/internal/compiler/passes/materialize_fake_properties.rs
+++ b/internal/compiler/passes/materialize_fake_properties.rs
@@ -121,7 +121,7 @@ fn should_materialize(
 
 /// Returns true if the property is declared in this element or parent
 /// (as opposed to being implicitly declared)
-fn has_declared_property(elem: &Element, prop: &str) -> bool {
+pub fn has_declared_property(elem: &Element, prop: &str) -> bool {
     if elem.property_declarations.contains_key(prop) {
         return true;
     }
diff --git a/internal/compiler/passes/remove_unused_properties.rs b/internal/compiler/passes/remove_unused_properties.rs
index 1763d28236b..50834f10640 100644
--- a/internal/compiler/passes/remove_unused_properties.rs
+++ b/internal/compiler/passes/remove_unused_properties.rs
@@ -28,6 +28,12 @@ pub fn remove_unused_properties(doc: &Document) {
                     elem.property_analysis.borrow_mut().remove(x);
                     elem.bindings.remove(x);
                 }
+                // Remove changed callbacks over properties that are not materialized as they are not used
+                let mut change_callbacks = std::mem::take(&mut elem.change_callbacks);
+                change_callbacks.retain(|prop, _| {
+                    super::materialize_fake_properties::has_declared_property(&elem, prop)
+                });
+                elem.change_callbacks = change_callbacks;
             },
         );
     }


Test patch:
diff --git a/tests/cases/crashes/issue_7316_changed_visibility.slint b/tests/cases/crashes/issue_7316_changed_visibility.slint
new file mode 100644
index 00000000000..579cec47118
--- /dev/null
+++ b/tests/cases/crashes/issue_7316_changed_visibility.slint
@@ -0,0 +1,13 @@
+// Copyright © SixtyFPS GmbH <info@slint.dev>
+// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
+
+component PostAction inherits Rectangle {
+    changed visible => {
+    }
+}
+
+
+
+export component TestCase inherits Window {
+    PostAction {}
+}
\ No newline at end of file