Skip to content

Commit

Permalink
Ensure invalidate_current_thread_spans has docs even if proc-macro di…
Browse files Browse the repository at this point in the history
…sabled
  • Loading branch information
dtolnay committed Jan 21, 2024
1 parent 7e7bb0f commit 43011bf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
21 changes: 20 additions & 1 deletion src/extra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,28 @@ use crate::marker::{ProcMacroAutoTraits, MARKER};
use crate::Span;
use core::fmt::{self, Debug};

/// Invalidate any `proc_macro2::Span` that exist on the current thread.
///
/// The implementation of `Span` uses thread-local data structures and this
/// function clears them. Calling any method on a `Span` on the current thread
/// created prior to the invalidation will return incorrect values or crash.
///
/// This function is useful for programs that process more than 2<sup>32</sup>
/// bytes of Rust source code on the same thread. Just like rustc, proc-macro2
/// uses 32-bit source locations, and these wrap around when the total source
/// code processed by the same thread exceeds 2<sup>32</sup> bytes (4
/// gigabytes). After a wraparound, `Span` methods such as `source_text()` can
/// return wrong data.
///
/// # Panics
///
/// This function is not applicable to and will panic if called from a
/// procedural macro.
#[cfg(span_locations)]
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
pub use crate::imp::invalidate_current_thread_spans;
pub fn invalidate_current_thread_spans() {
crate::imp::invalidate_current_thread_spans();
}

/// An object that holds a [`Group`]'s `span_open()` and `span_close()` together
/// in a more compact representation than holding those 2 spans individually.
Expand Down
2 changes: 1 addition & 1 deletion src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ thread_local! {
}

#[cfg(span_locations)]
pub fn invalidate_current_thread_spans() {
pub(crate) fn invalidate_current_thread_spans() {
#[cfg(not(fuzzing))]
SOURCE_MAP.with(|sm| sm.borrow_mut().files.truncate(1));
}
Expand Down
19 changes: 1 addition & 18 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,25 +929,8 @@ impl Debug for Literal {
}
}

/// Invalidate any `proc_macro2::Span` that exist on the current thread.
///
/// The implementation of `Span` uses thread-local data structures and this
/// function clears them. Calling any method on a `Span` on the current thread
/// created prior to the invalidation will return incorrect values or crash.
///
/// This function is useful for programs that process more than 2<sup>32</sup>
/// bytes of Rust source code on the same thread. Just like rustc, proc-macro2
/// uses 32-bit source locations, and these wrap around when the total source
/// code processed by the same thread exceeds 2<sup>32</sup> bytes (4
/// gigabytes). After a wraparound, `Span` methods such as `source_text()` can
/// return wrong data.
///
/// # Panics
///
/// This function is not applicable to and will panic if called from a
/// procedural macro.
#[cfg(span_locations)]
pub fn invalidate_current_thread_spans() {
pub(crate) fn invalidate_current_thread_spans() {
if inside_proc_macro() {
panic!(
"proc_macro2::extra::invalidate_current_thread_spans is not available in procedural macros"
Expand Down

0 comments on commit 43011bf

Please sign in to comment.