| 1 | |
| 2 | |
| 3 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
| 4 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 |
| 5 | |
| 6 | export component Foo { |
| 7 | property <int> prop; |
| 8 | callback c1; |
| 9 | pure callback c2; |
| 10 | |
| 11 | function f1() { |
| 12 | prop = 1; |
| 13 | } |
| 14 | function f2() -> int { 42 } |
| 15 | |
| 16 | pure function f3() { |
| 17 | f2(); |
| 18 | f1(); |
| 19 | // ^error{Call of impure function 'f1'} |
| 20 | |
| 21 | prop /= 5; |
| 22 | // ^error{Assignment in a pure context} |
| 23 | } |
| 24 | public function f4() {} |
| 25 | |
| 26 | |
| 27 | pure function f5() { |
| 28 | f1(); |
| 29 | // ^error{Call of impure function 'f1'} |
| 30 | f2(); // ok, private function auto-detected as pure |
| 31 | f3(); |
| 32 | f4(); |
| 33 | // ^error{Call of impure function 'f4'} |
| 34 | c1(); |
| 35 | // ^error{Call of impure callback 'c1'} |
| 36 | c2(); |
| 37 | } |
| 38 | |
| 39 | protected function f6() {} |
| 40 | |
| 41 | c1 => { f2() } |
| 42 | c2 => { |
| 43 | c1(); |
| 44 | // ^error{Call of impure callback 'c1'} |
| 45 | } |
| 46 | |
| 47 | |
| 48 | property <int> p1: f2(); |
| 49 | property <int> p2: { |
| 50 | p1 = 42; |
| 51 | // ^error{Assignment in a pure context} |
| 52 | 42 |
| 53 | }; |
| 54 | property <int> p3: { |
| 55 | pw.show(); |
| 56 | // ^error{Call of impure function} |
| 57 | fs.focus(); |
| 58 | // ^error{Call of impure function} |
| 59 | f6(); |
| 60 | // ^error{Call of impure function 'f6'} |
| 61 | 42 |
| 62 | }; |
| 63 | |
| 64 | pw := PopupWindow {} |
| 65 | fs := FocusScope {} |
| 66 | |
| 67 | callback model() -> [int]; |
| 68 | for xx in model() : Rectangle { |
| 69 | // ^error{Call of impure callback 'model'} |
| 70 | property <int> abc: xx; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
| 75 | export component Bar { |
| 76 | pure callback xc1 <=> f.c1; |
| 77 | // ^error{Purity of callbacks 'xc1' and 'f.c1' doesn't match} |
| 78 | callback xc2 <=> f.c2; |
| 79 | // ^error{Purity of callbacks 'xc2' and 'f.c2' doesn't match} |
| 80 | f := Foo { |
| 81 | c2 => { |
| 82 | self.c1(); |
| 83 | // ^error{Call of impure callback 'c1'} |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | export Foo_Legacy := Rectangle { |
| 93 | property <int> prop; |
| 94 | callback c1; |
| 95 | pure callback c2; |
| 96 | |
| 97 | function f1() { |
| 98 | prop = 1; |
| 99 | } |
| 100 | function f2() -> int { 42 } |
| 101 | |
| 102 | pure function f3() { |
| 103 | f2(); |
| 104 | f1(); |
| 105 | // ^warning{Call of impure function 'f1'} |
| 106 | |
| 107 | prop /= 5; |
| 108 | // ^warning{Assignment in a pure context} |
| 109 | } |
| 110 | public function f4() {} |
| 111 | |
| 112 | |
| 113 | pure function f5() { |
| 114 | f1(); |
| 115 | // ^warning{Call of impure function 'f1'} |
| 116 | f2(); // ok, private function auto-detected as pure |
| 117 | f3(); |
| 118 | f4(); |
| 119 | // ^warning{Call of impure function 'f4'} |
| 120 | c1(); |
| 121 | // ^warning{Call of impure callback 'c1'} |
| 122 | c2(); |
| 123 | } |
| 124 | |
| 125 | c1 => { f2() } |
| 126 | c2 => { |
| 127 | c1(); |
| 128 | // ^warning{Call of impure callback 'c1'} |
| 129 | } |
| 130 | |
| 131 | |
| 132 | property <int> p1: f2(); |
| 133 | property <int> p2: { |
| 134 | p1 = 42; |
| 135 | // ^warning{Assignment in a pure context} |
| 136 | 42 |
| 137 | }; |
| 138 | property <int> p3: { |
| 139 | pw.show(); |
| 140 | // ^warning{Call of impure function} |
| 141 | fs.focus(); |
| 142 | // ^warning{Call of impure function} |
| 143 | 42 |
| 144 | }; |
| 145 | |
| 146 | pw := PopupWindow {} |
| 147 | fs := FocusScope {} |
| 148 | } |
| 149 | |
| 150 | |
| 151 | export Bar_Legacy := Rectangle { |
| 152 | pure callback xc1 <=> f.c1; |
| 153 | // ^error{Purity of callbacks 'xc1' and 'f.c1' doesn't match} |
| 154 | callback xc2 <=> f.c2; |
| 155 | // ^error{Purity of callbacks 'xc2' and 'f.c2' doesn't match} |
| 156 | f := Foo { |
| 157 | c2 => { |
| 158 | self.c1(); |
| 159 | // ^warning{Call of impure callback 'c1'} |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |