fix(frontend): recognize signatures only from Exec payloads

signature::detect() scanned the function body independently of heading
modes, skipping only level>=3 headings. A 'params(...)' code span in an
### @knowledge section was therefore taken as the function signature
('inspect --signature' reported it as explicit), even though that span is
inert documentation and is never executed.

Unify recognition with the executable traversal:

- MappedBody now reports the params(...) payload the mapper accepted; the
  mapper already enforces Exec mode, directness, first-function-wide
  position and single declaration
- signature::from_payload validates that text and rewrites default-value
  spans; signature::detect is removed along with its mode-blind heuristic
- analyze uses the mapper result, so Knowledge sections cannot declare a
  signature and prose/Knowledge subsections may still precede the first
  Exec declaration
- tests: Knowledge params ignored; Exec params after prose/Knowledge is the
  signature; spec/mapping.md states the recognition rule

Verified: clippy -D warnings clean; cargo test --workspace --locked green
(62 targets).
This commit is contained in:
cflmy committed 2026-11-21 15:48:37 +08:00
1 parent d5002ac826
commit 9fb97bc2df
4 files changed
+83 -57

No files matched your search

+39 -7
View File
@@ -105,15 +105,14 @@ pub fn analyze_source(module_id: &str, source: &str) -> AnalysisResult {
let mut functions: Vec<Function> = Vec::new();
for scope in &units.functions {
let blocks: Vec<&marqdo_markdown::MdNode> = document.root.children
[scope.body_start..scope.body_end]
.iter()
.collect();
let signature = signature::detect(&document, &blocks);
let explicit = signature.as_ref().map(|(_, sig)| sig);
let mut mapped = map::map_function(&document, &units, scope);
diagnostics.append(&mut mapped.diagnostics);
// The mapper already proved that this payload is the function's first
// direct executable payload; a Knowledge section can never supply one.
let signature = mapped.params.as_ref().map(|declaration| {
signature::from_payload(&declaration.text, declaration.span, &declaration.segments)
});
let explicit = signature.as_ref();
let mut params: Vec<Param> = Vec::new();
let mut kind = SignatureKind::Inferred;
@@ -238,6 +237,39 @@ mod tests {
assert!(matches!(function.body[1], marqdo_ir::Stmt::Loop { .. }));
}
#[test]
fn knowledge_section_cannot_declare_a_signature() {
// The `params(...)` payload sits in a Knowledge section, so it is inert
// documentation and must not become the function's signature.
let source = "## @exec f\n\n### @knowledge 说明\n\n`params(x: Int)`\n\n### @exec 计算\n\n`return 1`\n";
let result = analyze(source);
assert!(
!result.has_errors(),
"unexpected diagnostics: {:#?}",
result.diagnostics
);
let function = result.module.function("f").expect("f");
assert_eq!(function.signature, SignatureKind::Inferred);
assert!(function.params.is_empty());
}
#[test]
fn exec_section_params_after_prose_is_the_signature() {
// Prose and a Knowledge subsection may precede the declaration, so a
// later Exec section can still declare the first executable payload.
let source = "## @exec f\n\n说明文字。\n\n### @knowledge 原理\n\n`params(ignored: Int)`\n\n### @exec 计算\n\n`params(x: Int)`\n\n`return x`\n";
let result = analyze(source);
assert!(
!result.has_errors(),
"unexpected diagnostics: {:#?}",
result.diagnostics
);
let function = result.module.function("f").expect("f");
assert_eq!(function.signature, SignatureKind::Explicit);
assert_eq!(function.params.len(), 1);
assert_eq!(function.params[0].name, "x");
}
#[test]
fn invalid_strong_payload_is_a_hard_error() {
let result = analyze("## @exec f\n\n**a *b* c**\n");