feat(check): M2.5 遮蔽标签诊断 — label.missing(error) / label.empty_payload(warning)

新增 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
This commit is contained in:
cflmy committed 2026-10-22 15:13:33 +08:00
1 parent 11417c42e0
commit 1a94cc8490
8 files changed
+329

No files matched your search

+2
View File
@@ -32,6 +32,8 @@ pub fn check_module(module: &Module, path: Option<&Path>) -> Vec<Diagnostic> {
let known = known_type_names(module);
let mut out = Vec::new();
walk(module, &module.functions, path, &known, &mut out);
// `<TAB>` M2.5: occlusion labels (`label.missing` / `label.empty_payload`).
out.extend(crate::label::check(module, path));
out
}
+169
View File
@@ -0,0 +1,169 @@
//! `<TAB>` 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<OcclusionSite> {
let mut out = Vec::new();
for fun in &module.functions {
walk_fun(fun, &mut out);
}
out
}
fn walk_fun(fun: &Function, out: &mut Vec<OcclusionSite>) {
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<OcclusionSite>) {
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<OcclusionSite>) {
// `condition: Option<Expr>` 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<OcclusionSite>) {
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. `<TAB>` 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<Diagnostic> {
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
}
+1
View File
@@ -5,6 +5,7 @@
pub mod aliases;
pub mod artifact;
pub mod label;
pub mod ast;
pub mod binding;
pub mod browser_session;