#[inline]
pub fn reset_span_locations() {
proc_macro2::extra::invalidate_current_thread_spans();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_reset_span_locations_basic() {
let code = "fn foo() {}";
let ast = syn::parse_file(code).expect("Failed to parse");
if let syn::Item::Fn(func) = &ast.items[0] {
let line = func.sig.ident.span().start().line;
assert_eq!(line, 1);
}
reset_span_locations();
}
#[test]
fn test_parse_many_files_with_reset_no_overflow() {
for i in 0..1000 {
let code = format!("fn func_{i}() {{ let x = {i}; }}");
let ast = syn::parse_file(&code).expect("Failed to parse");
assert_eq!(ast.items.len(), 1);
if let syn::Item::Fn(func) = &ast.items[0] {
let line = func.sig.ident.span().start().line;
assert_eq!(line, 1);
}
reset_span_locations();
}
}
#[test]
fn test_correct_usage_pattern() {
let files = vec![
"fn first() {}\nfn also_first() {}",
"fn second() {}",
"fn third() { let x = 1; }",
];
let mut extracted_lines = vec![];
for code in files {
let ast = syn::parse_file(code).expect("Failed to parse");
for item in &ast.items {
if let syn::Item::Fn(func) = item {
let line = func.sig.ident.span().start().line;
let name = func.sig.ident.to_string();
extracted_lines.push((name, line));
}
}
reset_span_locations();
}
assert_eq!(extracted_lines.len(), 4);
assert_eq!(extracted_lines[0], ("first".to_string(), 1));
assert_eq!(extracted_lines[1], ("also_first".to_string(), 2));
assert_eq!(extracted_lines[2], ("second".to_string(), 1)); assert_eq!(extracted_lines[3], ("third".to_string(), 1));
}
}