Skip to content

Commit

Permalink
Don't optimize array
Browse files Browse the repository at this point in the history
They are model and if the model is copied, the changes in model need to
apply to all copies.

Fixes slint-ui#5249
  • Loading branch information
ogoffart committed Jun 4, 2024
1 parent 48b628c commit 94af4d2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
5 changes: 4 additions & 1 deletion internal/compiler/expression_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,10 @@ impl Expression {
}
Expression::BinaryExpression { lhs, rhs, .. } => lhs.is_constant() && rhs.is_constant(),
Expression::UnaryOp { sub, .. } => sub.is_constant(),
Expression::Array { values, .. } => values.iter().all(Expression::is_constant),
// Array will turn into model, and they can't be considered as constant if the model
// is used and the model is changed. CF issue #5249
//Expression::Array { values, .. } => values.iter().all(Expression::is_constant),
Expression::Array { .. } => false,
Expression::Struct { values, .. } => values.iter().all(|(_, v)| v.is_constant()),
Expression::PathData(data) => match data {
Path::Elements(elements) => elements
Expand Down
4 changes: 3 additions & 1 deletion internal/compiler/llr/optim_passes/inline_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ fn expression_cost(exp: &Expression, ctx: &EvaluationContext) -> isize {
Expression::UnaryOp { .. } => 1,
Expression::ImageReference { .. } => 1,
Expression::Condition { .. } => 10,
Expression::Array { .. } => ALLOC_COST,
// Never inline an array because it is a model and when shared it needs to keep its identity
// (cf #5249) (otherwise it would be `ALLOC_COST`)
Expression::Array { .. } => return isize::MAX,
Expression::Struct { .. } => 1,
Expression::EasingCurve(_) => 1,
Expression::LinearGradient { .. } => ALLOC_COST,
Expand Down
61 changes: 61 additions & 0 deletions tests/cases/models/indirect_model_changes.slint
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

component IndirectChange {
in property <[int]> mod;
property <[int]> private: mod;

init => {
private[0] += 1;
}
}

export component TestCase {

property <[int]> m1: [5];
property <[int]> m2: [8];
property <[int]> indirect: m2;

public function up() {
indirect[0] += 10;
}

callback up2();
up2 => {up()}

IndirectChange { mod: m1; }

out property <int> t1: m1[0];
out property <int> t2: m2[0];

out property <bool> test: t1 == 5+1 && t2 == 8;
}

/*
```cpp
auto handle = TestCase::create();
const TestCase &instance = *handle;
assert_eq(instance.get_t1(), 6);
assert(instance.get_test());
instance.invoke_up();
assert_eq(instance.get_t2(), 18);
```
```rust
let instance = TestCase::new().unwrap();
assert_eq!(instance.get_t1(), 6);
assert!(instance.get_test());
instance.invoke_up();
assert_eq!(instance.get_t2(), 18);
```
```js
var instance = new slint.TestCase({});
assert.equal(instance.t1, 6);
assert(instance.test);
instance.up2();
assert.equal(instance.t2, 18);
```
*/

0 comments on commit 94af4d2

Please sign in to comment.