use std::path::PathBuf;
fn is_permitted(rel: &str) -> bool {
rel.ends_with("src/bridge.rs") || rel.ends_with("src/lib.rs")
|| rel.ends_with("tests/fallback_counter_completeness.rs")
}
const LAYER_TWO_FN: &str = "fn import_via_bridge";
#[test]
fn every_bridge_call_site_records_a_crossing() {
let mut root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
root.push("src");
let mut sites = 0usize;
let mut unrecorded: Vec<String> = Vec::new();
let mut stack = vec![root];
while let Some(dir) = stack.pop() {
let Ok(entries) = std::fs::read_dir(&dir) else {
continue;
};
for e in entries.flatten() {
let p = e.path();
if p.is_dir() {
stack.push(p);
continue;
}
if p.extension().and_then(|s| s.to_str()) != Some("rs") {
continue;
}
let rel = p.to_string_lossy().to_string();
if is_permitted(&rel) {
continue;
}
let Ok(src) = std::fs::read_to_string(&p) else {
continue;
};
let lines: Vec<&str> = src.lines().collect();
for (i, line) in lines.iter().enumerate() {
if !line.contains("call_builtin_bridge(") {
continue;
}
sites += 1;
let lo = i.saturating_sub(12);
let recorded = lines[lo..=i].iter().any(|l| l.contains("fallback::record"));
let in_layer_two = lines[..=i]
.iter()
.rev()
.take(60)
.any(|l| l.contains(LAYER_TWO_FN));
if !recorded && !in_layer_two {
unrecorded.push(format!(" {}:{}", rel, i + 1));
}
}
}
}
assert!(
sites >= 3,
"found only {sites} `call_builtin_bridge` call sites outside the \
bridge module; expected at least 3. The scan is broken, and a clean \
result over a broken scan is not a clean result."
);
assert!(
unrecorded.is_empty(),
"\nthese cross to the tree-walker WITHOUT counting it:\n{}\n\n\
A bridge crossing that is not recorded makes the counter report zero \
where the truth is unknown — which reads as an answer. That is how \
`match` and `fromTOML` were concluded to be native when they were \
bridged all along.\n({sites} call sites scanned)",
unrecorded.join("\n")
);
}