slint/tests/cases/expr/return.slint
Simon Hausmann 00471449b4 Fix compilation of Rust generated code when the generated code uses a return statement inside a block
We cast the result of the body of functions or binding expressions to
the target type via `as _`. When the code contains a return expression,
that works fine, because `return`'s type is the never type, which can be
coerced to a value of any type. However when the return statement is
inside a sub-block, the type becomes `()`, for which the `as _` cast
fails.

Work around this by attempting to detect the situation (return produces
Type::Invalid) and omit the trailing cast.
2023-03-22 18:07:34 +01:00

87 lines
2 KiB
Text

// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
TestCase := Rectangle {
property <bool> toggle: { return false; }
property <int> value: {
if (toggle) {
return 42;
}
return 100;
}
property <float> value2: {
return 100;
}
in-out property <int> index: -1;
public function rust_fn_codegen_bug() -> bool {
return index != 0 && index != 1;
}
in-out property <bool> rust_binding_codegen_bug: {
return index != 42 && index != 42;
}
callback test_signal;
property <bool> block_signal;
property <bool> signal_handled;
test_signal => {
if (block_signal) {
return;
}
signal_handled = true;
}
property<bool> test: { return value2 == value; return false; }
}
/*
```cpp
auto handle = TestCase::create();
const TestCase &instance = *handle;
assert_eq(instance.get_value(), 100);
assert(instance.get_test());
instance.set_toggle(true);
assert_eq(instance.get_value(), 42);
instance.invoke_test_signal();
assert(instance.get_signal_handled());
instance.set_signal_handled(false);
instance.set_block_signal(true);
instance.invoke_test_signal();
assert(!instance.get_signal_handled());
```
```rust
let instance = TestCase::new().unwrap();
assert_eq!(instance.get_value(), 100);
assert!(instance.get_test());
instance.set_toggle(true);
assert_eq!(instance.get_value(), 42);
instance.invoke_test_signal();
assert!(instance.get_signal_handled());
instance.set_signal_handled(false);
instance.set_block_signal(true);
instance.invoke_test_signal();
assert!(!instance.get_signal_handled());
```
```js
var instance = new slint.TestCase({});
assert.equal(instance.value, 100);
assert(instance.test);
instance.toggle = (true);
assert.equal(instance.value, 42);
instance.test_signal();
assert(instance.signal_handled);
instance.signal_handled = (false);
instance.block_signal = (true);
instance.test_signal();
assert(!instance._signal_handled);
```
*/