blob: 11d1a3b82efc1c6e35540bf543255c0eb747f2bd [file] [log] [blame]
Alex Crichtonce0904d2018-08-27 17:29:49 -07001#![cfg_attr(not(super_unstable), allow(dead_code))]
Alex Crichtonaf5bad42018-03-27 14:45:10 -07002
Alex Crichtoncbec8ec2017-06-02 13:19:33 -07003use std::fmt;
4use std::iter;
David Tolnay78ef7732018-09-01 19:20:23 -07005use std::panic::{self, PanicInfo};
David Tolnay9cd3b4c2018-11-11 16:47:32 -08006#[cfg(super_unstable)]
7use std::path::PathBuf;
David Tolnay3d9d6ad2018-05-18 10:51:55 -07008use std::str::FromStr;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -07009
David Tolnayaef075b2019-01-16 16:29:18 -080010use fallback;
David Tolnay471a7ed2019-01-19 21:00:11 -080011use proc_macro;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070012
David Tolnayf14813f2018-09-08 17:14:07 -070013use {Delimiter, Punct, Spacing, TokenTree};
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070014
15#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -070016pub enum TokenStream {
David Tolnay9204bac2019-01-19 19:26:17 -080017 Compiler(proc_macro::TokenStream),
18 Fallback(fallback::TokenStream),
Alex Crichton30a4e9e2018-04-27 17:02:19 -070019}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070020
Alex Crichton30a4e9e2018-04-27 17:02:19 -070021pub enum LexError {
David Tolnay9204bac2019-01-19 19:26:17 -080022 Compiler(proc_macro::LexError),
23 Fallback(fallback::LexError),
Alex Crichton30a4e9e2018-04-27 17:02:19 -070024}
25
26fn nightly_works() -> bool {
27 use std::sync::atomic::*;
David Tolnay2ed15d52018-09-01 08:46:12 -070028 use std::sync::Once;
29
Alex Crichton30a4e9e2018-04-27 17:02:19 -070030 static WORKS: AtomicUsize = ATOMIC_USIZE_INIT;
David Tolnay2ed15d52018-09-01 08:46:12 -070031 static INIT: Once = Once::new();
Alex Crichton30a4e9e2018-04-27 17:02:19 -070032
33 match WORKS.load(Ordering::SeqCst) {
34 1 => return false,
35 2 => return true,
36 _ => {}
37 }
David Tolnay2ed15d52018-09-01 08:46:12 -070038
39 // Swap in a null panic hook to avoid printing "thread panicked" to stderr,
40 // then use catch_unwind to determine whether the compiler's proc_macro is
41 // working. When proc-macro2 is used from outside of a procedural macro all
42 // of the proc_macro crate's APIs currently panic.
43 //
44 // The Once is to prevent the possibility of this ordering:
45 //
46 // thread 1 calls take_hook, gets the user's original hook
47 // thread 1 calls set_hook with the null hook
48 // thread 2 calls take_hook, thinks null hook is the original hook
49 // thread 2 calls set_hook with the null hook
50 // thread 1 calls set_hook with the actual original hook
51 // thread 2 calls set_hook with what it thinks is the original hook
52 //
53 // in which the user's hook has been lost.
54 //
55 // There is still a race condition where a panic in a different thread can
56 // happen during the interval that the user's original panic hook is
57 // unregistered such that their hook is incorrectly not called. This is
58 // sufficiently unlikely and less bad than printing panic messages to stderr
59 // on correct use of this crate. Maybe there is a libstd feature request
60 // here. For now, if a user needs to guarantee that this failure mode does
61 // not occur, they need to call e.g. `proc_macro2::Span::call_site()` from
62 // the main thread before launching any other threads.
63 INIT.call_once(|| {
David Tolnay78ef7732018-09-01 19:20:23 -070064 type PanicHook = Fn(&PanicInfo) + Sync + Send + 'static;
65
66 let null_hook: Box<PanicHook> = Box::new(|_panic_info| { /* ignore */ });
67 let sanity_check = &*null_hook as *const PanicHook;
David Tolnay2ed15d52018-09-01 08:46:12 -070068 let original_hook = panic::take_hook();
David Tolnay78ef7732018-09-01 19:20:23 -070069 panic::set_hook(null_hook);
70
David Tolnay2ed15d52018-09-01 08:46:12 -070071 let works = panic::catch_unwind(|| proc_macro::Span::call_site()).is_ok();
72 WORKS.store(works as usize + 1, Ordering::SeqCst);
David Tolnay78ef7732018-09-01 19:20:23 -070073
74 let hopefully_null_hook = panic::take_hook();
David Tolnay2ed15d52018-09-01 08:46:12 -070075 panic::set_hook(original_hook);
David Tolnay78ef7732018-09-01 19:20:23 -070076 if sanity_check != &*hopefully_null_hook {
77 panic!("observed race condition in proc_macro2::nightly_works");
78 }
David Tolnay2ed15d52018-09-01 08:46:12 -070079 });
80 nightly_works()
Alex Crichton30a4e9e2018-04-27 17:02:19 -070081}
82
83fn mismatch() -> ! {
84 panic!("stable/nightly mismatch")
85}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070086
87impl TokenStream {
David Tolnayc3bb4592018-05-28 20:09:44 -070088 pub fn new() -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070089 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -080090 TokenStream::Compiler(proc_macro::TokenStream::new())
Alex Crichton30a4e9e2018-04-27 17:02:19 -070091 } else {
David Tolnay9204bac2019-01-19 19:26:17 -080092 TokenStream::Fallback(fallback::TokenStream::new())
Alex Crichton30a4e9e2018-04-27 17:02:19 -070093 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070094 }
95
96 pub fn is_empty(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070097 match self {
David Tolnay9204bac2019-01-19 19:26:17 -080098 TokenStream::Compiler(tts) => tts.is_empty(),
99 TokenStream::Fallback(tts) => tts.is_empty(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700100 }
101 }
102
103 fn unwrap_nightly(self) -> proc_macro::TokenStream {
104 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800105 TokenStream::Compiler(s) => s,
106 TokenStream::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700107 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700108 }
David Tolnay5c58c532018-08-13 11:33:51 -0700109
David Tolnayaef075b2019-01-16 16:29:18 -0800110 fn unwrap_stable(self) -> fallback::TokenStream {
David Tolnay5c58c532018-08-13 11:33:51 -0700111 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800112 TokenStream::Compiler(_) => mismatch(),
113 TokenStream::Fallback(s) => s,
David Tolnay5c58c532018-08-13 11:33:51 -0700114 }
115 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700116}
117
118impl FromStr for TokenStream {
119 type Err = LexError;
120
121 fn from_str(src: &str) -> Result<TokenStream, LexError> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700122 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800123 Ok(TokenStream::Compiler(src.parse()?))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700124 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800125 Ok(TokenStream::Fallback(src.parse()?))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700126 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700127 }
128}
129
130impl fmt::Display for TokenStream {
131 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700132 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800133 TokenStream::Compiler(tts) => tts.fmt(f),
134 TokenStream::Fallback(tts) => tts.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700135 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700136 }
137}
138
139impl From<proc_macro::TokenStream> for TokenStream {
140 fn from(inner: proc_macro::TokenStream) -> TokenStream {
David Tolnay9204bac2019-01-19 19:26:17 -0800141 TokenStream::Compiler(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700142 }
143}
144
145impl From<TokenStream> for proc_macro::TokenStream {
146 fn from(inner: TokenStream) -> proc_macro::TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700147 match inner {
David Tolnay9204bac2019-01-19 19:26:17 -0800148 TokenStream::Compiler(inner) => inner,
149 TokenStream::Fallback(inner) => inner.to_string().parse().unwrap(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700150 }
151 }
152}
153
David Tolnayaef075b2019-01-16 16:29:18 -0800154impl From<fallback::TokenStream> for TokenStream {
155 fn from(inner: fallback::TokenStream) -> TokenStream {
David Tolnay9204bac2019-01-19 19:26:17 -0800156 TokenStream::Fallback(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700157 }
158}
159
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700160impl From<TokenTree> for TokenStream {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700161 fn from(token: TokenTree) -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700162 if !nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800163 return TokenStream::Fallback(token.into());
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700164 }
Alex Crichton9cd80a62018-04-05 17:46:58 -0700165 let tt: proc_macro::TokenTree = match token {
David Tolnayf14813f2018-09-08 17:14:07 -0700166 TokenTree::Group(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichtonf3888432018-05-16 09:11:05 -0700167 TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700168 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700169 Spacing::Joint => proc_macro::Spacing::Joint,
170 Spacing::Alone => proc_macro::Spacing::Alone,
171 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700172 let mut op = proc_macro::Punct::new(tt.as_char(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700173 op.set_span(tt.span().inner.unwrap_nightly());
Alex Crichton9cd80a62018-04-05 17:46:58 -0700174 op.into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700175 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700176 TokenTree::Ident(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700177 TokenTree::Literal(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700178 };
David Tolnay9204bac2019-01-19 19:26:17 -0800179 TokenStream::Compiler(tt.into())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700180 }
181}
182
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700183impl iter::FromIterator<TokenTree> for TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700184 fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self {
185 if nightly_works() {
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700186 let trees = trees
187 .into_iter()
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700188 .map(TokenStream::from)
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700189 .flat_map(|t| match t {
David Tolnay9204bac2019-01-19 19:26:17 -0800190 TokenStream::Compiler(s) => s,
191 TokenStream::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700192 });
David Tolnay9204bac2019-01-19 19:26:17 -0800193 TokenStream::Compiler(trees.collect())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700194 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800195 TokenStream::Fallback(trees.into_iter().collect())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700196 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700197 }
198}
199
Alex Crichton53b00672018-09-06 17:16:10 -0700200impl iter::FromIterator<TokenStream> for TokenStream {
201 fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self {
202 let mut streams = streams.into_iter();
203 match streams.next() {
204 #[cfg(slow_extend)]
David Tolnay9204bac2019-01-19 19:26:17 -0800205 Some(TokenStream::Compiler(first)) => {
David Tolnay4453fcc2019-01-16 12:15:01 -0800206 let stream = iter::once(first)
207 .chain(streams.map(|s| match s {
David Tolnay9204bac2019-01-19 19:26:17 -0800208 TokenStream::Compiler(s) => s,
209 TokenStream::Fallback(_) => mismatch(),
David Tolnay4453fcc2019-01-16 12:15:01 -0800210 }))
211 .collect();
David Tolnay9204bac2019-01-19 19:26:17 -0800212 TokenStream::Compiler(stream)
Alex Crichton53b00672018-09-06 17:16:10 -0700213 }
214 #[cfg(not(slow_extend))]
David Tolnay9204bac2019-01-19 19:26:17 -0800215 Some(TokenStream::Compiler(mut first)) => {
David Tolnay4453fcc2019-01-16 12:15:01 -0800216 first.extend(streams.map(|s| match s {
David Tolnay9204bac2019-01-19 19:26:17 -0800217 TokenStream::Compiler(s) => s,
218 TokenStream::Fallback(_) => mismatch(),
Alex Crichton53b00672018-09-06 17:16:10 -0700219 }));
David Tolnay9204bac2019-01-19 19:26:17 -0800220 TokenStream::Compiler(first)
Alex Crichton53b00672018-09-06 17:16:10 -0700221 }
David Tolnay9204bac2019-01-19 19:26:17 -0800222 Some(TokenStream::Fallback(mut first)) => {
David Tolnay4453fcc2019-01-16 12:15:01 -0800223 first.extend(streams.map(|s| match s {
David Tolnay9204bac2019-01-19 19:26:17 -0800224 TokenStream::Fallback(s) => s,
225 TokenStream::Compiler(_) => mismatch(),
Alex Crichton53b00672018-09-06 17:16:10 -0700226 }));
David Tolnay9204bac2019-01-19 19:26:17 -0800227 TokenStream::Fallback(first)
Alex Crichton53b00672018-09-06 17:16:10 -0700228 }
229 None => TokenStream::new(),
Alex Crichton53b00672018-09-06 17:16:10 -0700230 }
231 }
232}
233
Alex Crichtonf3888432018-05-16 09:11:05 -0700234impl Extend<TokenTree> for TokenStream {
235 fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
236 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800237 TokenStream::Compiler(tts) => {
David Tolnaye839e4f2018-09-06 09:36:43 -0700238 #[cfg(not(slow_extend))]
239 {
240 tts.extend(
241 streams
242 .into_iter()
243 .map(|t| TokenStream::from(t).unwrap_nightly()),
244 );
245 }
246 #[cfg(slow_extend)]
247 {
David Tolnay4453fcc2019-01-16 12:15:01 -0800248 *tts =
249 tts.clone()
250 .into_iter()
251 .chain(streams.into_iter().map(TokenStream::from).flat_map(
252 |t| match t {
David Tolnay9204bac2019-01-19 19:26:17 -0800253 TokenStream::Compiler(tts) => tts.into_iter(),
David Tolnaye839e4f2018-09-06 09:36:43 -0700254 _ => mismatch(),
David Tolnay4453fcc2019-01-16 12:15:01 -0800255 },
256 ))
257 .collect();
David Tolnaye839e4f2018-09-06 09:36:43 -0700258 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700259 }
David Tolnay9204bac2019-01-19 19:26:17 -0800260 TokenStream::Fallback(tts) => tts.extend(streams),
Alex Crichtonf3888432018-05-16 09:11:05 -0700261 }
262 }
263}
264
David Tolnay5c58c532018-08-13 11:33:51 -0700265impl Extend<TokenStream> for TokenStream {
266 fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
267 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800268 TokenStream::Compiler(tts) => {
David Tolnaye839e4f2018-09-06 09:36:43 -0700269 #[cfg(not(slow_extend))]
270 {
271 tts.extend(streams.into_iter().map(|stream| stream.unwrap_nightly()));
272 }
273 #[cfg(slow_extend)]
274 {
275 *tts = tts
276 .clone()
277 .into_iter()
David Tolnay4453fcc2019-01-16 12:15:01 -0800278 .chain(streams.into_iter().flat_map(|t| match t {
David Tolnay9204bac2019-01-19 19:26:17 -0800279 TokenStream::Compiler(tts) => tts.into_iter(),
David Tolnay4453fcc2019-01-16 12:15:01 -0800280 _ => mismatch(),
281 }))
282 .collect();
David Tolnaye839e4f2018-09-06 09:36:43 -0700283 }
David Tolnay5c58c532018-08-13 11:33:51 -0700284 }
David Tolnay9204bac2019-01-19 19:26:17 -0800285 TokenStream::Fallback(tts) => {
David Tolnay5c58c532018-08-13 11:33:51 -0700286 tts.extend(streams.into_iter().map(|stream| stream.unwrap_stable()))
287 }
288 }
289 }
290}
291
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700292impl fmt::Debug for TokenStream {
293 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700294 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800295 TokenStream::Compiler(tts) => tts.fmt(f),
296 TokenStream::Fallback(tts) => tts.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700297 }
298 }
299}
300
301impl From<proc_macro::LexError> for LexError {
302 fn from(e: proc_macro::LexError) -> LexError {
David Tolnay9204bac2019-01-19 19:26:17 -0800303 LexError::Compiler(e)
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700304 }
305}
306
David Tolnayaef075b2019-01-16 16:29:18 -0800307impl From<fallback::LexError> for LexError {
308 fn from(e: fallback::LexError) -> LexError {
David Tolnay9204bac2019-01-19 19:26:17 -0800309 LexError::Fallback(e)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700310 }
311}
312
313impl fmt::Debug for LexError {
314 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700315 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800316 LexError::Compiler(e) => e.fmt(f),
317 LexError::Fallback(e) => e.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700318 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700319 }
320}
321
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700322pub enum TokenTreeIter {
David Tolnay9204bac2019-01-19 19:26:17 -0800323 Compiler(proc_macro::token_stream::IntoIter),
324 Fallback(fallback::TokenTreeIter),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700325}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700326
327impl IntoIterator for TokenStream {
328 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700329 type IntoIter = TokenTreeIter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700330
Alex Crichton1a7f7622017-07-05 17:47:15 -0700331 fn into_iter(self) -> TokenTreeIter {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700332 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800333 TokenStream::Compiler(tts) => TokenTreeIter::Compiler(tts.into_iter()),
334 TokenStream::Fallback(tts) => TokenTreeIter::Fallback(tts.into_iter()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700335 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700336 }
337}
338
Alex Crichton1a7f7622017-07-05 17:47:15 -0700339impl Iterator for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700340 type Item = TokenTree;
341
342 fn next(&mut self) -> Option<TokenTree> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700343 let token = match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800344 TokenTreeIter::Compiler(iter) => iter.next()?,
345 TokenTreeIter::Fallback(iter) => return iter.next(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700346 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700347 Some(match token {
David Tolnay9204bac2019-01-19 19:26:17 -0800348 proc_macro::TokenTree::Group(tt) => ::Group::_new(Group::Compiler(tt)).into(),
Alex Crichtonf3888432018-05-16 09:11:05 -0700349 proc_macro::TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700350 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700351 proc_macro::Spacing::Joint => Spacing::Joint,
352 proc_macro::Spacing::Alone => Spacing::Alone,
353 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700354 let mut o = Punct::new(tt.as_char(), spacing);
David Tolnay9204bac2019-01-19 19:26:17 -0800355 o.set_span(::Span::_new(Span::Compiler(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700356 o.into()
357 }
David Tolnay9204bac2019-01-19 19:26:17 -0800358 proc_macro::TokenTree::Ident(s) => ::Ident::_new(Ident::Compiler(s)).into(),
359 proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Compiler(l)).into(),
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700360 })
361 }
362
363 fn size_hint(&self) -> (usize, Option<usize>) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700364 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800365 TokenTreeIter::Compiler(tts) => tts.size_hint(),
366 TokenTreeIter::Fallback(tts) => tts.size_hint(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700367 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700368 }
369}
370
Alex Crichton1a7f7622017-07-05 17:47:15 -0700371impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700372 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700373 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700374 }
375}
376
Nika Layzellb35a9a32017-12-30 14:34:35 -0500377#[derive(Clone, PartialEq, Eq)]
Alex Crichtonce0904d2018-08-27 17:29:49 -0700378#[cfg(super_unstable)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700379pub enum SourceFile {
David Tolnay9204bac2019-01-19 19:26:17 -0800380 Compiler(proc_macro::SourceFile),
381 Fallback(fallback::SourceFile),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700382}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500383
Alex Crichtonce0904d2018-08-27 17:29:49 -0700384#[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500385impl SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700386 fn nightly(sf: proc_macro::SourceFile) -> Self {
David Tolnay9204bac2019-01-19 19:26:17 -0800387 SourceFile::Compiler(sf)
Nika Layzellb35a9a32017-12-30 14:34:35 -0500388 }
389
Nika Layzellf8d5f212017-12-11 14:07:02 -0500390 /// Get the path to this source file as a string.
David Tolnay9cd3b4c2018-11-11 16:47:32 -0800391 pub fn path(&self) -> PathBuf {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700392 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800393 SourceFile::Compiler(a) => a.path(),
394 SourceFile::Fallback(a) => a.path(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700395 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500396 }
397
398 pub fn is_real(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700399 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800400 SourceFile::Compiler(a) => a.is_real(),
401 SourceFile::Fallback(a) => a.is_real(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700402 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500403 }
404}
405
Alex Crichtonce0904d2018-08-27 17:29:49 -0700406#[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500407impl fmt::Debug for SourceFile {
408 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700409 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800410 SourceFile::Compiler(a) => a.fmt(f),
411 SourceFile::Fallback(a) => a.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700412 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500413 }
414}
415
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500416pub struct LineColumn {
417 pub line: usize,
418 pub column: usize,
419}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500420
Alex Crichton9cd80a62018-04-05 17:46:58 -0700421#[derive(Copy, Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700422pub enum Span {
David Tolnay9204bac2019-01-19 19:26:17 -0800423 Compiler(proc_macro::Span),
424 Fallback(fallback::Span),
Sergio Benitez13805082018-01-04 01:25:45 -0800425}
426
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700427impl Span {
428 pub fn call_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700429 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800430 Span::Compiler(proc_macro::Span::call_site())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700431 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800432 Span::Fallback(fallback::Span::call_site())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700433 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700434 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700435
Alex Crichtonce0904d2018-08-27 17:29:49 -0700436 #[cfg(super_unstable)]
Alex Crichtone6085b72017-11-21 07:24:25 -0800437 pub fn def_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700438 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800439 Span::Compiler(proc_macro::Span::def_site())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700440 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800441 Span::Fallback(fallback::Span::def_site())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700442 }
Alex Crichton998f6422017-11-19 08:06:27 -0800443 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500444
Alex Crichtonce0904d2018-08-27 17:29:49 -0700445 #[cfg(super_unstable)]
David Tolnay4e8e3972018-01-05 18:10:22 -0800446 pub fn resolved_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700447 match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800448 (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.resolved_at(b)),
449 (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.resolved_at(b)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700450 _ => mismatch(),
451 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800452 }
453
Alex Crichtonce0904d2018-08-27 17:29:49 -0700454 #[cfg(super_unstable)]
David Tolnay4e8e3972018-01-05 18:10:22 -0800455 pub fn located_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700456 match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800457 (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.located_at(b)),
458 (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.located_at(b)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700459 _ => mismatch(),
460 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800461 }
462
David Tolnay40bbb1c2019-01-19 19:43:55 -0800463 pub fn unwrap(self) -> proc_macro::Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700464 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800465 Span::Compiler(s) => s,
466 Span::Fallback(_) => panic!("proc_macro::Span is only available in procedural macros"),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700467 }
David Tolnay16a17202017-12-31 10:47:24 -0500468 }
469
Alex Crichtonce0904d2018-08-27 17:29:49 -0700470 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500471 pub fn source_file(&self) -> SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700472 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800473 Span::Compiler(s) => SourceFile::nightly(s.source_file()),
474 Span::Fallback(s) => SourceFile::Fallback(s.source_file()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700475 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500476 }
477
Alex Crichtonce0904d2018-08-27 17:29:49 -0700478 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500479 pub fn start(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700480 match self {
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 Tolnay9204bac2019-01-19 19:26:17 -0800485 Span::Fallback(s) => {
David Tolnayaef075b2019-01-16 16:29:18 -0800486 let fallback::LineColumn { line, column } = s.start();
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700487 LineColumn { line, column }
488 }
489 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500490 }
491
Alex Crichtonce0904d2018-08-27 17:29:49 -0700492 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500493 pub fn end(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700494 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800495 Span::Compiler(s) => {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700496 let proc_macro::LineColumn { line, column } = s.end();
497 LineColumn { line, column }
498 }
David Tolnay9204bac2019-01-19 19:26:17 -0800499 Span::Fallback(s) => {
David Tolnayaef075b2019-01-16 16:29:18 -0800500 let fallback::LineColumn { line, column } = s.end();
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700501 LineColumn { line, column }
502 }
503 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500504 }
505
Alex Crichtonce0904d2018-08-27 17:29:49 -0700506 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500507 pub fn join(&self, other: Span) -> Option<Span> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700508 let ret = match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800509 (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.join(b)?),
510 (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.join(b)?),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700511 _ => return None,
512 };
513 Some(ret)
Nika Layzellf8d5f212017-12-11 14:07:02 -0500514 }
Alex Crichtone24f7342018-04-05 17:58:11 -0700515
Alex Crichtonce0904d2018-08-27 17:29:49 -0700516 #[cfg(super_unstable)]
Alex Crichtone24f7342018-04-05 17:58:11 -0700517 pub fn eq(&self, other: &Span) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700518 match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800519 (Span::Compiler(a), Span::Compiler(b)) => a.eq(b),
520 (Span::Fallback(a), Span::Fallback(b)) => a.eq(b),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700521 _ => false,
522 }
523 }
524
525 fn unwrap_nightly(self) -> proc_macro::Span {
526 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800527 Span::Compiler(s) => s,
528 Span::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700529 }
530 }
531}
532
533impl From<proc_macro::Span> for ::Span {
534 fn from(proc_span: proc_macro::Span) -> ::Span {
David Tolnay9204bac2019-01-19 19:26:17 -0800535 ::Span::_new(Span::Compiler(proc_span))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700536 }
537}
538
David Tolnayaef075b2019-01-16 16:29:18 -0800539impl From<fallback::Span> for Span {
540 fn from(inner: fallback::Span) -> Span {
David Tolnay9204bac2019-01-19 19:26:17 -0800541 Span::Fallback(inner)
Alex Crichtone24f7342018-04-05 17:58:11 -0700542 }
Alex Crichton998f6422017-11-19 08:06:27 -0800543}
544
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700545impl fmt::Debug for Span {
546 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700547 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800548 Span::Compiler(s) => s.fmt(f),
549 Span::Fallback(s) => s.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700550 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700551 }
552}
553
David Tolnayfd8cdc82019-01-19 19:23:59 -0800554pub fn debug_span_field_if_nontrivial(debug: &mut fmt::DebugStruct, span: Span) {
555 match span {
556 Span::Compiler(s) => {
557 debug.field("span", &s);
558 }
559 Span::Fallback(s) => fallback::debug_span_field_if_nontrivial(debug, s),
560 }
561}
562
Alex Crichtonf3888432018-05-16 09:11:05 -0700563#[derive(Clone)]
David Tolnayf14813f2018-09-08 17:14:07 -0700564pub enum Group {
David Tolnay9204bac2019-01-19 19:26:17 -0800565 Compiler(proc_macro::Group),
566 Fallback(fallback::Group),
David Tolnayf14813f2018-09-08 17:14:07 -0700567}
568
569impl Group {
570 pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
571 match stream {
David Tolnay9204bac2019-01-19 19:26:17 -0800572 TokenStream::Compiler(stream) => {
David Tolnayf14813f2018-09-08 17:14:07 -0700573 let delimiter = match delimiter {
574 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
575 Delimiter::Bracket => proc_macro::Delimiter::Bracket,
576 Delimiter::Brace => proc_macro::Delimiter::Brace,
577 Delimiter::None => proc_macro::Delimiter::None,
578 };
David Tolnay9204bac2019-01-19 19:26:17 -0800579 Group::Compiler(proc_macro::Group::new(delimiter, stream))
David Tolnayf14813f2018-09-08 17:14:07 -0700580 }
David Tolnay471a7ed2019-01-19 21:00:11 -0800581 TokenStream::Fallback(stream) => {
582 Group::Fallback(fallback::Group::new(delimiter, stream))
583 }
David Tolnayf14813f2018-09-08 17:14:07 -0700584 }
585 }
586
587 pub fn delimiter(&self) -> Delimiter {
588 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800589 Group::Compiler(g) => match g.delimiter() {
David Tolnayf14813f2018-09-08 17:14:07 -0700590 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
591 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
592 proc_macro::Delimiter::Brace => Delimiter::Brace,
593 proc_macro::Delimiter::None => Delimiter::None,
David Tolnay4453fcc2019-01-16 12:15:01 -0800594 },
David Tolnay9204bac2019-01-19 19:26:17 -0800595 Group::Fallback(g) => g.delimiter(),
David Tolnayf14813f2018-09-08 17:14:07 -0700596 }
597 }
598
599 pub fn stream(&self) -> TokenStream {
600 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800601 Group::Compiler(g) => TokenStream::Compiler(g.stream()),
602 Group::Fallback(g) => TokenStream::Fallback(g.stream()),
David Tolnayf14813f2018-09-08 17:14:07 -0700603 }
604 }
605
606 pub fn span(&self) -> Span {
607 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800608 Group::Compiler(g) => Span::Compiler(g.span()),
609 Group::Fallback(g) => Span::Fallback(g.span()),
David Tolnayf14813f2018-09-08 17:14:07 -0700610 }
611 }
612
613 #[cfg(super_unstable)]
614 pub fn span_open(&self) -> Span {
615 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800616 Group::Compiler(g) => Span::Compiler(g.span_open()),
617 Group::Fallback(g) => Span::Fallback(g.span_open()),
David Tolnayf14813f2018-09-08 17:14:07 -0700618 }
619 }
620
621 #[cfg(super_unstable)]
622 pub fn span_close(&self) -> Span {
623 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800624 Group::Compiler(g) => Span::Compiler(g.span_close()),
625 Group::Fallback(g) => Span::Fallback(g.span_close()),
David Tolnayf14813f2018-09-08 17:14:07 -0700626 }
627 }
628
629 pub fn set_span(&mut self, span: Span) {
630 match (self, span) {
David Tolnay9204bac2019-01-19 19:26:17 -0800631 (Group::Compiler(g), Span::Compiler(s)) => g.set_span(s),
632 (Group::Fallback(g), Span::Fallback(s)) => g.set_span(s),
David Tolnayf14813f2018-09-08 17:14:07 -0700633 _ => mismatch(),
634 }
635 }
636
637 fn unwrap_nightly(self) -> proc_macro::Group {
638 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800639 Group::Compiler(g) => g,
640 Group::Fallback(_) => mismatch(),
David Tolnayf14813f2018-09-08 17:14:07 -0700641 }
642 }
643}
644
David Tolnayaef075b2019-01-16 16:29:18 -0800645impl From<fallback::Group> for Group {
646 fn from(g: fallback::Group) -> Self {
David Tolnay9204bac2019-01-19 19:26:17 -0800647 Group::Fallback(g)
David Tolnayf14813f2018-09-08 17:14:07 -0700648 }
649}
650
651impl fmt::Display for Group {
652 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
653 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800654 Group::Compiler(group) => group.fmt(formatter),
655 Group::Fallback(group) => group.fmt(formatter),
David Tolnayf14813f2018-09-08 17:14:07 -0700656 }
657 }
658}
659
660impl fmt::Debug for Group {
661 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
662 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800663 Group::Compiler(group) => group.fmt(formatter),
664 Group::Fallback(group) => group.fmt(formatter),
David Tolnayf14813f2018-09-08 17:14:07 -0700665 }
666 }
667}
668
669#[derive(Clone)]
Alex Crichtonf3888432018-05-16 09:11:05 -0700670pub enum Ident {
David Tolnay9204bac2019-01-19 19:26:17 -0800671 Compiler(proc_macro::Ident),
672 Fallback(fallback::Ident),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700673}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700674
Alex Crichtonf3888432018-05-16 09:11:05 -0700675impl Ident {
676 pub fn new(string: &str, span: Span) -> Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700677 match span {
David Tolnay9204bac2019-01-19 19:26:17 -0800678 Span::Compiler(s) => Ident::Compiler(proc_macro::Ident::new(string, s)),
679 Span::Fallback(s) => Ident::Fallback(fallback::Ident::new(string, s)),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700680 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700681 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700682
Alex Crichtonf3888432018-05-16 09:11:05 -0700683 pub fn new_raw(string: &str, span: Span) -> Ident {
684 match span {
David Tolnay9204bac2019-01-19 19:26:17 -0800685 Span::Compiler(s) => {
Alex Crichtonce0904d2018-08-27 17:29:49 -0700686 let p: proc_macro::TokenStream = string.parse().unwrap();
687 let ident = match p.into_iter().next() {
688 Some(proc_macro::TokenTree::Ident(mut i)) => {
689 i.set_span(s);
690 i
691 }
692 _ => panic!(),
693 };
David Tolnay9204bac2019-01-19 19:26:17 -0800694 Ident::Compiler(ident)
Alex Crichtonce0904d2018-08-27 17:29:49 -0700695 }
David Tolnay9204bac2019-01-19 19:26:17 -0800696 Span::Fallback(s) => Ident::Fallback(fallback::Ident::new_raw(string, s)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700697 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700698 }
699
700 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700701 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800702 Ident::Compiler(t) => Span::Compiler(t.span()),
703 Ident::Fallback(t) => Span::Fallback(t.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700704 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700705 }
706
707 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700708 match (self, span) {
David Tolnay9204bac2019-01-19 19:26:17 -0800709 (Ident::Compiler(t), Span::Compiler(s)) => t.set_span(s),
710 (Ident::Fallback(t), Span::Fallback(s)) => t.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700711 _ => mismatch(),
712 }
713 }
714
Alex Crichtonf3888432018-05-16 09:11:05 -0700715 fn unwrap_nightly(self) -> proc_macro::Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700716 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800717 Ident::Compiler(s) => s,
718 Ident::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700719 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700720 }
721}
722
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700723impl PartialEq for Ident {
724 fn eq(&self, other: &Ident) -> bool {
725 match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800726 (Ident::Compiler(t), Ident::Compiler(o)) => t.to_string() == o.to_string(),
727 (Ident::Fallback(t), Ident::Fallback(o)) => t == o,
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700728 _ => mismatch(),
729 }
730 }
731}
732
733impl<T> PartialEq<T> for Ident
734where
735 T: ?Sized + AsRef<str>,
736{
737 fn eq(&self, other: &T) -> bool {
738 let other = other.as_ref();
739 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800740 Ident::Compiler(t) => t.to_string() == other,
741 Ident::Fallback(t) => t == other,
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700742 }
743 }
744}
745
Alex Crichtonf3888432018-05-16 09:11:05 -0700746impl fmt::Display for Ident {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700747 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700748 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800749 Ident::Compiler(t) => t.fmt(f),
750 Ident::Fallback(t) => t.fmt(f),
Alex Crichtonf3888432018-05-16 09:11:05 -0700751 }
752 }
753}
754
755impl fmt::Debug for Ident {
756 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
757 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800758 Ident::Compiler(t) => t.fmt(f),
759 Ident::Fallback(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700760 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700761 }
762}
763
764#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700765pub enum Literal {
David Tolnay9204bac2019-01-19 19:26:17 -0800766 Compiler(proc_macro::Literal),
767 Fallback(fallback::Literal),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700768}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700769
Alex Crichtona914a612018-04-04 07:48:44 -0700770macro_rules! suffixed_numbers {
771 ($($name:ident => $kind:ident,)*) => ($(
772 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700773 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800774 Literal::Compiler(proc_macro::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700775 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800776 Literal::Fallback(fallback::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700777 }
Alex Crichtona914a612018-04-04 07:48:44 -0700778 }
779 )*)
780}
781
782macro_rules! unsuffixed_integers {
783 ($($name:ident => $kind:ident,)*) => ($(
784 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700785 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800786 Literal::Compiler(proc_macro::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700787 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800788 Literal::Fallback(fallback::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700789 }
Alex Crichtona914a612018-04-04 07:48:44 -0700790 }
791 )*)
792}
793
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700794impl Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700795 suffixed_numbers! {
796 u8_suffixed => u8,
797 u16_suffixed => u16,
798 u32_suffixed => u32,
799 u64_suffixed => u64,
800 usize_suffixed => usize,
801 i8_suffixed => i8,
802 i16_suffixed => i16,
803 i32_suffixed => i32,
804 i64_suffixed => i64,
805 isize_suffixed => isize,
806
807 f32_suffixed => f32,
808 f64_suffixed => f64,
809 }
810
Alex Crichton69385662018-11-08 06:30:04 -0800811 #[cfg(u128)]
812 suffixed_numbers! {
813 i128_suffixed => i128,
814 u128_suffixed => u128,
815 }
816
Alex Crichtona914a612018-04-04 07:48:44 -0700817 unsuffixed_integers! {
818 u8_unsuffixed => u8,
819 u16_unsuffixed => u16,
820 u32_unsuffixed => u32,
821 u64_unsuffixed => u64,
822 usize_unsuffixed => usize,
823 i8_unsuffixed => i8,
824 i16_unsuffixed => i16,
825 i32_unsuffixed => i32,
826 i64_unsuffixed => i64,
827 isize_unsuffixed => isize,
828 }
829
Alex Crichton69385662018-11-08 06:30:04 -0800830 #[cfg(u128)]
831 unsuffixed_integers! {
832 i128_unsuffixed => i128,
833 u128_unsuffixed => u128,
834 }
835
Alex Crichtona914a612018-04-04 07:48:44 -0700836 pub fn f32_unsuffixed(f: f32) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700837 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800838 Literal::Compiler(proc_macro::Literal::f32_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700839 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800840 Literal::Fallback(fallback::Literal::f32_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700841 }
Alex Crichtona914a612018-04-04 07:48:44 -0700842 }
843
844 pub fn f64_unsuffixed(f: f64) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700845 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800846 Literal::Compiler(proc_macro::Literal::f64_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700847 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800848 Literal::Fallback(fallback::Literal::f64_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700849 }
Alex Crichtona914a612018-04-04 07:48:44 -0700850 }
851
Alex Crichtona914a612018-04-04 07:48:44 -0700852 pub fn string(t: &str) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700853 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800854 Literal::Compiler(proc_macro::Literal::string(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700855 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800856 Literal::Fallback(fallback::Literal::string(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700857 }
Alex Crichtona914a612018-04-04 07:48:44 -0700858 }
859
860 pub fn character(t: char) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700861 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800862 Literal::Compiler(proc_macro::Literal::character(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700863 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800864 Literal::Fallback(fallback::Literal::character(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700865 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700866 }
867
868 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700869 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800870 Literal::Compiler(proc_macro::Literal::byte_string(bytes))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700871 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800872 Literal::Fallback(fallback::Literal::byte_string(bytes))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700873 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700874 }
875
Alex Crichtonb2c94622018-04-04 07:36:41 -0700876 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700877 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800878 Literal::Compiler(lit) => Span::Compiler(lit.span()),
879 Literal::Fallback(lit) => Span::Fallback(lit.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700880 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700881 }
882
Alex Crichtonb2c94622018-04-04 07:36:41 -0700883 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700884 match (self, span) {
David Tolnay9204bac2019-01-19 19:26:17 -0800885 (Literal::Compiler(lit), Span::Compiler(s)) => lit.set_span(s),
886 (Literal::Fallback(lit), Span::Fallback(s)) => lit.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700887 _ => mismatch(),
888 }
889 }
890
891 fn unwrap_nightly(self) -> proc_macro::Literal {
892 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800893 Literal::Compiler(s) => s,
894 Literal::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700895 }
896 }
897}
898
David Tolnayaef075b2019-01-16 16:29:18 -0800899impl From<fallback::Literal> for Literal {
900 fn from(s: fallback::Literal) -> Literal {
David Tolnay9204bac2019-01-19 19:26:17 -0800901 Literal::Fallback(s)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700902 }
903}
904
905impl fmt::Display for Literal {
906 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700907 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800908 Literal::Compiler(t) => t.fmt(f),
909 Literal::Fallback(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700910 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700911 }
912}
913
914impl fmt::Debug for Literal {
915 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700916 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800917 Literal::Compiler(t) => t.fmt(f),
918 Literal::Fallback(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700919 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700920 }
921}