diff --git a/CHANGELOG.md b/CHANGELOG.md index 44c08e8..a07fb77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ ### Fixed +- **分支内共同局部变量**:if/else 各臂都赋值的局部变量不再被误提升为必选形参;修复 `# llm` 构造器无端要求 `key`,恢复 agent/llm/ext 全量测试。新增 `branch_else_common_local_is_not_a_param` 回归测试。 - **外联临时脚本路径**:入口目录为相对路径时,脚本参数现在使用绝对路径,避免 `tests/lib/tests/lib/...` 双拼接。 - **外联失败可观测**:解释器不存在时进入 `invoke` 记录,`run` 仍报错;view 显示 stderr / exit / ms。 diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 90e75f5..ec54f92 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -2002,6 +2002,8 @@ fn collect_names_needing_input( } } Stmt::Branch { arms, .. } => { + let has_else = arms.iter().any(|a| a.condition.is_none()); + let mut common: Option> = None; for a in arms { if let Some(c) = &a.condition { let mut reads = std::collections::HashSet::new(); @@ -2020,6 +2022,17 @@ fn collect_names_needing_input( loop_items, assign_decls, ); + if has_else { + common = Some(match common { + None => arm_bound, + Some(prev) => prev.intersection(&arm_bound).cloned().collect(), + }); + } + } + if let Some(c) = common { + for n in c { + bound.insert(n); + } } } Stmt::While { @@ -2574,6 +2587,37 @@ Candidates may include lexical `score`. other => panic!("expected code assign, got {other:?}"), } } + + #[test] + fn branch_else_common_local_is_not_a_param() { + let src = r#"# main + +## f + + `a`=None + +1. `a` + **k = a** +2. * + **k = None** + +| x | +|---| +| `k` | +"#; + let m = parse_source(src).unwrap(); + let f = m + .functions + .iter() + .flat_map(|outer| outer.children.iter()) + .find(|f| f.name == "f") + .unwrap(); + assert_eq!( + f.params.iter().map(|p| p.name.as_str()).collect::>(), + ["a"], + "{:?}", + f.params + ); + } } /// 核心表面构造注册表(T1.3)——`(id, Markdown 外形)`。