Verify control-flow invariants and document the development contract
This commit is contained in:
1 parent
71d29261a8
commit
1dc088b602
22 files changed
+269
-80
No files matched your search
@@ -437,6 +437,7 @@ fn multi_arm_branch_preserves_analysis_v2_and_carrier_positions() {
|
||||
"`if 数值 < 0`",
|
||||
"**else if 数值 == 0**",
|
||||
"*else if 数值 == 1*",
|
||||
"`else`",
|
||||
]) {
|
||||
let start = source.find(carrier).unwrap();
|
||||
let expected = serde_json::json!({"start": start, "end": start + carrier.len()});
|
||||
|
||||
@@ -429,3 +429,35 @@ fn narration_removal_preserves_execution() {
|
||||
);
|
||||
assert_eq!(values[0], marqdo_value::Value::int(120));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn control_flow_ir_is_independent_of_lists_quotes_and_prose() {
|
||||
let statements = [
|
||||
"`i = 0`",
|
||||
"`while true`",
|
||||
"`i = i + 1`",
|
||||
"`if i == 1`",
|
||||
"`continue`",
|
||||
"`else if i == 2`",
|
||||
"`break`",
|
||||
"`end`",
|
||||
"`end`",
|
||||
"`return i`",
|
||||
];
|
||||
let plain = format!("## @exec main\n\n{}\n", statements.join("\n\n"));
|
||||
let adorned = statements
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, statement)| match index % 3 {
|
||||
0 => format!("1. {statement} Ordinary prose follows the carrier."),
|
||||
1 => format!("> {statement}"),
|
||||
_ => format!("- {statement}"),
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n\n");
|
||||
let decorated = format!("## @exec main\n\n{adorned}\n");
|
||||
assert_eq!(
|
||||
executable_projection(&plain),
|
||||
executable_projection(&decorated)
|
||||
);
|
||||
}
|
||||
@@ -87,7 +87,7 @@ struct Analyzer<'a> {
|
||||
known: Option<&'a BTreeSet<String>>,
|
||||
reads: BTreeMap<String, Span>,
|
||||
diagnostics: Vec<Diagnostic>,
|
||||
ambiguous_reported: BTreeSet<String>,
|
||||
ambiguous_reads: BTreeMap<String, Span>,
|
||||
unknown_reported: BTreeSet<String>,
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ impl<'a> Analyzer<'a> {
|
||||
known,
|
||||
reads: BTreeMap::new(),
|
||||
diagnostics: Vec::new(),
|
||||
ambiguous_reported: BTreeSet::new(),
|
||||
ambiguous_reads: BTreeMap::new(),
|
||||
unknown_reported: BTreeSet::new(),
|
||||
}
|
||||
}
|
||||
@@ -107,19 +107,7 @@ impl<'a> Analyzer<'a> {
|
||||
return;
|
||||
}
|
||||
if state.may.contains(name) {
|
||||
if !self.ambiguous_reported.contains(name) {
|
||||
self.ambiguous_reported.insert(name.to_string());
|
||||
self.diagnostics.push(
|
||||
Diagnostic::error(
|
||||
codes::SYMBOL_UNINITIALIZED,
|
||||
Some(span),
|
||||
format!("`{name}` may be read before it is assigned"),
|
||||
)
|
||||
.with_hint(
|
||||
"assign it on every path before this read, or pass it in as a parameter",
|
||||
),
|
||||
);
|
||||
}
|
||||
self.ambiguous_reads.entry(name.to_string()).or_insert(span);
|
||||
return;
|
||||
}
|
||||
if let Some(known) = self.known {
|
||||
@@ -326,6 +314,22 @@ pub fn infer(
|
||||
}
|
||||
});
|
||||
analyzer.analyze_stmts(stmts, initial);
|
||||
for (name, span) in analyzer.ambiguous_reads {
|
||||
// A reachable free read makes the name an entry parameter throughout
|
||||
// an inferred function, including before or after a local rebinding.
|
||||
if !analyzer.reads.contains_key(&name) {
|
||||
analyzer.diagnostics.push(
|
||||
Diagnostic::error(
|
||||
codes::SYMBOL_UNINITIALIZED,
|
||||
Some(span),
|
||||
format!("`{name}` may be read before it is assigned"),
|
||||
)
|
||||
.with_hint(
|
||||
"assign it on every path before this read, or pass it in as a parameter",
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
let mut params: Vec<ParamCandidate> = analyzer
|
||||
.reads
|
||||
.iter()
|
||||
@@ -422,7 +426,14 @@ mod tests {
|
||||
},
|
||||
ret("n"),
|
||||
];
|
||||
assert_eq!(params(&stmts), vec!["n".to_string()]);
|
||||
let inference = infer(&stmts, None, &BTreeSet::new());
|
||||
assert_eq!(inference.params[0].name, "n");
|
||||
assert_eq!(inference.params.len(), 1);
|
||||
assert!(
|
||||
inference.diagnostics.is_empty(),
|
||||
"{:?}",
|
||||
inference.diagnostics
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -404,20 +404,19 @@ impl Mapper {
|
||||
span: raw.span,
|
||||
});
|
||||
}
|
||||
if matches!(
|
||||
nodes.get(*position).map(|raw| &raw.payload),
|
||||
Some(Payload::Else)
|
||||
) {
|
||||
if let Some(raw) = nodes
|
||||
.get(*position)
|
||||
.filter(|raw| matches!(raw.payload, Payload::Else))
|
||||
{
|
||||
*position += 1;
|
||||
let body = self.sequence(nodes, position, ArmContext::Default, in_loop);
|
||||
// Preserve the v2 projection and observation of the fallback arm.
|
||||
// An empty fallback has the same effect as ordinary fallthrough.
|
||||
// An empty fallback is ordinary fallthrough.
|
||||
if !body.is_empty() {
|
||||
arms.push(BranchArm {
|
||||
condition: Expr::Bool(true),
|
||||
condition_span: span,
|
||||
condition_span: raw.span,
|
||||
body,
|
||||
span,
|
||||
span: raw.span,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,7 +72,8 @@ struct BoundArgs {
|
||||
enum Flow {
|
||||
Next,
|
||||
Return(Value),
|
||||
LoopTransfer(LoopTransfer, Span),
|
||||
Break(Span),
|
||||
Continue(Span),
|
||||
}
|
||||
|
||||
/// Marqdo tree-walk interpreter.
|
||||
@@ -232,7 +233,7 @@ impl<'a> Interpreter<'a> {
|
||||
.and_then(|flow| match flow {
|
||||
Flow::Next => Ok(None),
|
||||
Flow::Return(value) => Ok(Some(value)),
|
||||
Flow::LoopTransfer(_, span) => Err(RuntimeError::new(
|
||||
Flow::Break(span) | Flow::Continue(span) => Err(RuntimeError::new(
|
||||
ErrorCategory::Internal,
|
||||
Some(span),
|
||||
"loop control escaped its enclosing function",
|
||||
@@ -421,10 +422,10 @@ impl<'a> Interpreter<'a> {
|
||||
self.eval_expr(expr, frame)?;
|
||||
}
|
||||
Stmt::Break { span } | Stmt::Continue { span } => {
|
||||
let transfer = if matches!(stmt, Stmt::Break { .. }) {
|
||||
LoopTransfer::Break
|
||||
let (flow, transfer) = if matches!(stmt, Stmt::Break { .. }) {
|
||||
(Flow::Break(*span), LoopTransfer::Break)
|
||||
} else {
|
||||
LoopTransfer::Continue
|
||||
(Flow::Continue(*span), LoopTransfer::Continue)
|
||||
};
|
||||
let module = frame.function.module.canonical();
|
||||
self.host.record_semantic(
|
||||
@@ -443,7 +444,7 @@ impl<'a> Interpreter<'a> {
|
||||
span: *span,
|
||||
})?;
|
||||
}
|
||||
return Ok(Flow::LoopTransfer(transfer, *span));
|
||||
return Ok(flow);
|
||||
}
|
||||
Stmt::Return { value, span } => {
|
||||
let value = self.eval_expr(value, frame)?;
|
||||
@@ -538,8 +539,8 @@ impl<'a> Interpreter<'a> {
|
||||
break;
|
||||
}
|
||||
match self.exec_stmts(program, body, frame)? {
|
||||
Flow::Next | Flow::LoopTransfer(LoopTransfer::Continue, _) => {}
|
||||
Flow::LoopTransfer(LoopTransfer::Break, _) => break,
|
||||
Flow::Next | Flow::Continue(_) => {}
|
||||
Flow::Break(_) => break,
|
||||
flow @ Flow::Return(_) => return Ok(flow),
|
||||
}
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user