blob: c45dff89d76cc26539862f3e48e2308fc047d449 [file] [log] [blame]
Alex Crichtoncbec8ec2017-06-02 13:19:33 -07001use std::fmt;
2use std::iter;
David Tolnay78ef7732018-09-01 19:20:23 -07003use std::panic::{self, PanicInfo};
David Tolnay9cd3b4c2018-11-11 16:47:32 -08004#[cfg(super_unstable)]
5use std::path::PathBuf;
David Tolnay3d9d6ad2018-05-18 10:51:55 -07006use std::str::FromStr;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -07007
David Tolnayaef075b2019-01-16 16:29:18 -08008use fallback;
David Tolnay471a7ed2019-01-19 21:00:11 -08009use proc_macro;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070010
David Tolnayf14813f2018-09-08 17:14:07 -070011use {Delimiter, Punct, Spacing, TokenTree};
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070012
13#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -070014pub enum TokenStream {
David Tolnay9204bac2019-01-19 19:26:17 -080015 Compiler(proc_macro::TokenStream),
16 Fallback(fallback::TokenStream),
Alex Crichton30a4e9e2018-04-27 17:02:19 -070017}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070018
Alex Crichton30a4e9e2018-04-27 17:02:19 -070019pub enum LexError {
David Tolnay9204bac2019-01-19 19:26:17 -080020 Compiler(proc_macro::LexError),
21 Fallback(fallback::LexError),
Alex Crichton30a4e9e2018-04-27 17:02:19 -070022}
23
24fn nightly_works() -> bool {
25 use std::sync::atomic::*;
David Tolnay2ed15d52018-09-01 08:46:12 -070026 use std::sync::Once;
27
Alex Crichton30a4e9e2018-04-27 17:02:19 -070028 static WORKS: AtomicUsize = ATOMIC_USIZE_INIT;
David Tolnay2ed15d52018-09-01 08:46:12 -070029 static INIT: Once = Once::new();
Alex Crichton30a4e9e2018-04-27 17:02:19 -070030
31 match WORKS.load(Ordering::SeqCst) {
32 1 => return false,
33 2 => return true,
34 _ => {}
35 }
David Tolnay2ed15d52018-09-01 08:46:12 -070036
37 // Swap in a null panic hook to avoid printing "thread panicked" to stderr,
38 // then use catch_unwind to determine whether the compiler's proc_macro is
39 // working. When proc-macro2 is used from outside of a procedural macro all
40 // of the proc_macro crate's APIs currently panic.
41 //
42 // The Once is to prevent the possibility of this ordering:
43 //
44 // thread 1 calls take_hook, gets the user's original hook
45 // thread 1 calls set_hook with the null hook
46 // thread 2 calls take_hook, thinks null hook is the original hook
47 // thread 2 calls set_hook with the null hook
48 // thread 1 calls set_hook with the actual original hook
49 // thread 2 calls set_hook with what it thinks is the original hook
50 //
51 // in which the user's hook has been lost.
52 //
53 // There is still a race condition where a panic in a different thread can
54 // happen during the interval that the user's original panic hook is
55 // unregistered such that their hook is incorrectly not called. This is
56 // sufficiently unlikely and less bad than printing panic messages to stderr
57 // on correct use of this crate. Maybe there is a libstd feature request
58 // here. For now, if a user needs to guarantee that this failure mode does
59 // not occur, they need to call e.g. `proc_macro2::Span::call_site()` from
60 // the main thread before launching any other threads.
61 INIT.call_once(|| {
David Tolnay78ef7732018-09-01 19:20:23 -070062 type PanicHook = Fn(&PanicInfo) + Sync + Send + 'static;
63
64 let null_hook: Box<PanicHook> = Box::new(|_panic_info| { /* ignore */ });
65 let sanity_check = &*null_hook as *const PanicHook;
David Tolnay2ed15d52018-09-01 08:46:12 -070066 let original_hook = panic::take_hook();
David Tolnay78ef7732018-09-01 19:20:23 -070067 panic::set_hook(null_hook);
68
David Tolnay2ed15d52018-09-01 08:46:12 -070069 let works = panic::catch_unwind(|| proc_macro::Span::call_site()).is_ok();
70 WORKS.store(works as usize + 1, Ordering::SeqCst);
David Tolnay78ef7732018-09-01 19:20:23 -070071
72 let hopefully_null_hook = panic::take_hook();
David Tolnay2ed15d52018-09-01 08:46:12 -070073 panic::set_hook(original_hook);
David Tolnay78ef7732018-09-01 19:20:23 -070074 if sanity_check != &*hopefully_null_hook {
75 panic!("observed race condition in proc_macro2::nightly_works");
76 }
David Tolnay2ed15d52018-09-01 08:46:12 -070077 });
78 nightly_works()
Alex Crichton30a4e9e2018-04-27 17:02:19 -070079}
80
81fn mismatch() -> ! {
82 panic!("stable/nightly mismatch")
83}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070084
85impl TokenStream {
David Tolnayc3bb4592018-05-28 20:09:44 -070086 pub fn new() -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070087 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -080088 TokenStream::Compiler(proc_macro::TokenStream::new())
Alex Crichton30a4e9e2018-04-27 17:02:19 -070089 } else {
David Tolnay9204bac2019-01-19 19:26:17 -080090 TokenStream::Fallback(fallback::TokenStream::new())
Alex Crichton30a4e9e2018-04-27 17:02:19 -070091 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070092 }
93
94 pub fn is_empty(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070095 match self {
David Tolnay9204bac2019-01-19 19:26:17 -080096 TokenStream::Compiler(tts) => tts.is_empty(),
97 TokenStream::Fallback(tts) => tts.is_empty(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -070098 }
99 }
100
101 fn unwrap_nightly(self) -> proc_macro::TokenStream {
102 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800103 TokenStream::Compiler(s) => s,
104 TokenStream::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700105 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700106 }
David Tolnay5c58c532018-08-13 11:33:51 -0700107
David Tolnayaef075b2019-01-16 16:29:18 -0800108 fn unwrap_stable(self) -> fallback::TokenStream {
David Tolnay5c58c532018-08-13 11:33:51 -0700109 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800110 TokenStream::Compiler(_) => mismatch(),
111 TokenStream::Fallback(s) => s,
David Tolnay5c58c532018-08-13 11:33:51 -0700112 }
113 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700114}
115
116impl FromStr for TokenStream {
117 type Err = LexError;
118
119 fn from_str(src: &str) -> Result<TokenStream, LexError> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700120 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800121 Ok(TokenStream::Compiler(src.parse()?))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700122 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800123 Ok(TokenStream::Fallback(src.parse()?))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700124 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700125 }
126}
127
128impl fmt::Display for TokenStream {
129 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700130 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800131 TokenStream::Compiler(tts) => tts.fmt(f),
132 TokenStream::Fallback(tts) => tts.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700133 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700134 }
135}
136
137impl From<proc_macro::TokenStream> for TokenStream {
138 fn from(inner: proc_macro::TokenStream) -> TokenStream {
David Tolnay9204bac2019-01-19 19:26:17 -0800139 TokenStream::Compiler(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700140 }
141}
142
143impl From<TokenStream> for proc_macro::TokenStream {
144 fn from(inner: TokenStream) -> proc_macro::TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700145 match inner {
David Tolnay9204bac2019-01-19 19:26:17 -0800146 TokenStream::Compiler(inner) => inner,
147 TokenStream::Fallback(inner) => inner.to_string().parse().unwrap(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700148 }
149 }
150}
151
David Tolnayaef075b2019-01-16 16:29:18 -0800152impl From<fallback::TokenStream> for TokenStream {
153 fn from(inner: fallback::TokenStream) -> TokenStream {
David Tolnay9204bac2019-01-19 19:26:17 -0800154 TokenStream::Fallback(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700155 }
156}
157
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700158impl From<TokenTree> for TokenStream {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700159 fn from(token: TokenTree) -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700160 if !nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800161 return TokenStream::Fallback(token.into());
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700162 }
Alex Crichton9cd80a62018-04-05 17:46:58 -0700163 let tt: proc_macro::TokenTree = match token {
David Tolnayf14813f2018-09-08 17:14:07 -0700164 TokenTree::Group(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichtonf3888432018-05-16 09:11:05 -0700165 TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700166 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700167 Spacing::Joint => proc_macro::Spacing::Joint,
168 Spacing::Alone => proc_macro::Spacing::Alone,
169 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700170 let mut op = proc_macro::Punct::new(tt.as_char(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700171 op.set_span(tt.span().inner.unwrap_nightly());
Alex Crichton9cd80a62018-04-05 17:46:58 -0700172 op.into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700173 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700174 TokenTree::Ident(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700175 TokenTree::Literal(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700176 };
David Tolnay9204bac2019-01-19 19:26:17 -0800177 TokenStream::Compiler(tt.into())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700178 }
179}
180
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700181impl iter::FromIterator<TokenTree> for TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700182 fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self {
183 if nightly_works() {
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700184 let trees = trees
185 .into_iter()
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700186 .map(TokenStream::from)
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700187 .flat_map(|t| match t {
David Tolnay9204bac2019-01-19 19:26:17 -0800188 TokenStream::Compiler(s) => s,
189 TokenStream::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700190 });
David Tolnay9204bac2019-01-19 19:26:17 -0800191 TokenStream::Compiler(trees.collect())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700192 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800193 TokenStream::Fallback(trees.into_iter().collect())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700194 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700195 }
196}
197
Alex Crichton53b00672018-09-06 17:16:10 -0700198impl iter::FromIterator<TokenStream> for TokenStream {
199 fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self {
200 let mut streams = streams.into_iter();
201 match streams.next() {
202 #[cfg(slow_extend)]
David Tolnay9204bac2019-01-19 19:26:17 -0800203 Some(TokenStream::Compiler(first)) => {
David Tolnay4453fcc2019-01-16 12:15:01 -0800204 let stream = iter::once(first)
205 .chain(streams.map(|s| match s {
David Tolnay9204bac2019-01-19 19:26:17 -0800206 TokenStream::Compiler(s) => s,
207 TokenStream::Fallback(_) => mismatch(),
David Tolnay4453fcc2019-01-16 12:15:01 -0800208 }))
209 .collect();
David Tolnay9204bac2019-01-19 19:26:17 -0800210 TokenStream::Compiler(stream)
Alex Crichton53b00672018-09-06 17:16:10 -0700211 }
212 #[cfg(not(slow_extend))]
David Tolnay9204bac2019-01-19 19:26:17 -0800213 Some(TokenStream::Compiler(mut first)) => {
David Tolnay4453fcc2019-01-16 12:15:01 -0800214 first.extend(streams.map(|s| match s {
David Tolnay9204bac2019-01-19 19:26:17 -0800215 TokenStream::Compiler(s) => s,
216 TokenStream::Fallback(_) => mismatch(),
Alex Crichton53b00672018-09-06 17:16:10 -0700217 }));
David Tolnay9204bac2019-01-19 19:26:17 -0800218 TokenStream::Compiler(first)
Alex Crichton53b00672018-09-06 17:16:10 -0700219 }
David Tolnay9204bac2019-01-19 19:26:17 -0800220 Some(TokenStream::Fallback(mut first)) => {
David Tolnay4453fcc2019-01-16 12:15:01 -0800221 first.extend(streams.map(|s| match s {
David Tolnay9204bac2019-01-19 19:26:17 -0800222 TokenStream::Fallback(s) => s,
223 TokenStream::Compiler(_) => mismatch(),
Alex Crichton53b00672018-09-06 17:16:10 -0700224 }));
David Tolnay9204bac2019-01-19 19:26:17 -0800225 TokenStream::Fallback(first)
Alex Crichton53b00672018-09-06 17:16:10 -0700226 }
227 None => TokenStream::new(),
Alex Crichton53b00672018-09-06 17:16:10 -0700228 }
229 }
230}
231
Alex Crichtonf3888432018-05-16 09:11:05 -0700232impl Extend<TokenTree> for TokenStream {
233 fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
234 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800235 TokenStream::Compiler(tts) => {
David Tolnaye839e4f2018-09-06 09:36:43 -0700236 #[cfg(not(slow_extend))]
237 {
238 tts.extend(
239 streams
240 .into_iter()
241 .map(|t| TokenStream::from(t).unwrap_nightly()),
242 );
243 }
244 #[cfg(slow_extend)]
245 {
David Tolnay4453fcc2019-01-16 12:15:01 -0800246 *tts =
247 tts.clone()
248 .into_iter()
249 .chain(streams.into_iter().map(TokenStream::from).flat_map(
250 |t| match t {
David Tolnay9204bac2019-01-19 19:26:17 -0800251 TokenStream::Compiler(tts) => tts.into_iter(),
David Tolnaye839e4f2018-09-06 09:36:43 -0700252 _ => mismatch(),
David Tolnay4453fcc2019-01-16 12:15:01 -0800253 },
254 ))
255 .collect();
David Tolnaye839e4f2018-09-06 09:36:43 -0700256 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700257 }
David Tolnay9204bac2019-01-19 19:26:17 -0800258 TokenStream::Fallback(tts) => tts.extend(streams),
Alex Crichtonf3888432018-05-16 09:11:05 -0700259 }
260 }
261}
262
David Tolnay5c58c532018-08-13 11:33:51 -0700263impl Extend<TokenStream> for TokenStream {
264 fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
265 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800266 TokenStream::Compiler(tts) => {
David Tolnaye839e4f2018-09-06 09:36:43 -0700267 #[cfg(not(slow_extend))]
268 {
269 tts.extend(streams.into_iter().map(|stream| stream.unwrap_nightly()));
270 }
271 #[cfg(slow_extend)]
272 {
273 *tts = tts
274 .clone()
275 .into_iter()
David Tolnay4453fcc2019-01-16 12:15:01 -0800276 .chain(streams.into_iter().flat_map(|t| match t {
David Tolnay9204bac2019-01-19 19:26:17 -0800277 TokenStream::Compiler(tts) => tts.into_iter(),
David Tolnay4453fcc2019-01-16 12:15:01 -0800278 _ => mismatch(),
279 }))
280 .collect();
David Tolnaye839e4f2018-09-06 09:36:43 -0700281 }
David Tolnay5c58c532018-08-13 11:33:51 -0700282 }
David Tolnay9204bac2019-01-19 19:26:17 -0800283 TokenStream::Fallback(tts) => {
David Tolnay5c58c532018-08-13 11:33:51 -0700284 tts.extend(streams.into_iter().map(|stream| stream.unwrap_stable()))
285 }
286 }
287 }
288}
289
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700290impl fmt::Debug for TokenStream {
291 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700292 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800293 TokenStream::Compiler(tts) => tts.fmt(f),
294 TokenStream::Fallback(tts) => tts.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700295 }
296 }
297}
298
299impl From<proc_macro::LexError> for LexError {
300 fn from(e: proc_macro::LexError) -> LexError {
David Tolnay9204bac2019-01-19 19:26:17 -0800301 LexError::Compiler(e)
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700302 }
303}
304
David Tolnayaef075b2019-01-16 16:29:18 -0800305impl From<fallback::LexError> for LexError {
306 fn from(e: fallback::LexError) -> LexError {
David Tolnay9204bac2019-01-19 19:26:17 -0800307 LexError::Fallback(e)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700308 }
309}
310
311impl fmt::Debug for LexError {
312 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700313 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800314 LexError::Compiler(e) => e.fmt(f),
315 LexError::Fallback(e) => e.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700316 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700317 }
318}
319
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700320pub enum TokenTreeIter {
David Tolnay9204bac2019-01-19 19:26:17 -0800321 Compiler(proc_macro::token_stream::IntoIter),
322 Fallback(fallback::TokenTreeIter),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700323}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700324
325impl IntoIterator for TokenStream {
326 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700327 type IntoIter = TokenTreeIter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700328
Alex Crichton1a7f7622017-07-05 17:47:15 -0700329 fn into_iter(self) -> TokenTreeIter {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700330 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800331 TokenStream::Compiler(tts) => TokenTreeIter::Compiler(tts.into_iter()),
332 TokenStream::Fallback(tts) => TokenTreeIter::Fallback(tts.into_iter()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700333 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700334 }
335}
336
Alex Crichton1a7f7622017-07-05 17:47:15 -0700337impl Iterator for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700338 type Item = TokenTree;
339
340 fn next(&mut self) -> Option<TokenTree> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700341 let token = match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800342 TokenTreeIter::Compiler(iter) => iter.next()?,
343 TokenTreeIter::Fallback(iter) => return iter.next(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700344 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700345 Some(match token {
David Tolnay9204bac2019-01-19 19:26:17 -0800346 proc_macro::TokenTree::Group(tt) => ::Group::_new(Group::Compiler(tt)).into(),
Alex Crichtonf3888432018-05-16 09:11:05 -0700347 proc_macro::TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700348 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700349 proc_macro::Spacing::Joint => Spacing::Joint,
350 proc_macro::Spacing::Alone => Spacing::Alone,
351 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700352 let mut o = Punct::new(tt.as_char(), spacing);
David Tolnay9204bac2019-01-19 19:26:17 -0800353 o.set_span(::Span::_new(Span::Compiler(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700354 o.into()
355 }
David Tolnay9204bac2019-01-19 19:26:17 -0800356 proc_macro::TokenTree::Ident(s) => ::Ident::_new(Ident::Compiler(s)).into(),
357 proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Compiler(l)).into(),
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700358 })
359 }
360
361 fn size_hint(&self) -> (usize, Option<usize>) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700362 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800363 TokenTreeIter::Compiler(tts) => tts.size_hint(),
364 TokenTreeIter::Fallback(tts) => tts.size_hint(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700365 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700366 }
367}
368
Alex Crichton1a7f7622017-07-05 17:47:15 -0700369impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700370 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700371 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700372 }
373}
374
Nika Layzellb35a9a32017-12-30 14:34:35 -0500375#[derive(Clone, PartialEq, Eq)]
Alex Crichtonce0904d2018-08-27 17:29:49 -0700376#[cfg(super_unstable)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700377pub enum SourceFile {
David Tolnay9204bac2019-01-19 19:26:17 -0800378 Compiler(proc_macro::SourceFile),
379 Fallback(fallback::SourceFile),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700380}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500381
Alex Crichtonce0904d2018-08-27 17:29:49 -0700382#[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500383impl SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700384 fn nightly(sf: proc_macro::SourceFile) -> Self {
David Tolnay9204bac2019-01-19 19:26:17 -0800385 SourceFile::Compiler(sf)
Nika Layzellb35a9a32017-12-30 14:34:35 -0500386 }
387
Nika Layzellf8d5f212017-12-11 14:07:02 -0500388 /// Get the path to this source file as a string.
David Tolnay9cd3b4c2018-11-11 16:47:32 -0800389 pub fn path(&self) -> PathBuf {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700390 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800391 SourceFile::Compiler(a) => a.path(),
392 SourceFile::Fallback(a) => a.path(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700393 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500394 }
395
396 pub fn is_real(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700397 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800398 SourceFile::Compiler(a) => a.is_real(),
399 SourceFile::Fallback(a) => a.is_real(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700400 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500401 }
402}
403
Alex Crichtonce0904d2018-08-27 17:29:49 -0700404#[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500405impl fmt::Debug for SourceFile {
406 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700407 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800408 SourceFile::Compiler(a) => a.fmt(f),
409 SourceFile::Fallback(a) => a.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700410 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500411 }
412}
413
David Tolnay3b1f7d22019-01-28 12:22:11 -0800414#[cfg(any(super_unstable, feature = "span-locations"))]
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500415pub struct LineColumn {
416 pub line: usize,
417 pub column: usize,
418}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500419
Alex Crichton9cd80a62018-04-05 17:46:58 -0700420#[derive(Copy, Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700421pub enum Span {
David Tolnay9204bac2019-01-19 19:26:17 -0800422 Compiler(proc_macro::Span),
423 Fallback(fallback::Span),
Sergio Benitez13805082018-01-04 01:25:45 -0800424}
425
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700426impl Span {
427 pub fn call_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700428 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800429 Span::Compiler(proc_macro::Span::call_site())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700430 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800431 Span::Fallback(fallback::Span::call_site())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700432 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700433 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700434
Alex Crichtonce0904d2018-08-27 17:29:49 -0700435 #[cfg(super_unstable)]
Alex Crichtone6085b72017-11-21 07:24:25 -0800436 pub fn def_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700437 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800438 Span::Compiler(proc_macro::Span::def_site())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700439 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800440 Span::Fallback(fallback::Span::def_site())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700441 }
Alex Crichton998f6422017-11-19 08:06:27 -0800442 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500443
Alex Crichtonce0904d2018-08-27 17:29:49 -0700444 #[cfg(super_unstable)]
David Tolnay4e8e3972018-01-05 18:10:22 -0800445 pub fn resolved_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700446 match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800447 (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.resolved_at(b)),
448 (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.resolved_at(b)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700449 _ => mismatch(),
450 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800451 }
452
Alex Crichtonce0904d2018-08-27 17:29:49 -0700453 #[cfg(super_unstable)]
David Tolnay4e8e3972018-01-05 18:10:22 -0800454 pub fn located_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700455 match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800456 (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.located_at(b)),
457 (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.located_at(b)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700458 _ => mismatch(),
459 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800460 }
461
David Tolnay40bbb1c2019-01-19 19:43:55 -0800462 pub fn unwrap(self) -> proc_macro::Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700463 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800464 Span::Compiler(s) => s,
465 Span::Fallback(_) => panic!("proc_macro::Span is only available in procedural macros"),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700466 }
David Tolnay16a17202017-12-31 10:47:24 -0500467 }
468
Alex Crichtonce0904d2018-08-27 17:29:49 -0700469 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500470 pub fn source_file(&self) -> SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700471 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800472 Span::Compiler(s) => SourceFile::nightly(s.source_file()),
473 Span::Fallback(s) => SourceFile::Fallback(s.source_file()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700474 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500475 }
476
David Tolnay3b1f7d22019-01-28 12:22:11 -0800477 #[cfg(any(super_unstable, feature = "span-locations"))]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500478 pub fn start(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700479 match self {
David Tolnay3b1f7d22019-01-28 12:22:11 -0800480 #[cfg(nightly)]
David Tolnay9204bac2019-01-19 19:26:17 -0800481 Span::Compiler(s) => {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700482 let proc_macro::LineColumn { line, column } = s.start();
483 LineColumn { line, column }
484 }
David Tolnay3b1f7d22019-01-28 12:22:11 -0800485 #[cfg(not(nightly))]
486 Span::Compiler(_) => LineColumn { line: 0, column: 0 },
David Tolnay9204bac2019-01-19 19:26:17 -0800487 Span::Fallback(s) => {
David Tolnayaef075b2019-01-16 16:29:18 -0800488 let fallback::LineColumn { line, column } = s.start();
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700489 LineColumn { line, column }
490 }
491 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500492 }
493
David Tolnay3b1f7d22019-01-28 12:22:11 -0800494 #[cfg(any(super_unstable, feature = "span-locations"))]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500495 pub fn end(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700496 match self {
David Tolnay3b1f7d22019-01-28 12:22:11 -0800497 #[cfg(nightly)]
David Tolnay9204bac2019-01-19 19:26:17 -0800498 Span::Compiler(s) => {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700499 let proc_macro::LineColumn { line, column } = s.end();
500 LineColumn { line, column }
501 }
David Tolnay3b1f7d22019-01-28 12:22:11 -0800502 #[cfg(not(nightly))]
503 Span::Compiler(_) => LineColumn { line: 0, column: 0 },
David Tolnay9204bac2019-01-19 19:26:17 -0800504 Span::Fallback(s) => {
David Tolnayaef075b2019-01-16 16:29:18 -0800505 let fallback::LineColumn { line, column } = s.end();
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700506 LineColumn { line, column }
507 }
508 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500509 }
510
Alex Crichtonce0904d2018-08-27 17:29:49 -0700511 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500512 pub fn join(&self, other: Span) -> Option<Span> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700513 let ret = match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800514 (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.join(b)?),
515 (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.join(b)?),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700516 _ => return None,
517 };
518 Some(ret)
Nika Layzellf8d5f212017-12-11 14:07:02 -0500519 }
Alex Crichtone24f7342018-04-05 17:58:11 -0700520
Alex Crichtonce0904d2018-08-27 17:29:49 -0700521 #[cfg(super_unstable)]
Alex Crichtone24f7342018-04-05 17:58:11 -0700522 pub fn eq(&self, other: &Span) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700523 match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800524 (Span::Compiler(a), Span::Compiler(b)) => a.eq(b),
525 (Span::Fallback(a), Span::Fallback(b)) => a.eq(b),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700526 _ => false,
527 }
528 }
529
530 fn unwrap_nightly(self) -> proc_macro::Span {
531 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800532 Span::Compiler(s) => s,
533 Span::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700534 }
535 }
536}
537
538impl From<proc_macro::Span> for ::Span {
539 fn from(proc_span: proc_macro::Span) -> ::Span {
David Tolnay9204bac2019-01-19 19:26:17 -0800540 ::Span::_new(Span::Compiler(proc_span))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700541 }
542}
543
David Tolnayaef075b2019-01-16 16:29:18 -0800544impl From<fallback::Span> for Span {
545 fn from(inner: fallback::Span) -> Span {
David Tolnay9204bac2019-01-19 19:26:17 -0800546 Span::Fallback(inner)
Alex Crichtone24f7342018-04-05 17:58:11 -0700547 }
Alex Crichton998f6422017-11-19 08:06:27 -0800548}
549
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700550impl fmt::Debug for Span {
551 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700552 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800553 Span::Compiler(s) => s.fmt(f),
554 Span::Fallback(s) => s.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700555 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700556 }
557}
558
David Tolnayfd8cdc82019-01-19 19:23:59 -0800559pub fn debug_span_field_if_nontrivial(debug: &mut fmt::DebugStruct, span: Span) {
560 match span {
561 Span::Compiler(s) => {
562 debug.field("span", &s);
563 }
564 Span::Fallback(s) => fallback::debug_span_field_if_nontrivial(debug, s),
565 }
566}
567
Alex Crichtonf3888432018-05-16 09:11:05 -0700568#[derive(Clone)]
David Tolnayf14813f2018-09-08 17:14:07 -0700569pub enum Group {
David Tolnay9204bac2019-01-19 19:26:17 -0800570 Compiler(proc_macro::Group),
571 Fallback(fallback::Group),
David Tolnayf14813f2018-09-08 17:14:07 -0700572}
573
574impl Group {
575 pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
576 match stream {
David Tolnay9204bac2019-01-19 19:26:17 -0800577 TokenStream::Compiler(stream) => {
David Tolnayf14813f2018-09-08 17:14:07 -0700578 let delimiter = match delimiter {
579 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
580 Delimiter::Bracket => proc_macro::Delimiter::Bracket,
581 Delimiter::Brace => proc_macro::Delimiter::Brace,
582 Delimiter::None => proc_macro::Delimiter::None,
583 };
David Tolnay9204bac2019-01-19 19:26:17 -0800584 Group::Compiler(proc_macro::Group::new(delimiter, stream))
David Tolnayf14813f2018-09-08 17:14:07 -0700585 }
David Tolnay471a7ed2019-01-19 21:00:11 -0800586 TokenStream::Fallback(stream) => {
587 Group::Fallback(fallback::Group::new(delimiter, stream))
588 }
David Tolnayf14813f2018-09-08 17:14:07 -0700589 }
590 }
591
592 pub fn delimiter(&self) -> Delimiter {
593 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800594 Group::Compiler(g) => match g.delimiter() {
David Tolnayf14813f2018-09-08 17:14:07 -0700595 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
596 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
597 proc_macro::Delimiter::Brace => Delimiter::Brace,
598 proc_macro::Delimiter::None => Delimiter::None,
David Tolnay4453fcc2019-01-16 12:15:01 -0800599 },
David Tolnay9204bac2019-01-19 19:26:17 -0800600 Group::Fallback(g) => g.delimiter(),
David Tolnayf14813f2018-09-08 17:14:07 -0700601 }
602 }
603
604 pub fn stream(&self) -> TokenStream {
605 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800606 Group::Compiler(g) => TokenStream::Compiler(g.stream()),
607 Group::Fallback(g) => TokenStream::Fallback(g.stream()),
David Tolnayf14813f2018-09-08 17:14:07 -0700608 }
609 }
610
611 pub fn span(&self) -> Span {
612 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800613 Group::Compiler(g) => Span::Compiler(g.span()),
614 Group::Fallback(g) => Span::Fallback(g.span()),
David Tolnayf14813f2018-09-08 17:14:07 -0700615 }
616 }
617
618 #[cfg(super_unstable)]
619 pub fn span_open(&self) -> Span {
620 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800621 Group::Compiler(g) => Span::Compiler(g.span_open()),
622 Group::Fallback(g) => Span::Fallback(g.span_open()),
David Tolnayf14813f2018-09-08 17:14:07 -0700623 }
624 }
625
626 #[cfg(super_unstable)]
627 pub fn span_close(&self) -> Span {
628 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800629 Group::Compiler(g) => Span::Compiler(g.span_close()),
630 Group::Fallback(g) => Span::Fallback(g.span_close()),
David Tolnayf14813f2018-09-08 17:14:07 -0700631 }
632 }
633
634 pub fn set_span(&mut self, span: Span) {
635 match (self, span) {
David Tolnay9204bac2019-01-19 19:26:17 -0800636 (Group::Compiler(g), Span::Compiler(s)) => g.set_span(s),
637 (Group::Fallback(g), Span::Fallback(s)) => g.set_span(s),
David Tolnayf14813f2018-09-08 17:14:07 -0700638 _ => mismatch(),
639 }
640 }
641
642 fn unwrap_nightly(self) -> proc_macro::Group {
643 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800644 Group::Compiler(g) => g,
645 Group::Fallback(_) => mismatch(),
David Tolnayf14813f2018-09-08 17:14:07 -0700646 }
647 }
648}
649
David Tolnayaef075b2019-01-16 16:29:18 -0800650impl From<fallback::Group> for Group {
651 fn from(g: fallback::Group) -> Self {
David Tolnay9204bac2019-01-19 19:26:17 -0800652 Group::Fallback(g)
David Tolnayf14813f2018-09-08 17:14:07 -0700653 }
654}
655
656impl fmt::Display for Group {
657 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
658 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800659 Group::Compiler(group) => group.fmt(formatter),
660 Group::Fallback(group) => group.fmt(formatter),
David Tolnayf14813f2018-09-08 17:14:07 -0700661 }
662 }
663}
664
665impl fmt::Debug for Group {
666 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
667 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800668 Group::Compiler(group) => group.fmt(formatter),
669 Group::Fallback(group) => group.fmt(formatter),
David Tolnayf14813f2018-09-08 17:14:07 -0700670 }
671 }
672}
673
674#[derive(Clone)]
Alex Crichtonf3888432018-05-16 09:11:05 -0700675pub enum Ident {
David Tolnay9204bac2019-01-19 19:26:17 -0800676 Compiler(proc_macro::Ident),
677 Fallback(fallback::Ident),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700678}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700679
Alex Crichtonf3888432018-05-16 09:11:05 -0700680impl Ident {
681 pub fn new(string: &str, span: Span) -> Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700682 match span {
David Tolnay9204bac2019-01-19 19:26:17 -0800683 Span::Compiler(s) => Ident::Compiler(proc_macro::Ident::new(string, s)),
684 Span::Fallback(s) => Ident::Fallback(fallback::Ident::new(string, s)),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700685 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700686 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700687
Alex Crichtonf3888432018-05-16 09:11:05 -0700688 pub fn new_raw(string: &str, span: Span) -> Ident {
689 match span {
David Tolnay9204bac2019-01-19 19:26:17 -0800690 Span::Compiler(s) => {
Alex Crichtonce0904d2018-08-27 17:29:49 -0700691 let p: proc_macro::TokenStream = string.parse().unwrap();
692 let ident = match p.into_iter().next() {
693 Some(proc_macro::TokenTree::Ident(mut i)) => {
694 i.set_span(s);
695 i
696 }
697 _ => panic!(),
698 };
David Tolnay9204bac2019-01-19 19:26:17 -0800699 Ident::Compiler(ident)
Alex Crichtonce0904d2018-08-27 17:29:49 -0700700 }
David Tolnay9204bac2019-01-19 19:26:17 -0800701 Span::Fallback(s) => Ident::Fallback(fallback::Ident::new_raw(string, s)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700702 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700703 }
704
705 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700706 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800707 Ident::Compiler(t) => Span::Compiler(t.span()),
708 Ident::Fallback(t) => Span::Fallback(t.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700709 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700710 }
711
712 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700713 match (self, span) {
David Tolnay9204bac2019-01-19 19:26:17 -0800714 (Ident::Compiler(t), Span::Compiler(s)) => t.set_span(s),
715 (Ident::Fallback(t), Span::Fallback(s)) => t.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700716 _ => mismatch(),
717 }
718 }
719
Alex Crichtonf3888432018-05-16 09:11:05 -0700720 fn unwrap_nightly(self) -> proc_macro::Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700721 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800722 Ident::Compiler(s) => s,
723 Ident::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700724 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700725 }
726}
727
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700728impl PartialEq for Ident {
729 fn eq(&self, other: &Ident) -> bool {
730 match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800731 (Ident::Compiler(t), Ident::Compiler(o)) => t.to_string() == o.to_string(),
732 (Ident::Fallback(t), Ident::Fallback(o)) => t == o,
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700733 _ => mismatch(),
734 }
735 }
736}
737
738impl<T> PartialEq<T> for Ident
739where
740 T: ?Sized + AsRef<str>,
741{
742 fn eq(&self, other: &T) -> bool {
743 let other = other.as_ref();
744 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800745 Ident::Compiler(t) => t.to_string() == other,
746 Ident::Fallback(t) => t == other,
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700747 }
748 }
749}
750
Alex Crichtonf3888432018-05-16 09:11:05 -0700751impl fmt::Display for Ident {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700752 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700753 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800754 Ident::Compiler(t) => t.fmt(f),
755 Ident::Fallback(t) => t.fmt(f),
Alex Crichtonf3888432018-05-16 09:11:05 -0700756 }
757 }
758}
759
760impl fmt::Debug for Ident {
761 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
762 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800763 Ident::Compiler(t) => t.fmt(f),
764 Ident::Fallback(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700765 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700766 }
767}
768
769#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700770pub enum Literal {
David Tolnay9204bac2019-01-19 19:26:17 -0800771 Compiler(proc_macro::Literal),
772 Fallback(fallback::Literal),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700773}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700774
Alex Crichtona914a612018-04-04 07:48:44 -0700775macro_rules! suffixed_numbers {
776 ($($name:ident => $kind:ident,)*) => ($(
777 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700778 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800779 Literal::Compiler(proc_macro::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700780 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800781 Literal::Fallback(fallback::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700782 }
Alex Crichtona914a612018-04-04 07:48:44 -0700783 }
784 )*)
785}
786
787macro_rules! unsuffixed_integers {
788 ($($name:ident => $kind:ident,)*) => ($(
789 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700790 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800791 Literal::Compiler(proc_macro::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700792 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800793 Literal::Fallback(fallback::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700794 }
Alex Crichtona914a612018-04-04 07:48:44 -0700795 }
796 )*)
797}
798
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700799impl Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700800 suffixed_numbers! {
801 u8_suffixed => u8,
802 u16_suffixed => u16,
803 u32_suffixed => u32,
804 u64_suffixed => u64,
805 usize_suffixed => usize,
806 i8_suffixed => i8,
807 i16_suffixed => i16,
808 i32_suffixed => i32,
809 i64_suffixed => i64,
810 isize_suffixed => isize,
811
812 f32_suffixed => f32,
813 f64_suffixed => f64,
814 }
815
Alex Crichton69385662018-11-08 06:30:04 -0800816 #[cfg(u128)]
817 suffixed_numbers! {
818 i128_suffixed => i128,
819 u128_suffixed => u128,
820 }
821
Alex Crichtona914a612018-04-04 07:48:44 -0700822 unsuffixed_integers! {
823 u8_unsuffixed => u8,
824 u16_unsuffixed => u16,
825 u32_unsuffixed => u32,
826 u64_unsuffixed => u64,
827 usize_unsuffixed => usize,
828 i8_unsuffixed => i8,
829 i16_unsuffixed => i16,
830 i32_unsuffixed => i32,
831 i64_unsuffixed => i64,
832 isize_unsuffixed => isize,
833 }
834
Alex Crichton69385662018-11-08 06:30:04 -0800835 #[cfg(u128)]
836 unsuffixed_integers! {
837 i128_unsuffixed => i128,
838 u128_unsuffixed => u128,
839 }
840
Alex Crichtona914a612018-04-04 07:48:44 -0700841 pub fn f32_unsuffixed(f: f32) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700842 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800843 Literal::Compiler(proc_macro::Literal::f32_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700844 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800845 Literal::Fallback(fallback::Literal::f32_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700846 }
Alex Crichtona914a612018-04-04 07:48:44 -0700847 }
848
849 pub fn f64_unsuffixed(f: f64) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700850 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800851 Literal::Compiler(proc_macro::Literal::f64_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700852 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800853 Literal::Fallback(fallback::Literal::f64_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700854 }
Alex Crichtona914a612018-04-04 07:48:44 -0700855 }
856
Alex Crichtona914a612018-04-04 07:48:44 -0700857 pub fn string(t: &str) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700858 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800859 Literal::Compiler(proc_macro::Literal::string(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700860 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800861 Literal::Fallback(fallback::Literal::string(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700862 }
Alex Crichtona914a612018-04-04 07:48:44 -0700863 }
864
865 pub fn character(t: char) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700866 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800867 Literal::Compiler(proc_macro::Literal::character(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700868 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800869 Literal::Fallback(fallback::Literal::character(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700870 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700871 }
872
873 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700874 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800875 Literal::Compiler(proc_macro::Literal::byte_string(bytes))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700876 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800877 Literal::Fallback(fallback::Literal::byte_string(bytes))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700878 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700879 }
880
Alex Crichtonb2c94622018-04-04 07:36:41 -0700881 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700882 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800883 Literal::Compiler(lit) => Span::Compiler(lit.span()),
884 Literal::Fallback(lit) => Span::Fallback(lit.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700885 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700886 }
887
Alex Crichtonb2c94622018-04-04 07:36:41 -0700888 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700889 match (self, span) {
David Tolnay9204bac2019-01-19 19:26:17 -0800890 (Literal::Compiler(lit), Span::Compiler(s)) => lit.set_span(s),
891 (Literal::Fallback(lit), Span::Fallback(s)) => lit.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700892 _ => mismatch(),
893 }
894 }
895
896 fn unwrap_nightly(self) -> proc_macro::Literal {
897 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800898 Literal::Compiler(s) => s,
899 Literal::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700900 }
901 }
902}
903
David Tolnayaef075b2019-01-16 16:29:18 -0800904impl From<fallback::Literal> for Literal {
905 fn from(s: fallback::Literal) -> Literal {
David Tolnay9204bac2019-01-19 19:26:17 -0800906 Literal::Fallback(s)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700907 }
908}
909
910impl fmt::Display for Literal {
911 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700912 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800913 Literal::Compiler(t) => t.fmt(f),
914 Literal::Fallback(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700915 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700916 }
917}
918
919impl fmt::Debug for Literal {
920 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700921 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800922 Literal::Compiler(t) => t.fmt(f),
923 Literal::Fallback(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700924 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700925 }
926}