From deac53d0f51160b02346644145af615c09479f01 Mon Sep 17 00:00:00 2001 From: cflmy Date: Thu, 22 Oct 2026 15:44:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor(view):=20AST=20=E9=81=8D=E5=8E=86?= =?UTF-8?q?=E5=99=A8=E5=90=88=E6=B5=81=20=E2=80=94=20walk=5Fstmts=20/=20st?= =?UTF-8?q?mt=5Fline=20=E6=94=B6=E8=BF=9B=20ast?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 三处各自实现的「深度优先遍历函数体」合并为一个: - ast::walk_stmts(fun, &mut |stmt| ...):body + 分支臂 + 循环体 + 子函数 - ast::stmt_line(stmt):语句行号(原 view::document 私有副本) 改造:view/annot.rs::fun_stmt_lines、view/document.rs::index_stmt_lines (annot 的 collect_name_sites 保留自己的收集逻辑,只借遍历) cargo build 零警告;cargo test --release 609 passed / 0 failed。 view/annot.rs -30 行、view/document.rs -41 行。 --- src/ast.rs | 45 ++++++++++++++++++++++++++++++++++++++++++++ src/view/annot.rs | 36 +++-------------------------------- src/view/document.rs | 43 +++--------------------------------------- 3 files changed, 51 insertions(+), 73 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index be9378d..47dbbc2 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -80,6 +80,51 @@ impl Function { } } +/// Source line of a statement (every variant carries a span). +pub fn stmt_line(st: &Stmt) -> u32 { + match st { + Stmt::Assign { span, .. } + | Stmt::Return { span, .. } + | Stmt::Call { span, .. } + | Stmt::Expr { span, .. } + | Stmt::Branch { span, .. } + | Stmt::While { span, .. } + | Stmt::ForEach { span, .. } => span.line, + } +} + +/// Depth-first walk over every statement of a function: its body, nested block +/// bodies (branch arms / loops) and child functions. +/// +/// `` slimming: three view modules each re-implemented this recursion; the +/// traversal lives here so collectors only supply the per-statement logic. +pub fn walk_stmts(fun: &Function, f: &mut impl FnMut(&Stmt)) { + fn go(st: &Stmt, f: &mut impl FnMut(&Stmt)) { + f(st); + match st { + Stmt::Branch { arms, .. } => { + for arm in arms { + for b in &arm.body { + go(b, f); + } + } + } + Stmt::While { body, .. } | Stmt::ForEach { body, .. } => { + for b in body { + go(b, f); + } + } + _ => {} + } + } + for st in &fun.body { + go(st, f); + } + for c in &fun.children { + walk_stmts(c, f); + } +} + #[derive(Debug, Clone)] pub enum Stmt { Assign { diff --git a/src/view/annot.rs b/src/view/annot.rs index 8cc15cb..2f0d2be 100644 --- a/src/view/annot.rs +++ b/src/view/annot.rs @@ -566,39 +566,9 @@ fn find_fun_by_line(module: &Module, line: u32) -> Option<&Function> { fn fun_stmt_lines(fun: &Function) -> std::collections::HashSet { let mut s = std::collections::HashSet::new(); - fn walk_stmt(st: &Stmt, s: &mut std::collections::HashSet) { - let line = match st { - Stmt::Assign { span, .. } - | Stmt::Return { span, .. } - | Stmt::Call { span, .. } - | Stmt::Expr { span, .. } - | Stmt::Branch { span, .. } - | Stmt::While { span, .. } - | Stmt::ForEach { span, .. } => span.line, - }; - s.insert(line); - match st { - Stmt::Branch { arms, .. } => { - for arm in arms { - for b in &arm.body { - walk_stmt(b, s); - } - } - } - Stmt::While { body, .. } | Stmt::ForEach { body, .. } => { - for b in body { - walk_stmt(b, s); - } - } - _ => {} - } - } - for st in &fun.body { - walk_stmt(st, &mut s); - } - for c in &fun.children { - s.extend(fun_stmt_lines(c)); - } + crate::ast::walk_stmts(fun, &mut |st| { + s.insert(crate::ast::stmt_line(st)); + }); s } diff --git a/src/view/document.rs b/src/view/document.rs index 96c225d..f65da86 100644 --- a/src/view/document.rs +++ b/src/view/document.rs @@ -983,51 +983,14 @@ fn index_functions(module: &Module) -> HashMap { fn index_stmt_lines(module: &Module) -> HashSet { let mut s = HashSet::new(); - fn walk(fun: &Function, s: &mut HashSet) { - for st in &fun.body { - s.insert(stmt_line(st)); - walk_stmt(st, s); - } - for c in &fun.children { - walk(c, s); - } - } - fn walk_stmt(st: &Stmt, s: &mut HashSet) { - match st { - Stmt::Branch { arms, .. } => { - for arm in arms { - for b in &arm.body { - s.insert(stmt_line(b)); - walk_stmt(b, s); - } - } - } - Stmt::While { body, .. } | Stmt::ForEach { body, .. } => { - for b in body { - s.insert(stmt_line(b)); - walk_stmt(b, s); - } - } - _ => {} - } - } for f in &module.functions { - walk(f, &mut s); + crate::ast::walk_stmts(f, &mut |st| { + s.insert(crate::ast::stmt_line(st)); + }); } s } -fn stmt_line(st: &Stmt) -> u32 { - match st { - Stmt::Assign { span, .. } - | Stmt::Return { span, .. } - | Stmt::Call { span, .. } - | Stmt::Expr { span, .. } - | Stmt::Branch { span, .. } - | Stmt::While { span, .. } - | Stmt::ForEach { span, .. } => span.line, - } -} fn index_special_assigns(module: &Module, source: &str) -> HashMap { let src_lines: Vec<&str> = source.lines().collect();