blob: cbb651530ada5fa790d67b9913a5404a63de554b [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
David Tolnay35d24c32019-01-31 15:32:26 -080028 #[allow(deprecated)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -070029 static WORKS: AtomicUsize = ATOMIC_USIZE_INIT;
David Tolnay2ed15d52018-09-01 08:46:12 -070030 static INIT: Once = Once::new();
Alex Crichton30a4e9e2018-04-27 17:02:19 -070031
32 match WORKS.load(Ordering::SeqCst) {
33 1 => return false,
34 2 => return true,
35 _ => {}
36 }
David Tolnay2ed15d52018-09-01 08:46:12 -070037
38 // Swap in a null panic hook to avoid printing "thread panicked" to stderr,
39 // then use catch_unwind to determine whether the compiler's proc_macro is
40 // working. When proc-macro2 is used from outside of a procedural macro all
41 // of the proc_macro crate's APIs currently panic.
42 //
43 // The Once is to prevent the possibility of this ordering:
44 //
45 // thread 1 calls take_hook, gets the user's original hook
46 // thread 1 calls set_hook with the null hook
47 // thread 2 calls take_hook, thinks null hook is the original hook
48 // thread 2 calls set_hook with the null hook
49 // thread 1 calls set_hook with the actual original hook
50 // thread 2 calls set_hook with what it thinks is the original hook
51 //
52 // in which the user's hook has been lost.
53 //
54 // There is still a race condition where a panic in a different thread can
55 // happen during the interval that the user's original panic hook is
56 // unregistered such that their hook is incorrectly not called. This is
57 // sufficiently unlikely and less bad than printing panic messages to stderr
58 // on correct use of this crate. Maybe there is a libstd feature request
59 // here. For now, if a user needs to guarantee that this failure mode does
60 // not occur, they need to call e.g. `proc_macro2::Span::call_site()` from
61 // the main thread before launching any other threads.
62 INIT.call_once(|| {
David Tolnay78ef7732018-09-01 19:20:23 -070063 type PanicHook = Fn(&PanicInfo) + Sync + Send + 'static;
64
65 let null_hook: Box<PanicHook> = Box::new(|_panic_info| { /* ignore */ });
66 let sanity_check = &*null_hook as *const PanicHook;
David Tolnay2ed15d52018-09-01 08:46:12 -070067 let original_hook = panic::take_hook();
David Tolnay78ef7732018-09-01 19:20:23 -070068 panic::set_hook(null_hook);
69
David Tolnay2ed15d52018-09-01 08:46:12 -070070 let works = panic::catch_unwind(|| proc_macro::Span::call_site()).is_ok();
71 WORKS.store(works as usize + 1, Ordering::SeqCst);
David Tolnay78ef7732018-09-01 19:20:23 -070072
73 let hopefully_null_hook = panic::take_hook();
David Tolnay2ed15d52018-09-01 08:46:12 -070074 panic::set_hook(original_hook);
David Tolnay78ef7732018-09-01 19:20:23 -070075 if sanity_check != &*hopefully_null_hook {
76 panic!("observed race condition in proc_macro2::nightly_works");
77 }
David Tolnay2ed15d52018-09-01 08:46:12 -070078 });
79 nightly_works()
Alex Crichton30a4e9e2018-04-27 17:02:19 -070080}
81
82fn mismatch() -> ! {
83 panic!("stable/nightly mismatch")
84}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070085
86impl TokenStream {
David Tolnayc3bb4592018-05-28 20:09:44 -070087 pub fn new() -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070088 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -080089 TokenStream::Compiler(proc_macro::TokenStream::new())
Alex Crichton30a4e9e2018-04-27 17:02:19 -070090 } else {
David Tolnay9204bac2019-01-19 19:26:17 -080091 TokenStream::Fallback(fallback::TokenStream::new())
Alex Crichton30a4e9e2018-04-27 17:02:19 -070092 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070093 }
94
95 pub fn is_empty(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070096 match self {
David Tolnay9204bac2019-01-19 19:26:17 -080097 TokenStream::Compiler(tts) => tts.is_empty(),
98 TokenStream::Fallback(tts) => tts.is_empty(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -070099 }
100 }
101
102 fn unwrap_nightly(self) -> proc_macro::TokenStream {
103 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800104 TokenStream::Compiler(s) => s,
105 TokenStream::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700106 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700107 }
David Tolnay5c58c532018-08-13 11:33:51 -0700108
David Tolnayaef075b2019-01-16 16:29:18 -0800109 fn unwrap_stable(self) -> fallback::TokenStream {
David Tolnay5c58c532018-08-13 11:33:51 -0700110 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800111 TokenStream::Compiler(_) => mismatch(),
112 TokenStream::Fallback(s) => s,
David Tolnay5c58c532018-08-13 11:33:51 -0700113 }
114 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700115}
116
117impl FromStr for TokenStream {
118 type Err = LexError;
119
120 fn from_str(src: &str) -> Result<TokenStream, LexError> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700121 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800122 Ok(TokenStream::Compiler(src.parse()?))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700123 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800124 Ok(TokenStream::Fallback(src.parse()?))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700125 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700126 }
127}
128
129impl fmt::Display for TokenStream {
130 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700131 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800132 TokenStream::Compiler(tts) => tts.fmt(f),
133 TokenStream::Fallback(tts) => tts.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700134 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700135 }
136}
137
138impl From<proc_macro::TokenStream> for TokenStream {
139 fn from(inner: proc_macro::TokenStream) -> TokenStream {
David Tolnay9204bac2019-01-19 19:26:17 -0800140 TokenStream::Compiler(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700141 }
142}
143
144impl From<TokenStream> for proc_macro::TokenStream {
145 fn from(inner: TokenStream) -> proc_macro::TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700146 match inner {
David Tolnay9204bac2019-01-19 19:26:17 -0800147 TokenStream::Compiler(inner) => inner,
148 TokenStream::Fallback(inner) => inner.to_string().parse().unwrap(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700149 }
150 }
151}
152
David Tolnayaef075b2019-01-16 16:29:18 -0800153impl From<fallback::TokenStream> for TokenStream {
154 fn from(inner: fallback::TokenStream) -> TokenStream {
David Tolnay9204bac2019-01-19 19:26:17 -0800155 TokenStream::Fallback(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700156 }
157}
158
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700159impl From<TokenTree> for TokenStream {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700160 fn from(token: TokenTree) -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700161 if !nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800162 return TokenStream::Fallback(token.into());
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700163 }
Alex Crichton9cd80a62018-04-05 17:46:58 -0700164 let tt: proc_macro::TokenTree = match token {
David Tolnayf14813f2018-09-08 17:14:07 -0700165 TokenTree::Group(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichtonf3888432018-05-16 09:11:05 -0700166 TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700167 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700168 Spacing::Joint => proc_macro::Spacing::Joint,
169 Spacing::Alone => proc_macro::Spacing::Alone,
170 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700171 let mut op = proc_macro::Punct::new(tt.as_char(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700172 op.set_span(tt.span().inner.unwrap_nightly());
Alex Crichton9cd80a62018-04-05 17:46:58 -0700173 op.into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700174 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700175 TokenTree::Ident(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700176 TokenTree::Literal(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700177 };
David Tolnay9204bac2019-01-19 19:26:17 -0800178 TokenStream::Compiler(tt.into())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700179 }
180}
181
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700182impl iter::FromIterator<TokenTree> for TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700183 fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self {
184 if nightly_works() {
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700185 let trees = trees
186 .into_iter()
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700187 .map(TokenStream::from)
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700188 .flat_map(|t| match t {
David Tolnay9204bac2019-01-19 19:26:17 -0800189 TokenStream::Compiler(s) => s,
190 TokenStream::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700191 });
David Tolnay9204bac2019-01-19 19:26:17 -0800192 TokenStream::Compiler(trees.collect())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700193 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800194 TokenStream::Fallback(trees.into_iter().collect())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700195 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700196 }
197}
198
Alex Crichton53b00672018-09-06 17:16:10 -0700199impl iter::FromIterator<TokenStream> for TokenStream {
200 fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self {
201 let mut streams = streams.into_iter();
202 match streams.next() {
203 #[cfg(slow_extend)]
David Tolnay9204bac2019-01-19 19:26:17 -0800204 Some(TokenStream::Compiler(first)) => {
David Tolnay4453fcc2019-01-16 12:15:01 -0800205 let stream = iter::once(first)
206 .chain(streams.map(|s| match s {
David Tolnay9204bac2019-01-19 19:26:17 -0800207 TokenStream::Compiler(s) => s,
208 TokenStream::Fallback(_) => mismatch(),
David Tolnay4453fcc2019-01-16 12:15:01 -0800209 }))
210 .collect();
David Tolnay9204bac2019-01-19 19:26:17 -0800211 TokenStream::Compiler(stream)
Alex Crichton53b00672018-09-06 17:16:10 -0700212 }
213 #[cfg(not(slow_extend))]
David Tolnay9204bac2019-01-19 19:26:17 -0800214 Some(TokenStream::Compiler(mut first)) => {
David Tolnay4453fcc2019-01-16 12:15:01 -0800215 first.extend(streams.map(|s| match s {
David Tolnay9204bac2019-01-19 19:26:17 -0800216 TokenStream::Compiler(s) => s,
217 TokenStream::Fallback(_) => mismatch(),
Alex Crichton53b00672018-09-06 17:16:10 -0700218 }));
David Tolnay9204bac2019-01-19 19:26:17 -0800219 TokenStream::Compiler(first)
Alex Crichton53b00672018-09-06 17:16:10 -0700220 }
David Tolnay9204bac2019-01-19 19:26:17 -0800221 Some(TokenStream::Fallback(mut first)) => {
David Tolnay4453fcc2019-01-16 12:15:01 -0800222 first.extend(streams.map(|s| match s {
David Tolnay9204bac2019-01-19 19:26:17 -0800223 TokenStream::Fallback(s) => s,
224 TokenStream::Compiler(_) => mismatch(),
Alex Crichton53b00672018-09-06 17:16:10 -0700225 }));
David Tolnay9204bac2019-01-19 19:26:17 -0800226 TokenStream::Fallback(first)
Alex Crichton53b00672018-09-06 17:16:10 -0700227 }
228 None => TokenStream::new(),
Alex Crichton53b00672018-09-06 17:16:10 -0700229 }
230 }
231}
232
Alex Crichtonf3888432018-05-16 09:11:05 -0700233impl Extend<TokenTree> for TokenStream {
234 fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
235 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800236 TokenStream::Compiler(tts) => {
David Tolnaye839e4f2018-09-06 09:36:43 -0700237 #[cfg(not(slow_extend))]
238 {
239 tts.extend(
240 streams
241 .into_iter()
242 .map(|t| TokenStream::from(t).unwrap_nightly()),
243 );
244 }
245 #[cfg(slow_extend)]
246 {
David Tolnay4453fcc2019-01-16 12:15:01 -0800247 *tts =
248 tts.clone()
249 .into_iter()
250 .chain(streams.into_iter().map(TokenStream::from).flat_map(
251 |t| match t {
David Tolnay9204bac2019-01-19 19:26:17 -0800252 TokenStream::Compiler(tts) => tts.into_iter(),
David Tolnaye839e4f2018-09-06 09:36:43 -0700253 _ => mismatch(),
David Tolnay4453fcc2019-01-16 12:15:01 -0800254 },
255 ))
256 .collect();
David Tolnaye839e4f2018-09-06 09:36:43 -0700257 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700258 }
David Tolnay9204bac2019-01-19 19:26:17 -0800259 TokenStream::Fallback(tts) => tts.extend(streams),
Alex Crichtonf3888432018-05-16 09:11:05 -0700260 }
261 }
262}
263
David Tolnay5c58c532018-08-13 11:33:51 -0700264impl Extend<TokenStream> for TokenStream {
265 fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
266 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800267 TokenStream::Compiler(tts) => {
David Tolnaye839e4f2018-09-06 09:36:43 -0700268 #[cfg(not(slow_extend))]
269 {
270 tts.extend(streams.into_iter().map(|stream| stream.unwrap_nightly()));
271 }
272 #[cfg(slow_extend)]
273 {
274 *tts = tts
275 .clone()
276 .into_iter()
David Tolnay4453fcc2019-01-16 12:15:01 -0800277 .chain(streams.into_iter().flat_map(|t| match t {
David Tolnay9204bac2019-01-19 19:26:17 -0800278 TokenStream::Compiler(tts) => tts.into_iter(),
David Tolnay4453fcc2019-01-16 12:15:01 -0800279 _ => mismatch(),
280 }))
281 .collect();
David Tolnaye839e4f2018-09-06 09:36:43 -0700282 }
David Tolnay5c58c532018-08-13 11:33:51 -0700283 }
David Tolnay9204bac2019-01-19 19:26:17 -0800284 TokenStream::Fallback(tts) => {
David Tolnay5c58c532018-08-13 11:33:51 -0700285 tts.extend(streams.into_iter().map(|stream| stream.unwrap_stable()))
286 }
287 }
288 }
289}
290
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700291impl fmt::Debug for TokenStream {
292 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700293 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800294 TokenStream::Compiler(tts) => tts.fmt(f),
295 TokenStream::Fallback(tts) => tts.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700296 }
297 }
298}
299
300impl From<proc_macro::LexError> for LexError {
301 fn from(e: proc_macro::LexError) -> LexError {
David Tolnay9204bac2019-01-19 19:26:17 -0800302 LexError::Compiler(e)
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700303 }
304}
305
David Tolnayaef075b2019-01-16 16:29:18 -0800306impl From<fallback::LexError> for LexError {
307 fn from(e: fallback::LexError) -> LexError {
David Tolnay9204bac2019-01-19 19:26:17 -0800308 LexError::Fallback(e)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700309 }
310}
311
312impl fmt::Debug for LexError {
313 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700314 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800315 LexError::Compiler(e) => e.fmt(f),
316 LexError::Fallback(e) => e.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700317 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700318 }
319}
320
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700321pub enum TokenTreeIter {
David Tolnay9204bac2019-01-19 19:26:17 -0800322 Compiler(proc_macro::token_stream::IntoIter),
323 Fallback(fallback::TokenTreeIter),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700324}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700325
326impl IntoIterator for TokenStream {
327 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700328 type IntoIter = TokenTreeIter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700329
Alex Crichton1a7f7622017-07-05 17:47:15 -0700330 fn into_iter(self) -> TokenTreeIter {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700331 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800332 TokenStream::Compiler(tts) => TokenTreeIter::Compiler(tts.into_iter()),
333 TokenStream::Fallback(tts) => TokenTreeIter::Fallback(tts.into_iter()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700334 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700335 }
336}
337
Alex Crichton1a7f7622017-07-05 17:47:15 -0700338impl Iterator for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700339 type Item = TokenTree;
340
341 fn next(&mut self) -> Option<TokenTree> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700342 let token = match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800343 TokenTreeIter::Compiler(iter) => iter.next()?,
344 TokenTreeIter::Fallback(iter) => return iter.next(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700345 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700346 Some(match token {
David Tolnay9204bac2019-01-19 19:26:17 -0800347 proc_macro::TokenTree::Group(tt) => ::Group::_new(Group::Compiler(tt)).into(),
Alex Crichtonf3888432018-05-16 09:11:05 -0700348 proc_macro::TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700349 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700350 proc_macro::Spacing::Joint => Spacing::Joint,
351 proc_macro::Spacing::Alone => Spacing::Alone,
352 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700353 let mut o = Punct::new(tt.as_char(), spacing);
David Tolnay9204bac2019-01-19 19:26:17 -0800354 o.set_span(::Span::_new(Span::Compiler(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700355 o.into()
356 }
David Tolnay9204bac2019-01-19 19:26:17 -0800357 proc_macro::TokenTree::Ident(s) => ::Ident::_new(Ident::Compiler(s)).into(),
358 proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Compiler(l)).into(),
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700359 })
360 }
361
362 fn size_hint(&self) -> (usize, Option<usize>) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700363 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800364 TokenTreeIter::Compiler(tts) => tts.size_hint(),
365 TokenTreeIter::Fallback(tts) => tts.size_hint(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700366 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700367 }
368}
369
Alex Crichton1a7f7622017-07-05 17:47:15 -0700370impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700371 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700372 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700373 }
374}
375
Nika Layzellb35a9a32017-12-30 14:34:35 -0500376#[derive(Clone, PartialEq, Eq)]
Alex Crichtonce0904d2018-08-27 17:29:49 -0700377#[cfg(super_unstable)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700378pub enum SourceFile {
David Tolnay9204bac2019-01-19 19:26:17 -0800379 Compiler(proc_macro::SourceFile),
380 Fallback(fallback::SourceFile),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700381}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500382
Alex Crichtonce0904d2018-08-27 17:29:49 -0700383#[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500384impl SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700385 fn nightly(sf: proc_macro::SourceFile) -> Self {
David Tolnay9204bac2019-01-19 19:26:17 -0800386 SourceFile::Compiler(sf)
Nika Layzellb35a9a32017-12-30 14:34:35 -0500387 }
388
Nika Layzellf8d5f212017-12-11 14:07:02 -0500389 /// Get the path to this source file as a string.
David Tolnay9cd3b4c2018-11-11 16:47:32 -0800390 pub fn path(&self) -> PathBuf {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700391 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800392 SourceFile::Compiler(a) => a.path(),
393 SourceFile::Fallback(a) => a.path(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700394 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500395 }
396
397 pub fn is_real(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700398 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800399 SourceFile::Compiler(a) => a.is_real(),
400 SourceFile::Fallback(a) => a.is_real(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700401 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500402 }
403}
404
Alex Crichtonce0904d2018-08-27 17:29:49 -0700405#[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500406impl fmt::Debug for SourceFile {
407 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700408 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800409 SourceFile::Compiler(a) => a.fmt(f),
410 SourceFile::Fallback(a) => a.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700411 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500412 }
413}
414
David Tolnay3b1f7d22019-01-28 12:22:11 -0800415#[cfg(any(super_unstable, feature = "span-locations"))]
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
David Tolnay3b1f7d22019-01-28 12:22:11 -0800478 #[cfg(any(super_unstable, feature = "span-locations"))]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500479 pub fn start(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700480 match self {
David Tolnay3b1f7d22019-01-28 12:22:11 -0800481 #[cfg(nightly)]
David Tolnay9204bac2019-01-19 19:26:17 -0800482 Span::Compiler(s) => {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700483 let proc_macro::LineColumn { line, column } = s.start();
484 LineColumn { line, column }
485 }
David Tolnay3b1f7d22019-01-28 12:22:11 -0800486 #[cfg(not(nightly))]
487 Span::Compiler(_) => LineColumn { line: 0, column: 0 },
David Tolnay9204bac2019-01-19 19:26:17 -0800488 Span::Fallback(s) => {
David Tolnayaef075b2019-01-16 16:29:18 -0800489 let fallback::LineColumn { line, column } = s.start();
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700490 LineColumn { line, column }
491 }
492 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500493 }
494
David Tolnay3b1f7d22019-01-28 12:22:11 -0800495 #[cfg(any(super_unstable, feature = "span-locations"))]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500496 pub fn end(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700497 match self {
David Tolnay3b1f7d22019-01-28 12:22:11 -0800498 #[cfg(nightly)]
David Tolnay9204bac2019-01-19 19:26:17 -0800499 Span::Compiler(s) => {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700500 let proc_macro::LineColumn { line, column } = s.end();
501 LineColumn { line, column }
502 }
David Tolnay3b1f7d22019-01-28 12:22:11 -0800503 #[cfg(not(nightly))]
504 Span::Compiler(_) => LineColumn { line: 0, column: 0 },
David Tolnay9204bac2019-01-19 19:26:17 -0800505 Span::Fallback(s) => {
David Tolnayaef075b2019-01-16 16:29:18 -0800506 let fallback::LineColumn { line, column } = s.end();
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700507 LineColumn { line, column }
508 }
509 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500510 }
511
Alex Crichtonce0904d2018-08-27 17:29:49 -0700512 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500513 pub fn join(&self, other: Span) -> Option<Span> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700514 let ret = match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800515 (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.join(b)?),
516 (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.join(b)?),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700517 _ => return None,
518 };
519 Some(ret)
Nika Layzellf8d5f212017-12-11 14:07:02 -0500520 }
Alex Crichtone24f7342018-04-05 17:58:11 -0700521
Alex Crichtonce0904d2018-08-27 17:29:49 -0700522 #[cfg(super_unstable)]
Alex Crichtone24f7342018-04-05 17:58:11 -0700523 pub fn eq(&self, other: &Span) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700524 match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800525 (Span::Compiler(a), Span::Compiler(b)) => a.eq(b),
526 (Span::Fallback(a), Span::Fallback(b)) => a.eq(b),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700527 _ => false,
528 }
529 }
530
531 fn unwrap_nightly(self) -> proc_macro::Span {
532 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800533 Span::Compiler(s) => s,
534 Span::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700535 }
536 }
537}
538
539impl From<proc_macro::Span> for ::Span {
540 fn from(proc_span: proc_macro::Span) -> ::Span {
David Tolnay9204bac2019-01-19 19:26:17 -0800541 ::Span::_new(Span::Compiler(proc_span))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700542 }
543}
544
David Tolnayaef075b2019-01-16 16:29:18 -0800545impl From<fallback::Span> for Span {
546 fn from(inner: fallback::Span) -> Span {
David Tolnay9204bac2019-01-19 19:26:17 -0800547 Span::Fallback(inner)
Alex Crichtone24f7342018-04-05 17:58:11 -0700548 }
Alex Crichton998f6422017-11-19 08:06:27 -0800549}
550
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700551impl fmt::Debug for Span {
552 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700553 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800554 Span::Compiler(s) => s.fmt(f),
555 Span::Fallback(s) => s.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700556 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700557 }
558}
559
David Tolnayfd8cdc82019-01-19 19:23:59 -0800560pub fn debug_span_field_if_nontrivial(debug: &mut fmt::DebugStruct, span: Span) {
561 match span {
562 Span::Compiler(s) => {
563 debug.field("span", &s);
564 }
565 Span::Fallback(s) => fallback::debug_span_field_if_nontrivial(debug, s),
566 }
567}
568
Alex Crichtonf3888432018-05-16 09:11:05 -0700569#[derive(Clone)]
David Tolnayf14813f2018-09-08 17:14:07 -0700570pub enum Group {
David Tolnay9204bac2019-01-19 19:26:17 -0800571 Compiler(proc_macro::Group),
572 Fallback(fallback::Group),
David Tolnayf14813f2018-09-08 17:14:07 -0700573}
574
575impl Group {
576 pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
577 match stream {
David Tolnay9204bac2019-01-19 19:26:17 -0800578 TokenStream::Compiler(stream) => {
David Tolnayf14813f2018-09-08 17:14:07 -0700579 let delimiter = match delimiter {
580 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
581 Delimiter::Bracket => proc_macro::Delimiter::Bracket,
582 Delimiter::Brace => proc_macro::Delimiter::Brace,
583 Delimiter::None => proc_macro::Delimiter::None,
584 };
David Tolnay9204bac2019-01-19 19:26:17 -0800585 Group::Compiler(proc_macro::Group::new(delimiter, stream))
David Tolnayf14813f2018-09-08 17:14:07 -0700586 }
David Tolnay471a7ed2019-01-19 21:00:11 -0800587 TokenStream::Fallback(stream) => {
588 Group::Fallback(fallback::Group::new(delimiter, stream))
589 }
David Tolnayf14813f2018-09-08 17:14:07 -0700590 }
591 }
592
593 pub fn delimiter(&self) -> Delimiter {
594 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800595 Group::Compiler(g) => match g.delimiter() {
David Tolnayf14813f2018-09-08 17:14:07 -0700596 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
597 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
598 proc_macro::Delimiter::Brace => Delimiter::Brace,
599 proc_macro::Delimiter::None => Delimiter::None,
David Tolnay4453fcc2019-01-16 12:15:01 -0800600 },
David Tolnay9204bac2019-01-19 19:26:17 -0800601 Group::Fallback(g) => g.delimiter(),
David Tolnayf14813f2018-09-08 17:14:07 -0700602 }
603 }
604
605 pub fn stream(&self) -> TokenStream {
606 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800607 Group::Compiler(g) => TokenStream::Compiler(g.stream()),
608 Group::Fallback(g) => TokenStream::Fallback(g.stream()),
David Tolnayf14813f2018-09-08 17:14:07 -0700609 }
610 }
611
612 pub fn span(&self) -> Span {
613 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800614 Group::Compiler(g) => Span::Compiler(g.span()),
615 Group::Fallback(g) => Span::Fallback(g.span()),
David Tolnayf14813f2018-09-08 17:14:07 -0700616 }
617 }
618
619 #[cfg(super_unstable)]
620 pub fn span_open(&self) -> Span {
621 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800622 Group::Compiler(g) => Span::Compiler(g.span_open()),
623 Group::Fallback(g) => Span::Fallback(g.span_open()),
David Tolnayf14813f2018-09-08 17:14:07 -0700624 }
625 }
626
627 #[cfg(super_unstable)]
628 pub fn span_close(&self) -> Span {
629 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800630 Group::Compiler(g) => Span::Compiler(g.span_close()),
631 Group::Fallback(g) => Span::Fallback(g.span_close()),
David Tolnayf14813f2018-09-08 17:14:07 -0700632 }
633 }
634
635 pub fn set_span(&mut self, span: Span) {
636 match (self, span) {
David Tolnay9204bac2019-01-19 19:26:17 -0800637 (Group::Compiler(g), Span::Compiler(s)) => g.set_span(s),
638 (Group::Fallback(g), Span::Fallback(s)) => g.set_span(s),
David Tolnayf14813f2018-09-08 17:14:07 -0700639 _ => mismatch(),
640 }
641 }
642
643 fn unwrap_nightly(self) -> proc_macro::Group {
644 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800645 Group::Compiler(g) => g,
646 Group::Fallback(_) => mismatch(),
David Tolnayf14813f2018-09-08 17:14:07 -0700647 }
648 }
649}
650
David Tolnayaef075b2019-01-16 16:29:18 -0800651impl From<fallback::Group> for Group {
652 fn from(g: fallback::Group) -> Self {
David Tolnay9204bac2019-01-19 19:26:17 -0800653 Group::Fallback(g)
David Tolnayf14813f2018-09-08 17:14:07 -0700654 }
655}
656
657impl fmt::Display for Group {
658 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
659 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800660 Group::Compiler(group) => group.fmt(formatter),
661 Group::Fallback(group) => group.fmt(formatter),
David Tolnayf14813f2018-09-08 17:14:07 -0700662 }
663 }
664}
665
666impl fmt::Debug for Group {
667 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
668 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800669 Group::Compiler(group) => group.fmt(formatter),
670 Group::Fallback(group) => group.fmt(formatter),
David Tolnayf14813f2018-09-08 17:14:07 -0700671 }
672 }
673}
674
675#[derive(Clone)]
Alex Crichtonf3888432018-05-16 09:11:05 -0700676pub enum Ident {
David Tolnay9204bac2019-01-19 19:26:17 -0800677 Compiler(proc_macro::Ident),
678 Fallback(fallback::Ident),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700679}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700680
Alex Crichtonf3888432018-05-16 09:11:05 -0700681impl Ident {
682 pub fn new(string: &str, span: Span) -> Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700683 match span {
David Tolnay9204bac2019-01-19 19:26:17 -0800684 Span::Compiler(s) => Ident::Compiler(proc_macro::Ident::new(string, s)),
685 Span::Fallback(s) => Ident::Fallback(fallback::Ident::new(string, s)),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700686 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700687 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700688
Alex Crichtonf3888432018-05-16 09:11:05 -0700689 pub fn new_raw(string: &str, span: Span) -> Ident {
690 match span {
David Tolnay9204bac2019-01-19 19:26:17 -0800691 Span::Compiler(s) => {
Alex Crichtonce0904d2018-08-27 17:29:49 -0700692 let p: proc_macro::TokenStream = string.parse().unwrap();
693 let ident = match p.into_iter().next() {
694 Some(proc_macro::TokenTree::Ident(mut i)) => {
695 i.set_span(s);
696 i
697 }
698 _ => panic!(),
699 };
David Tolnay9204bac2019-01-19 19:26:17 -0800700 Ident::Compiler(ident)
Alex Crichtonce0904d2018-08-27 17:29:49 -0700701 }
David Tolnay9204bac2019-01-19 19:26:17 -0800702 Span::Fallback(s) => Ident::Fallback(fallback::Ident::new_raw(string, s)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700703 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700704 }
705
706 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700707 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800708 Ident::Compiler(t) => Span::Compiler(t.span()),
709 Ident::Fallback(t) => Span::Fallback(t.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700710 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700711 }
712
713 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700714 match (self, span) {
David Tolnay9204bac2019-01-19 19:26:17 -0800715 (Ident::Compiler(t), Span::Compiler(s)) => t.set_span(s),
716 (Ident::Fallback(t), Span::Fallback(s)) => t.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700717 _ => mismatch(),
718 }
719 }
720
Alex Crichtonf3888432018-05-16 09:11:05 -0700721 fn unwrap_nightly(self) -> proc_macro::Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700722 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800723 Ident::Compiler(s) => s,
724 Ident::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700725 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700726 }
727}
728
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700729impl PartialEq for Ident {
730 fn eq(&self, other: &Ident) -> bool {
731 match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800732 (Ident::Compiler(t), Ident::Compiler(o)) => t.to_string() == o.to_string(),
733 (Ident::Fallback(t), Ident::Fallback(o)) => t == o,
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700734 _ => mismatch(),
735 }
736 }
737}
738
739impl<T> PartialEq<T> for Ident
740where
741 T: ?Sized + AsRef<str>,
742{
743 fn eq(&self, other: &T) -> bool {
744 let other = other.as_ref();
745 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800746 Ident::Compiler(t) => t.to_string() == other,
747 Ident::Fallback(t) => t == other,
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700748 }
749 }
750}
751
Alex Crichtonf3888432018-05-16 09:11:05 -0700752impl fmt::Display for Ident {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700753 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700754 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800755 Ident::Compiler(t) => t.fmt(f),
756 Ident::Fallback(t) => t.fmt(f),
Alex Crichtonf3888432018-05-16 09:11:05 -0700757 }
758 }
759}
760
761impl fmt::Debug for Ident {
762 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
763 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800764 Ident::Compiler(t) => t.fmt(f),
765 Ident::Fallback(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700766 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700767 }
768}
769
770#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700771pub enum Literal {
David Tolnay9204bac2019-01-19 19:26:17 -0800772 Compiler(proc_macro::Literal),
773 Fallback(fallback::Literal),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700774}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700775
Alex Crichtona914a612018-04-04 07:48:44 -0700776macro_rules! suffixed_numbers {
777 ($($name:ident => $kind:ident,)*) => ($(
778 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700779 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800780 Literal::Compiler(proc_macro::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700781 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800782 Literal::Fallback(fallback::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700783 }
Alex Crichtona914a612018-04-04 07:48:44 -0700784 }
785 )*)
786}
787
788macro_rules! unsuffixed_integers {
789 ($($name:ident => $kind:ident,)*) => ($(
790 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700791 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800792 Literal::Compiler(proc_macro::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700793 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800794 Literal::Fallback(fallback::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700795 }
Alex Crichtona914a612018-04-04 07:48:44 -0700796 }
797 )*)
798}
799
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700800impl Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700801 suffixed_numbers! {
802 u8_suffixed => u8,
803 u16_suffixed => u16,
804 u32_suffixed => u32,
805 u64_suffixed => u64,
806 usize_suffixed => usize,
807 i8_suffixed => i8,
808 i16_suffixed => i16,
809 i32_suffixed => i32,
810 i64_suffixed => i64,
811 isize_suffixed => isize,
812
813 f32_suffixed => f32,
814 f64_suffixed => f64,
815 }
816
Alex Crichton69385662018-11-08 06:30:04 -0800817 #[cfg(u128)]
818 suffixed_numbers! {
819 i128_suffixed => i128,
820 u128_suffixed => u128,
821 }
822
Alex Crichtona914a612018-04-04 07:48:44 -0700823 unsuffixed_integers! {
824 u8_unsuffixed => u8,
825 u16_unsuffixed => u16,
826 u32_unsuffixed => u32,
827 u64_unsuffixed => u64,
828 usize_unsuffixed => usize,
829 i8_unsuffixed => i8,
830 i16_unsuffixed => i16,
831 i32_unsuffixed => i32,
832 i64_unsuffixed => i64,
833 isize_unsuffixed => isize,
834 }
835
Alex Crichton69385662018-11-08 06:30:04 -0800836 #[cfg(u128)]
837 unsuffixed_integers! {
838 i128_unsuffixed => i128,
839 u128_unsuffixed => u128,
840 }
841
Alex Crichtona914a612018-04-04 07:48:44 -0700842 pub fn f32_unsuffixed(f: f32) -> 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::f32_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700845 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800846 Literal::Fallback(fallback::Literal::f32_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700847 }
Alex Crichtona914a612018-04-04 07:48:44 -0700848 }
849
850 pub fn f64_unsuffixed(f: f64) -> 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::f64_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700853 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800854 Literal::Fallback(fallback::Literal::f64_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700855 }
Alex Crichtona914a612018-04-04 07:48:44 -0700856 }
857
Alex Crichtona914a612018-04-04 07:48:44 -0700858 pub fn string(t: &str) -> 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::string(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700861 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800862 Literal::Fallback(fallback::Literal::string(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700863 }
Alex Crichtona914a612018-04-04 07:48:44 -0700864 }
865
866 pub fn character(t: char) -> 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::character(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700869 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800870 Literal::Fallback(fallback::Literal::character(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700871 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700872 }
873
874 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700875 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800876 Literal::Compiler(proc_macro::Literal::byte_string(bytes))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700877 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800878 Literal::Fallback(fallback::Literal::byte_string(bytes))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700879 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700880 }
881
Alex Crichtonb2c94622018-04-04 07:36:41 -0700882 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700883 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800884 Literal::Compiler(lit) => Span::Compiler(lit.span()),
885 Literal::Fallback(lit) => Span::Fallback(lit.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700886 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700887 }
888
Alex Crichtonb2c94622018-04-04 07:36:41 -0700889 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700890 match (self, span) {
David Tolnay9204bac2019-01-19 19:26:17 -0800891 (Literal::Compiler(lit), Span::Compiler(s)) => lit.set_span(s),
892 (Literal::Fallback(lit), Span::Fallback(s)) => lit.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700893 _ => mismatch(),
894 }
895 }
896
897 fn unwrap_nightly(self) -> proc_macro::Literal {
898 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800899 Literal::Compiler(s) => s,
900 Literal::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700901 }
902 }
903}
904
David Tolnayaef075b2019-01-16 16:29:18 -0800905impl From<fallback::Literal> for Literal {
906 fn from(s: fallback::Literal) -> Literal {
David Tolnay9204bac2019-01-19 19:26:17 -0800907 Literal::Fallback(s)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700908 }
909}
910
911impl fmt::Display for Literal {
912 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700913 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800914 Literal::Compiler(t) => t.fmt(f),
915 Literal::Fallback(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700916 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700917 }
918}
919
920impl fmt::Debug for Literal {
921 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700922 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800923 Literal::Compiler(t) => t.fmt(f),
924 Literal::Fallback(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700925 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700926 }
927}