From b3429cbeb3fbf4960a6e0d89dccae6190e5a8b32 Mon Sep 17 00:00:00 2001 From: cflmy Date: Sat, 21 Nov 2026 15:55:23 +0800 Subject: [PATCH] fix(frontend): emit diagnostics in the specified stable order MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit spec/mapping.md §8 requires diagnostics ordered by source position, then code, so IDE/CI diffs are stable. analyze_source appended diagnostics by pipeline stage (units, mapping, inference, call validation), so a later heading error could precede an earlier payload error. Sort the final list by (span.start, code) with span-less diagnostics last, and pin the order with a test that mixes an indexing-stage duplicate-heading error with an earlier mapping-stage syntax error. Verified: clippy -D warnings clean; cargo test --workspace --locked green. --- crates/marqdo-frontend/src/analyze.rs | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/crates/marqdo-frontend/src/analyze.rs b/crates/marqdo-frontend/src/analyze.rs index 4991322..ceed252 100644 --- a/crates/marqdo-frontend/src/analyze.rs +++ b/crates/marqdo-frontend/src/analyze.rs @@ -163,6 +163,19 @@ pub fn analyze_source(module_id: &str, source: &str) -> AnalysisResult { }; diagnostics.extend(calls::validate_module(&module)); + // One stable order for every consumer: source position first, then the + // stable code, as required by `spec/mapping.md` §8. Diagnostics without a + // precise span sort last. + diagnostics.sort_by(|left, right| { + let left_key = (left.span.map(|span| span.start), left.code.as_str()); + let right_key = (right.span.map(|span| span.start), right.code.as_str()); + left_key + .0 + .unwrap_or(usize::MAX) + .cmp(&right_key.0.unwrap_or(usize::MAX)) + .then_with(|| left_key.1.cmp(right_key.1)) + }); + AnalysisResult { module, document, @@ -237,6 +250,26 @@ mod tests { assert!(matches!(function.body[1], marqdo_ir::Stmt::Loop { .. })); } + #[test] + fn diagnostics_are_sorted_by_position_then_code() { + // The duplicate-heading error is found while indexing (later in the + // file); the payload syntax error is found while mapping (earlier). + // The public order must be by source position, not pipeline stage. + let source = "## @exec f\n\n`x =`\n\n## @exec f\n"; + let result = analyze(source); + let codes: Vec<&str> = result + .diagnostics + .iter() + .map(|diagnostic| diagnostic.code.as_str()) + .collect(); + assert_eq!( + codes, + vec!["sem.syntax", "sem.duplicate_unit_name"], + "{:#?}", + result.diagnostics + ); + } + #[test] fn knowledge_section_cannot_declare_a_signature() { // The `params(...)` payload sits in a Knowledge section, so it is inert