Skip to content

Commit ec33281

Browse files
EliasDerHailpil
authored andcommitted
fix guard expression polluting outer variable scope
1 parent 1e75b6d commit ec33281

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

compiler-core/src/javascript/decision.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -584,20 +584,24 @@ impl<'a> CasePrinter<'_, '_, 'a, '_> {
584584
.iter()
585585
.partition(|(variable, _)| guard_variables.contains(variable));
586586

587-
let check_bindings = self.variables.bindings_ref_doc(&check_bindings);
588-
let check = self.variables.expression_generator.guard(guard);
589-
let if_true = self.inside_new_scope(|this| {
587+
let (check_bindings, check, if_true) = self.inside_new_scope(|this| {
588+
// check_bindings and if_true generation have to be in this scope so that pattern-bound
589+
// variables used in guards don't leak into other case branches (if_false).
590+
let check_bindings = this.variables.bindings_ref_doc(&check_bindings);
591+
let check = this.variables.expression_generator.guard(guard);
590592
// All the other bindings that are not needed by the guard check will
591593
// end up directly in the body of the if clause.
592594
let if_true_bindings = this.variables.bindings_ref_doc(&if_true_bindings);
593595
let if_true_body = this.body_expression(if_true.clause_index);
594-
match if_true_body {
596+
let if_true = match if_true_body {
595597
BodyExpression::Variable(variable) => variable,
596598
BodyExpression::Expressions(if_true_body) => {
597599
join_with_line(if_true_bindings, if_true_body)
598600
}
599-
}
601+
};
602+
(check_bindings, check, if_true)
600603
});
604+
601605
let if_false_body = self.inside_new_scope(|this| this.decision(if_false));
602606

603607
// We can now piece everything together into a case body!

0 commit comments

Comments
 (0)