From a3f9770761b09ac1e041fbc7a4baad811d0c464d Mon Sep 17 00:00:00 2001 From: cflmy Date: Sat, 21 Nov 2026 15:50:46 +0800 Subject: [PATCH] fix(frontend): point unmatched end diagnostics at the end payload A stray end after an earlier statement was reported with the span of the previous payload (position-1) and a confusing 'ends outside its owning function segment' message. Report it on the end payload itself with the standard sem.unmatched_end wording, and pin the span with a unit test. Verified: clippy -D warnings clean and cargo test --workspace --locked green (62 targets). --- crates/marqdo-frontend/src/analyze.rs | 17 +++++++++++++++++ crates/marqdo-frontend/src/map.rs | 7 +++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/crates/marqdo-frontend/src/analyze.rs b/crates/marqdo-frontend/src/analyze.rs index 648b513..4991322 100644 --- a/crates/marqdo-frontend/src/analyze.rs +++ b/crates/marqdo-frontend/src/analyze.rs @@ -270,6 +270,23 @@ mod tests { assert_eq!(function.params[0].name, "x"); } + #[test] + fn unmatched_end_points_at_the_end_payload() { + let source = "## @exec f\n\n`x = 1`\n\n`end`\n"; + let result = analyze(source); + let diagnostic = result + .diagnostics + .iter() + .find(|diagnostic| diagnostic.code == "sem.unmatched_end") + .expect("sem.unmatched_end"); + let span = diagnostic.span.expect("span"); + assert_eq!( + &source[span.start..span.end], + "`end`", + "the diagnostic must point at the stray end payload" + ); + } + #[test] fn invalid_strong_payload_is_a_hard_error() { let result = analyze("## @exec f\n\n**a *b* c**\n"); diff --git a/crates/marqdo-frontend/src/map.rs b/crates/marqdo-frontend/src/map.rs index 0e1f1fc..fcdfcdb 100644 --- a/crates/marqdo-frontend/src/map.rs +++ b/crates/marqdo-frontend/src/map.rs @@ -141,10 +141,13 @@ impl Mapper { let mut position = 0; let (stmts, stop) = self.sequence(&raw, &mut position, false, false); if stop != Stop::Eof { + // `sequence` leaves `position` on the stray `end` payload, so the + // diagnostic points at the keyword instead of the previous + // statement. A control frame may never close across a heading. self.error( codes::SEM_UNMATCHED_END, - raw.get(position.saturating_sub(1)).map(|node| node.span), - "control frame ends outside its owning function segment", + raw.get(position).map(|node| node.span), + "`end` has no open `if`/`while` in this heading segment", ); } stmts