// SPDX-License-Identifier: Apache-2.0 OR MIT12//! Items which do not have a correspondence to any API in the proc_macro crate,3//! but are necessary to include in proc-macro2.45use crate::fallback;6use crate::imp;7use crate::marker::{ProcMacroAutoTraits, MARKER};8use crate::Span;9use core::fmt::{self, Debug};1011/// Invalidate any `proc_macro2::Span` that exist on the current thread.12///13/// The implementation of `Span` uses thread-local data structures and this14/// function clears them. Calling any method on a `Span` on the current thread15/// created prior to the invalidation will return incorrect values or crash.16///17/// This function is useful for programs that process more than 2<sup>32</sup>18/// bytes of Rust source code on the same thread. Just like rustc, proc-macro219/// uses 32-bit source locations, and these wrap around when the total source20/// code processed by the same thread exceeds 2<sup>32</sup> bytes (421/// gigabytes). After a wraparound, `Span` methods such as `source_text()` can22/// return wrong data.23///24/// # Example25///26/// As of late 2023, there is 200 GB of Rust code published on crates.io.27/// Looking at just the newest version of every crate, it is 16 GB of code. So a28/// workload that involves parsing it all would overflow a 32-bit source29/// location unless spans are being invalidated.30///31/// ```32/// use flate2::read::GzDecoder;33/// use std::ffi::OsStr;34/// use std::io::{BufReader, Read};35/// use std::str::FromStr;36/// use tar::Archive;37///38/// rayon::scope(|s| {39/// for krate in every_version_of_every_crate() {40/// s.spawn(move |_| {41/// proc_macro2::extra::invalidate_current_thread_spans();42///43/// let reader = BufReader::new(krate);44/// let tar = GzDecoder::new(reader);45/// let mut archive = Archive::new(tar);46/// for entry in archive.entries().unwrap() {47/// let mut entry = entry.unwrap();48/// let path = entry.path().unwrap();49/// if path.extension() != Some(OsStr::new("rs")) {50/// continue;51/// }52/// let mut content = String::new();53/// entry.read_to_string(&mut content).unwrap();54/// match proc_macro2::TokenStream::from_str(&content) {55/// Ok(tokens) => {/* ... */},56/// Err(_) => continue,57/// }58/// }59/// });60/// }61/// });62/// #63/// # fn every_version_of_every_crate() -> Vec<std::fs::File> {64/// # Vec::new()65/// # }66/// ```67///68/// # Panics69///70/// This function is not applicable to and will panic if called from a71/// procedural macro.72#[cfg(span_locations)]73#[cfg_attr(docsrs, doc(cfg(feature = "span-locations")))]74pub fn invalidate_current_thread_spans() {75crate::imp::invalidate_current_thread_spans();76}7778/// An object that holds a [`Group`]'s `span_open()` and `span_close()` together79/// in a more compact representation than holding those 2 spans individually.80///81/// [`Group`]: crate::Group82#[derive(Copy, Clone)]83pub struct DelimSpan {84inner: DelimSpanEnum,85_marker: ProcMacroAutoTraits,86}8788#[derive(Copy, Clone)]89enum DelimSpanEnum {90#[cfg(wrap_proc_macro)]91Compiler {92join: proc_macro::Span,93open: proc_macro::Span,94close: proc_macro::Span,95},96Fallback(fallback::Span),97}9899impl DelimSpan {100pub(crate) fn new(group: &imp::Group) -> Self {101#[cfg(wrap_proc_macro)]102let inner = match group {103imp::Group::Compiler(group) => DelimSpanEnum::Compiler {104join: group.span(),105open: group.span_open(),106close: group.span_close(),107},108imp::Group::Fallback(group) => DelimSpanEnum::Fallback(group.span()),109};110111#[cfg(not(wrap_proc_macro))]112let inner = DelimSpanEnum::Fallback(group.span());113114DelimSpan {115inner,116_marker: MARKER,117}118}119120/// Returns a span covering the entire delimited group.121pub fn join(&self) -> Span {122match &self.inner {123#[cfg(wrap_proc_macro)]124DelimSpanEnum::Compiler { join, .. } => Span::_new(imp::Span::Compiler(*join)),125DelimSpanEnum::Fallback(span) => Span::_new_fallback(*span),126}127}128129/// Returns a span for the opening punctuation of the group only.130pub fn open(&self) -> Span {131match &self.inner {132#[cfg(wrap_proc_macro)]133DelimSpanEnum::Compiler { open, .. } => Span::_new(imp::Span::Compiler(*open)),134DelimSpanEnum::Fallback(span) => Span::_new_fallback(span.first_byte()),135}136}137138/// Returns a span for the closing punctuation of the group only.139pub fn close(&self) -> Span {140match &self.inner {141#[cfg(wrap_proc_macro)]142DelimSpanEnum::Compiler { close, .. } => Span::_new(imp::Span::Compiler(*close)),143DelimSpanEnum::Fallback(span) => Span::_new_fallback(span.last_byte()),144}145}146}147148impl Debug for DelimSpan {149fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {150Debug::fmt(&self.join(), f)151}152}153154155