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