Skip to content

Commit c3b22c6

Browse files
committed
rust: proc-macro2: import crate
This is a subset of the Rust `proc-macro2` crate, version 1.0.79, licensed under "Apache-2.0 OR MIT", from: https://github.com/dtolnay/proc-macro2/raw/1.0.79/src The files are copied as-is, with no modifications whatsoever (not even adding the SPDX identifiers). For copyright details, please see: https://github.com/dtolnay/proc-macro2/blob/1.0.79/README.md#license https://github.com/dtolnay/proc-macro2/blob/1.0.79/LICENSE-APACHE https://github.com/dtolnay/proc-macro2/blob/1.0.79/LICENSE-MIT The next two patches modify these files as needed for use within the kernel. This patch split allows reviewers to double-check the import and to clearly see the differences introduced. The following script may be used to verify the contents: for path in $(cd rust/proc-macro2/ && find . -type f -name '*.rs'); do curl --silent --show-error --location \ https://github.com/dtolnay/proc-macro2/raw/1.0.79/src/$path \ | diff --unified rust/proc-macro2/$path - && echo $path: OK done Signed-off-by: Miguel Ojeda <[email protected]>
1 parent e4527ff commit c3b22c6

File tree

9 files changed

+4887
-0
lines changed

9 files changed

+4887
-0
lines changed

rust/proc-macro2/detection.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
pub(crate) fn inside_proc_macro() -> bool {
8+
match WORKS.load(Ordering::Relaxed) {
9+
1 => return false,
10+
2 => return true,
11+
_ => {}
12+
}
13+
14+
INIT.call_once(initialize);
15+
inside_proc_macro()
16+
}
17+
18+
pub(crate) fn force_fallback() {
19+
WORKS.store(1, Ordering::Relaxed);
20+
}
21+
22+
pub(crate) fn unforce_fallback() {
23+
initialize();
24+
}
25+
26+
#[cfg(not(no_is_available))]
27+
fn initialize() {
28+
let available = proc_macro::is_available();
29+
WORKS.store(available as usize + 1, Ordering::Relaxed);
30+
}
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+
}

rust/proc-macro2/extra.rs

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

0 commit comments

Comments
 (0)