Skip to content

Commit

Permalink
Check for unknown labels in usage commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tirix committed Jan 6, 2025
1 parent 064494e commit d718d04
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
18 changes: 16 additions & 2 deletions metamath-rs/src/axiom_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ impl<'a> UsagePass<'a> {
let mut diags = vec![];
for cmd in axioms {
let axiom = &*cmd.value(buf);
if !axiom.starts_with(b"ax-") {
continue;
}
if let Some(index) = self.axioms.iter().position(|&x| x == axiom) {
if usage.has_bit(index) {
// TODO possibly research the usage "path" leading to this error.
Expand All @@ -61,6 +64,8 @@ impl<'a> UsagePass<'a> {
axiom.into(),
));
}
} else {
diags.push(Diagnostic::UnknownLabel(cmd.full_span()));
}
}
Err(diags)
Expand All @@ -71,9 +76,18 @@ impl<'a> UsagePass<'a> {

/// Verify that all usage declarations are correct.
fn verify_usage(&'a mut self, sset: &'a SegmentSet) {
for sref in sset.segments(..) {
for stmt in sref.range(..) {
if stmt.statement_type() == StatementType::Axiom && stmt.label().starts_with(b"ax-")
{
self.axioms.push(stmt.label());
}
}
}
for sref in sset.segments(..) {
let mut j_commands: std::slice::Iter<'_, (i32, (crate::Span, Vec<CommandToken>))> =
sref.j_commands.iter();
let mut axiom_index = 0;
for stmt in sref.range(..) {
match stmt.statement_type() {
StatementType::AdditionalInfoComment => {
Expand Down Expand Up @@ -110,8 +124,8 @@ impl<'a> UsagePass<'a> {
let label = stmt.label();
if label.starts_with(b"ax-") {
let mut usage = Bitset::new();
usage.set_bit(self.axioms.len());
self.axioms.push(label);
usage.set_bit(axiom_index);
axiom_index += 1;
self.axiom_use_map.insert(label, usage);
}
}
Expand Down
7 changes: 4 additions & 3 deletions metamath-rs/src/usage_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::diag::Diagnostic::UsageViolation;
use crate::diag::Diagnostic::{UnknownLabel, UsageViolation};
use crate::grammar_tests::mkdb;
use assert_matches::assert_matches;

Expand All @@ -9,7 +9,7 @@ ax-1 $a |- > a $.
thm1 $p |- > a $= wa ax-1 $.
$( $j usage 'thm1' avoids 'ax-1'; $)
thm2 $p |- > a $= ( ax-1 ) AB $.
$( $j usage 'thm2' avoids 'ax-1'; $)
$( $j usage 'thm2' avoids 'ax-1' 'ax-unknown'; $)
";

#[test]
Expand All @@ -19,7 +19,8 @@ fn test_usage() {
let usage = db.verify_usage_pass();
let diags = usage.diagnostics();

assert_eq!(diags.len(), 2);
assert_eq!(diags.len(), 3);
assert_matches!(diags[0], (_, UsageViolation(..)));
assert_matches!(diags[1], (_, UsageViolation(..)));
assert_matches!(diags[2], (_, UnknownLabel(..)));
}

0 comments on commit d718d04

Please sign in to comment.