use antigen::learn::{propose, self_tolerance};
use antigen_fingerprint::Constraint;
const DROP_FAMILY: &str = r#"
pub struct GuardA;
impl Drop for GuardA {
fn drop(&mut self) { let _ = flush(self.h).take().unwrap(); }
}
pub struct GuardB;
impl Drop for GuardB {
fn drop(&mut self) { let _ = flush(self.h).take().expect("must flush"); }
}
pub struct CleanGuard;
impl Drop for CleanGuard {
fn drop(&mut self) { let _ = flush(self.h).take().ok(); }
}
"#;
fn drop_impl_for(items: &[syn::Item], ty: &str) -> syn::Item {
items
.iter()
.find(|it| {
let syn::Item::Impl(i) = it else { return false };
let Some((_, trait_path, _)) = &i.trait_ else {
return false;
};
let is_drop = trait_path
.segments
.last()
.is_some_and(|s| s.ident == "Drop");
let syn::Type::Path(p) = &*i.self_ty else {
return false;
};
is_drop && p.path.segments.last().is_some_and(|s| s.ident == ty)
})
.expect("impl Drop for the named type is present")
.clone()
}
fn describe(c: &Constraint) -> String {
match c {
Constraint::Item(kind) => format!("item = {kind:?}"),
Constraint::ImplOfTrait(t) => format!("impl_of_trait(\"{t}\")"),
Constraint::BodyCalls(n) => format!("body_calls(\"{n}\")"),
Constraint::BodyContainsMacro(n) => format!("body_contains_macro(\"{n}\")"),
Constraint::AnyOf(arms) => {
let inner: Vec<String> = arms.iter().map(describe).collect();
format!("any_of([{}])", inner.join(", "))
},
other => format!("{other:?}"),
}
}
fn main() {
let family = syn::parse_file(DROP_FAMILY).expect("the toy family parses");
let cluster = vec![
drop_impl_for(&family.items, "GuardA"),
drop_impl_for(&family.items, "GuardB"),
];
let clean_sibling = drop_impl_for(&family.items, "CleanGuard");
println!("== PROPOSE: anti-unify a cluster of two panic-in-Drop sites ==\n");
println!("cluster:");
println!(" GuardA::drop — flush(..).take().unwrap()");
println!(" GuardB::drop — flush(..).take().expect(\"must flush\")");
println!("clean sibling (must be spared):");
println!(" CleanGuard::drop — flush(..).take().ok()\n");
let draft = propose::anti_unify(&cluster).expect("the cluster shares a skeleton to generalize");
println!("drafted fingerprint (the anti-unification):");
for c in &draft.constraints {
println!(" {}", describe(c));
}
println!();
println!(" the signals both members share (`flush`, `take`) are required conjuncts.");
println!(" the differing panic sources `unwrap`/`expect` become an any_of —");
println!(" the load-bearing wall that carries the defect signal without");
println!(" collapsing to the bare `impl Drop` skeleton clean code also has.\n");
let binds_a = draft.matches(&cluster[0]);
let binds_b = draft.matches(&cluster[1]);
let binds_clean = draft.matches(&clean_sibling);
println!("does the raw draft bind each member?");
println!(" GuardA -> {binds_a}");
println!(" GuardB -> {binds_b}");
println!(" CleanGuard -> {binds_clean} (the any_of arm is no-match on .ok())\n");
let clean_corpus = vec![clean_sibling.clone()];
let promoted = propose::propose(&cluster, &clean_corpus);
match &promoted {
Ok(token) => {
let fp = token.fingerprint();
println!("== self-tolerance gate: PROMOTED ==\n");
println!("the draft spared the clean corpus AND a near-miss witnessed its");
println!("generalization, so the gate minted a PromotedDraft token.");
println!("a promoted draft is guaranteed to spare every clean-corpus item:");
println!(" binds GuardA -> {}", fp.matches(&cluster[0]));
println!(" binds GuardB -> {}", fp.matches(&cluster[1]));
println!(" binds CleanGuard -> {}", fp.matches(&clean_corpus[0]));
println!(
" score tier -> {:?} (gate-assigned floor)",
token.tier()
);
},
Err(outcome) => {
println!("== self-tolerance gate: REJECTED ==\n");
println!("the gate refused to promote: {outcome:?}");
},
}
println!("\n== why the gate is not optional ==\n");
let naive =
antigen_fingerprint::Fingerprint::parse(r#"all_of([item = impl, impl_of_trait("Drop")])"#)
.expect("the naive draft parses");
println!("the naive generalization (drop the differing leaves) is just:");
println!(" all_of([item = impl, impl_of_trait(\"Drop\")])");
println!(
" binds CleanGuard -> {} (it flags clean code)",
naive.matches(&clean_sibling)
);
let naive_promoted = self_tolerance::promote_if_safe(naive, &clean_corpus);
println!(
" promote_if_safe(naive) -> {:?} (the gate rejects it: bare-structural \
over-general fails the (A)-binary check)",
naive_promoted.map(|_| "Ok(PromotedDraft)")
);
}