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
@@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user