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 {
+3 -33
View File
@@ -566,39 +566,9 @@ fn find_fun_by_line(module: &Module, line: u32) -> Option<&Function> {
fn fun_stmt_lines(fun: &Function) -> std::collections::HashSet<u32> {
let mut s = std::collections::HashSet::new();
fn walk_stmt(st: &Stmt, s: &mut std::collections::HashSet<u32>) {
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
}
+3 -40
View File
@@ -983,51 +983,14 @@ fn index_functions(module: &Module) -> HashMap<u32, &Function> {
fn index_stmt_lines(module: &Module) -> HashSet<u32> {
let mut s = HashSet::new();
fn walk(fun: &Function, s: &mut HashSet<u32>) {
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<u32>) {
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<u32, SpecialAssign> {
let src_lines: Vec<&str> = source.lines().collect();