[fine] Bindings in if remain in scope!

This commit is contained in:
John Doty 2024-02-03 09:54:46 -08:00
parent 198dc5bdb3
commit f00b9e22e7
2 changed files with 34 additions and 3 deletions

View file

@ -433,6 +433,34 @@ fn set_logical_parents(
set_logical_parents(parents, syntax_tree, rhs, Some(t));
}
}
TreeKind::ConditionalExpression => {
// Special case! If the condition expression is a `is` expression
// then it is the parent for the body of the `then` clause so
// that any variable bindings it makes remain in scope. The
// `else` clause and the condition itself do not have the
// bindings in scope, obviously.
let body_parent = if let Some(is_condition) =
tree.child_of_kind(syntax_tree, TreeKind::IsExpression)
{
Some(is_condition)
} else {
Some(t)
};
let then_body = tree.nth_tree(2);
for child in &tree.children {
match child {
Child::Token(_) => (),
Child::Tree(ct) => {
if Some(*ct) == then_body {
set_logical_parents(parents, syntax_tree, *ct, body_parent);
} else {
set_logical_parents(parents, syntax_tree, *ct, Some(t));
}
}
}
}
}
_ => {
// By default, the parent for each child is current tree.
for child in &tree.children {

View file

@ -3,17 +3,20 @@ class Foo {
}
fun test() -> f64 {
let b = new Foo { a : 23 };
let b = new Foo { a : 1000 };
let result = 0;
if b is c:Foo and c.a == 23 {
if b is c:Foo and c.a == 1000 {
result = result + 1;
}
if b is c:Foo and c.a == 24 {
result = result + 10;
}
if b is c:Foo {
result = result + c.a; // c should still be in scope!
}
result
}
// @no-errors
// @eval: Float(1.0)
// @eval: Float(1001.0)