fix(frontend): enforce function-wide params position

The concurrent mapper tracked the first params declaration in the Mapper (so
duplicates across heading segments were caught) but checked 'first payload'
with a per-segment vector index. A params(...) in a later ### @exec segment
was therefore accepted even after an earlier segment had executed statements,
and a duplicate could even be reported as a position error.

- track whether any statement has been lowered for the function and require
  params(...) to come before it, across all heading segments
- check duplicates first, then execution order, then the in-segment index
- add negative fixtures 48 (after an earlier segment) and 49 (duplicate
  across segments); update the corpus count and spec wording

Verified: cargo test --workspace --locked green (62 targets).
This commit is contained in:
cflmy committed 2026-11-21 15:36:30 +08:00
1 parent bbeb1de768
commit b38b769932
7 files changed
+57 -15

No files matched your search

+18 -8
View File
@@ -34,6 +34,7 @@ pub fn map_function(
let mut mapper = Mapper {
diagnostics: Vec::new(),
saw_params: None,
executed: false,
};
let mut stmts = Vec::new();
let mut segment = Vec::new();
@@ -67,7 +68,13 @@ pub fn map_function(
struct Mapper {
diagnostics: Vec<Diagnostic>,
/// First direct `params(...)` payload of the **function** (not just the
/// current heading segment), used to reject duplicate declarations.
saw_params: Option<Span>,
/// Whether any executable statement has already been lowered for this
/// function. `params(...)` is legal only before that point, even when the
/// earlier statements live in an earlier heading segment.
executed: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -243,6 +250,7 @@ impl Mapper {
return (out, Stop::End);
}
Payload::If(condition) => {
self.executed = true;
let condition = condition.clone();
let span = raw.span;
*position += 1;
@@ -283,6 +291,7 @@ impl Mapper {
out.push(Stmt::Branch { arms, span });
}
Payload::While(condition) => {
self.executed = true;
let condition = condition.clone();
let span = raw.span;
*position += 1;
@@ -304,6 +313,7 @@ impl Mapper {
});
}
Payload::Return(value) => {
self.executed = true;
out.push(Stmt::Return {
value: value.clone(),
span: raw.span,
@@ -311,31 +321,31 @@ impl Mapper {
*position += 1;
}
Payload::Params => {
let duplicate = self.saw_params.is_some();
if !raw.direct {
self.error(
codes::SEM_SIGNATURE_POSITION,
Some(raw.span),
"`params(...)` must be a direct payload, not inside a list or block quote",
);
} else if *position != 0 {
self.error(
codes::SEM_SIGNATURE_POSITION,
Some(raw.span),
"`params(...)` must be the first payload of an Exec H2",
);
} else if duplicate {
} else if self.saw_params.is_some() {
self.error(
codes::SEM_SIGNATURE_POSITION,
Some(raw.span),
"`params(...)` may be declared only once per function",
);
} else if self.executed || *position != 0 {
self.error(
codes::SEM_SIGNATURE_POSITION,
Some(raw.span),
"`params(...)` must be the first executable payload of the Exec H2, before any statement in any heading segment",
);
} else {
self.saw_params = Some(raw.span);
}
*position += 1;
}
Payload::Ordinary(statement) => {
self.executed = true;
let mut statement = statement.clone();
raw.segments.rewrite_statement(&mut statement);
let span = raw.span;
+7 -5
View File
@@ -277,9 +277,11 @@ Rules:
segment are visible in later segments. They do not create new functions.
* `params(name: Type, ...)` is legal only as the **first direct execution
payload of an Exec H2 function**, at most once, and not inside a list or
block quote. It is compile-time metadata and may be preceded by prose or a
Knowledge subsection but not by an executing statement. Violations are
`sem.signature_position`; a duplicate declaration is also rejected.
block quote. The position is function-wide: it may be preceded by prose or a
Knowledge subsection, but not by an executing statement in **any** heading
segment of that function (a later `### @exec` section may not add a
signature after earlier code ran). Violations are `sem.signature_position`;
a duplicate declaration anywhere in the function is also rejected.
* A function without `params` keeps the existing free-variable inference.
As before, an inferred signature accepts **named arguments only**; declare
`params(...)` to accept positional arguments. Public functions should declare
@@ -327,7 +329,7 @@ Stable machine-readable codes (the human `message`/`hint` text may change):
| `sem.duplicate_else` | a second `else` for the same `if` |
| `sem.unmatched_end` | `end` with no open control frame |
| `sem.unclosed_control` | an open `if`/`while` at a heading/function/file boundary |
| `sem.signature_position` | `params` not at the first direct payload, duplicated, or in a container |
| `sem.signature_position` | `params` not the first function-wide direct payload, duplicated, or in a container |
| `sem.unsupported_version_directive` | legacy `marqdo_semantics`/syntax-selection front matter |
| `sem.no_entry_function` | `run` with no Exec H2 `main` |
| `sem.inferred_signature` | lint: public function without explicit `params` |
@@ -361,7 +363,7 @@ the existing `Assign`/`Eval`/`Branch`/`Loop`/`Return` IR.
| Branch | ordered list with a first code-span condition | `if cond ... else ... end`; containers are presentation only |
| Loop | bullet list with a first code-span condition | `while cond ... end` |
| Call | block quote containing a call, or a Strong expression | any carrier whose expression is a call |
| Signature | first standalone code span `(n: Int)` after the H2 | `params(n: Int)` as the first direct payload |
| Signature | first standalone code span `(n: Int)` after the H2 | `params(n: Int)` as the first function-wide direct payload |
| Ordinary list | error inside a function | ordinary document structure, zero statements |
| Ordinary quote | error inside a function | ordinary document structure, zero statements |
| Knowledge | implied by position/inertness only | explicit `@knowledge` scope |
+4 -2
View File
@@ -29,7 +29,7 @@ tests/
│ ├── ast/ 160 × *.mq.md + sibling *.ast.json goldens
│ └── semantics/ 73 × *.mq.md + sibling *.expected.json goldens
├── negative/
│ ├── *.mq.md 47 × compile-time hard-error fixtures + *.expected.json
│ ├── *.mq.md 49 × compile-time hard-error fixtures + *.expected.json
│ ├── warnings/ 2 × warning-model/control fixtures + *.expected.json
│ └── runtime/ 21 × runtime-error fixtures + *.expected.json
└── README.md this file
@@ -79,7 +79,9 @@ The power/control-flow slice is pinned by:
trailing colon, `end if`, empty `return`, multi-statement payload, bad
`params` syntax); fixtures 24–29 and 46 pin control-stack pairing, including the
heading boundary that reports `sem.unclosed_control` and `sem.unmatched_end`
independently and `else` under an open `while`;
independently, `else` under an open `while`, and a `params(...)` declaration
that appears after code in an earlier heading segment or is duplicated across
segments (48–49);
* runtime negatives 11–12: negative integer exponent and exponent budget;
* `crates/marqdo-frontend/src/payload.rs` span tests and
`crates/cli/tests/integration_cli.rs` CLI span/E2E assertions.
@@ -0,0 +1,5 @@
{
"codes": [
"sem.signature_position"
]
}
@@ -0,0 +1,9 @@
## @exec f
`x = 1`
### @exec 续写
`params(y: Int)`
`return x`
@@ -0,0 +1,5 @@
{
"codes": [
"sem.signature_position"
]
}
@@ -0,0 +1,9 @@
## @exec f
`params(x: Int)`
### @exec 续写
`params(y: Int)`
`return x`