blob: 8ead052f24d0be3e61736dec88459987dbebe3e9 [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
10use proc_macro;
David Tolnayaef075b2019-01-16 16:29:18 -080011use fallback;
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 Tolnay16a17202017-12-31 10:47:24 -0500463 pub fn unstable(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 Tolnay9204bac2019-01-19 19:26:17 -0800581 TokenStream::Fallback(stream) => Group::Fallback(fallback::Group::new(delimiter, stream)),
David Tolnayf14813f2018-09-08 17:14:07 -0700582 }
583 }
584
585 pub fn delimiter(&self) -> Delimiter {
586 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800587 Group::Compiler(g) => match g.delimiter() {
David Tolnayf14813f2018-09-08 17:14:07 -0700588 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
589 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
590 proc_macro::Delimiter::Brace => Delimiter::Brace,
591 proc_macro::Delimiter::None => Delimiter::None,
David Tolnay4453fcc2019-01-16 12:15:01 -0800592 },
David Tolnay9204bac2019-01-19 19:26:17 -0800593 Group::Fallback(g) => g.delimiter(),
David Tolnayf14813f2018-09-08 17:14:07 -0700594 }
595 }
596
597 pub fn stream(&self) -> TokenStream {
598 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800599 Group::Compiler(g) => TokenStream::Compiler(g.stream()),
600 Group::Fallback(g) => TokenStream::Fallback(g.stream()),
David Tolnayf14813f2018-09-08 17:14:07 -0700601 }
602 }
603
604 pub fn span(&self) -> Span {
605 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800606 Group::Compiler(g) => Span::Compiler(g.span()),
607 Group::Fallback(g) => Span::Fallback(g.span()),
David Tolnayf14813f2018-09-08 17:14:07 -0700608 }
609 }
610
611 #[cfg(super_unstable)]
612 pub fn span_open(&self) -> Span {
613 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800614 Group::Compiler(g) => Span::Compiler(g.span_open()),
615 Group::Fallback(g) => Span::Fallback(g.span_open()),
David Tolnayf14813f2018-09-08 17:14:07 -0700616 }
617 }
618
619 #[cfg(super_unstable)]
620 pub fn span_close(&self) -> Span {
621 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800622 Group::Compiler(g) => Span::Compiler(g.span_close()),
623 Group::Fallback(g) => Span::Fallback(g.span_close()),
David Tolnayf14813f2018-09-08 17:14:07 -0700624 }
625 }
626
627 pub fn set_span(&mut self, span: Span) {
628 match (self, span) {
David Tolnay9204bac2019-01-19 19:26:17 -0800629 (Group::Compiler(g), Span::Compiler(s)) => g.set_span(s),
630 (Group::Fallback(g), Span::Fallback(s)) => g.set_span(s),
David Tolnayf14813f2018-09-08 17:14:07 -0700631 _ => mismatch(),
632 }
633 }
634
635 fn unwrap_nightly(self) -> proc_macro::Group {
636 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800637 Group::Compiler(g) => g,
638 Group::Fallback(_) => mismatch(),
David Tolnayf14813f2018-09-08 17:14:07 -0700639 }
640 }
641}
642
David Tolnayaef075b2019-01-16 16:29:18 -0800643impl From<fallback::Group> for Group {
644 fn from(g: fallback::Group) -> Self {
David Tolnay9204bac2019-01-19 19:26:17 -0800645 Group::Fallback(g)
David Tolnayf14813f2018-09-08 17:14:07 -0700646 }
647}
648
649impl fmt::Display for Group {
650 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
651 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800652 Group::Compiler(group) => group.fmt(formatter),
653 Group::Fallback(group) => group.fmt(formatter),
David Tolnayf14813f2018-09-08 17:14:07 -0700654 }
655 }
656}
657
658impl fmt::Debug for Group {
659 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
660 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800661 Group::Compiler(group) => group.fmt(formatter),
662 Group::Fallback(group) => group.fmt(formatter),
David Tolnayf14813f2018-09-08 17:14:07 -0700663 }
664 }
665}
666
667#[derive(Clone)]
Alex Crichtonf3888432018-05-16 09:11:05 -0700668pub enum Ident {
David Tolnay9204bac2019-01-19 19:26:17 -0800669 Compiler(proc_macro::Ident),
670 Fallback(fallback::Ident),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700671}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700672
Alex Crichtonf3888432018-05-16 09:11:05 -0700673impl Ident {
674 pub fn new(string: &str, span: Span) -> Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700675 match span {
David Tolnay9204bac2019-01-19 19:26:17 -0800676 Span::Compiler(s) => Ident::Compiler(proc_macro::Ident::new(string, s)),
677 Span::Fallback(s) => Ident::Fallback(fallback::Ident::new(string, s)),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700678 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700679 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700680
Alex Crichtonf3888432018-05-16 09:11:05 -0700681 pub fn new_raw(string: &str, span: Span) -> Ident {
682 match span {
David Tolnay9204bac2019-01-19 19:26:17 -0800683 Span::Compiler(s) => {
Alex Crichtonce0904d2018-08-27 17:29:49 -0700684 let p: proc_macro::TokenStream = string.parse().unwrap();
685 let ident = match p.into_iter().next() {
686 Some(proc_macro::TokenTree::Ident(mut i)) => {
687 i.set_span(s);
688 i
689 }
690 _ => panic!(),
691 };
David Tolnay9204bac2019-01-19 19:26:17 -0800692 Ident::Compiler(ident)
Alex Crichtonce0904d2018-08-27 17:29:49 -0700693 }
David Tolnay9204bac2019-01-19 19:26:17 -0800694 Span::Fallback(s) => Ident::Fallback(fallback::Ident::new_raw(string, s)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700695 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700696 }
697
698 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700699 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800700 Ident::Compiler(t) => Span::Compiler(t.span()),
701 Ident::Fallback(t) => Span::Fallback(t.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700702 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700703 }
704
705 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700706 match (self, span) {
David Tolnay9204bac2019-01-19 19:26:17 -0800707 (Ident::Compiler(t), Span::Compiler(s)) => t.set_span(s),
708 (Ident::Fallback(t), Span::Fallback(s)) => t.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700709 _ => mismatch(),
710 }
711 }
712
Alex Crichtonf3888432018-05-16 09:11:05 -0700713 fn unwrap_nightly(self) -> proc_macro::Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700714 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800715 Ident::Compiler(s) => s,
716 Ident::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700717 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700718 }
719}
720
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700721impl PartialEq for Ident {
722 fn eq(&self, other: &Ident) -> bool {
723 match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800724 (Ident::Compiler(t), Ident::Compiler(o)) => t.to_string() == o.to_string(),
725 (Ident::Fallback(t), Ident::Fallback(o)) => t == o,
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700726 _ => mismatch(),
727 }
728 }
729}
730
731impl<T> PartialEq<T> for Ident
732where
733 T: ?Sized + AsRef<str>,
734{
735 fn eq(&self, other: &T) -> bool {
736 let other = other.as_ref();
737 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800738 Ident::Compiler(t) => t.to_string() == other,
739 Ident::Fallback(t) => t == other,
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700740 }
741 }
742}
743
Alex Crichtonf3888432018-05-16 09:11:05 -0700744impl fmt::Display for Ident {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700745 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700746 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800747 Ident::Compiler(t) => t.fmt(f),
748 Ident::Fallback(t) => t.fmt(f),
Alex Crichtonf3888432018-05-16 09:11:05 -0700749 }
750 }
751}
752
753impl fmt::Debug for Ident {
754 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
755 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800756 Ident::Compiler(t) => t.fmt(f),
757 Ident::Fallback(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700758 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700759 }
760}
761
762#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700763pub enum Literal {
David Tolnay9204bac2019-01-19 19:26:17 -0800764 Compiler(proc_macro::Literal),
765 Fallback(fallback::Literal),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700766}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700767
Alex Crichtona914a612018-04-04 07:48:44 -0700768macro_rules! suffixed_numbers {
769 ($($name:ident => $kind:ident,)*) => ($(
770 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700771 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800772 Literal::Compiler(proc_macro::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700773 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800774 Literal::Fallback(fallback::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700775 }
Alex Crichtona914a612018-04-04 07:48:44 -0700776 }
777 )*)
778}
779
780macro_rules! unsuffixed_integers {
781 ($($name:ident => $kind:ident,)*) => ($(
782 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700783 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800784 Literal::Compiler(proc_macro::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700785 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800786 Literal::Fallback(fallback::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700787 }
Alex Crichtona914a612018-04-04 07:48:44 -0700788 }
789 )*)
790}
791
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700792impl Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700793 suffixed_numbers! {
794 u8_suffixed => u8,
795 u16_suffixed => u16,
796 u32_suffixed => u32,
797 u64_suffixed => u64,
798 usize_suffixed => usize,
799 i8_suffixed => i8,
800 i16_suffixed => i16,
801 i32_suffixed => i32,
802 i64_suffixed => i64,
803 isize_suffixed => isize,
804
805 f32_suffixed => f32,
806 f64_suffixed => f64,
807 }
808
Alex Crichton69385662018-11-08 06:30:04 -0800809 #[cfg(u128)]
810 suffixed_numbers! {
811 i128_suffixed => i128,
812 u128_suffixed => u128,
813 }
814
Alex Crichtona914a612018-04-04 07:48:44 -0700815 unsuffixed_integers! {
816 u8_unsuffixed => u8,
817 u16_unsuffixed => u16,
818 u32_unsuffixed => u32,
819 u64_unsuffixed => u64,
820 usize_unsuffixed => usize,
821 i8_unsuffixed => i8,
822 i16_unsuffixed => i16,
823 i32_unsuffixed => i32,
824 i64_unsuffixed => i64,
825 isize_unsuffixed => isize,
826 }
827
Alex Crichton69385662018-11-08 06:30:04 -0800828 #[cfg(u128)]
829 unsuffixed_integers! {
830 i128_unsuffixed => i128,
831 u128_unsuffixed => u128,
832 }
833
Alex Crichtona914a612018-04-04 07:48:44 -0700834 pub fn f32_unsuffixed(f: f32) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700835 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800836 Literal::Compiler(proc_macro::Literal::f32_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700837 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800838 Literal::Fallback(fallback::Literal::f32_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700839 }
Alex Crichtona914a612018-04-04 07:48:44 -0700840 }
841
842 pub fn f64_unsuffixed(f: f64) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700843 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800844 Literal::Compiler(proc_macro::Literal::f64_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700845 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800846 Literal::Fallback(fallback::Literal::f64_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700847 }
Alex Crichtona914a612018-04-04 07:48:44 -0700848 }
849
Alex Crichtona914a612018-04-04 07:48:44 -0700850 pub fn string(t: &str) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700851 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800852 Literal::Compiler(proc_macro::Literal::string(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700853 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800854 Literal::Fallback(fallback::Literal::string(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700855 }
Alex Crichtona914a612018-04-04 07:48:44 -0700856 }
857
858 pub fn character(t: char) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700859 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800860 Literal::Compiler(proc_macro::Literal::character(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700861 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800862 Literal::Fallback(fallback::Literal::character(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700863 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700864 }
865
866 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700867 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800868 Literal::Compiler(proc_macro::Literal::byte_string(bytes))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700869 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800870 Literal::Fallback(fallback::Literal::byte_string(bytes))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700871 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700872 }
873
Alex Crichtonb2c94622018-04-04 07:36:41 -0700874 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700875 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800876 Literal::Compiler(lit) => Span::Compiler(lit.span()),
877 Literal::Fallback(lit) => Span::Fallback(lit.span()),
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 set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700882 match (self, span) {
David Tolnay9204bac2019-01-19 19:26:17 -0800883 (Literal::Compiler(lit), Span::Compiler(s)) => lit.set_span(s),
884 (Literal::Fallback(lit), Span::Fallback(s)) => lit.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700885 _ => mismatch(),
886 }
887 }
888
889 fn unwrap_nightly(self) -> proc_macro::Literal {
890 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800891 Literal::Compiler(s) => s,
892 Literal::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700893 }
894 }
895}
896
David Tolnayaef075b2019-01-16 16:29:18 -0800897impl From<fallback::Literal> for Literal {
898 fn from(s: fallback::Literal) -> Literal {
David Tolnay9204bac2019-01-19 19:26:17 -0800899 Literal::Fallback(s)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700900 }
901}
902
903impl fmt::Display for Literal {
904 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700905 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800906 Literal::Compiler(t) => t.fmt(f),
907 Literal::Fallback(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700908 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700909 }
910}
911
912impl fmt::Debug for Literal {
913 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700914 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800915 Literal::Compiler(t) => t.fmt(f),
916 Literal::Fallback(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700917 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700918 }
919}