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

Issue:
LSP panic: `range end index 2 out of range for slice of length 0`
### Bug Description

I refactored my UI and I wanted to open the preview.
It's likely that I still have errors in my UI, but I tried to open the preview and got this error message.
I'm reporting this as requested:

```
thread 'main' panicked at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/alloc/src/vec/mod.rs:2605:36:
range end index 2 out of range for slice of length 0
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
[Info  - 2:15:34 PM] Connection to server got closed. Server will restart.
true
```

### Reproducible Code (if applicable)

_No response_

### Environment Details

- Slint Version: 1.9
- Platform/OS: Linux
- Programming Language: Rust
- Backend/Renderer: Linux (the one used by the previewer)


### Product Impact

_No response_


Discussion / hints:
I've been able to narrow the problem to the following:

I have a Component which contains something like:

```
GridLayout {
   padding: 8px;
   spacing: 8px;
   Row {
      @children
   }
}
```

In the crashing case, children comes from something like:

```
GridLayout {
  // Various cols and rows
}
```
But in fact I think that even a simply Rectangle causes the crash.

I was able to workaround the crash by changing the first snippet as follow:

```
GridLayout {
   padding: 8px;
   spacing: 8px;
   Row {
      Rectangle {
         @children
      }
   }
}
```
Thanks for filing a bug.

I couldn't reproduce the panic with the example. 

I would love it if you could isolate a self-contained reproducible example. 

Could you perhaps define `RUST_BACKTRACE=1` env variable (bedore loading vscode) so we can get a backtrace that could help figuring it out?
I've encountered a similar problem.

**Operating System:** Windows 11, 24H2. Build 26100.2605
**Code Editor:** Visual Studio Code
**Project:** Unmodified Slint template
**Operation:** Clicked on the "Show Preview" button
**Message:**
The Slint Language Server crashed. This is a bug. Please open an issue on https://github.com/slint-ui/slint/issues
[Info  - 11:54:17] Connection to server got closed. Server will restart.
true
The Slint Language Server crashed. This is a bug. Please open an issue on https://github.com/slint-ui/slint/issues
[Error - 11:54:17] Server process exited with code 3221225477.

I'm ignorant about what kind of information I should provide. If you need further information, feel free to ask me.
 @ogoffart I've been able o strip the code down for the repro, you can find it attached here.
[minimal-repro.zip](https://github.com/user-attachments/files/18330253/minimal-repro.zip)

The backtrace looks like this:

```bash
thread 'main' panicked at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/alloc/src/vec/mod.rs:2605:36:
range end index 1 out of range for slice of length 0
stack backtrace:
   0: rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::slice::index::slice_end_index_len_fail
   3: i_slint_compiler::passes::inlining::inline_element
   4: i_slint_compiler::object_tree::recurse_elem_no_borrow
   5: i_slint_compiler::object_tree::recurse_elem_no_borrow
   6: i_slint_compiler::passes::inlining::inline::inline_components_recursively
   7: i_slint_compiler::passes::inlining::inline
   8: slint_interpreter::api::Compiler::build_from_source::{{closure}}
   9: slint_lsp::preview::reload_timer_function::{{closure}}
  10: core::ops::function::FnOnce::call_once{{vtable.shim}}
  11: core::ops::function::impls::<impl core::ops::function::FnMut<A> for &mut F>::call_mut
  12: winit::platform_impl::linux::wayland::event_loop::EventLoop<T>::pump_events
  13: <i_slint_backend_winit::Backend as i_slint_core::platform::Platform>::run_event_loop
  14: slint_lsp::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
[Error - 10:15:10 AM] Server process exited with signal SIGABRT.
[Info  - 10:15:10 AM] Connection to server got closed. Server will restart.
true
The Slint Language Server crashed. This is a bug. Please open an issue on https://github.com/slint-ui/slint/issues
```
Thanks a lot. I can reproduce.

In debug mode, i get
``` 
thread 'main' panicked at internal/compiler/passes/inlining.rs:152:17:
assertion failed: inlined_component.popup_windows.borrow().iter().any(|p|
        Rc::ptr_eq(&p.component,
            &insertion_element.borrow().enclosing_component.upgrade().unwrap()))
```
Smaller testcase:
```slint
component HeaderScreen {
    GridLayout {
        Row {
            @children
        }
    }
}

export component TestCase inherits Window {
    HeaderScreen { }
}
```

Fix patch:
diff --git a/internal/compiler/passes/inlining.rs b/internal/compiler/passes/inlining.rs
index 0d0a57b8e61..d3779cb9366 100644
--- a/internal/compiler/passes/inlining.rs
+++ b/internal/compiler/passes/inlining.rs
@@ -147,7 +147,7 @@ fn inline_element(
                 } else if Rc::ptr_eq(elem, &root_component.root_element) {
                     *cip = Some((insertion_element.clone(), *index + old_count, cip_node.clone()));
                 };
-            } else {
+            } else if old_count > 0 {
                 // @children was into a PopupWindow
                 debug_assert!(inlined_component.popup_windows.borrow().iter().any(|p| Rc::ptr_eq(
                     &p.component,


Test patch:
diff --git a/tests/cases/children/children_in_row.slint b/tests/cases/children/children_in_row.slint
new file mode 100644
index 00000000000..18244cd990a
--- /dev/null
+++ b/tests/cases/children/children_in_row.slint
@@ -0,0 +1,40 @@
+// 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
+
+// Issue 7284
+
+component HeaderScreen {
+    GridLayout {
+        Row {
+            @children
+        }
+    }
+}
+
+component Intermediate {
+    HeaderScreen {
+        @children
+    }
+}
+
+export component TestCase inherits Window {
+    h := HeaderScreen {  }
+    i := Intermediate { }
+    i2 := Intermediate { Rectangle { height: 32px; } }
+
+    out property <bool> test: h.preferred-height == 0 && i.preferred-height == 0 && i2.preferred-height == 32px;
+}
+
+/*
+```cpp
+auto handle = TestCase::create();
+const TestCase &instance = *handle;
+assert(instance.get_test());
+```
+
+
+```rust
+let instance = TestCase::new().unwrap();
+assert!(instance.get_test());
+```
+*/