From 1c074499f38df88d9e2fe86792e036c58bdebeee Mon Sep 17 00:00:00 2001 From: Moritz Vetter Date: Sun, 13 Feb 2022 04:05:52 +0100 Subject: [PATCH 1/5] add some breaking tests (TDD - style) --- crates/ide/src/highlight_related.rs | 109 ++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/crates/ide/src/highlight_related.rs b/crates/ide/src/highlight_related.rs index b6d9e4021d..a445d713cd 100644 --- a/crates/ide/src/highlight_related.rs +++ b/crates/ide/src/highlight_related.rs @@ -804,6 +804,115 @@ fn foo() { ); } + #[test] + fn test_hl_break_for_but_not_continue() { + check( + r#" +fn foo() { + 'outer: for _ in () { + // ^^^^^^^^^^^ + break; + // ^^^^^ + continue; + 'inner: for _ in () { + break; + continue; + 'innermost: for _ in () { + continue 'outer; + break 'outer; + // ^^^^^^^^^^^^ + continue 'inner; + break 'inner; + } + break$0 'outer; + // ^^^^^^^^^^^^ + continue 'outer; + break; + continue; + } + break; + // ^^^^^ + continue; + } +} +"#, + ); + } + + #[test] + fn test_hl_continue_for_but_not_break() { + check( + r#" +fn foo() { + 'outer: for _ in () { + // ^^^^^^^^^^^ + break; + continue; + // ^^^^^^^^ + 'inner: for _ in () { + break; + continue; + 'innermost: for _ in () { + continue 'outer; + // ^^^^^^^^^^^^^^^ + break 'outer; + continue 'inner; + break 'inner; + } + break 'outer; + continue$0 'outer; + // ^^^^^^^^^^^^^^^ + break; + continue; + } + break; + continue; + // ^^^^^^^^ + } +} +"#, + ); + } + + #[test] + fn test_hl_break_and_continue() { + check( + r#" +fn foo() { + 'outer$0: for _ in () { + // ^^^^^^^^^^^ + break; + // ^^^^^ + continue; + // ^^^^^^^^ + 'inner: for _ in () { + break; + continue; + 'innermost: for _ in () { + continue 'outer; + // ^^^^^^^^^^^^^^^ + break 'outer; + // ^^^^^^^^^^^^ + continue 'inner; + break 'inner; + } + break 'outer; + // ^^^^^^^^^^^^ + continue 'outer; + // ^^^^^^^^^^^^^^^ + break; + continue; + } + break; + // ^^^^^ + continue; + // ^^^^^^^^ + } +} +"#, + ); + } + #[test] fn test_hl_break_while() { check( From cad0cf6950fb73da4d137e005110d9921e6c04be Mon Sep 17 00:00:00 2001 From: Moritz Vetter Date: Sun, 13 Feb 2022 12:04:51 +0100 Subject: [PATCH 2/5] refactor helper function to work with function taking expression enum instead of break expression --- crates/ide/src/highlight_related.rs | 13 ++++++++----- crates/ide_db/src/helpers.rs | 12 +++++++----- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/crates/ide/src/highlight_related.rs b/crates/ide/src/highlight_related.rs index a445d713cd..2f82ac9755 100644 --- a/crates/ide/src/highlight_related.rs +++ b/crates/ide/src/highlight_related.rs @@ -197,11 +197,14 @@ fn highlight_break_points(token: SyntaxToken) -> Option> { label.as_ref().map(|it| it.syntax().text_range()), ); highlights.extend(range.map(|range| HighlightedRange { category: None, range })); - for_each_break_expr(label, body, &mut |break_| { - let range = cover_range( - break_.break_token().map(|it| it.text_range()), - break_.lifetime().map(|it| it.syntax().text_range()), - ); + for_each_break_expr(label, body, &mut |expr| { + let range: Option = match expr { + ast::Expr::BreakExpr(break_) => cover_range( + break_.break_token().map(|it| it.text_range()), + break_.lifetime().map(|it| it.syntax().text_range()), + ), + _ => None, + }; highlights.extend(range.map(|range| HighlightedRange { category: None, range })); }); Some(highlights) diff --git a/crates/ide_db/src/helpers.rs b/crates/ide_db/src/helpers.rs index cbe4adf1b9..0fb6ca27b2 100644 --- a/crates/ide_db/src/helpers.rs +++ b/crates/ide_db/src/helpers.rs @@ -121,7 +121,7 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) { Some(ast::BlockModifier::Label(label)) => { for_each_break_expr(Some(label), b.stmt_list(), &mut |b| { - cb(&ast::Expr::BreakExpr(b)) + cb(&b) }); } Some(ast::BlockModifier::Unsafe(_)) => (), @@ -151,12 +151,14 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) { } ast::Expr::LoopExpr(l) => { for_each_break_expr(l.label(), l.loop_body().and_then(|it| it.stmt_list()), &mut |b| { - cb(&ast::Expr::BreakExpr(b)) + cb(&b) }) } ast::Expr::MatchExpr(m) => { if let Some(arms) = m.match_arm_list() { - arms.arms().filter_map(|arm| arm.expr()).for_each(|e| for_each_tail_expr(&e, cb)); + arms.arms().filter_map(|arm| arm.expr()).for_each(|e| for_each_tail_expr(&e, &mut |b| { + cb(&b) + })); } } ast::Expr::ArrayExpr(_) @@ -194,7 +196,7 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) { pub fn for_each_break_expr( label: Option, body: Option, - cb: &mut dyn FnMut(ast::BreakExpr), + cb: &mut dyn FnMut(ast::Expr), ) { let label = label.and_then(|lbl| lbl.lifetime()); let mut depth = 0; @@ -217,7 +219,7 @@ pub fn for_each_break_expr( ast::Expr::BreakExpr(b) if (depth == 0 && b.lifetime().is_none()) || eq_label(b.lifetime()) => { - cb(b); + cb(ast::Expr::BreakExpr(b)); } _ => (), }, From 3da08071ce13da084acdbb99272e6b85d7fd116d Mon Sep 17 00:00:00 2001 From: Moritz Vetter Date: Sun, 13 Feb 2022 12:48:04 +0100 Subject: [PATCH 3/5] add logic to highlight continue and break keywords according to expectations --- crates/ide/src/highlight_related.rs | 63 ++++++++++++++++++++--------- crates/ide_db/src/helpers.rs | 29 +++++++------ 2 files changed, 60 insertions(+), 32 deletions(-) diff --git a/crates/ide/src/highlight_related.rs b/crates/ide/src/highlight_related.rs index 2f82ac9755..f886ff7837 100644 --- a/crates/ide/src/highlight_related.rs +++ b/crates/ide/src/highlight_related.rs @@ -2,7 +2,9 @@ use hir::Semantics; use ide_db::{ base_db::{FileId, FilePosition}, defs::{Definition, IdentClass}, - helpers::{for_each_break_expr, for_each_tail_expr, node_ext::walk_expr, pick_best_token}, + helpers::{ + for_each_break_and_continue_expr, for_each_tail_expr, node_ext::walk_expr, pick_best_token, + }, search::{FileReference, ReferenceCategory, SearchScope}, RootDatabase, }; @@ -10,7 +12,7 @@ use rustc_hash::FxHashSet; use syntax::{ ast::{self, HasLoopBody}, match_ast, AstNode, - SyntaxKind::{IDENT, INT_NUMBER}, + SyntaxKind::{self, IDENT, INT_NUMBER}, SyntaxNode, SyntaxToken, TextRange, T, }; @@ -66,7 +68,9 @@ pub(crate) fn highlight_related( T![for] if config.break_points && token.parent().and_then(ast::ForExpr::cast).is_some() => { highlight_break_points(token) } - T![break] | T![loop] | T![while] if config.break_points => highlight_break_points(token), + T![break] | T![loop] | T![while] | T![continue] if config.break_points => { + highlight_break_points(token) + } _ if config.references => highlight_references(sema, &syntax, token, file_id), _ => None, } @@ -187,6 +191,7 @@ fn highlight_exit_points( fn highlight_break_points(token: SyntaxToken) -> Option> { fn hl( + cursor_token_kind: SyntaxKind, token: Option, label: Option, body: Option, @@ -197,11 +202,20 @@ fn highlight_break_points(token: SyntaxToken) -> Option> { label.as_ref().map(|it| it.syntax().text_range()), ); highlights.extend(range.map(|range| HighlightedRange { category: None, range })); - for_each_break_expr(label, body, &mut |expr| { - let range: Option = match expr { - ast::Expr::BreakExpr(break_) => cover_range( - break_.break_token().map(|it| it.text_range()), - break_.lifetime().map(|it| it.syntax().text_range()), + for_each_break_and_continue_expr(label, body, &mut |expr| { + let range: Option = match (cursor_token_kind, expr) { + (T![for] | T![while] | T![loop] | T![break], ast::Expr::BreakExpr(break_)) => { + cover_range( + break_.break_token().map(|it| it.text_range()), + break_.lifetime().map(|it| it.syntax().text_range()), + ) + } + ( + T![for] | T![while] | T![loop] | T![continue], + ast::Expr::ContinueExpr(continue_), + ) => cover_range( + continue_.continue_token().map(|it| it.text_range()), + continue_.lifetime().map(|it| it.syntax().text_range()), ), _ => None, }; @@ -213,6 +227,7 @@ fn highlight_break_points(token: SyntaxToken) -> Option> { let lbl = match_ast! { match parent { ast::BreakExpr(b) => b.lifetime(), + ast::ContinueExpr(c) => c.lifetime(), ast::LoopExpr(l) => l.label().and_then(|it| it.lifetime()), ast::ForExpr(f) => f.label().and_then(|it| it.lifetime()), ast::WhileExpr(w) => w.label().and_then(|it| it.lifetime()), @@ -227,19 +242,29 @@ fn highlight_break_points(token: SyntaxToken) -> Option> { } None => true, }; + let token_kind = token.kind(); for anc in token.ancestors().flat_map(ast::Expr::cast) { return match anc { - ast::Expr::LoopExpr(l) if label_matches(l.label()) => { - hl(l.loop_token(), l.label(), l.loop_body().and_then(|it| it.stmt_list())) - } - ast::Expr::ForExpr(f) if label_matches(f.label()) => { - hl(f.for_token(), f.label(), f.loop_body().and_then(|it| it.stmt_list())) - } - ast::Expr::WhileExpr(w) if label_matches(w.label()) => { - hl(w.while_token(), w.label(), w.loop_body().and_then(|it| it.stmt_list())) - } + ast::Expr::LoopExpr(l) if label_matches(l.label()) => hl( + token_kind, + l.loop_token(), + l.label(), + l.loop_body().and_then(|it| it.stmt_list()), + ), + ast::Expr::ForExpr(f) if label_matches(f.label()) => hl( + token_kind, + f.for_token(), + f.label(), + f.loop_body().and_then(|it| it.stmt_list()), + ), + ast::Expr::WhileExpr(w) if label_matches(w.label()) => hl( + token_kind, + w.while_token(), + w.label(), + w.loop_body().and_then(|it| it.stmt_list()), + ), ast::Expr::BlockExpr(e) if e.label().is_some() && label_matches(e.label()) => { - hl(None, e.label(), e.stmt_list()) + hl(token_kind, None, e.label(), e.stmt_list()) } _ => continue, }; @@ -882,7 +907,7 @@ fn foo() { check( r#" fn foo() { - 'outer$0: for _ in () { + 'outer: fo$0r _ in () { // ^^^^^^^^^^^ break; // ^^^^^ diff --git a/crates/ide_db/src/helpers.rs b/crates/ide_db/src/helpers.rs index 0fb6ca27b2..4c59629bc6 100644 --- a/crates/ide_db/src/helpers.rs +++ b/crates/ide_db/src/helpers.rs @@ -120,9 +120,7 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) { ) => return cb(expr), Some(ast::BlockModifier::Label(label)) => { - for_each_break_expr(Some(label), b.stmt_list(), &mut |b| { - cb(&b) - }); + for_each_break_and_continue_expr(Some(label), b.stmt_list(), &mut |b| cb(&b)); } Some(ast::BlockModifier::Unsafe(_)) => (), None => (), @@ -149,16 +147,16 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) { } } } - ast::Expr::LoopExpr(l) => { - for_each_break_expr(l.label(), l.loop_body().and_then(|it| it.stmt_list()), &mut |b| { - cb(&b) - }) - } + ast::Expr::LoopExpr(l) => for_each_break_and_continue_expr( + l.label(), + l.loop_body().and_then(|it| it.stmt_list()), + &mut |b| cb(&b), + ), ast::Expr::MatchExpr(m) => { if let Some(arms) = m.match_arm_list() { - arms.arms().filter_map(|arm| arm.expr()).for_each(|e| for_each_tail_expr(&e, &mut |b| { - cb(&b) - })); + arms.arms() + .filter_map(|arm| arm.expr()) + .for_each(|e| for_each_tail_expr(&e, &mut |b| cb(&b))); } } ast::Expr::ArrayExpr(_) @@ -192,8 +190,8 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) { } } -/// Calls `cb` on each break expr inside of `body` that is applicable for the given label. -pub fn for_each_break_expr( +/// Calls `cb` on each break expr and continue expr inside of `body` that is applicable for the given label. +pub fn for_each_break_and_continue_expr( label: Option, body: Option, cb: &mut dyn FnMut(ast::Expr), @@ -221,6 +219,11 @@ pub fn for_each_break_expr( { cb(ast::Expr::BreakExpr(b)); } + ast::Expr::ContinueExpr(c) + if (depth == 0 && c.lifetime().is_none()) || eq_label(c.lifetime()) => + { + cb(ast::Expr::ContinueExpr(c)) + } _ => (), }, WalkEvent::Leave(expr) => match expr { From 8848186213bb7f54f034c2f6f5fab724bfc3b451 Mon Sep 17 00:00:00 2001 From: Moritz Vetter Date: Thu, 24 Feb 2022 18:56:08 +0100 Subject: [PATCH 4/5] fix(11422): have two different funuctions - one for iterating breaks, one for iteraating breaks and continues --- crates/ide_db/src/helpers.rs | 146 ++++++++++++++++++++++++----------- crates/syntax/src/lib.rs | 7 +- 2 files changed, 104 insertions(+), 49 deletions(-) diff --git a/crates/ide_db/src/helpers.rs b/crates/ide_db/src/helpers.rs index 4c59629bc6..944b69c1ac 100644 --- a/crates/ide_db/src/helpers.rs +++ b/crates/ide_db/src/helpers.rs @@ -16,7 +16,8 @@ use hir::{ItemInNs, MacroDef, ModuleDef, Name, Semantics}; use itertools::Itertools; use syntax::{ ast::{self, make, HasLoopBody}, - AstNode, AstToken, SyntaxKind, SyntaxToken, TokenAtOffset, WalkEvent, T, + AstNode, AstToken, Preorder, RustLanguage, SyntaxKind, SyntaxToken, TokenAtOffset, WalkEvent, + T, }; use crate::{defs::Definition, RootDatabase}; @@ -120,7 +121,9 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) { ) => return cb(expr), Some(ast::BlockModifier::Label(label)) => { - for_each_break_and_continue_expr(Some(label), b.stmt_list(), &mut |b| cb(&b)); + for_each_break_expr(Some(label), b.stmt_list(), &mut |b| { + cb(&ast::Expr::BreakExpr(b)) + }); } Some(ast::BlockModifier::Unsafe(_)) => (), None => (), @@ -147,16 +150,14 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) { } } } - ast::Expr::LoopExpr(l) => for_each_break_and_continue_expr( - l.label(), - l.loop_body().and_then(|it| it.stmt_list()), - &mut |b| cb(&b), - ), + ast::Expr::LoopExpr(l) => { + for_each_break_expr(l.label(), l.loop_body().and_then(|it| it.stmt_list()), &mut |b| { + cb(&ast::Expr::BreakExpr(b)) + }) + } ast::Expr::MatchExpr(m) => { if let Some(arms) = m.match_arm_list() { - arms.arms() - .filter_map(|arm| arm.expr()) - .for_each(|e| for_each_tail_expr(&e, &mut |b| cb(&b))); + arms.arms().filter_map(|arm| arm.expr()).for_each(|e| for_each_tail_expr(&e, cb)); } } ast::Expr::ArrayExpr(_) @@ -190,54 +191,107 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) { } } -/// Calls `cb` on each break expr and continue expr inside of `body` that is applicable for the given label. pub fn for_each_break_and_continue_expr( label: Option, body: Option, cb: &mut dyn FnMut(ast::Expr), ) { let label = label.and_then(|lbl| lbl.lifetime()); - let mut depth = 0; if let Some(b) = body { - let preorder = &mut b.syntax().preorder(); - let ev_as_expr = |ev| match ev { - WalkEvent::Enter(it) => Some(WalkEvent::Enter(ast::Expr::cast(it)?)), - WalkEvent::Leave(it) => Some(WalkEvent::Leave(ast::Expr::cast(it)?)), - }; - let eq_label = |lt: Option| { - lt.zip(label.as_ref()).map_or(false, |(lt, lbl)| lt.text() == lbl.text()) - }; - while let Some(node) = preorder.find_map(ev_as_expr) { - match node { - WalkEvent::Enter(expr) => match expr { - ast::Expr::LoopExpr(_) | ast::Expr::WhileExpr(_) | ast::Expr::ForExpr(_) => { - depth += 1 - } - ast::Expr::BlockExpr(e) if e.label().is_some() => depth += 1, - ast::Expr::BreakExpr(b) - if (depth == 0 && b.lifetime().is_none()) || eq_label(b.lifetime()) => - { - cb(ast::Expr::BreakExpr(b)); - } - ast::Expr::ContinueExpr(c) - if (depth == 0 && c.lifetime().is_none()) || eq_label(c.lifetime()) => - { - cb(ast::Expr::ContinueExpr(c)) - } - _ => (), - }, - WalkEvent::Leave(expr) => match expr { - ast::Expr::LoopExpr(_) | ast::Expr::WhileExpr(_) | ast::Expr::ForExpr(_) => { - depth -= 1 - } - ast::Expr::BlockExpr(e) if e.label().is_some() => depth -= 1, - _ => (), - }, + let tree_depth_iterator = TreeWithDepthIterator::new(b); + for (expr, depth) in tree_depth_iterator { + match expr { + ast::Expr::BreakExpr(b) + if (depth == 0 && b.lifetime().is_none()) + || eq_label_lt(&label, &b.lifetime()) => + { + cb(ast::Expr::BreakExpr(b)); + } + ast::Expr::ContinueExpr(c) + if (depth == 0 && c.lifetime().is_none()) + || eq_label_lt(&label, &c.lifetime()) => + { + cb(ast::Expr::ContinueExpr(c)); + } + _ => (), } } } } +fn for_each_break_expr( + label: Option, + body: Option, + cb: &mut dyn FnMut(ast::BreakExpr), +) { + let label = label.and_then(|lbl| lbl.lifetime()); + if let Some(b) = body { + let tree_depth_iterator = TreeWithDepthIterator::new(b); + for (expr, depth) in tree_depth_iterator { + match expr { + ast::Expr::BreakExpr(b) + if (depth == 0 && b.lifetime().is_none()) + || eq_label_lt(&label, &b.lifetime()) => + { + cb(b); + } + _ => (), + } + } + } +} + +fn eq_label_lt(lt1: &Option, lt2: &Option) -> bool { + lt1.as_ref().zip(lt2.as_ref()).map_or(false, |(lt, lbl)| lt.text() == lbl.text()) +} + +struct TreeWithDepthIterator { + preorder: Preorder, + depth: i32, +} + +impl TreeWithDepthIterator { + fn new(body: ast::StmtList) -> Self { + let preorder = body.syntax().preorder(); + Self { preorder, depth: 0 } + } +} + +impl<'a> Iterator for TreeWithDepthIterator { + type Item = (ast::Expr, i32); + + fn next(&mut self) -> Option { + while let Some((event, expr)) = self.preorder.find_map(|ev| match ev { + WalkEvent::Enter(it) => Some(WalkEvent::Enter(())).zip(ast::Expr::cast(it)), + WalkEvent::Leave(it) => Some(WalkEvent::Leave(())).zip(ast::Expr::cast(it)), + }) { + match (event, expr) { + ( + WalkEvent::Enter(_), + ast::Expr::LoopExpr(_) | ast::Expr::WhileExpr(_) | ast::Expr::ForExpr(_), + ) => { + self.depth += 1; + } + ( + WalkEvent::Leave(_), + ast::Expr::LoopExpr(_) | ast::Expr::WhileExpr(_) | ast::Expr::ForExpr(_), + ) => { + self.depth -= 1; + } + (WalkEvent::Enter(_), ast::Expr::BlockExpr(e)) if e.label().is_some() => { + self.depth += 1; + } + (WalkEvent::Leave(_), ast::Expr::BlockExpr(e)) if e.label().is_some() => { + self.depth -= 1; + } + (WalkEvent::Enter(_), expr) => return Some((expr, self.depth)), + _ => (), + } + } + None + } +} + /// Checks if the given lint is equal or is contained by the other lint which may or may not be a group. pub fn lint_eq_or_in_group(lint: &str, lint_is: &str) -> bool { if lint == lint_is { diff --git a/crates/syntax/src/lib.rs b/crates/syntax/src/lib.rs index 0f7855a053..e30f6cd79c 100644 --- a/crates/syntax/src/lib.rs +++ b/crates/syntax/src/lib.rs @@ -52,14 +52,15 @@ pub use crate::{ ptr::{AstPtr, SyntaxNodePtr}, syntax_error::SyntaxError, syntax_node::{ - PreorderWithTokens, SyntaxElement, SyntaxElementChildren, SyntaxNode, SyntaxNodeChildren, - SyntaxToken, SyntaxTreeBuilder, + PreorderWithTokens, RustLanguage, SyntaxElement, SyntaxElementChildren, SyntaxNode, + SyntaxNodeChildren, SyntaxToken, SyntaxTreeBuilder, }, token_text::TokenText, }; pub use parser::{SyntaxKind, T}; pub use rowan::{ - Direction, GreenNode, NodeOrToken, SyntaxText, TextRange, TextSize, TokenAtOffset, WalkEvent, + api::Preorder, Direction, GreenNode, NodeOrToken, SyntaxText, TextRange, TextSize, + TokenAtOffset, WalkEvent, }; pub use smol_str::SmolStr; From 71d158b6bab007f56b9f80c56125063a62d06fd4 Mon Sep 17 00:00:00 2001 From: Moritz Vetter Date: Thu, 24 Feb 2022 21:29:26 +0100 Subject: [PATCH 5/5] refactor(11422): make number unsigned, nest enums into each other --- crates/ide_db/src/helpers.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/crates/ide_db/src/helpers.rs b/crates/ide_db/src/helpers.rs index 944b69c1ac..fcad172984 100644 --- a/crates/ide_db/src/helpers.rs +++ b/crates/ide_db/src/helpers.rs @@ -247,7 +247,7 @@ fn eq_label_lt(lt1: &Option, lt2: &Option) -> bool struct TreeWithDepthIterator { preorder: Preorder, - depth: i32, + depth: u32, } impl TreeWithDepthIterator { @@ -258,33 +258,31 @@ impl TreeWithDepthIterator { } impl<'a> Iterator for TreeWithDepthIterator { - type Item = (ast::Expr, i32); + type Item = (ast::Expr, u32); fn next(&mut self) -> Option { - while let Some((event, expr)) = self.preorder.find_map(|ev| match ev { - WalkEvent::Enter(it) => Some(WalkEvent::Enter(())).zip(ast::Expr::cast(it)), - WalkEvent::Leave(it) => Some(WalkEvent::Leave(())).zip(ast::Expr::cast(it)), + while let Some(event) = self.preorder.find_map(|ev| match ev { + WalkEvent::Enter(it) => ast::Expr::cast(it).map(WalkEvent::Enter), + WalkEvent::Leave(it) => ast::Expr::cast(it).map(WalkEvent::Leave), }) { - match (event, expr) { - ( - WalkEvent::Enter(_), + match event { + WalkEvent::Enter( ast::Expr::LoopExpr(_) | ast::Expr::WhileExpr(_) | ast::Expr::ForExpr(_), ) => { self.depth += 1; } - ( - WalkEvent::Leave(_), + WalkEvent::Leave( ast::Expr::LoopExpr(_) | ast::Expr::WhileExpr(_) | ast::Expr::ForExpr(_), ) => { self.depth -= 1; } - (WalkEvent::Enter(_), ast::Expr::BlockExpr(e)) if e.label().is_some() => { + WalkEvent::Enter(ast::Expr::BlockExpr(e)) if e.label().is_some() => { self.depth += 1; } - (WalkEvent::Leave(_), ast::Expr::BlockExpr(e)) if e.label().is_some() => { + WalkEvent::Leave(ast::Expr::BlockExpr(e)) if e.label().is_some() => { self.depth -= 1; } - (WalkEvent::Enter(_), expr) => return Some((expr, self.depth)), + WalkEvent::Enter(expr) => return Some((expr, self.depth)), _ => (), } }