Commit 0217465
committed
yeast: Add BuildCtx::scoped for isolated context modification
A previous commit added a translate_reset method on BuildCtx, which had
the effect of performing a translation in a completely empty context.
One issue with this is that this is an all-or-nothing proposition -- If
you want to preserve _some_ parts of the context, you have to do
something more complicated. Moreover, if you introduce a contextual
value that _should_ be preserved, all of the existing uses of
translate_reset now silently do the wrong thing.
There are two patterns that we want to address. The first one is "modify
the context in some way, then do a translation". If the translation is
the last step of a Rust block, then we don't actually need
translate_reset -- we could just reset the context and then call
`translate`. The fact that the outer context is restored afterwards
means it's okay to make destructive changes to `ctx.user_ctx` -- none of
these changes will persist.
The second pattern is the same, but where we want to do more
translations using the original context, after having performed a
translation with a modified context. In this case, we cannot just
overwrite the context, since that would invalidate the subsequent
translations.
Instead, we introduce a new method `ctx.scoped` which takes a closure as
an argument. With this we can now write
```
ctx.scoped(|ctx| ctx.reset(); ctx.translate(...));
```
and the closure is run with a copy of `ctx` that has a clone of
`user_ctx` on the inside, so no changes will persist.
(You may wonder: why not just clone `ctx` and use the clone? The answer
is that `ctx` owns mutable pointers to the AST etc., and this makes it
awkward to just "clone" it. The closure circumvents this issue nicely,
since it can borrow these pointers internally.)
For now, this rewrite has the same behaviour as the version that used
translate_reset -- we clear the entire `user_ctx`. However, we could
imagine being more fine-grained in this approach, by implementing, say,
SwiftContext::reset_modifiers (which would only affect what modifiers
are currently in the context, leaving everything else as-is).1 parent 72ab57e commit 0217465
3 files changed
Lines changed: 120 additions & 53 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
169 | 173 | | |
170 | 174 | | |
171 | 175 | | |
172 | 176 | | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
178 | 189 | | |
| 190 | + | |
179 | 191 | | |
180 | 192 | | |
181 | | - | |
182 | | - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
183 | 199 | | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
193 | 218 | | |
194 | | - | |
| 219 | + | |
195 | 220 | | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
210 | 232 | | |
211 | 233 | | |
212 | 234 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
741 | 741 | | |
742 | 742 | | |
743 | 743 | | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
744 | 754 | | |
745 | 755 | | |
746 | 756 | | |
| |||
761 | 771 | | |
762 | 772 | | |
763 | 773 | | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
764 | 784 | | |
765 | 785 | | |
766 | 786 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
48 | | - | |
49 | | - | |
| 48 | + | |
| 49 | + | |
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
54 | 71 | | |
55 | 72 | | |
56 | 73 | | |
| |||
239 | 256 | | |
240 | 257 | | |
241 | 258 | | |
242 | | - | |
| 259 | + | |
243 | 260 | | |
244 | 261 | | |
245 | 262 | | |
| |||
260 | 277 | | |
261 | 278 | | |
262 | 279 | | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
269 | 294 | | |
270 | 295 | | |
271 | 296 | | |
| |||
295 | 320 | | |
296 | 321 | | |
297 | 322 | | |
298 | | - | |
| 323 | + | |
299 | 324 | | |
300 | 325 | | |
301 | 326 | | |
| |||
307 | 332 | | |
308 | 333 | | |
309 | 334 | | |
310 | | - | |
| 335 | + | |
311 | 336 | | |
312 | 337 | | |
313 | 338 | | |
| |||
1118 | 1143 | | |
1119 | 1144 | | |
1120 | 1145 | | |
1121 | | - | |
| 1146 | + | |
1122 | 1147 | | |
1123 | 1148 | | |
1124 | 1149 | | |
| |||
1131 | 1156 | | |
1132 | 1157 | | |
1133 | 1158 | | |
1134 | | - | |
| 1159 | + | |
1135 | 1160 | | |
1136 | 1161 | | |
1137 | 1162 | | |
| |||
1143 | 1168 | | |
1144 | 1169 | | |
1145 | 1170 | | |
1146 | | - | |
| 1171 | + | |
1147 | 1172 | | |
1148 | 1173 | | |
1149 | 1174 | | |
| |||
1155 | 1180 | | |
1156 | 1181 | | |
1157 | 1182 | | |
1158 | | - | |
| 1183 | + | |
1159 | 1184 | | |
1160 | 1185 | | |
1161 | 1186 | | |
| |||
1173 | 1198 | | |
1174 | 1199 | | |
1175 | 1200 | | |
1176 | | - | |
| 1201 | + | |
1177 | 1202 | | |
1178 | 1203 | | |
1179 | 1204 | | |
| |||
1184 | 1209 | | |
1185 | 1210 | | |
1186 | 1211 | | |
1187 | | - | |
| 1212 | + | |
1188 | 1213 | | |
1189 | 1214 | | |
1190 | 1215 | | |
| |||
0 commit comments