Skip to content

Commit 161ee6c

Browse files
committed
Unified: Use isolated interpolation contexts
This allows us to get rid of two uses of `ctx.scoped`, cleaning up the code a bit in the process.
1 parent b672a33 commit 161ee6c

1 file changed

Lines changed: 11 additions & 24 deletions

File tree

  • unified/extractor/src/languages/swift

‎unified/extractor/src/languages/swift/swift.rs‎

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use codeql_extractor::extractor::desugaring;
2-
use yeast::{ConcreteDesugarer, DesugaringConfig, Rule, tree};
2+
use yeast::{tree, ConcreteDesugarer, DesugaringConfig, Rule};
33

44
/// User context propagated from outer rules down to the inner rules that
55
/// emit the corresponding output declarations, so that each emitted node
@@ -51,10 +51,9 @@ impl SwiftContext {
5151
/// Called before recursively translating a body / initializer
5252
/// slot. Most rules mutate `ctx` in place — the framework invokes
5353
/// each rule with a private clone of the user context, so
54-
/// mutations are discarded on rule exit anyway. Rules that need
55-
/// the outer context intact *after* the reset-and-translate (see
56-
/// e.g. the `property_binding` willSet/didSet rule) wrap the
57-
/// mutation in `ctx.scoped(...)` instead.
54+
/// mutations are discarded on rule exit anyway. Output-template
55+
/// interpolations additionally restore the context after evaluation,
56+
/// preventing mutations from leaking into sibling fields.
5857
fn reset(&mut self) {
5958
*self = SwiftContext::default();
6059
}
@@ -382,17 +381,13 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
382381
let mut result = Vec::new();
383382
if let Some(val) = val {
384383
// Stored property with observers: the initializer is not part
385-
// of the binding, so translate it in a reset scope.
386-
let val = ctx.scoped(|ctx| {
387-
ctx.reset();
388-
ctx.translate(val)
389-
});
384+
// of the binding, so translate it in a reset interpolation.
390385
result.push(tree!(
391386
(variable_declaration
392387
modifier: {ctx.outer_modifiers.clone()}
393388
pattern: (identifier #{name})
394389
type: {ty}
395-
value: {val})
390+
value: {ctx.reset(); val})
396391
));
397392
}
398393
for acc in accessors.into_iter() {
@@ -747,19 +742,11 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
747742
trailingClosure: _? @trailing
748743
additionalTrailingClosures: _* @additional)
749744
=>
750-
expr {
751-
// Always translate the callee in non-pattern context.
752-
let callee = ctx.scoped(|ctx| {
753-
ctx.in_pattern = false;
754-
ctx.translate(rawCallee)
755-
});
756-
tree!((call_expr
757-
callee: {callee}
758-
argument: {args}
759-
argument: {trailing.map(|trailing| tree!((argument value: {trailing})))}
760-
argument: {additional}
761-
))
762-
}
745+
(call_expr
746+
callee: {ctx.in_pattern = false; rawCallee}
747+
argument: {args}
748+
argument: {trailing.map(|trailing| tree!((argument value: {trailing})))}
749+
argument: {additional})
763750
),
764751
// A call or enum-case pattern argument.
765752
rule!(

0 commit comments

Comments
 (0)