From 0bbe0f6524c7be07a45c4d54e2de13415d44c7f1 Mon Sep 17 00:00:00 2001 From: cflmy Date: Sat, 21 Nov 2026 15:52:46 +0800 Subject: [PATCH] fix(frontend): honour the Setext no-declaration rule; drop dead helper RFC-LANG-0002 allows a mode declaration only as an ATX heading prefix; a Setext heading merely inherits the nearest ancestor mode. heading_declaration was applied to every heading, so a Setext 'title' beginning with @exec/ @knowledge silently switched modes and could create a function. - skip declaration parsing for Setext headings (they keep their literal name and inherited mode); add a regression test - remove the dead normalized_heading_name helper and its imports, which only existed for the removed link-form call resolution Verified: clippy -D warnings clean; cargo test --workspace --locked green (505 passed, 0 failed, 2 ignored). --- crates/marqdo-frontend/src/map.rs | 7 +------ crates/marqdo-frontend/src/units.rs | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/crates/marqdo-frontend/src/map.rs b/crates/marqdo-frontend/src/map.rs index fcdfcdb..71ba7c9 100644 --- a/crates/marqdo-frontend/src/map.rs +++ b/crates/marqdo-frontend/src/map.rs @@ -9,7 +9,7 @@ use marqdo_expression::{ parse_payload_statement, AssignTarget, Expr, PayloadStatement as Payload, StatementKind, }; use marqdo_ir::{PlaceTarget, Stmt}; -use marqdo_markdown::{normalize_name, MarkdownDocument, MdKind, MdNode}; +use marqdo_markdown::{MarkdownDocument, MdKind, MdNode}; use marqdo_span::Span; use marqdo_value::{codes, Diagnostic}; @@ -409,8 +409,3 @@ impl Mapper { (out, Stop::Eof) } } - -/// Normalise a heading name (re-exported for callers that resolve link targets). -pub fn normalized_heading_name(node: &MdNode) -> Option { - normalize_name(&marqdo_markdown::plain_text(node)) -} diff --git a/crates/marqdo-frontend/src/units.rs b/crates/marqdo-frontend/src/units.rs index cb9742c..a675c9b 100644 --- a/crates/marqdo-frontend/src/units.rs +++ b/crates/marqdo-frontend/src/units.rs @@ -204,11 +204,17 @@ pub fn build_units(doc: &MarkdownDocument, module: &ModuleId) -> UnitIndex { let mut heading_infos: Vec = Vec::new(); for (node_index, child) in doc.root.children.iter().enumerate() { - let MdKind::Heading { level, .. } = &child.kind else { + let MdKind::Heading { level, setext } = &child.kind else { continue; }; let level = *level; - let (declared, declared_name, declaration_error) = heading_declaration(child); + // RFC-LANG-0002: only ATX headings may carry a mode declaration; a + // Setext heading simply inherits the nearest ancestor mode. + let (declared, declared_name, declaration_error) = if *setext { + (None, None, None) + } else { + heading_declaration(child) + }; if let Some(error) = declaration_error { index.diagnostics.push(error); if declared_name.is_none() { @@ -488,6 +494,21 @@ mod tests { assert!(index.diagnostics.is_empty()); } + #[test] + fn setext_headings_inherit_and_never_declare_a_mode() { + // `@exec f` is the level-2 heading text under a Setext underline; it is + // not a declaration, so the H2 stays Knowledge and creates no function. + let index = index_of("# @knowledge 项目\n\n@exec f\n---\n\n`return 1`\n"); + assert!(index.functions.is_empty(), "{:?}", index.functions); + let heading = index + .headings + .iter() + .find(|heading| heading.level == 2) + .expect("level-2 heading"); + assert_eq!(heading.mode, SemanticMode::Knowledge); + assert_eq!(heading.name, "@exec f"); + } + #[test] fn empty_heading_is_an_error() { let index = index_of("## @exec\n\n正文\n");