flm_core/stdlib/features/blocks/check.rs
1//! The contract check stage of the [`blocks`](super) feature,
2//! `flm.blocks.check-contract`: it checks every frozen tree against the tree
3//! contract and reports each violation.
4//!
5//! The *tree contract* is the set of rules every tree satisfies after parsing
6//! (the parse part) and the further rules a tree satisfies after the
7//! block-structure pass (the block-structure part). Its checker,
8//! [`crate::lang::rewrites::blocks::check`], is two plain functions over a
9//! tree. This module makes it a check stage of the pipeline: a stage of
10//! per-tree scope that runs, over every [frozen
11//! tree](crate::guide::concepts_overview#frozen-trees-and-computed-data), each
12//! part of the contract
13//! that the plan's trees are subject to, and reports every violation as the
14//! Error diagnostic `flm.blocks.contract-violated` ([`ContractViolated`]). A
15//! violation is a fault in the program (the parser, a rewrite pass or an
16//! element's aspect), not in the document.
17//!
18//! The stage provides [`CONTRACT_CHECKED`] (`flm.blocks.contract-checked`),
19//! which the `blocks` feature adds to the collection
20//! [`flm.validated`](crate::stdlib::VALIDATED). Every workflow of the standard
21//! package demands that collection (the [`check`
22//! workflow](crate::stdlib::CHECK_WORKFLOW) and the workflow of each standard
23//! backend), so the stage runs in every standard plan.
24//!
25//! # What the stage requires, and which rules it checks
26//!
27//! The stage requires the decision about block structure, never one
28//! particular outcome, so that it fits into any plan. It does not require the
29//! decision about user definitions; it is only ordered after it. It finds out
30//! which outcomes the plan chose by reading each outcome's trees table as an
31//! optional read (a read that finds nothing when the table was not written),
32//! and checks the rules that apply to those outcomes:
33//!
34//! - It requires [`BLOCKS`] (`flm.blocks`), the decision about block
35//! structure, and reads [`STRUCTURED`] (`flm.blocks.structured`) optionally.
36//! Where that table was written, the trees were structured and both parts of
37//! the contract apply. Where it was not, the plan chose the trees as parsed
38//! (`flm.blocks.raw`, which the lowering rules of the `latex-recomposed`
39//! backend require) and only
40//! the parse part applies.
41//! - Whenever the registry declares `flm.userdefs.expanded`, it reads that
42//! table optionally and is ordered after `flm.userdefs`, the decision about
43//! user definitions whose expansion policy is `per-plan`. Where the table was
44//! written, no use site of a user definition (an element that carries the
45//! [`Expand`](crate::lang::rewrites::userdefs::Expand) aspect; see
46//! [`crate::lang::rewrites::userdefs`]) may remain in a structured tree, and
47//! a remaining one is a violation. Where it
48//! was not, the plan chose to keep those use sites in the tree for the
49//! lowering rules (`flm.userdefs.kept`), and that one rule of the
50//! block-structure part does not apply; every other rule still does. The
51//! stage never adds the expansion to a plan that does not otherwise need it.
52//! - Whenever the registry declares `flm.userdefs.always-expanded`, it
53//! requires that capability: the definitions whose expansion policy is
54//! `always` are expanded in every plan, so the check is meaningful only after
55//! that rewrite stage has run. The capability refines nothing, so requiring
56//! it chooses no alternative.
57//!
58//! [`ContractViolated`]: crate::lang::rewrites::blocks::ContractViolated
59
60use crate::core::plan::PlanCx;
61use crate::core::run::StageCx;
62use crate::core::stage::{Effect, Needs, Scope, Stage, StageDecl};
63use crate::core::data::store::StageOut;
64use crate::lang::rewrites::blocks::{
65 ContractViolationKind, check_block_contract, check_parse_contract,
66};
67use crate::stdlib::features::userdefs::{ALWAYS_EXPANDED, EXPANDED, USERDEFS};
68
69use super::{BLOCKS, STRUCTURED};
70
71/// The name of the contract check stage, `flm.blocks.check-contract`. See the
72/// module documentation.
73pub const CONTRACT_CHECK: &str = "flm.blocks.check-contract";
74
75/// The capability the contract check stage provides,
76/// `flm.blocks.contract-checked`: every frozen tree satisfies the parts of the
77/// tree contract that apply to the plan's trees, or its violations have been
78/// reported. A member of the collection
79/// [`flm.validated`](crate::stdlib::VALIDATED). The module documentation says
80/// which parts apply to which plan.
81pub const CONTRACT_CHECKED: &str = "flm.blocks.contract-checked";
82
83/// The contract check stage, `flm.blocks.check-contract`. See the module
84/// documentation.
85pub(super) struct ContractCheck;
86
87impl Stage for ContractCheck {
88 fn decl(&self) -> StageDecl {
89 StageDecl::new(CONTRACT_CHECK, Effect::Check, Scope::PerTree)
90 .provides(CONTRACT_CHECKED)
91 .docs(
92 "Checks every frozen tree against the parts of the tree contract that apply to \
93 the plan's trees, and reports each violation as \
94 `flm.blocks.contract-violated`.",
95 )
96 }
97
98 fn needs(&self, cx: &PlanCx<'_>) -> Needs {
99 // Each decision is asked for separately, as in the structure stage, so
100 // that a registry declaring one without the other never makes the check
101 // require a capability nobody declared.
102 let registry = cx.registry();
103 let mut needs = Needs::new().requires(BLOCKS).reads_opt(STRUCTURED);
104 if registry.cap_id(EXPANDED.name()).is_some() {
105 needs = needs.reads_opt(EXPANDED).after(USERDEFS);
106 }
107 if registry.cap_id(ALWAYS_EXPANDED.name()).is_some() {
108 needs = needs.requires(ALWAYS_EXPANDED.name());
109 }
110 needs
111 }
112
113 fn run(&self, cx: &StageCx<'_>, out: &mut StageOut<'_>) {
114 // A tree whose rewrite failed is absent; its one diagnostic stands.
115 let Some(tree) = cx.my_tree() else { return };
116 for violation in check_parse_contract(&tree) {
117 out.diags.push(violation.diagnostic(&tree));
118 }
119 // The block-structure part is defined over structured trees only: the
120 // structured trees table is written exactly where the structure rewrite
121 // ran.
122 if cx.get(STRUCTURED).ok().flatten().is_none() {
123 return;
124 }
125 // Under the kept alternative a use site of a user definition is meant to
126 // survive, so that one rule of the part does not apply; every other one
127 // does.
128 let use_sites_kept = cx.get(EXPANDED).ok().flatten().is_none();
129 for violation in check_block_contract(&tree) {
130 if use_sites_kept
131 && matches!(violation.kind, ContractViolationKind::UseSiteSurvived { .. })
132 {
133 continue;
134 }
135 out.diags.push(violation.diagnostic(&tree));
136 }
137 }
138}
139
140#[cfg(test)]
141mod tests {
142 use super::*;
143
144 #[test]
145 fn the_stage_is_a_per_tree_check_providing_its_capability() {
146 let decl = ContractCheck.decl();
147 assert_eq!(decl.name, CONTRACT_CHECK);
148 assert_eq!(decl.effect, Effect::Check);
149 assert_eq!(decl.scope, Scope::PerTree);
150 assert_eq!(decl.provides, [crate::model::names::CapName::from(CONTRACT_CHECKED)]);
151 assert!(decl.writes.is_empty());
152 }
153}