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