/rust/registry/src/index.crates.io-1949cf8c6b5b557f/proc-macro2-1.0.106/src/wrapper.rs
Line | Count | Source |
1 | | use crate::detection::inside_proc_macro; |
2 | | use crate::fallback::{self, FromStr2 as _}; |
3 | | #[cfg(span_locations)] |
4 | | use crate::location::LineColumn; |
5 | | #[cfg(proc_macro_span)] |
6 | | use crate::probe::proc_macro_span; |
7 | | #[cfg(all(span_locations, proc_macro_span_file))] |
8 | | use crate::probe::proc_macro_span_file; |
9 | | #[cfg(all(span_locations, proc_macro_span_location))] |
10 | | use crate::probe::proc_macro_span_location; |
11 | | use crate::{Delimiter, Punct, Spacing, TokenTree}; |
12 | | #[cfg(all(span_locations, not(proc_macro_span_file)))] |
13 | | use alloc::borrow::ToOwned as _; |
14 | | use alloc::string::{String, ToString as _}; |
15 | | use alloc::vec::Vec; |
16 | | use core::ffi::CStr; |
17 | | use core::fmt::{self, Debug, Display}; |
18 | | #[cfg(span_locations)] |
19 | | use core::ops::Range; |
20 | | use core::ops::RangeBounds; |
21 | | #[cfg(span_locations)] |
22 | | use std::path::PathBuf; |
23 | | |
24 | | #[derive(Clone)] |
25 | | pub(crate) enum TokenStream { |
26 | | Compiler(DeferredTokenStream), |
27 | | Fallback(fallback::TokenStream), |
28 | | } |
29 | | |
30 | | // Work around https://github.com/rust-lang/rust/issues/65080. |
31 | | // In `impl Extend<TokenTree> for TokenStream` which is used heavily by quote, |
32 | | // we hold on to the appended tokens and do proc_macro::TokenStream::extend as |
33 | | // late as possible to batch together consecutive uses of the Extend impl. |
34 | | #[derive(Clone)] |
35 | | pub(crate) struct DeferredTokenStream { |
36 | | stream: proc_macro::TokenStream, |
37 | | extra: Vec<proc_macro::TokenTree>, |
38 | | } |
39 | | |
40 | | pub(crate) enum LexError { |
41 | | Compiler(proc_macro::LexError), |
42 | | Fallback(fallback::LexError), |
43 | | |
44 | | // Rustc was supposed to return a LexError, but it panicked instead. |
45 | | // https://github.com/rust-lang/rust/issues/58736 |
46 | | CompilerPanic, |
47 | | } |
48 | | |
49 | | #[cold] |
50 | 0 | fn mismatch(line: u32) -> ! { |
51 | | #[cfg(procmacro2_backtrace)] |
52 | | { |
53 | | let backtrace = std::backtrace::Backtrace::force_capture(); |
54 | | panic!("compiler/fallback mismatch L{}\n\n{}", line, backtrace) |
55 | | } |
56 | | #[cfg(not(procmacro2_backtrace))] |
57 | | { |
58 | 0 | panic!("compiler/fallback mismatch L{}", line) |
59 | | } |
60 | | } |
61 | | |
62 | | impl DeferredTokenStream { |
63 | 0 | fn new(stream: proc_macro::TokenStream) -> Self { |
64 | 0 | DeferredTokenStream { |
65 | 0 | stream, |
66 | 0 | extra: Vec::new(), |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | 0 | fn is_empty(&self) -> bool { |
71 | 0 | self.stream.is_empty() && self.extra.is_empty() |
72 | 0 | } |
73 | | |
74 | 0 | fn evaluate_now(&mut self) { |
75 | | // If-check provides a fast short circuit for the common case of `extra` |
76 | | // being empty, which saves a round trip over the proc macro bridge. |
77 | | // Improves macro expansion time in winrt by 6% in debug mode. |
78 | 0 | if !self.extra.is_empty() { |
79 | 0 | self.stream.extend(self.extra.drain(..)); |
80 | 0 | } |
81 | 0 | } |
82 | | |
83 | 0 | fn into_token_stream(mut self) -> proc_macro::TokenStream { |
84 | 0 | self.evaluate_now(); |
85 | 0 | self.stream |
86 | 0 | } |
87 | | } |
88 | | |
89 | | impl TokenStream { |
90 | 0 | pub(crate) fn new() -> Self { |
91 | 0 | if inside_proc_macro() { |
92 | 0 | TokenStream::Compiler(DeferredTokenStream::new(proc_macro::TokenStream::new())) |
93 | | } else { |
94 | 0 | TokenStream::Fallback(fallback::TokenStream::new()) |
95 | | } |
96 | 0 | } |
97 | | |
98 | 439 | pub(crate) fn from_str_checked(src: &str) -> Result<Self, LexError> { |
99 | 439 | if inside_proc_macro() { |
100 | 0 | Ok(TokenStream::Compiler(DeferredTokenStream::new( |
101 | 0 | proc_macro::TokenStream::from_str_checked(src)?, |
102 | | ))) |
103 | | } else { |
104 | | Ok(TokenStream::Fallback( |
105 | 439 | fallback::TokenStream::from_str_checked(src)?, |
106 | | )) |
107 | | } |
108 | 439 | } |
109 | | |
110 | 0 | pub(crate) fn is_empty(&self) -> bool { |
111 | 0 | match self { |
112 | 0 | TokenStream::Compiler(tts) => tts.is_empty(), |
113 | 0 | TokenStream::Fallback(tts) => tts.is_empty(), |
114 | | } |
115 | 0 | } |
116 | | |
117 | 0 | fn unwrap_nightly(self) -> proc_macro::TokenStream { |
118 | 0 | match self { |
119 | 0 | TokenStream::Compiler(s) => s.into_token_stream(), |
120 | 0 | TokenStream::Fallback(_) => mismatch(line!()), |
121 | | } |
122 | 0 | } |
123 | | |
124 | 0 | fn unwrap_stable(self) -> fallback::TokenStream { |
125 | 0 | match self { |
126 | 0 | TokenStream::Compiler(_) => mismatch(line!()), |
127 | 0 | TokenStream::Fallback(s) => s, |
128 | | } |
129 | 0 | } |
130 | | } |
131 | | |
132 | | impl Display for TokenStream { |
133 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
134 | 0 | match self { |
135 | 0 | TokenStream::Compiler(tts) => Display::fmt(&tts.clone().into_token_stream(), f), |
136 | 0 | TokenStream::Fallback(tts) => Display::fmt(tts, f), |
137 | | } |
138 | 0 | } |
139 | | } |
140 | | |
141 | | impl From<proc_macro::TokenStream> for TokenStream { |
142 | 0 | fn from(inner: proc_macro::TokenStream) -> Self { |
143 | 0 | TokenStream::Compiler(DeferredTokenStream::new(inner)) |
144 | 0 | } |
145 | | } |
146 | | |
147 | | impl From<TokenStream> for proc_macro::TokenStream { |
148 | 0 | fn from(inner: TokenStream) -> Self { |
149 | 0 | match inner { |
150 | 0 | TokenStream::Compiler(inner) => inner.into_token_stream(), |
151 | 0 | TokenStream::Fallback(inner) => { |
152 | 0 | proc_macro::TokenStream::from_str_unchecked(&inner.to_string()) |
153 | | } |
154 | | } |
155 | 0 | } |
156 | | } |
157 | | |
158 | | impl From<fallback::TokenStream> for TokenStream { |
159 | 0 | fn from(inner: fallback::TokenStream) -> Self { |
160 | 0 | TokenStream::Fallback(inner) |
161 | 0 | } |
162 | | } |
163 | | |
164 | | // Assumes inside_proc_macro(). |
165 | 0 | fn into_compiler_token(token: TokenTree) -> proc_macro::TokenTree { |
166 | 0 | match token { |
167 | 0 | TokenTree::Group(tt) => proc_macro::TokenTree::Group(tt.inner.unwrap_nightly()), |
168 | 0 | TokenTree::Punct(tt) => { |
169 | 0 | let spacing = match tt.spacing() { |
170 | 0 | Spacing::Joint => proc_macro::Spacing::Joint, |
171 | 0 | Spacing::Alone => proc_macro::Spacing::Alone, |
172 | | }; |
173 | 0 | let mut punct = proc_macro::Punct::new(tt.as_char(), spacing); |
174 | 0 | punct.set_span(tt.span().inner.unwrap_nightly()); |
175 | 0 | proc_macro::TokenTree::Punct(punct) |
176 | | } |
177 | 0 | TokenTree::Ident(tt) => proc_macro::TokenTree::Ident(tt.inner.unwrap_nightly()), |
178 | 0 | TokenTree::Literal(tt) => proc_macro::TokenTree::Literal(tt.inner.unwrap_nightly()), |
179 | | } |
180 | 0 | } |
181 | | |
182 | | impl From<TokenTree> for TokenStream { |
183 | 0 | fn from(token: TokenTree) -> Self { |
184 | 0 | if inside_proc_macro() { |
185 | 0 | TokenStream::Compiler(DeferredTokenStream::new(proc_macro::TokenStream::from( |
186 | 0 | into_compiler_token(token), |
187 | 0 | ))) |
188 | | } else { |
189 | 0 | TokenStream::Fallback(fallback::TokenStream::from(token)) |
190 | | } |
191 | 0 | } |
192 | | } |
193 | | |
194 | | impl FromIterator<TokenTree> for TokenStream { |
195 | 0 | fn from_iter<I: IntoIterator<Item = TokenTree>>(tokens: I) -> Self { |
196 | 0 | if inside_proc_macro() { |
197 | 0 | TokenStream::Compiler(DeferredTokenStream::new( |
198 | 0 | tokens.into_iter().map(into_compiler_token).collect(), |
199 | 0 | )) |
200 | | } else { |
201 | 0 | TokenStream::Fallback(tokens.into_iter().collect()) |
202 | | } |
203 | 0 | } |
204 | | } |
205 | | |
206 | | impl FromIterator<TokenStream> for TokenStream { |
207 | 0 | fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self { |
208 | 0 | let mut streams = streams.into_iter(); |
209 | 0 | match streams.next() { |
210 | 0 | Some(TokenStream::Compiler(mut first)) => { |
211 | 0 | first.evaluate_now(); |
212 | 0 | first.stream.extend(streams.map(|s| match s { |
213 | 0 | TokenStream::Compiler(s) => s.into_token_stream(), |
214 | 0 | TokenStream::Fallback(_) => mismatch(line!()), |
215 | 0 | })); |
216 | 0 | TokenStream::Compiler(first) |
217 | | } |
218 | 0 | Some(TokenStream::Fallback(mut first)) => { |
219 | 0 | first.extend(streams.map(|s| match s { |
220 | 0 | TokenStream::Fallback(s) => s, |
221 | 0 | TokenStream::Compiler(_) => mismatch(line!()), |
222 | 0 | })); |
223 | 0 | TokenStream::Fallback(first) |
224 | | } |
225 | 0 | None => TokenStream::new(), |
226 | | } |
227 | 0 | } |
228 | | } |
229 | | |
230 | | impl Extend<TokenTree> for TokenStream { |
231 | 0 | fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, tokens: I) { |
232 | 0 | match self { |
233 | 0 | TokenStream::Compiler(tts) => { |
234 | | // Here is the reason for DeferredTokenStream. |
235 | 0 | for token in tokens { |
236 | 0 | tts.extra.push(into_compiler_token(token)); |
237 | 0 | } |
238 | | } |
239 | 0 | TokenStream::Fallback(tts) => tts.extend(tokens), |
240 | | } |
241 | 0 | } |
242 | | } |
243 | | |
244 | | impl Extend<TokenStream> for TokenStream { |
245 | 0 | fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) { |
246 | 0 | match self { |
247 | 0 | TokenStream::Compiler(tts) => { |
248 | 0 | tts.evaluate_now(); |
249 | 0 | tts.stream |
250 | 0 | .extend(streams.into_iter().map(TokenStream::unwrap_nightly)); |
251 | 0 | } |
252 | 0 | TokenStream::Fallback(tts) => { |
253 | 0 | tts.extend(streams.into_iter().map(TokenStream::unwrap_stable)); |
254 | 0 | } |
255 | | } |
256 | 0 | } |
257 | | } |
258 | | |
259 | | impl Debug for TokenStream { |
260 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
261 | 0 | match self { |
262 | 0 | TokenStream::Compiler(tts) => Debug::fmt(&tts.clone().into_token_stream(), f), |
263 | 0 | TokenStream::Fallback(tts) => Debug::fmt(tts, f), |
264 | | } |
265 | 0 | } |
266 | | } |
267 | | |
268 | | impl LexError { |
269 | 0 | pub(crate) fn span(&self) -> Span { |
270 | 0 | match self { |
271 | 0 | LexError::Compiler(_) | LexError::CompilerPanic => Span::call_site(), |
272 | 0 | LexError::Fallback(e) => Span::Fallback(e.span()), |
273 | | } |
274 | 0 | } |
275 | | } |
276 | | |
277 | | impl From<proc_macro::LexError> for LexError { |
278 | 0 | fn from(e: proc_macro::LexError) -> Self { |
279 | 0 | LexError::Compiler(e) |
280 | 0 | } |
281 | | } |
282 | | |
283 | | impl From<fallback::LexError> for LexError { |
284 | 293 | fn from(e: fallback::LexError) -> Self { |
285 | 293 | LexError::Fallback(e) |
286 | 293 | } |
287 | | } |
288 | | |
289 | | impl Debug for LexError { |
290 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
291 | 0 | match self { |
292 | 0 | LexError::Compiler(e) => Debug::fmt(e, f), |
293 | 0 | LexError::Fallback(e) => Debug::fmt(e, f), |
294 | | LexError::CompilerPanic => { |
295 | 0 | let fallback = fallback::LexError::call_site(); |
296 | 0 | Debug::fmt(&fallback, f) |
297 | | } |
298 | | } |
299 | 0 | } |
300 | | } |
301 | | |
302 | | impl Display for LexError { |
303 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
304 | 0 | match self { |
305 | 0 | LexError::Compiler(e) => Display::fmt(e, f), |
306 | 0 | LexError::Fallback(e) => Display::fmt(e, f), |
307 | | LexError::CompilerPanic => { |
308 | 0 | let fallback = fallback::LexError::call_site(); |
309 | 0 | Display::fmt(&fallback, f) |
310 | | } |
311 | | } |
312 | 0 | } |
313 | | } |
314 | | |
315 | | #[derive(Clone)] |
316 | | pub(crate) enum TokenTreeIter { |
317 | | Compiler(proc_macro::token_stream::IntoIter), |
318 | | Fallback(fallback::TokenTreeIter), |
319 | | } |
320 | | |
321 | | impl IntoIterator for TokenStream { |
322 | | type Item = TokenTree; |
323 | | type IntoIter = TokenTreeIter; |
324 | | |
325 | 0 | fn into_iter(self) -> TokenTreeIter { |
326 | 0 | match self { |
327 | 0 | TokenStream::Compiler(tts) => { |
328 | 0 | TokenTreeIter::Compiler(tts.into_token_stream().into_iter()) |
329 | | } |
330 | 0 | TokenStream::Fallback(tts) => TokenTreeIter::Fallback(tts.into_iter()), |
331 | | } |
332 | 0 | } |
333 | | } |
334 | | |
335 | | impl Iterator for TokenTreeIter { |
336 | | type Item = TokenTree; |
337 | | |
338 | 0 | fn next(&mut self) -> Option<TokenTree> { |
339 | 0 | let token = match self { |
340 | 0 | TokenTreeIter::Compiler(iter) => iter.next()?, |
341 | 0 | TokenTreeIter::Fallback(iter) => return iter.next(), |
342 | | }; |
343 | 0 | Some(match token { |
344 | 0 | proc_macro::TokenTree::Group(tt) => { |
345 | 0 | TokenTree::Group(crate::Group::_new(Group::Compiler(tt))) |
346 | | } |
347 | 0 | proc_macro::TokenTree::Punct(tt) => { |
348 | 0 | let spacing = match tt.spacing() { |
349 | 0 | proc_macro::Spacing::Joint => Spacing::Joint, |
350 | 0 | proc_macro::Spacing::Alone => Spacing::Alone, |
351 | | }; |
352 | 0 | let mut o = Punct::new(tt.as_char(), spacing); |
353 | 0 | o.set_span(crate::Span::_new(Span::Compiler(tt.span()))); |
354 | 0 | TokenTree::Punct(o) |
355 | | } |
356 | 0 | proc_macro::TokenTree::Ident(s) => { |
357 | 0 | TokenTree::Ident(crate::Ident::_new(Ident::Compiler(s))) |
358 | | } |
359 | 0 | proc_macro::TokenTree::Literal(l) => { |
360 | 0 | TokenTree::Literal(crate::Literal::_new(Literal::Compiler(l))) |
361 | | } |
362 | | }) |
363 | 0 | } |
364 | | |
365 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
366 | 0 | match self { |
367 | 0 | TokenTreeIter::Compiler(tts) => tts.size_hint(), |
368 | 0 | TokenTreeIter::Fallback(tts) => tts.size_hint(), |
369 | | } |
370 | 0 | } |
371 | | } |
372 | | |
373 | | #[derive(Copy, Clone)] |
374 | | pub(crate) enum Span { |
375 | | Compiler(proc_macro::Span), |
376 | | Fallback(fallback::Span), |
377 | | } |
378 | | |
379 | | impl Span { |
380 | 7.09M | pub(crate) fn call_site() -> Self { |
381 | 7.09M | if inside_proc_macro() { |
382 | 0 | Span::Compiler(proc_macro::Span::call_site()) |
383 | | } else { |
384 | 7.09M | Span::Fallback(fallback::Span::call_site()) |
385 | | } |
386 | 7.09M | } |
387 | | |
388 | 0 | pub(crate) fn mixed_site() -> Self { |
389 | 0 | if inside_proc_macro() { |
390 | 0 | Span::Compiler(proc_macro::Span::mixed_site()) |
391 | | } else { |
392 | 0 | Span::Fallback(fallback::Span::mixed_site()) |
393 | | } |
394 | 0 | } |
395 | | |
396 | | #[cfg(super_unstable)] |
397 | | pub(crate) fn def_site() -> Self { |
398 | | if inside_proc_macro() { |
399 | | Span::Compiler(proc_macro::Span::def_site()) |
400 | | } else { |
401 | | Span::Fallback(fallback::Span::def_site()) |
402 | | } |
403 | | } |
404 | | |
405 | 0 | pub(crate) fn resolved_at(&self, other: Span) -> Span { |
406 | 0 | match (self, other) { |
407 | 0 | (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.resolved_at(b)), |
408 | 0 | (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.resolved_at(b)), |
409 | 0 | (Span::Compiler(_), Span::Fallback(_)) => mismatch(line!()), |
410 | 0 | (Span::Fallback(_), Span::Compiler(_)) => mismatch(line!()), |
411 | | } |
412 | 0 | } |
413 | | |
414 | 0 | pub(crate) fn located_at(&self, other: Span) -> Span { |
415 | 0 | match (self, other) { |
416 | 0 | (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.located_at(b)), |
417 | 0 | (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.located_at(b)), |
418 | 0 | (Span::Compiler(_), Span::Fallback(_)) => mismatch(line!()), |
419 | 0 | (Span::Fallback(_), Span::Compiler(_)) => mismatch(line!()), |
420 | | } |
421 | 0 | } |
422 | | |
423 | 0 | pub(crate) fn unwrap(self) -> proc_macro::Span { |
424 | 0 | match self { |
425 | 0 | Span::Compiler(s) => s, |
426 | 0 | Span::Fallback(_) => panic!("proc_macro::Span is only available in procedural macros"), |
427 | | } |
428 | 0 | } |
429 | | |
430 | | #[cfg(span_locations)] |
431 | | pub(crate) fn byte_range(&self) -> Range<usize> { |
432 | | match self { |
433 | | #[cfg(proc_macro_span)] |
434 | | Span::Compiler(s) => proc_macro_span::byte_range(s), |
435 | | #[cfg(not(proc_macro_span))] |
436 | | Span::Compiler(_) => 0..0, |
437 | | Span::Fallback(s) => s.byte_range(), |
438 | | } |
439 | | } |
440 | | |
441 | | #[cfg(span_locations)] |
442 | | pub(crate) fn start(&self) -> LineColumn { |
443 | | match self { |
444 | | #[cfg(proc_macro_span_location)] |
445 | | Span::Compiler(s) => LineColumn { |
446 | | line: proc_macro_span_location::line(s), |
447 | | column: proc_macro_span_location::column(s).saturating_sub(1), |
448 | | }, |
449 | | #[cfg(not(proc_macro_span_location))] |
450 | | Span::Compiler(_) => LineColumn { line: 0, column: 0 }, |
451 | | Span::Fallback(s) => s.start(), |
452 | | } |
453 | | } |
454 | | |
455 | | #[cfg(span_locations)] |
456 | | pub(crate) fn end(&self) -> LineColumn { |
457 | | match self { |
458 | | #[cfg(proc_macro_span_location)] |
459 | | Span::Compiler(s) => { |
460 | | let end = proc_macro_span_location::end(s); |
461 | | LineColumn { |
462 | | line: proc_macro_span_location::line(&end), |
463 | | column: proc_macro_span_location::column(&end).saturating_sub(1), |
464 | | } |
465 | | } |
466 | | #[cfg(not(proc_macro_span_location))] |
467 | | Span::Compiler(_) => LineColumn { line: 0, column: 0 }, |
468 | | Span::Fallback(s) => s.end(), |
469 | | } |
470 | | } |
471 | | |
472 | | #[cfg(span_locations)] |
473 | | pub(crate) fn file(&self) -> String { |
474 | | match self { |
475 | | #[cfg(proc_macro_span_file)] |
476 | | Span::Compiler(s) => proc_macro_span_file::file(s), |
477 | | #[cfg(not(proc_macro_span_file))] |
478 | | Span::Compiler(_) => "<token stream>".to_owned(), |
479 | | Span::Fallback(s) => s.file(), |
480 | | } |
481 | | } |
482 | | |
483 | | #[cfg(span_locations)] |
484 | | pub(crate) fn local_file(&self) -> Option<PathBuf> { |
485 | | match self { |
486 | | #[cfg(proc_macro_span_file)] |
487 | | Span::Compiler(s) => proc_macro_span_file::local_file(s), |
488 | | #[cfg(not(proc_macro_span_file))] |
489 | | Span::Compiler(_) => None, |
490 | | Span::Fallback(s) => s.local_file(), |
491 | | } |
492 | | } |
493 | | |
494 | 0 | pub(crate) fn join(&self, other: Span) -> Option<Span> { |
495 | 0 | let ret = match (self, other) { |
496 | | #[cfg(proc_macro_span)] |
497 | 0 | (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(proc_macro_span::join(a, b)?), |
498 | 0 | (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.join(b)?), |
499 | 0 | _ => return None, |
500 | | }; |
501 | 0 | Some(ret) |
502 | 0 | } |
503 | | |
504 | | #[cfg(super_unstable)] |
505 | | pub(crate) fn eq(&self, other: &Span) -> bool { |
506 | | match (self, other) { |
507 | | (Span::Compiler(a), Span::Compiler(b)) => a.eq(b), |
508 | | (Span::Fallback(a), Span::Fallback(b)) => a.eq(b), |
509 | | _ => false, |
510 | | } |
511 | | } |
512 | | |
513 | 0 | pub(crate) fn source_text(&self) -> Option<String> { |
514 | 0 | match self { |
515 | | #[cfg(not(no_source_text))] |
516 | 0 | Span::Compiler(s) => s.source_text(), |
517 | | #[cfg(no_source_text)] |
518 | | Span::Compiler(_) => None, |
519 | 0 | Span::Fallback(s) => s.source_text(), |
520 | | } |
521 | 0 | } |
522 | | |
523 | 0 | fn unwrap_nightly(self) -> proc_macro::Span { |
524 | 0 | match self { |
525 | 0 | Span::Compiler(s) => s, |
526 | 0 | Span::Fallback(_) => mismatch(line!()), |
527 | | } |
528 | 0 | } |
529 | | } |
530 | | |
531 | | impl From<proc_macro::Span> for crate::Span { |
532 | 0 | fn from(proc_span: proc_macro::Span) -> Self { |
533 | 0 | crate::Span::_new(Span::Compiler(proc_span)) |
534 | 0 | } |
535 | | } |
536 | | |
537 | | impl From<fallback::Span> for Span { |
538 | 9.92M | fn from(inner: fallback::Span) -> Self { |
539 | 9.92M | Span::Fallback(inner) |
540 | 9.92M | } |
541 | | } |
542 | | |
543 | | impl Debug for Span { |
544 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
545 | 0 | match self { |
546 | 0 | Span::Compiler(s) => Debug::fmt(s, f), |
547 | 0 | Span::Fallback(s) => Debug::fmt(s, f), |
548 | | } |
549 | 0 | } |
550 | | } |
551 | | |
552 | 0 | pub(crate) fn debug_span_field_if_nontrivial(debug: &mut fmt::DebugStruct, span: Span) { |
553 | 0 | match span { |
554 | 0 | Span::Compiler(s) => { |
555 | 0 | debug.field("span", &s); |
556 | 0 | } |
557 | 0 | Span::Fallback(s) => fallback::debug_span_field_if_nontrivial(debug, s), |
558 | | } |
559 | 0 | } |
560 | | |
561 | | #[derive(Clone)] |
562 | | pub(crate) enum Group { |
563 | | Compiler(proc_macro::Group), |
564 | | Fallback(fallback::Group), |
565 | | } |
566 | | |
567 | | impl Group { |
568 | 0 | pub(crate) fn new(delimiter: Delimiter, stream: TokenStream) -> Self { |
569 | 0 | match stream { |
570 | 0 | TokenStream::Compiler(tts) => { |
571 | 0 | let delimiter = match delimiter { |
572 | 0 | Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis, |
573 | 0 | Delimiter::Bracket => proc_macro::Delimiter::Bracket, |
574 | 0 | Delimiter::Brace => proc_macro::Delimiter::Brace, |
575 | 0 | Delimiter::None => proc_macro::Delimiter::None, |
576 | | }; |
577 | 0 | Group::Compiler(proc_macro::Group::new(delimiter, tts.into_token_stream())) |
578 | | } |
579 | 0 | TokenStream::Fallback(stream) => { |
580 | 0 | Group::Fallback(fallback::Group::new(delimiter, stream)) |
581 | | } |
582 | | } |
583 | 0 | } |
584 | | |
585 | 0 | pub(crate) fn delimiter(&self) -> Delimiter { |
586 | 0 | match self { |
587 | 0 | Group::Compiler(g) => match g.delimiter() { |
588 | 0 | proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis, |
589 | 0 | proc_macro::Delimiter::Bracket => Delimiter::Bracket, |
590 | 0 | proc_macro::Delimiter::Brace => Delimiter::Brace, |
591 | 0 | proc_macro::Delimiter::None => Delimiter::None, |
592 | | }, |
593 | 0 | Group::Fallback(g) => g.delimiter(), |
594 | | } |
595 | 0 | } |
596 | | |
597 | 0 | pub(crate) fn stream(&self) -> TokenStream { |
598 | 0 | match self { |
599 | 0 | Group::Compiler(g) => TokenStream::Compiler(DeferredTokenStream::new(g.stream())), |
600 | 0 | Group::Fallback(g) => TokenStream::Fallback(g.stream()), |
601 | | } |
602 | 0 | } |
603 | | |
604 | 0 | pub(crate) fn span(&self) -> Span { |
605 | 0 | match self { |
606 | 0 | Group::Compiler(g) => Span::Compiler(g.span()), |
607 | 0 | Group::Fallback(g) => Span::Fallback(g.span()), |
608 | | } |
609 | 0 | } |
610 | | |
611 | 0 | pub(crate) fn span_open(&self) -> Span { |
612 | 0 | match self { |
613 | 0 | Group::Compiler(g) => Span::Compiler(g.span_open()), |
614 | 0 | Group::Fallback(g) => Span::Fallback(g.span_open()), |
615 | | } |
616 | 0 | } |
617 | | |
618 | 0 | pub(crate) fn span_close(&self) -> Span { |
619 | 0 | match self { |
620 | 0 | Group::Compiler(g) => Span::Compiler(g.span_close()), |
621 | 0 | Group::Fallback(g) => Span::Fallback(g.span_close()), |
622 | | } |
623 | 0 | } |
624 | | |
625 | 11.1k | pub(crate) fn set_span(&mut self, span: Span) { |
626 | 11.1k | match (self, span) { |
627 | 0 | (Group::Compiler(g), Span::Compiler(s)) => g.set_span(s), |
628 | 11.1k | (Group::Fallback(g), Span::Fallback(s)) => g.set_span(s), |
629 | 0 | (Group::Compiler(_), Span::Fallback(_)) => mismatch(line!()), |
630 | 0 | (Group::Fallback(_), Span::Compiler(_)) => mismatch(line!()), |
631 | | } |
632 | 11.1k | } |
633 | | |
634 | 0 | fn unwrap_nightly(self) -> proc_macro::Group { |
635 | 0 | match self { |
636 | 0 | Group::Compiler(g) => g, |
637 | 0 | Group::Fallback(_) => mismatch(line!()), |
638 | | } |
639 | 0 | } |
640 | | } |
641 | | |
642 | | impl From<fallback::Group> for Group { |
643 | 559k | fn from(g: fallback::Group) -> Self { |
644 | 559k | Group::Fallback(g) |
645 | 559k | } |
646 | | } |
647 | | |
648 | | impl Display for Group { |
649 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
650 | 0 | match self { |
651 | 0 | Group::Compiler(group) => Display::fmt(group, formatter), |
652 | 0 | Group::Fallback(group) => Display::fmt(group, formatter), |
653 | | } |
654 | 0 | } |
655 | | } |
656 | | |
657 | | impl Debug for Group { |
658 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
659 | 0 | match self { |
660 | 0 | Group::Compiler(group) => Debug::fmt(group, formatter), |
661 | 0 | Group::Fallback(group) => Debug::fmt(group, formatter), |
662 | | } |
663 | 0 | } |
664 | | } |
665 | | |
666 | | #[derive(Clone)] |
667 | | pub(crate) enum Ident { |
668 | | Compiler(proc_macro::Ident), |
669 | | Fallback(fallback::Ident), |
670 | | } |
671 | | |
672 | | impl Ident { |
673 | | #[track_caller] |
674 | 0 | pub(crate) fn new_checked(string: &str, span: Span) -> Self { |
675 | 0 | match span { |
676 | 0 | Span::Compiler(s) => Ident::Compiler(proc_macro::Ident::new(string, s)), |
677 | 0 | Span::Fallback(s) => Ident::Fallback(fallback::Ident::new_checked(string, s)), |
678 | | } |
679 | 0 | } |
680 | | |
681 | | #[track_caller] |
682 | 0 | pub(crate) fn new_raw_checked(string: &str, span: Span) -> Self { |
683 | 0 | match span { |
684 | 0 | Span::Compiler(s) => Ident::Compiler(proc_macro::Ident::new_raw(string, s)), |
685 | 0 | Span::Fallback(s) => Ident::Fallback(fallback::Ident::new_raw_checked(string, s)), |
686 | | } |
687 | 0 | } |
688 | | |
689 | 0 | pub(crate) fn span(&self) -> Span { |
690 | 0 | match self { |
691 | 0 | Ident::Compiler(t) => Span::Compiler(t.span()), |
692 | 0 | Ident::Fallback(t) => Span::Fallback(t.span()), |
693 | | } |
694 | 0 | } |
695 | | |
696 | 543k | pub(crate) fn set_span(&mut self, span: Span) { |
697 | 543k | match (self, span) { |
698 | 0 | (Ident::Compiler(t), Span::Compiler(s)) => t.set_span(s), |
699 | 543k | (Ident::Fallback(t), Span::Fallback(s)) => t.set_span(s), |
700 | 0 | (Ident::Compiler(_), Span::Fallback(_)) => mismatch(line!()), |
701 | 0 | (Ident::Fallback(_), Span::Compiler(_)) => mismatch(line!()), |
702 | | } |
703 | 543k | } |
704 | | |
705 | 0 | fn unwrap_nightly(self) -> proc_macro::Ident { |
706 | 0 | match self { |
707 | 0 | Ident::Compiler(s) => s, |
708 | 0 | Ident::Fallback(_) => mismatch(line!()), |
709 | | } |
710 | 0 | } |
711 | | } |
712 | | |
713 | | impl From<fallback::Ident> for Ident { |
714 | 555k | fn from(inner: fallback::Ident) -> Self { |
715 | 555k | Ident::Fallback(inner) |
716 | 555k | } |
717 | | } |
718 | | |
719 | | impl PartialEq for Ident { |
720 | 0 | fn eq(&self, other: &Ident) -> bool { |
721 | 0 | match (self, other) { |
722 | 0 | (Ident::Compiler(t), Ident::Compiler(o)) => t.to_string() == o.to_string(), |
723 | 0 | (Ident::Fallback(t), Ident::Fallback(o)) => t == o, |
724 | 0 | (Ident::Compiler(_), Ident::Fallback(_)) => mismatch(line!()), |
725 | 0 | (Ident::Fallback(_), Ident::Compiler(_)) => mismatch(line!()), |
726 | | } |
727 | 0 | } |
728 | | } |
729 | | |
730 | | impl<T> PartialEq<T> for Ident |
731 | | where |
732 | | T: ?Sized + AsRef<str>, |
733 | | { |
734 | 0 | fn eq(&self, other: &T) -> bool { |
735 | 0 | let other = other.as_ref(); |
736 | 0 | match self { |
737 | 0 | Ident::Compiler(t) => t.to_string() == other, |
738 | 0 | Ident::Fallback(t) => t == other, |
739 | | } |
740 | 0 | } |
741 | | } |
742 | | |
743 | | impl Display for Ident { |
744 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
745 | 0 | match self { |
746 | 0 | Ident::Compiler(t) => Display::fmt(t, f), |
747 | 0 | Ident::Fallback(t) => Display::fmt(t, f), |
748 | | } |
749 | 0 | } |
750 | | } |
751 | | |
752 | | impl Debug for Ident { |
753 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
754 | 0 | match self { |
755 | 0 | Ident::Compiler(t) => Debug::fmt(t, f), |
756 | 0 | Ident::Fallback(t) => Debug::fmt(t, f), |
757 | | } |
758 | 0 | } |
759 | | } |
760 | | |
761 | | #[derive(Clone)] |
762 | | pub(crate) enum Literal { |
763 | | Compiler(proc_macro::Literal), |
764 | | Fallback(fallback::Literal), |
765 | | } |
766 | | |
767 | | macro_rules! suffixed_numbers { |
768 | | ($($name:ident => $kind:ident,)*) => ($( |
769 | 0 | pub(crate) fn $name(n: $kind) -> Literal { |
770 | 0 | if inside_proc_macro() { |
771 | 0 | Literal::Compiler(proc_macro::Literal::$name(n)) |
772 | | } else { |
773 | 0 | Literal::Fallback(fallback::Literal::$name(n)) |
774 | | } |
775 | 0 | } Unexecuted instantiation: <proc_macro2::imp::Literal>::i8_suffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::u8_suffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::f32_suffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::f64_suffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::i16_suffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::i32_suffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::i64_suffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::u16_suffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::u32_suffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::u64_suffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::i128_suffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::u128_suffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::isize_suffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::usize_suffixed |
776 | | )*) |
777 | | } |
778 | | |
779 | | macro_rules! unsuffixed_integers { |
780 | | ($($name:ident => $kind:ident,)*) => ($( |
781 | 0 | pub(crate) fn $name(n: $kind) -> Literal { |
782 | 0 | if inside_proc_macro() { |
783 | 0 | Literal::Compiler(proc_macro::Literal::$name(n)) |
784 | | } else { |
785 | 0 | Literal::Fallback(fallback::Literal::$name(n)) |
786 | | } |
787 | 0 | } Unexecuted instantiation: <proc_macro2::imp::Literal>::i8_unsuffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::u8_unsuffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::i16_unsuffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::i32_unsuffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::i64_unsuffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::u16_unsuffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::u32_unsuffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::u64_unsuffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::i128_unsuffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::u128_unsuffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::isize_unsuffixed Unexecuted instantiation: <proc_macro2::imp::Literal>::usize_unsuffixed |
788 | | )*) |
789 | | } |
790 | | |
791 | | impl Literal { |
792 | 0 | pub(crate) fn from_str_checked(repr: &str) -> Result<Self, LexError> { |
793 | 0 | if inside_proc_macro() { |
794 | 0 | let literal = proc_macro::Literal::from_str_checked(repr)?; |
795 | 0 | Ok(Literal::Compiler(literal)) |
796 | | } else { |
797 | 0 | let literal = fallback::Literal::from_str_checked(repr)?; |
798 | 0 | Ok(Literal::Fallback(literal)) |
799 | | } |
800 | 0 | } |
801 | | |
802 | 0 | pub(crate) unsafe fn from_str_unchecked(repr: &str) -> Self { |
803 | 0 | if inside_proc_macro() { |
804 | 0 | Literal::Compiler(proc_macro::Literal::from_str_unchecked(repr)) |
805 | | } else { |
806 | 0 | Literal::Fallback(unsafe { fallback::Literal::from_str_unchecked(repr) }) |
807 | | } |
808 | 0 | } |
809 | | |
810 | | suffixed_numbers! { |
811 | | u8_suffixed => u8, |
812 | | u16_suffixed => u16, |
813 | | u32_suffixed => u32, |
814 | | u64_suffixed => u64, |
815 | | u128_suffixed => u128, |
816 | | usize_suffixed => usize, |
817 | | i8_suffixed => i8, |
818 | | i16_suffixed => i16, |
819 | | i32_suffixed => i32, |
820 | | i64_suffixed => i64, |
821 | | i128_suffixed => i128, |
822 | | isize_suffixed => isize, |
823 | | |
824 | | f32_suffixed => f32, |
825 | | f64_suffixed => f64, |
826 | | } |
827 | | |
828 | | unsuffixed_integers! { |
829 | | u8_unsuffixed => u8, |
830 | | u16_unsuffixed => u16, |
831 | | u32_unsuffixed => u32, |
832 | | u64_unsuffixed => u64, |
833 | | u128_unsuffixed => u128, |
834 | | usize_unsuffixed => usize, |
835 | | i8_unsuffixed => i8, |
836 | | i16_unsuffixed => i16, |
837 | | i32_unsuffixed => i32, |
838 | | i64_unsuffixed => i64, |
839 | | i128_unsuffixed => i128, |
840 | | isize_unsuffixed => isize, |
841 | | } |
842 | | |
843 | 0 | pub(crate) fn f32_unsuffixed(f: f32) -> Literal { |
844 | 0 | if inside_proc_macro() { |
845 | 0 | Literal::Compiler(proc_macro::Literal::f32_unsuffixed(f)) |
846 | | } else { |
847 | 0 | Literal::Fallback(fallback::Literal::f32_unsuffixed(f)) |
848 | | } |
849 | 0 | } |
850 | | |
851 | 0 | pub(crate) fn f64_unsuffixed(f: f64) -> Literal { |
852 | 0 | if inside_proc_macro() { |
853 | 0 | Literal::Compiler(proc_macro::Literal::f64_unsuffixed(f)) |
854 | | } else { |
855 | 0 | Literal::Fallback(fallback::Literal::f64_unsuffixed(f)) |
856 | | } |
857 | 0 | } |
858 | | |
859 | 0 | pub(crate) fn string(string: &str) -> Literal { |
860 | 0 | if inside_proc_macro() { |
861 | 0 | Literal::Compiler(proc_macro::Literal::string(string)) |
862 | | } else { |
863 | 0 | Literal::Fallback(fallback::Literal::string(string)) |
864 | | } |
865 | 0 | } |
866 | | |
867 | 0 | pub(crate) fn character(ch: char) -> Literal { |
868 | 0 | if inside_proc_macro() { |
869 | 0 | Literal::Compiler(proc_macro::Literal::character(ch)) |
870 | | } else { |
871 | 0 | Literal::Fallback(fallback::Literal::character(ch)) |
872 | | } |
873 | 0 | } |
874 | | |
875 | 0 | pub(crate) fn byte_character(byte: u8) -> Literal { |
876 | 0 | if inside_proc_macro() { |
877 | 0 | Literal::Compiler({ |
878 | 0 | #[cfg(not(no_literal_byte_character))] |
879 | 0 | { |
880 | 0 | proc_macro::Literal::byte_character(byte) |
881 | 0 | } |
882 | 0 |
|
883 | 0 | #[cfg(no_literal_byte_character)] |
884 | 0 | { |
885 | 0 | let fallback = fallback::Literal::byte_character(byte); |
886 | 0 | proc_macro::Literal::from_str_unchecked(&fallback.repr) |
887 | 0 | } |
888 | 0 | }) |
889 | | } else { |
890 | 0 | Literal::Fallback(fallback::Literal::byte_character(byte)) |
891 | | } |
892 | 0 | } |
893 | | |
894 | 0 | pub(crate) fn byte_string(bytes: &[u8]) -> Literal { |
895 | 0 | if inside_proc_macro() { |
896 | 0 | Literal::Compiler(proc_macro::Literal::byte_string(bytes)) |
897 | | } else { |
898 | 0 | Literal::Fallback(fallback::Literal::byte_string(bytes)) |
899 | | } |
900 | 0 | } |
901 | | |
902 | 0 | pub(crate) fn c_string(string: &CStr) -> Literal { |
903 | 0 | if inside_proc_macro() { |
904 | 0 | Literal::Compiler({ |
905 | 0 | #[cfg(not(no_literal_c_string))] |
906 | 0 | { |
907 | 0 | proc_macro::Literal::c_string(string) |
908 | 0 | } |
909 | 0 |
|
910 | 0 | #[cfg(no_literal_c_string)] |
911 | 0 | { |
912 | 0 | let fallback = fallback::Literal::c_string(string); |
913 | 0 | proc_macro::Literal::from_str_unchecked(&fallback.repr) |
914 | 0 | } |
915 | 0 | }) |
916 | | } else { |
917 | 0 | Literal::Fallback(fallback::Literal::c_string(string)) |
918 | | } |
919 | 0 | } |
920 | | |
921 | 0 | pub(crate) fn span(&self) -> Span { |
922 | 0 | match self { |
923 | 0 | Literal::Compiler(lit) => Span::Compiler(lit.span()), |
924 | 0 | Literal::Fallback(lit) => Span::Fallback(lit.span()), |
925 | | } |
926 | 0 | } |
927 | | |
928 | 2.30M | pub(crate) fn set_span(&mut self, span: Span) { |
929 | 2.30M | match (self, span) { |
930 | 0 | (Literal::Compiler(lit), Span::Compiler(s)) => lit.set_span(s), |
931 | 2.30M | (Literal::Fallback(lit), Span::Fallback(s)) => lit.set_span(s), |
932 | 0 | (Literal::Compiler(_), Span::Fallback(_)) => mismatch(line!()), |
933 | 0 | (Literal::Fallback(_), Span::Compiler(_)) => mismatch(line!()), |
934 | | } |
935 | 2.30M | } |
936 | | |
937 | 0 | pub(crate) fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> { |
938 | 0 | match self { |
939 | | #[cfg(proc_macro_span)] |
940 | 0 | Literal::Compiler(lit) => proc_macro_span::subspan(lit, range).map(Span::Compiler), |
941 | | #[cfg(not(proc_macro_span))] |
942 | | Literal::Compiler(_lit) => None, |
943 | 0 | Literal::Fallback(lit) => lit.subspan(range).map(Span::Fallback), |
944 | | } |
945 | 0 | } |
946 | | |
947 | 0 | fn unwrap_nightly(self) -> proc_macro::Literal { |
948 | 0 | match self { |
949 | 0 | Literal::Compiler(s) => s, |
950 | 0 | Literal::Fallback(_) => mismatch(line!()), |
951 | | } |
952 | 0 | } |
953 | | } |
954 | | |
955 | | impl From<fallback::Literal> for Literal { |
956 | 2.30M | fn from(s: fallback::Literal) -> Self { |
957 | 2.30M | Literal::Fallback(s) |
958 | 2.30M | } |
959 | | } |
960 | | |
961 | | impl Display for Literal { |
962 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
963 | 0 | match self { |
964 | 0 | Literal::Compiler(t) => Display::fmt(t, f), |
965 | 0 | Literal::Fallback(t) => Display::fmt(t, f), |
966 | | } |
967 | 0 | } |
968 | | } |
969 | | |
970 | | impl Debug for Literal { |
971 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
972 | 0 | match self { |
973 | 0 | Literal::Compiler(t) => Debug::fmt(t, f), |
974 | 0 | Literal::Fallback(t) => Debug::fmt(t, f), |
975 | | } |
976 | 0 | } |
977 | | } |
978 | | |
979 | | #[cfg(span_locations)] |
980 | | pub(crate) fn invalidate_current_thread_spans() { |
981 | | if inside_proc_macro() { |
982 | | panic!( |
983 | | "proc_macro2::extra::invalidate_current_thread_spans is not available in procedural macros" |
984 | | ); |
985 | | } else { |
986 | | crate::fallback::invalidate_current_thread_spans(); |
987 | | } |
988 | | } |