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).
This commit is contained in:
cflmy committed 2026-11-21 15:50:46 +08:00
1 parent 9fb97bc2df
commit a3f9770761
2 files changed
+22 -2

No files matched your search

+17
View File
@@ -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");
+5 -2
View File
@@ -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