fix(frontend): emit diagnostics in the specified stable order

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.
This commit is contained in:
cflmy committed 2026-11-21 15:55:23 +08:00
1 parent 0bbe0f6524
commit b3429cbeb3
1 file changed
+33
+33
View File
@@ -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