Commit 159a10d
committed
rustc: Tweak the borrow on closure invocations
This alters the borrow checker's requirements on invoking closures from
requiring an immutable borrow to requiring a unique immutable borrow. This means
that it is illegal to invoke a closure through a `&` pointer because there is no
guarantee that is not aliased. This does not mean that a closure is required to
be in a mutable location, but rather a location which can be proven to be
unique (often through a mutable pointer).
For example, the following code is unsound and is no longer allowed:
type Fn<'a> = ||:'a;
fn call(f: |Fn|) {
f(|| {
f(|| {})
});
}
fn main() {
call(|a| {
a();
});
}
There is no replacement for this pattern. For all closures which are stored in
structures, it was previously allowed to invoke the closure through `&self` but
it now requires invocation through `&mut self`.
The standard library has a good number of violations of this new rule, but the
fixes will be separated into multiple breaking change commits.
Closes #12224
[breaking-change]1 parent 1e33589 commit 159a10d
File tree
5 files changed
+125
-19
lines changed- src
- librustc/middle
- borrowck
- gather_loans
- typeck/check
- test/compile-fail
5 files changed
+125
-19
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
327 | 327 | | |
328 | 328 | | |
329 | 329 | | |
330 | | - | |
| 330 | + | |
331 | 331 | | |
332 | 332 | | |
333 | 333 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
292 | 292 | | |
293 | 293 | | |
294 | 294 | | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
295 | 315 | | |
296 | 316 | | |
297 | 317 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
202 | 202 | | |
203 | 203 | | |
204 | 204 | | |
| 205 | + | |
205 | 206 | | |
206 | 207 | | |
207 | 208 | | |
| |||
629 | 630 | | |
630 | 631 | | |
631 | 632 | | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
632 | 637 | | |
633 | 638 | | |
634 | 639 | | |
| |||
677 | 682 | | |
678 | 683 | | |
679 | 684 | | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
680 | 689 | | |
681 | 690 | | |
682 | 691 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
751 | 751 | | |
752 | 752 | | |
753 | 753 | | |
754 | | - | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
755 | 763 | | |
756 | 764 | | |
757 | 765 | | |
| |||
874 | 882 | | |
875 | 883 | | |
876 | 884 | | |
877 | | - | |
| 885 | + | |
| 886 | + | |
878 | 887 | | |
879 | 888 | | |
880 | 889 | | |
| |||
1092 | 1101 | | |
1093 | 1102 | | |
1094 | 1103 | | |
1095 | | - | |
| 1104 | + | |
| 1105 | + | |
1096 | 1106 | | |
1097 | 1107 | | |
1098 | 1108 | | |
| |||
1118 | 1128 | | |
1119 | 1129 | | |
1120 | 1130 | | |
1121 | | - | |
| 1131 | + | |
| 1132 | + | |
1122 | 1133 | | |
1123 | 1134 | | |
1124 | 1135 | | |
1125 | 1136 | | |
1126 | | - | |
| 1137 | + | |
| 1138 | + | |
1127 | 1139 | | |
1128 | 1140 | | |
1129 | 1141 | | |
1130 | 1142 | | |
1131 | | - | |
| 1143 | + | |
| 1144 | + | |
1132 | 1145 | | |
1133 | 1146 | | |
1134 | 1147 | | |
| |||
1150 | 1163 | | |
1151 | 1164 | | |
1152 | 1165 | | |
1153 | | - | |
| 1166 | + | |
1154 | 1167 | | |
1155 | 1168 | | |
1156 | 1169 | | |
| |||
1169 | 1182 | | |
1170 | 1183 | | |
1171 | 1184 | | |
1172 | | - | |
| 1185 | + | |
| 1186 | + | |
1173 | 1187 | | |
1174 | 1188 | | |
1175 | 1189 | | |
1176 | 1190 | | |
1177 | 1191 | | |
1178 | 1192 | | |
1179 | | - | |
| 1193 | + | |
1180 | 1194 | | |
1181 | 1195 | | |
1182 | 1196 | | |
1183 | | - | |
| 1197 | + | |
1184 | 1198 | | |
1185 | 1199 | | |
1186 | 1200 | | |
| |||
1190 | 1204 | | |
1191 | 1205 | | |
1192 | 1206 | | |
1193 | | - | |
| 1207 | + | |
1194 | 1208 | | |
1195 | | - | |
| 1209 | + | |
1196 | 1210 | | |
1197 | 1211 | | |
1198 | 1212 | | |
| |||
1214 | 1228 | | |
1215 | 1229 | | |
1216 | 1230 | | |
1217 | | - | |
| 1231 | + | |
1218 | 1232 | | |
1219 | 1233 | | |
1220 | 1234 | | |
| |||
1236 | 1250 | | |
1237 | 1251 | | |
1238 | 1252 | | |
1239 | | - | |
| 1253 | + | |
1240 | 1254 | | |
1241 | 1255 | | |
1242 | 1256 | | |
| |||
1428 | 1442 | | |
1429 | 1443 | | |
1430 | 1444 | | |
1431 | | - | |
| 1445 | + | |
1432 | 1446 | | |
1433 | | - | |
| 1447 | + | |
1434 | 1448 | | |
1435 | | - | |
1436 | | - | |
| 1449 | + | |
1437 | 1450 | | |
1438 | 1451 | | |
1439 | 1452 | | |
| |||
Lines changed: 64 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
0 commit comments