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