From 1a94cc8490699ef705664ddf19cc66a06428b8c7 Mon Sep 17 00:00:00 2001 From: cflmy Date: Thu, 22 Oct 2026 15:13:33 +0800 Subject: [PATCH] =?UTF-8?q?feat(check):=20M2.5=20=E9=81=AE=E8=94=BD?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E8=AF=8A=E6=96=AD=20=E2=80=94=20label.missin?= =?UTF-8?q?g(error)=20/=20label.empty=5Fpayload(warning)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 src/label.rs: - OcclusionSite { label, has_payload, line, unit } - collect(module):遍历函数/分支/循环/表达式收集遮蔽点(描述只读,不产生读引用) - check(module, path):产出两条诊断 两条诊断: - label.missing(error):[](...) 描述为空 —— 写一句人话,或改用不带遮蔽的写法 - label.empty_payload(warning):[描述]() 载荷为空 —— 写清桩返回什么(None/0),或补实现 warning 不影响退出码;JSON 出口自动继承既有 schema(含 doc_quote/span/suggestion)。 接入 check_module:walk 之后 extend(crate::label::check(..)),保持一行; M4(引用一致性)与 M9(检索)都往 label 模块加,check 不再膨胀。 测试: - 3 个集成(tests/occlusion.rs):空描述 exit 1、空载荷 exit 0、干净样例无诊断 - 全量 cargo test --release:578 passed / 0 failed(M2 后 575 + 3) 反向验证:干净样例必须零输出,证明不会误响。 实测: MARQDO_MARKUP=occlusion marqdo check empty-label.mq.md # 1 error, exit 1 MARQDO_MARKUP=occlusion marqdo check empty-payload.mq.md # 1 warning, exit 0 文档:doc/design/change-m25-label-diagnostics.md --- doc/design/change-m25-label-diagnostics.md | 91 +++++++++++ src/check.rs | 2 + src/label.rs | 169 +++++++++++++++++++++ src/lib.rs | 1 + tests/occlusion.rs | 40 +++++ tests/occlusion/clean.mq.md | 10 ++ tests/occlusion/empty-label.mq.md | 8 + tests/occlusion/empty-payload.mq.md | 8 + 8 files changed, 329 insertions(+) create mode 100644 doc/design/change-m25-label-diagnostics.md create mode 100644 src/label.rs create mode 100644 tests/occlusion/clean.mq.md create mode 100644 tests/occlusion/empty-label.mq.md create mode 100644 tests/occlusion/empty-payload.mq.md diff --git a/doc/design/change-m25-label-diagnostics.md b/doc/design/change-m25-label-diagnostics.md new file mode 100644 index 0000000..b7b6696 --- /dev/null +++ b/doc/design/change-m25-label-diagnostics.md @@ -0,0 +1,91 @@ +# 变更记录:M2.5 遮蔽标签诊断(`label.missing` / `label.empty_payload`) + +| | | +|---|---| +| 状态 | **Implemented** | +| 日期 | 2026-10-22 | +| 里程碑 | M2.5([occlusion-syntax.md](occlusion-syntax.md) §13,诊断规范 §10) | +| 改动文件 | **新增 `src/label.rs`** · `src/check.rs` · `src/lib.rs` · `tests/occlusion.rs` | +| 影响面 | `marqdo check`(新增两类诊断) | + +--- + +## 1. 两条诊断 + +| code | 级别 | 触发 | 文案要点 | +|------|------|------|---------| +| `label.missing` | **error** | `[](实现)` 描述为空 | 「给这段实现写一句人话,或改用不带遮蔽的写法」 | +| `label.empty_payload` | **warning** | `[描述]()` 载荷为空 | 「这里没有可执行内容」,建议写 `(None)` 或补实现 | + +实测: + +``` +$ MARQDO_MARKUP=occlusion marqdo check empty-label.mq.md +empty-label.mq.md:3:1: 遮蔽缺少描述:`[](…)` —— 给这段实现写一句人话,或改用不带遮蔽的写法 +contract check: 1 条诊断(1 error) # exit 1 + +$ MARQDO_MARKUP=occlusion marqdo check empty-payload.mq.md +empty-payload.mq.md:3:1: 空载荷遮蔽:`[某某功能开发中]()` —— 这里没有可执行内容 +contract check: 1 条诊断(0 error) # exit 0(warning 不影响退出码) +``` + +JSON 出口(自动继承既有 schema,含 `doc_quote`): + +```json +{"code":"label.empty_payload","severity":"warning","span":{"line":3,"col":1}, + "doc_quote":"某某功能开发中","message":"空载荷遮蔽:…","suggestion":"写清桩的返回值(如 `(None)` 或 `(0)`)…"} +``` + +--- + +## 2. 实现 + +### 2.1 新增 `src/label.rs` + +- `OcclusionSite { label, has_payload, line, unit }` +- `collect(module)`:遍历函数体与嵌套语句/表达式,收集所有遮蔽点 + (描述只读,**不产生任何读引用**) +- `check(module, path)`:产出上面两条诊断 + +### 2.2 接入 `check_module` + +```rust +walk(module, &module.functions, path, &known, &mut out); +out.extend(crate::label::check(module, path)); // M2.5 +``` + +设计上把"标签诊断"独立成模块,M4(引用一致性)与 M9(检索)都往这里加, +`check` 只做一行汇总。 + +--- + +## 3. 测试 + +| 层 | 内容 | 结果 | +|----|------|------| +| 集成(3 新) | 空描述 → exit 1 + 文案;空载荷 → exit 0 + 文案;干净样例 → 无诊断 | ✅ | +| 全量 | `cargo test --release` | **578 passed / 0 failed**(M2 后 575 + 3) | + +反向验证:三条断言分别验证"会响"与"不会误响"(干净样例必须无输出)。 + +--- + +## 4. 边界 + +| 项 | 说明 | +|----|------| +| 描述内容不校验 | 写什么都可以;M4 才校验反引号引用 | +| 空载荷仅在 occlusion 模式可能出现 | classic 模式下 `()` 空是语法错 | +| warning 不影响退出码 | 与既有 `Severity` 语义一致 | + +--- + +## 5. 变更清单 + +| 文件 | 变更 | +|------|------| +| `src/label.rs` | **新增**:遮蔽点收集 + 两类诊断 | +| `src/lib.rs` | `pub mod label;` | +| `src/check.rs` | `check_module` 追加 label 诊断 | +| `tests/occlusion.rs` + `tests/occlusion/{empty-label,empty-payload,clean}.mq.md` | 新增 3 个集成测试与样例 | +| `doc/design/change-m25-label-diagnostics.md` | 本文件 | diff --git a/src/check.rs b/src/check.rs index 7cf6199..eed5084 100644 --- a/src/check.rs +++ b/src/check.rs @@ -32,6 +32,8 @@ pub fn check_module(module: &Module, path: Option<&Path>) -> Vec { let known = known_type_names(module); let mut out = Vec::new(); walk(module, &module.functions, path, &known, &mut out); + // `` M2.5: occlusion labels (`label.missing` / `label.empty_payload`). + out.extend(crate::label::check(module, path)); out } diff --git a/src/label.rs b/src/label.rs new file mode 100644 index 0000000..a8b8e54 --- /dev/null +++ b/src/label.rs @@ -0,0 +1,169 @@ +//! `` M2.5/M4: occlusion-label collection and diagnostics. +//! +//! A label is **inert prose**: it never runs, never binds, never types. This +//! module only *reads* it — to report structural problems (`label.missing`, +//! `label.empty_payload`) and, later, to check staleness (`label.ref_dangling`). +//! +//! Design: [occlusion-description-and-search.md](../../doc/design/occlusion-description-and-search.md) + +use crate::ast::{Arg, BranchArm, Expr, Function, Module, Stmt}; +use crate::diagnostics::{Diagnostic, Severity, Span}; + +/// One `[描述](实现)` site found in the module. +#[derive(Debug, Clone)] +pub struct OcclusionSite { + /// Raw label text (already trimmed by the parser). + pub label: String, + /// `false` when written as `[描述]()` — the empty-payload stub. + pub has_payload: bool, + /// 1-based source line of the enclosing statement. + pub line: u32, + /// Enclosing function/object name (`# main` → `main`). + pub unit: String, +} + +/// Collect every occlusion in `module` (declaration order). +pub fn collect(module: &Module) -> Vec { + let mut out = Vec::new(); + for fun in &module.functions { + walk_fun(fun, &mut out); + } + out +} + +fn walk_fun(fun: &Function, out: &mut Vec) { + for stmt in &fun.body { + walk_stmt(stmt, &fun.name, out); + } + for child in &fun.children { + walk_fun(child, out); + } +} + +fn walk_stmt(stmt: &Stmt, unit: &str, out: &mut Vec) { + match stmt { + Stmt::Assign { value, span, .. } + | Stmt::Return { value, span } + | Stmt::Expr { value, span } => walk_expr(value, span.line, unit, out), + Stmt::Call { call, span } => { + for a in &call.args { + match a { + Arg::Positional(e) | Arg::Named { value: e, .. } => { + walk_expr(e, span.line, unit, out) + } + } + } + } + Stmt::Branch { arms, span } => { + for arm in arms { + walk_arm(arm, span.line, unit, out); + } + } + Stmt::While { condition, body, span } => { + walk_expr(condition, span.line, unit, out); + for s in body { + walk_stmt(s, unit, out); + } + } + Stmt::ForEach { body, span, .. } => { + for s in body { + walk_stmt(s, unit, out); + } + let _ = span; + } + } +} + +fn walk_arm(arm: &BranchArm, line: u32, unit: &str, out: &mut Vec) { + // `condition: Option` for the `*` else-arm. + if let Some(cond) = &arm.condition { + walk_expr(cond, line, unit, out); + } + for s in &arm.body { + walk_stmt(s, unit, out); + } +} + +fn walk_expr(expr: &Expr, line: u32, unit: &str, out: &mut Vec) { + match expr { + Expr::Occluded { label, payload } => { + out.push(OcclusionSite { + label: label.clone(), + has_payload: payload.is_some(), + line, + unit: unit.to_string(), + }); + if let Some(inner) = payload { + walk_expr(inner, line, unit, out); + } + } + Expr::Unary { expr, .. } => walk_expr(expr, line, unit, out), + Expr::Binary { left, right, .. } => { + walk_expr(left, line, unit, out); + walk_expr(right, line, unit, out); + } + Expr::Call(call) => { + for a in &call.args { + match a { + Arg::Positional(e) | Arg::Named { value: e, .. } => walk_expr(e, line, unit, out), + } + } + } + Expr::List(items) => { + for e in items { + walk_expr(e, line, unit, out); + } + } + Expr::Map(pairs) => { + for (_, e) in pairs { + walk_expr(e, line, unit, out); + } + } + Expr::Index { base, .. } => walk_expr(base, line, unit, out), + // Interpolation carries only variable *names* (strings), never nested + // expressions, so there is nothing to walk into. + Expr::Interp(_) => {} + Expr::Literal(_) | Expr::Var(_) | Expr::Formula(_) | Expr::Code(_) => {} + } +} + +/// Diagnostics for label structure. `` M2.5 +/// +/// - empty label → `label.missing` (**error**): you wrote a shape without prose +/// - empty payload → `label.empty_payload` (**warning**): say what the stub returns +pub fn check(module: &Module, path: Option<&std::path::Path>) -> Vec { + let mut out = Vec::new(); + for site in collect(module) { + let span = Span::new(site.line, 1); + if site.label.is_empty() { + out.push( + Diagnostic::new( + path, + span, + format!( + "遮蔽缺少描述:`[]({})` —— 给这段实现写一句人话,或改用不带遮蔽的写法", + if site.has_payload { "…" } else { "" } + ), + ) + .with_code("label.missing") + .with_severity(Severity::Error) + .with_suggestion("写成 `[这段实现做什么](实现)`;描述是自由文本,不参与执行"), + ); + continue; + } + if !site.has_payload { + out.push( + Diagnostic::new( + path, + span, + format!("空载荷遮蔽:`[{}]()` —— 这里没有可执行内容", site.label), + ) + .with_code("label.empty_payload") + .with_severity(Severity::Warning) + .with_doc_quote(site.label.clone()) + .with_suggestion("写清桩的返回值(如 `(None)` 或 `(0)`),或补上真实实现"), + ); + } + } + out +} diff --git a/src/lib.rs b/src/lib.rs index 04a929f..1ec82b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ pub mod aliases; pub mod artifact; +pub mod label; pub mod ast; pub mod binding; pub mod browser_session; diff --git a/tests/occlusion.rs b/tests/occlusion.rs index 6512a77..85324e0 100644 --- a/tests/occlusion.rs +++ b/tests/occlusion.rs @@ -43,3 +43,43 @@ fn occlusion_mode_rejects_legacy_index_shape() { assert_eq!(code, 0, "stderr={stderr}"); assert_ne!(stdout.trim_end(), "值", "occlusion mode must not index"); } + +// ---- M2.5: label diagnostics (`label.missing` / `label.empty_payload`) ---- + +fn check(path: &str, markup: &str) -> (i32, String) { + let out = Command::new(env!("CARGO_BIN_EXE_marqdo")) + .args(["check", path]) + .env("MARQDO_MARKUP", markup) + .output() + .expect("run marqdo check"); + ( + out.status.code().unwrap_or(1), + format!( + "{}{}", + String::from_utf8_lossy(&out.stdout), + String::from_utf8_lossy(&out.stderr) + ), + ) +} + +#[test] +fn label_missing_is_an_error() { + let (code, text) = check("tests/occlusion/empty-label.mq.md", "occlusion"); + assert_eq!(code, 1, "text={text}"); + assert!(text.contains("遮蔽缺少描述"), "text={text}"); +} + +#[test] +fn label_empty_payload_is_a_warning() { + let (code, text) = check("tests/occlusion/empty-payload.mq.md", "occlusion"); + assert_eq!(code, 0, "warnings must not fail the build: text={text}"); + assert!(text.contains("空载荷遮蔽"), "text={text}"); +} + +#[test] +fn label_ok_has_no_diagnostics() { + let (code, text) = check("tests/occlusion/clean.mq.md", "occlusion"); + assert_eq!(code, 0, "text={text}"); + assert!(!text.contains("空载荷"), "text={text}"); + assert!(!text.contains("缺少描述"), "text={text}"); +} diff --git a/tests/occlusion/clean.mq.md b/tests/occlusion/clean.mq.md new file mode 100644 index 0000000..0a739bd --- /dev/null +++ b/tests/occlusion/clean.mq.md @@ -0,0 +1,10 @@ +--- +title: clean occlusion +--- + +# main + +**a = [加一之后的值](1 + 1)** +**b = [外层]([内层](40 + 2))** +> print text=`a` +> print text=`b` diff --git a/tests/occlusion/empty-label.mq.md b/tests/occlusion/empty-label.mq.md new file mode 100644 index 0000000..750da3a --- /dev/null +++ b/tests/occlusion/empty-label.mq.md @@ -0,0 +1,8 @@ +--- +title: empty label +--- + +# main + +**a = [](1)** +> print text=`a` diff --git a/tests/occlusion/empty-payload.mq.md b/tests/occlusion/empty-payload.mq.md new file mode 100644 index 0000000..b950459 --- /dev/null +++ b/tests/occlusion/empty-payload.mq.md @@ -0,0 +1,8 @@ +--- +title: empty payload +--- + +# main + +**a = [某某功能开发中]()** +> print text="is-none=" + (`a` == None)