use borrowscope_macro::trace_borrow;
use borrowscope_runtime::{get_events, reset};
fn c_read(_ptr: *const u8) -> i32 {
42
}
fn malloc(_size: usize) -> *mut u8 {
std::ptr::null_mut()
}
fn free(_ptr: *mut u8) {}
const GLOBAL_CONFIG: i32 = 100;
const MAX_SIZE: usize = 1024;
struct RawData {
value: i32,
}
struct PackedValue {
data: u64,
}
#[trace_borrow(warn)]
fn with_warnings_enabled() {
let ptr = std::ptr::null();
let _result = c_read(ptr);
let _mem = malloc(100);
let _config = GLOBAL_CONFIG;
let _size = MAX_SIZE;
let raw = RawData { value: 42 };
let _v = raw.value;
println!(" Function completed with warnings shown during compilation");
}
#[trace_borrow(warn, ffi = ["c_read", "malloc", "free"], statics = ["GLOBAL_CONFIG", "MAX_SIZE"], unions = ["RawData", "PackedValue"])]
fn with_known_declarations() {
let ptr = std::ptr::null();
let _result = c_read(ptr);
let mem = malloc(100);
free(mem);
let _config = GLOBAL_CONFIG;
let _size = MAX_SIZE;
let raw = RawData { value: 42 };
let _v = raw.value;
let packed = PackedValue { data: 0x1234 };
let _d = packed.data;
println!(" Function completed - no warnings for declared items");
}
#[trace_borrow]
fn without_warnings() {
let ptr = std::ptr::null();
let _result = c_read(ptr);
let _config = GLOBAL_CONFIG;
println!(" Function completed - no warnings (default behavior)");
}
#[trace_borrow(warn)]
fn transmute_example() {
let x: u32 = 0x12345678;
let _y: f32 = unsafe { std::mem::transmute(x) };
println!(" Transmute completed - warning shown during compilation");
}
fn main() {
println!("=== Better Error Messages Demo ===\n");
println!("Note: Warnings appear during COMPILATION, not at runtime.\n");
println!("Look at the compiler output above to see the warnings.\n");
reset();
println!("1. Function with warnings enabled:");
with_warnings_enabled();
println!(" Events: {}\n", get_events().len());
reset();
println!("2. Function with known declarations (warnings suppressed):");
with_known_declarations();
println!(" Events: {}\n", get_events().len());
reset();
println!("3. Function without warnings (default):");
without_warnings();
println!(" Events: {}\n", get_events().len());
reset();
println!("4. Transmute example:");
transmute_example();
println!(" Events: {}\n", get_events().len());
println!("=== Demo Complete ===");
println!("\nTo see the warnings, look at the compiler output during build.");
println!("The warnings include:");
println!(" - Possible FFI calls (c_read, malloc, free)");
println!(" - Possible static variables (GLOBAL_CONFIG, MAX_SIZE)");
println!(" - Possible union field access (RawData, PackedValue)");
println!(" - Transmute type information unavailable");
}