feat(contract): Phase 2 渐进式契约层 —— 契约表提取 + 四边界校验 + marqdo check 防漂移(T2.1–T2.5)

- src/contract.rs: 类型词汇(渐进标注)、参数/返回/字段三形状契约表解析、合并、四边界校验诊断
- src/parse/mod.rs: extract_function_contract —— 位置+形状双重判定,在升参推断之前提取;
  绑定表/普通文档表绝不吃(P1 零回归),契约表从 body 移除(元数据永不执行)
- src/interp.rs: 四边界校验 —— 调用实参(3 条调用路径)/返回/表绑定/字段访问
- src/check.rs + marqdo check [--json]: 漂移双向互查、未知类型名(附建议)、错位契约
- src/load.rs: label_error 保留结构化诊断(错误出口唯一,不拍平成字符串)
- T2.4: 全部契约诊断带 doc_anchor/doc_quote/contract_ref
- tests/contracts.rs 12 例全绿;全量回归零失败(含 262 gold)

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-22 19:40:08 +08:00
1 parent 97fda7aa10
commit 7a449c40b1
17 files changed
+1586 -6

No files matched your search

+39 -2
View File
@@ -70,6 +70,16 @@ enum Commands {
#[arg(long)]
json: bool,
},
/// Static contract cross-check (渐进式契约): drift, unknown types, misplaced tables
Check {
/// Program path (.mq.md)
#[arg(value_name = "FILE")]
file: PathBuf,
/// Emit one JSON diagnostic per line on stdout (machine-readable; AI/MLSP 面)
#[arg(long)]
json: bool,
},
/// Browse `.mq.md` structure + execution (live server or static output)
View {
#[command(subcommand)]
@@ -179,14 +189,19 @@ enum ViewAction {
fn main() -> ExitCode {
let cli = Cli::parse();
let json_errors = matches!(&cli.command, Commands::Run { json: true, .. });
let json_errors = matches!(
&cli.command,
Commands::Run { json: true, .. } | Commands::Check { json: true, .. }
);
match try_main(cli) {
Ok(code) => ExitCode::from(code as u8),
Err(err) => {
if json_errors {
let diag = match err.downcast::<Diagnostic>() {
Ok(d) => d,
Err(e) => Diagnostic::new(None, Span::new(0, 0), format!("{e:#}")),
Err(e) => Diagnostic::find(&e).cloned().unwrap_or_else(|| {
Diagnostic::new(None, Span::new(0, 0), format!("{e:#}"))
}),
};
eprintln!("{}", diag.to_json());
} else {
@@ -244,6 +259,28 @@ fn try_main(cli: Cli) -> Result<i32> {
marqdo::run_file(&path, &opts)?;
Ok(0)
}
Commands::Check { file, json } => {
let diags = marqdo::check::check_path(&file)?;
let mut errors = 0i32;
for d in &diags {
if matches!(d.severity, marqdo::diagnostics::Severity::Error) {
errors += 1;
}
if json {
println!("{}", d.to_json());
} else {
println!("{}", d.format_message());
}
}
if !json {
if diags.is_empty() {
println!("contract check OK: 无契约问题");
} else {
println!("contract check: {} 条诊断({} error)", diags.len(), errors);
}
}
Ok(errors.min(1))
}
Commands::View {
action,
path,