refactor(view): AST 遍历器合流 — walk_stmts / stmt_line 收进 ast

三处各自实现的「深度优先遍历函数体」合并为一个:
- 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 行。
This commit is contained in:
cflmy committed 2026-10-22 15:44:20 +08:00
1 parent 932403237a
commit deac53d0f5
3 files changed
+51 -73

No files matched your search

+45
View File
@@ -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.
///
/// `<TAB>` 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 {