Coverage Report

Created: 2026-09-14 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/proc-macro2-1.0.107/src/detection.rs
Line
Count
Source
1
use core::sync::atomic::{AtomicUsize, Ordering};
2
use std::sync::Once;
3
4
static WORKS: AtomicUsize = AtomicUsize::new(0);
5
static INIT: Once = Once::new();
6
7
5.93M
pub(crate) fn inside_proc_macro() -> bool {
8
5.93M
    match WORKS.load(Ordering::Relaxed) {
9
5.93M
        1 => return false,
10
0
        2 => return true,
11
1
        _ => {}
12
    }
13
14
1
    INIT.call_once(initialize);
15
1
    inside_proc_macro()
16
5.93M
}
17
18
0
pub(crate) fn force_fallback() {
19
0
    WORKS.store(1, Ordering::Relaxed);
20
0
}
21
22
0
pub(crate) fn unforce_fallback() {
23
0
    initialize();
24
0
}
25
26
#[cfg(not(no_is_available))]
27
1
fn initialize() {
28
1
    let available = proc_macro::is_available();
29
1
    WORKS.store(available as usize + 1, Ordering::Relaxed);
30
1
}
31
32
// Swap in a null panic hook to avoid printing "thread panicked" to stderr,
33
// then use catch_unwind to determine whether the compiler's proc_macro is
34
// working. When proc-macro2 is used from outside of a procedural macro all
35
// of the proc_macro crate's APIs currently panic.
36
//
37
// The Once is to prevent the possibility of this ordering:
38
//
39
//     thread 1 calls take_hook, gets the user's original hook
40
//     thread 1 calls set_hook with the null hook
41
//     thread 2 calls take_hook, thinks null hook is the original hook
42
//     thread 2 calls set_hook with the null hook
43
//     thread 1 calls set_hook with the actual original hook
44
//     thread 2 calls set_hook with what it thinks is the original hook
45
//
46
// in which the user's hook has been lost.
47
//
48
// There is still a race condition where a panic in a different thread can
49
// happen during the interval that the user's original panic hook is
50
// unregistered such that their hook is incorrectly not called. This is
51
// sufficiently unlikely and less bad than printing panic messages to stderr
52
// on correct use of this crate. Maybe there is a libstd feature request
53
// here. For now, if a user needs to guarantee that this failure mode does
54
// not occur, they need to call e.g. `proc_macro2::Span::call_site()` from
55
// the main thread before launching any other threads.
56
#[cfg(no_is_available)]
57
fn initialize() {
58
    use std::panic::{self, PanicInfo};
59
60
    type PanicHook = dyn Fn(&PanicInfo) + Sync + Send + 'static;
61
62
    let null_hook: Box<PanicHook> = Box::new(|_panic_info| { /* ignore */ });
63
    let sanity_check = &*null_hook as *const PanicHook;
64
    let original_hook = panic::take_hook();
65
    panic::set_hook(null_hook);
66
67
    let works = panic::catch_unwind(proc_macro::Span::call_site).is_ok();
68
    WORKS.store(works as usize + 1, Ordering::Relaxed);
69
70
    let hopefully_null_hook = panic::take_hook();
71
    panic::set_hook(original_hook);
72
    if sanity_check != &*hopefully_null_hook {
73
        panic!("observed race condition in proc_macro2::inside_proc_macro");
74
    }
75
}