blob: 04cf3066c67ee3919e428c376c59ca14fb8a73ce [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 Tolnay0f884b12019-07-19 11:59:20 -07008use crate::{fallback, Delimiter, Punct, Spacing, TokenTree};
Alex Crichtoncbec8ec2017-06-02 13:19:33 -07009
10#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -070011pub enum TokenStream {
David Tolnay9204bac2019-01-19 19:26:17 -080012 Compiler(proc_macro::TokenStream),
13 Fallback(fallback::TokenStream),
Alex Crichton30a4e9e2018-04-27 17:02:19 -070014}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070015
Alex Crichton30a4e9e2018-04-27 17:02:19 -070016pub enum LexError {
David Tolnay9204bac2019-01-19 19:26:17 -080017 Compiler(proc_macro::LexError),
18 Fallback(fallback::LexError),
Alex Crichton30a4e9e2018-04-27 17:02:19 -070019}
20
21fn nightly_works() -> bool {
22 use std::sync::atomic::*;
David Tolnay2ed15d52018-09-01 08:46:12 -070023 use std::sync::Once;
24
David Tolnay35d24c32019-01-31 15:32:26 -080025 #[allow(deprecated)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -070026 static WORKS: AtomicUsize = ATOMIC_USIZE_INIT;
David Tolnay2ed15d52018-09-01 08:46:12 -070027 static INIT: Once = Once::new();
Alex Crichton30a4e9e2018-04-27 17:02:19 -070028
29 match WORKS.load(Ordering::SeqCst) {
30 1 => return false,
31 2 => return true,
32 _ => {}
33 }
David Tolnay2ed15d52018-09-01 08:46:12 -070034
35 // Swap in a null panic hook to avoid printing "thread panicked" to stderr,
36 // then use catch_unwind to determine whether the compiler's proc_macro is
37 // working. When proc-macro2 is used from outside of a procedural macro all
38 // of the proc_macro crate's APIs currently panic.
39 //
40 // The Once is to prevent the possibility of this ordering:
41 //
42 // thread 1 calls take_hook, gets the user's original hook
43 // thread 1 calls set_hook with the null hook
44 // thread 2 calls take_hook, thinks null hook is the original hook
45 // thread 2 calls set_hook with the null hook
46 // thread 1 calls set_hook with the actual original hook
47 // thread 2 calls set_hook with what it thinks is the original hook
48 //
49 // in which the user's hook has been lost.
50 //
51 // There is still a race condition where a panic in a different thread can
52 // happen during the interval that the user's original panic hook is
53 // unregistered such that their hook is incorrectly not called. This is
54 // sufficiently unlikely and less bad than printing panic messages to stderr
55 // on correct use of this crate. Maybe there is a libstd feature request
56 // here. For now, if a user needs to guarantee that this failure mode does
57 // not occur, they need to call e.g. `proc_macro2::Span::call_site()` from
58 // the main thread before launching any other threads.
59 INIT.call_once(|| {
David Tolnayeac2b752019-07-19 11:55:12 -070060 type PanicHook = dyn Fn(&PanicInfo) + Sync + Send + 'static;
David Tolnay78ef7732018-09-01 19:20:23 -070061
62 let null_hook: Box<PanicHook> = Box::new(|_panic_info| { /* ignore */ });
63 let sanity_check = &*null_hook as *const PanicHook;
David Tolnay2ed15d52018-09-01 08:46:12 -070064 let original_hook = panic::take_hook();
David Tolnay78ef7732018-09-01 19:20:23 -070065 panic::set_hook(null_hook);
66
David Tolnay2ed15d52018-09-01 08:46:12 -070067 let works = panic::catch_unwind(|| proc_macro::Span::call_site()).is_ok();
68 WORKS.store(works as usize + 1, Ordering::SeqCst);
David Tolnay78ef7732018-09-01 19:20:23 -070069
70 let hopefully_null_hook = panic::take_hook();
David Tolnay2ed15d52018-09-01 08:46:12 -070071 panic::set_hook(original_hook);
David Tolnay78ef7732018-09-01 19:20:23 -070072 if sanity_check != &*hopefully_null_hook {
73 panic!("observed race condition in proc_macro2::nightly_works");
74 }
David Tolnay2ed15d52018-09-01 08:46:12 -070075 });
76 nightly_works()
Alex Crichton30a4e9e2018-04-27 17:02:19 -070077}
78
79fn mismatch() -> ! {
80 panic!("stable/nightly mismatch")
81}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070082
83impl TokenStream {
David Tolnayc3bb4592018-05-28 20:09:44 -070084 pub fn new() -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070085 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -080086 TokenStream::Compiler(proc_macro::TokenStream::new())
Alex Crichton30a4e9e2018-04-27 17:02:19 -070087 } else {
David Tolnay9204bac2019-01-19 19:26:17 -080088 TokenStream::Fallback(fallback::TokenStream::new())
Alex Crichton30a4e9e2018-04-27 17:02:19 -070089 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070090 }
91
92 pub fn is_empty(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070093 match self {
David Tolnay9204bac2019-01-19 19:26:17 -080094 TokenStream::Compiler(tts) => tts.is_empty(),
95 TokenStream::Fallback(tts) => tts.is_empty(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -070096 }
97 }
98
99 fn unwrap_nightly(self) -> proc_macro::TokenStream {
100 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800101 TokenStream::Compiler(s) => s,
102 TokenStream::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700103 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700104 }
David Tolnay5c58c532018-08-13 11:33:51 -0700105
David Tolnayaef075b2019-01-16 16:29:18 -0800106 fn unwrap_stable(self) -> fallback::TokenStream {
David Tolnay5c58c532018-08-13 11:33:51 -0700107 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800108 TokenStream::Compiler(_) => mismatch(),
109 TokenStream::Fallback(s) => s,
David Tolnay5c58c532018-08-13 11:33:51 -0700110 }
111 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700112}
113
114impl FromStr for TokenStream {
115 type Err = LexError;
116
117 fn from_str(src: &str) -> Result<TokenStream, LexError> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700118 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800119 Ok(TokenStream::Compiler(src.parse()?))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700120 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800121 Ok(TokenStream::Fallback(src.parse()?))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700122 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700123 }
124}
125
126impl fmt::Display for TokenStream {
127 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700128 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800129 TokenStream::Compiler(tts) => tts.fmt(f),
130 TokenStream::Fallback(tts) => tts.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700131 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700132 }
133}
134
135impl From<proc_macro::TokenStream> for TokenStream {
136 fn from(inner: proc_macro::TokenStream) -> TokenStream {
David Tolnay9204bac2019-01-19 19:26:17 -0800137 TokenStream::Compiler(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700138 }
139}
140
141impl From<TokenStream> for proc_macro::TokenStream {
142 fn from(inner: TokenStream) -> proc_macro::TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700143 match inner {
David Tolnay9204bac2019-01-19 19:26:17 -0800144 TokenStream::Compiler(inner) => inner,
145 TokenStream::Fallback(inner) => inner.to_string().parse().unwrap(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700146 }
147 }
148}
149
David Tolnayaef075b2019-01-16 16:29:18 -0800150impl From<fallback::TokenStream> for TokenStream {
151 fn from(inner: fallback::TokenStream) -> TokenStream {
David Tolnay9204bac2019-01-19 19:26:17 -0800152 TokenStream::Fallback(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700153 }
154}
155
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700156impl From<TokenTree> for TokenStream {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700157 fn from(token: TokenTree) -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700158 if !nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800159 return TokenStream::Fallback(token.into());
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700160 }
Alex Crichton9cd80a62018-04-05 17:46:58 -0700161 let tt: proc_macro::TokenTree = match token {
David Tolnayf14813f2018-09-08 17:14:07 -0700162 TokenTree::Group(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichtonf3888432018-05-16 09:11:05 -0700163 TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700164 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700165 Spacing::Joint => proc_macro::Spacing::Joint,
166 Spacing::Alone => proc_macro::Spacing::Alone,
167 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700168 let mut op = proc_macro::Punct::new(tt.as_char(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700169 op.set_span(tt.span().inner.unwrap_nightly());
Alex Crichton9cd80a62018-04-05 17:46:58 -0700170 op.into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700171 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700172 TokenTree::Ident(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700173 TokenTree::Literal(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700174 };
David Tolnay9204bac2019-01-19 19:26:17 -0800175 TokenStream::Compiler(tt.into())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700176 }
177}
178
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700179impl iter::FromIterator<TokenTree> for TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700180 fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self {
181 if nightly_works() {
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700182 let trees = trees
183 .into_iter()
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700184 .map(TokenStream::from)
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700185 .flat_map(|t| match t {
David Tolnay9204bac2019-01-19 19:26:17 -0800186 TokenStream::Compiler(s) => s,
187 TokenStream::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700188 });
David Tolnay9204bac2019-01-19 19:26:17 -0800189 TokenStream::Compiler(trees.collect())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700190 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800191 TokenStream::Fallback(trees.into_iter().collect())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700192 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700193 }
194}
195
Alex Crichton53b00672018-09-06 17:16:10 -0700196impl iter::FromIterator<TokenStream> for TokenStream {
197 fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self {
198 let mut streams = streams.into_iter();
199 match streams.next() {
David Tolnay9204bac2019-01-19 19:26:17 -0800200 Some(TokenStream::Compiler(mut first)) => {
David Tolnay4453fcc2019-01-16 12:15:01 -0800201 first.extend(streams.map(|s| match s {
David Tolnay9204bac2019-01-19 19:26:17 -0800202 TokenStream::Compiler(s) => s,
203 TokenStream::Fallback(_) => mismatch(),
Alex Crichton53b00672018-09-06 17:16:10 -0700204 }));
David Tolnay9204bac2019-01-19 19:26:17 -0800205 TokenStream::Compiler(first)
Alex Crichton53b00672018-09-06 17:16:10 -0700206 }
David Tolnay9204bac2019-01-19 19:26:17 -0800207 Some(TokenStream::Fallback(mut first)) => {
David Tolnay4453fcc2019-01-16 12:15:01 -0800208 first.extend(streams.map(|s| match s {
David Tolnay9204bac2019-01-19 19:26:17 -0800209 TokenStream::Fallback(s) => s,
210 TokenStream::Compiler(_) => mismatch(),
Alex Crichton53b00672018-09-06 17:16:10 -0700211 }));
David Tolnay9204bac2019-01-19 19:26:17 -0800212 TokenStream::Fallback(first)
Alex Crichton53b00672018-09-06 17:16:10 -0700213 }
214 None => TokenStream::new(),
Alex Crichton53b00672018-09-06 17:16:10 -0700215 }
216 }
217}
218
Alex Crichtonf3888432018-05-16 09:11:05 -0700219impl Extend<TokenTree> for TokenStream {
220 fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
221 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800222 TokenStream::Compiler(tts) => {
David Tolnay8418d422019-07-19 11:48:00 -0700223 tts.extend(
224 streams
225 .into_iter()
226 .map(|t| TokenStream::from(t).unwrap_nightly()),
227 );
Alex Crichtonf3888432018-05-16 09:11:05 -0700228 }
David Tolnay9204bac2019-01-19 19:26:17 -0800229 TokenStream::Fallback(tts) => tts.extend(streams),
Alex Crichtonf3888432018-05-16 09:11:05 -0700230 }
231 }
232}
233
David Tolnay5c58c532018-08-13 11:33:51 -0700234impl Extend<TokenStream> for TokenStream {
235 fn extend<I: IntoIterator<Item = TokenStream>>(&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(streams.into_iter().map(|stream| stream.unwrap_nightly()));
241 }
242 #[cfg(slow_extend)]
243 {
244 *tts = tts
245 .clone()
246 .into_iter()
David Tolnay4453fcc2019-01-16 12:15:01 -0800247 .chain(streams.into_iter().flat_map(|t| match t {
David Tolnay9204bac2019-01-19 19:26:17 -0800248 TokenStream::Compiler(tts) => tts.into_iter(),
David Tolnay4453fcc2019-01-16 12:15:01 -0800249 _ => mismatch(),
250 }))
251 .collect();
David Tolnaye839e4f2018-09-06 09:36:43 -0700252 }
David Tolnay5c58c532018-08-13 11:33:51 -0700253 }
David Tolnay9204bac2019-01-19 19:26:17 -0800254 TokenStream::Fallback(tts) => {
David Tolnay5c58c532018-08-13 11:33:51 -0700255 tts.extend(streams.into_iter().map(|stream| stream.unwrap_stable()))
256 }
257 }
258 }
259}
260
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700261impl fmt::Debug for TokenStream {
262 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700263 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800264 TokenStream::Compiler(tts) => tts.fmt(f),
265 TokenStream::Fallback(tts) => tts.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700266 }
267 }
268}
269
270impl From<proc_macro::LexError> for LexError {
271 fn from(e: proc_macro::LexError) -> LexError {
David Tolnay9204bac2019-01-19 19:26:17 -0800272 LexError::Compiler(e)
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700273 }
274}
275
David Tolnayaef075b2019-01-16 16:29:18 -0800276impl From<fallback::LexError> for LexError {
277 fn from(e: fallback::LexError) -> LexError {
David Tolnay9204bac2019-01-19 19:26:17 -0800278 LexError::Fallback(e)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700279 }
280}
281
282impl fmt::Debug for LexError {
283 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700284 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800285 LexError::Compiler(e) => e.fmt(f),
286 LexError::Fallback(e) => e.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700287 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700288 }
289}
290
Isaac van Bakelf6754d32019-05-08 20:21:06 +0100291#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700292pub enum TokenTreeIter {
David Tolnay9204bac2019-01-19 19:26:17 -0800293 Compiler(proc_macro::token_stream::IntoIter),
294 Fallback(fallback::TokenTreeIter),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700295}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700296
297impl IntoIterator for TokenStream {
298 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700299 type IntoIter = TokenTreeIter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700300
Alex Crichton1a7f7622017-07-05 17:47:15 -0700301 fn into_iter(self) -> TokenTreeIter {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700302 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800303 TokenStream::Compiler(tts) => TokenTreeIter::Compiler(tts.into_iter()),
304 TokenStream::Fallback(tts) => TokenTreeIter::Fallback(tts.into_iter()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700305 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700306 }
307}
308
Alex Crichton1a7f7622017-07-05 17:47:15 -0700309impl Iterator for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700310 type Item = TokenTree;
311
312 fn next(&mut self) -> Option<TokenTree> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700313 let token = match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800314 TokenTreeIter::Compiler(iter) => iter.next()?,
315 TokenTreeIter::Fallback(iter) => return iter.next(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700316 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700317 Some(match token {
David Tolnay0f884b12019-07-19 11:59:20 -0700318 proc_macro::TokenTree::Group(tt) => crate::Group::_new(Group::Compiler(tt)).into(),
Alex Crichtonf3888432018-05-16 09:11:05 -0700319 proc_macro::TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700320 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700321 proc_macro::Spacing::Joint => Spacing::Joint,
322 proc_macro::Spacing::Alone => Spacing::Alone,
323 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700324 let mut o = Punct::new(tt.as_char(), spacing);
David Tolnay0f884b12019-07-19 11:59:20 -0700325 o.set_span(crate::Span::_new(Span::Compiler(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700326 o.into()
327 }
David Tolnay0f884b12019-07-19 11:59:20 -0700328 proc_macro::TokenTree::Ident(s) => crate::Ident::_new(Ident::Compiler(s)).into(),
329 proc_macro::TokenTree::Literal(l) => crate::Literal::_new(Literal::Compiler(l)).into(),
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700330 })
331 }
332
333 fn size_hint(&self) -> (usize, Option<usize>) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700334 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800335 TokenTreeIter::Compiler(tts) => tts.size_hint(),
336 TokenTreeIter::Fallback(tts) => tts.size_hint(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700337 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700338 }
339}
340
Alex Crichton1a7f7622017-07-05 17:47:15 -0700341impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700342 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700343 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700344 }
345}
346
Nika Layzellb35a9a32017-12-30 14:34:35 -0500347#[derive(Clone, PartialEq, Eq)]
Alex Crichtonce0904d2018-08-27 17:29:49 -0700348#[cfg(super_unstable)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700349pub enum SourceFile {
David Tolnay9204bac2019-01-19 19:26:17 -0800350 Compiler(proc_macro::SourceFile),
351 Fallback(fallback::SourceFile),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700352}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500353
Alex Crichtonce0904d2018-08-27 17:29:49 -0700354#[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500355impl SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700356 fn nightly(sf: proc_macro::SourceFile) -> Self {
David Tolnay9204bac2019-01-19 19:26:17 -0800357 SourceFile::Compiler(sf)
Nika Layzellb35a9a32017-12-30 14:34:35 -0500358 }
359
Nika Layzellf8d5f212017-12-11 14:07:02 -0500360 /// Get the path to this source file as a string.
David Tolnay9cd3b4c2018-11-11 16:47:32 -0800361 pub fn path(&self) -> PathBuf {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700362 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800363 SourceFile::Compiler(a) => a.path(),
364 SourceFile::Fallback(a) => a.path(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700365 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500366 }
367
368 pub fn is_real(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700369 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800370 SourceFile::Compiler(a) => a.is_real(),
371 SourceFile::Fallback(a) => a.is_real(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700372 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500373 }
374}
375
Alex Crichtonce0904d2018-08-27 17:29:49 -0700376#[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500377impl fmt::Debug for SourceFile {
378 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700379 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800380 SourceFile::Compiler(a) => a.fmt(f),
381 SourceFile::Fallback(a) => a.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700382 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500383 }
384}
385
David Tolnay3b1f7d22019-01-28 12:22:11 -0800386#[cfg(any(super_unstable, feature = "span-locations"))]
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500387pub struct LineColumn {
388 pub line: usize,
389 pub column: usize,
390}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500391
Alex Crichton9cd80a62018-04-05 17:46:58 -0700392#[derive(Copy, Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700393pub enum Span {
David Tolnay9204bac2019-01-19 19:26:17 -0800394 Compiler(proc_macro::Span),
395 Fallback(fallback::Span),
Sergio Benitez13805082018-01-04 01:25:45 -0800396}
397
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700398impl Span {
399 pub fn call_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700400 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800401 Span::Compiler(proc_macro::Span::call_site())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700402 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800403 Span::Fallback(fallback::Span::call_site())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700404 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700405 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700406
Alex Crichtonce0904d2018-08-27 17:29:49 -0700407 #[cfg(super_unstable)]
Alex Crichtone6085b72017-11-21 07:24:25 -0800408 pub fn def_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700409 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800410 Span::Compiler(proc_macro::Span::def_site())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700411 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800412 Span::Fallback(fallback::Span::def_site())
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700413 }
Alex Crichton998f6422017-11-19 08:06:27 -0800414 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500415
Alex Crichtonce0904d2018-08-27 17:29:49 -0700416 #[cfg(super_unstable)]
David Tolnay4e8e3972018-01-05 18:10:22 -0800417 pub fn resolved_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700418 match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800419 (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.resolved_at(b)),
420 (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.resolved_at(b)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700421 _ => mismatch(),
422 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800423 }
424
Alex Crichtonce0904d2018-08-27 17:29:49 -0700425 #[cfg(super_unstable)]
David Tolnay4e8e3972018-01-05 18:10:22 -0800426 pub fn located_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700427 match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800428 (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.located_at(b)),
429 (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.located_at(b)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700430 _ => mismatch(),
431 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800432 }
433
David Tolnay40bbb1c2019-01-19 19:43:55 -0800434 pub fn unwrap(self) -> proc_macro::Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700435 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800436 Span::Compiler(s) => s,
437 Span::Fallback(_) => panic!("proc_macro::Span is only available in procedural macros"),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700438 }
David Tolnay16a17202017-12-31 10:47:24 -0500439 }
440
Alex Crichtonce0904d2018-08-27 17:29:49 -0700441 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500442 pub fn source_file(&self) -> SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700443 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800444 Span::Compiler(s) => SourceFile::nightly(s.source_file()),
445 Span::Fallback(s) => SourceFile::Fallback(s.source_file()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700446 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500447 }
448
David Tolnay3b1f7d22019-01-28 12:22:11 -0800449 #[cfg(any(super_unstable, feature = "span-locations"))]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500450 pub fn start(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700451 match self {
David Tolnayb455dd72019-04-28 13:50:51 -0700452 #[cfg(proc_macro_span)]
David Tolnay9204bac2019-01-19 19:26:17 -0800453 Span::Compiler(s) => {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700454 let proc_macro::LineColumn { line, column } = s.start();
455 LineColumn { line, column }
456 }
David Tolnayb455dd72019-04-28 13:50:51 -0700457 #[cfg(not(proc_macro_span))]
David Tolnay3b1f7d22019-01-28 12:22:11 -0800458 Span::Compiler(_) => LineColumn { line: 0, column: 0 },
David Tolnay9204bac2019-01-19 19:26:17 -0800459 Span::Fallback(s) => {
David Tolnayaef075b2019-01-16 16:29:18 -0800460 let fallback::LineColumn { line, column } = s.start();
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700461 LineColumn { line, column }
462 }
463 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500464 }
465
David Tolnay3b1f7d22019-01-28 12:22:11 -0800466 #[cfg(any(super_unstable, feature = "span-locations"))]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500467 pub fn end(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700468 match self {
David Tolnayb455dd72019-04-28 13:50:51 -0700469 #[cfg(proc_macro_span)]
David Tolnay9204bac2019-01-19 19:26:17 -0800470 Span::Compiler(s) => {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700471 let proc_macro::LineColumn { line, column } = s.end();
472 LineColumn { line, column }
473 }
David Tolnayb455dd72019-04-28 13:50:51 -0700474 #[cfg(not(proc_macro_span))]
David Tolnay3b1f7d22019-01-28 12:22:11 -0800475 Span::Compiler(_) => LineColumn { line: 0, column: 0 },
David Tolnay9204bac2019-01-19 19:26:17 -0800476 Span::Fallback(s) => {
David Tolnayaef075b2019-01-16 16:29:18 -0800477 let fallback::LineColumn { line, column } = s.end();
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700478 LineColumn { line, column }
479 }
480 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500481 }
482
Alex Crichtonce0904d2018-08-27 17:29:49 -0700483 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500484 pub fn join(&self, other: Span) -> Option<Span> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700485 let ret = match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800486 (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.join(b)?),
487 (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.join(b)?),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700488 _ => return None,
489 };
490 Some(ret)
Nika Layzellf8d5f212017-12-11 14:07:02 -0500491 }
Alex Crichtone24f7342018-04-05 17:58:11 -0700492
Alex Crichtonce0904d2018-08-27 17:29:49 -0700493 #[cfg(super_unstable)]
Alex Crichtone24f7342018-04-05 17:58:11 -0700494 pub fn eq(&self, other: &Span) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700495 match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800496 (Span::Compiler(a), Span::Compiler(b)) => a.eq(b),
497 (Span::Fallback(a), Span::Fallback(b)) => a.eq(b),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700498 _ => false,
499 }
500 }
501
502 fn unwrap_nightly(self) -> proc_macro::Span {
503 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800504 Span::Compiler(s) => s,
505 Span::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700506 }
507 }
508}
509
David Tolnay0f884b12019-07-19 11:59:20 -0700510impl From<proc_macro::Span> for crate::Span {
511 fn from(proc_span: proc_macro::Span) -> crate::Span {
512 crate::Span::_new(Span::Compiler(proc_span))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700513 }
514}
515
David Tolnayaef075b2019-01-16 16:29:18 -0800516impl From<fallback::Span> for Span {
517 fn from(inner: fallback::Span) -> Span {
David Tolnay9204bac2019-01-19 19:26:17 -0800518 Span::Fallback(inner)
Alex Crichtone24f7342018-04-05 17:58:11 -0700519 }
Alex Crichton998f6422017-11-19 08:06:27 -0800520}
521
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700522impl fmt::Debug for Span {
523 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700524 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800525 Span::Compiler(s) => s.fmt(f),
526 Span::Fallback(s) => s.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700527 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700528 }
529}
530
David Tolnayfd8cdc82019-01-19 19:23:59 -0800531pub fn debug_span_field_if_nontrivial(debug: &mut fmt::DebugStruct, span: Span) {
532 match span {
533 Span::Compiler(s) => {
534 debug.field("span", &s);
535 }
536 Span::Fallback(s) => fallback::debug_span_field_if_nontrivial(debug, s),
537 }
538}
539
Alex Crichtonf3888432018-05-16 09:11:05 -0700540#[derive(Clone)]
David Tolnayf14813f2018-09-08 17:14:07 -0700541pub enum Group {
David Tolnay9204bac2019-01-19 19:26:17 -0800542 Compiler(proc_macro::Group),
543 Fallback(fallback::Group),
David Tolnayf14813f2018-09-08 17:14:07 -0700544}
545
546impl Group {
547 pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
548 match stream {
David Tolnay9204bac2019-01-19 19:26:17 -0800549 TokenStream::Compiler(stream) => {
David Tolnayf14813f2018-09-08 17:14:07 -0700550 let delimiter = match delimiter {
551 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
552 Delimiter::Bracket => proc_macro::Delimiter::Bracket,
553 Delimiter::Brace => proc_macro::Delimiter::Brace,
554 Delimiter::None => proc_macro::Delimiter::None,
555 };
David Tolnay9204bac2019-01-19 19:26:17 -0800556 Group::Compiler(proc_macro::Group::new(delimiter, stream))
David Tolnayf14813f2018-09-08 17:14:07 -0700557 }
David Tolnay471a7ed2019-01-19 21:00:11 -0800558 TokenStream::Fallback(stream) => {
559 Group::Fallback(fallback::Group::new(delimiter, stream))
560 }
David Tolnayf14813f2018-09-08 17:14:07 -0700561 }
562 }
563
564 pub fn delimiter(&self) -> Delimiter {
565 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800566 Group::Compiler(g) => match g.delimiter() {
David Tolnayf14813f2018-09-08 17:14:07 -0700567 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
568 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
569 proc_macro::Delimiter::Brace => Delimiter::Brace,
570 proc_macro::Delimiter::None => Delimiter::None,
David Tolnay4453fcc2019-01-16 12:15:01 -0800571 },
David Tolnay9204bac2019-01-19 19:26:17 -0800572 Group::Fallback(g) => g.delimiter(),
David Tolnayf14813f2018-09-08 17:14:07 -0700573 }
574 }
575
576 pub fn stream(&self) -> TokenStream {
577 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800578 Group::Compiler(g) => TokenStream::Compiler(g.stream()),
579 Group::Fallback(g) => TokenStream::Fallback(g.stream()),
David Tolnayf14813f2018-09-08 17:14:07 -0700580 }
581 }
582
583 pub fn span(&self) -> Span {
584 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800585 Group::Compiler(g) => Span::Compiler(g.span()),
586 Group::Fallback(g) => Span::Fallback(g.span()),
David Tolnayf14813f2018-09-08 17:14:07 -0700587 }
588 }
589
590 #[cfg(super_unstable)]
591 pub fn span_open(&self) -> Span {
592 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800593 Group::Compiler(g) => Span::Compiler(g.span_open()),
594 Group::Fallback(g) => Span::Fallback(g.span_open()),
David Tolnayf14813f2018-09-08 17:14:07 -0700595 }
596 }
597
598 #[cfg(super_unstable)]
599 pub fn span_close(&self) -> Span {
600 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800601 Group::Compiler(g) => Span::Compiler(g.span_close()),
602 Group::Fallback(g) => Span::Fallback(g.span_close()),
David Tolnayf14813f2018-09-08 17:14:07 -0700603 }
604 }
605
606 pub fn set_span(&mut self, span: Span) {
607 match (self, span) {
David Tolnay9204bac2019-01-19 19:26:17 -0800608 (Group::Compiler(g), Span::Compiler(s)) => g.set_span(s),
609 (Group::Fallback(g), Span::Fallback(s)) => g.set_span(s),
David Tolnayf14813f2018-09-08 17:14:07 -0700610 _ => mismatch(),
611 }
612 }
613
614 fn unwrap_nightly(self) -> proc_macro::Group {
615 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800616 Group::Compiler(g) => g,
617 Group::Fallback(_) => mismatch(),
David Tolnayf14813f2018-09-08 17:14:07 -0700618 }
619 }
620}
621
David Tolnayaef075b2019-01-16 16:29:18 -0800622impl From<fallback::Group> for Group {
623 fn from(g: fallback::Group) -> Self {
David Tolnay9204bac2019-01-19 19:26:17 -0800624 Group::Fallback(g)
David Tolnayf14813f2018-09-08 17:14:07 -0700625 }
626}
627
628impl fmt::Display for Group {
629 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
630 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800631 Group::Compiler(group) => group.fmt(formatter),
632 Group::Fallback(group) => group.fmt(formatter),
David Tolnayf14813f2018-09-08 17:14:07 -0700633 }
634 }
635}
636
637impl fmt::Debug for Group {
638 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
639 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800640 Group::Compiler(group) => group.fmt(formatter),
641 Group::Fallback(group) => group.fmt(formatter),
David Tolnayf14813f2018-09-08 17:14:07 -0700642 }
643 }
644}
645
646#[derive(Clone)]
Alex Crichtonf3888432018-05-16 09:11:05 -0700647pub enum Ident {
David Tolnay9204bac2019-01-19 19:26:17 -0800648 Compiler(proc_macro::Ident),
649 Fallback(fallback::Ident),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700650}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700651
Alex Crichtonf3888432018-05-16 09:11:05 -0700652impl Ident {
653 pub fn new(string: &str, span: Span) -> Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700654 match span {
David Tolnay9204bac2019-01-19 19:26:17 -0800655 Span::Compiler(s) => Ident::Compiler(proc_macro::Ident::new(string, s)),
656 Span::Fallback(s) => Ident::Fallback(fallback::Ident::new(string, s)),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700657 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700658 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700659
Alex Crichtonf3888432018-05-16 09:11:05 -0700660 pub fn new_raw(string: &str, span: Span) -> Ident {
661 match span {
David Tolnay9204bac2019-01-19 19:26:17 -0800662 Span::Compiler(s) => {
Alex Crichtonce0904d2018-08-27 17:29:49 -0700663 let p: proc_macro::TokenStream = string.parse().unwrap();
664 let ident = match p.into_iter().next() {
665 Some(proc_macro::TokenTree::Ident(mut i)) => {
666 i.set_span(s);
667 i
668 }
669 _ => panic!(),
670 };
David Tolnay9204bac2019-01-19 19:26:17 -0800671 Ident::Compiler(ident)
Alex Crichtonce0904d2018-08-27 17:29:49 -0700672 }
David Tolnay9204bac2019-01-19 19:26:17 -0800673 Span::Fallback(s) => Ident::Fallback(fallback::Ident::new_raw(string, s)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700674 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700675 }
676
677 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700678 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800679 Ident::Compiler(t) => Span::Compiler(t.span()),
680 Ident::Fallback(t) => Span::Fallback(t.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700681 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700682 }
683
684 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700685 match (self, span) {
David Tolnay9204bac2019-01-19 19:26:17 -0800686 (Ident::Compiler(t), Span::Compiler(s)) => t.set_span(s),
687 (Ident::Fallback(t), Span::Fallback(s)) => t.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700688 _ => mismatch(),
689 }
690 }
691
Alex Crichtonf3888432018-05-16 09:11:05 -0700692 fn unwrap_nightly(self) -> proc_macro::Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700693 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800694 Ident::Compiler(s) => s,
695 Ident::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700696 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700697 }
698}
699
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700700impl PartialEq for Ident {
701 fn eq(&self, other: &Ident) -> bool {
702 match (self, other) {
David Tolnay9204bac2019-01-19 19:26:17 -0800703 (Ident::Compiler(t), Ident::Compiler(o)) => t.to_string() == o.to_string(),
704 (Ident::Fallback(t), Ident::Fallback(o)) => t == o,
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700705 _ => mismatch(),
706 }
707 }
708}
709
710impl<T> PartialEq<T> for Ident
711where
712 T: ?Sized + AsRef<str>,
713{
714 fn eq(&self, other: &T) -> bool {
715 let other = other.as_ref();
716 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800717 Ident::Compiler(t) => t.to_string() == other,
718 Ident::Fallback(t) => t == other,
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700719 }
720 }
721}
722
Alex Crichtonf3888432018-05-16 09:11:05 -0700723impl fmt::Display for Ident {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700724 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700725 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800726 Ident::Compiler(t) => t.fmt(f),
727 Ident::Fallback(t) => t.fmt(f),
Alex Crichtonf3888432018-05-16 09:11:05 -0700728 }
729 }
730}
731
732impl fmt::Debug for Ident {
733 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
734 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800735 Ident::Compiler(t) => t.fmt(f),
736 Ident::Fallback(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700737 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700738 }
739}
740
741#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700742pub enum Literal {
David Tolnay9204bac2019-01-19 19:26:17 -0800743 Compiler(proc_macro::Literal),
744 Fallback(fallback::Literal),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700745}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700746
Alex Crichtona914a612018-04-04 07:48:44 -0700747macro_rules! suffixed_numbers {
748 ($($name:ident => $kind:ident,)*) => ($(
749 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700750 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800751 Literal::Compiler(proc_macro::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700752 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800753 Literal::Fallback(fallback::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700754 }
Alex Crichtona914a612018-04-04 07:48:44 -0700755 }
756 )*)
757}
758
759macro_rules! unsuffixed_integers {
760 ($($name:ident => $kind:ident,)*) => ($(
761 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700762 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800763 Literal::Compiler(proc_macro::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700764 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800765 Literal::Fallback(fallback::Literal::$name(n))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700766 }
Alex Crichtona914a612018-04-04 07:48:44 -0700767 }
768 )*)
769}
770
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700771impl Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700772 suffixed_numbers! {
773 u8_suffixed => u8,
774 u16_suffixed => u16,
775 u32_suffixed => u32,
776 u64_suffixed => u64,
David Tolnay1596a8c2019-07-19 11:45:26 -0700777 u128_suffixed => u128,
Alex Crichtona914a612018-04-04 07:48:44 -0700778 usize_suffixed => usize,
779 i8_suffixed => i8,
780 i16_suffixed => i16,
781 i32_suffixed => i32,
782 i64_suffixed => i64,
David Tolnay1596a8c2019-07-19 11:45:26 -0700783 i128_suffixed => i128,
Alex Crichtona914a612018-04-04 07:48:44 -0700784 isize_suffixed => isize,
785
786 f32_suffixed => f32,
787 f64_suffixed => f64,
788 }
789
790 unsuffixed_integers! {
791 u8_unsuffixed => u8,
792 u16_unsuffixed => u16,
793 u32_unsuffixed => u32,
794 u64_unsuffixed => u64,
David Tolnay1596a8c2019-07-19 11:45:26 -0700795 u128_unsuffixed => u128,
Alex Crichtona914a612018-04-04 07:48:44 -0700796 usize_unsuffixed => usize,
797 i8_unsuffixed => i8,
798 i16_unsuffixed => i16,
799 i32_unsuffixed => i32,
800 i64_unsuffixed => i64,
Alex Crichton69385662018-11-08 06:30:04 -0800801 i128_unsuffixed => i128,
David Tolnay1596a8c2019-07-19 11:45:26 -0700802 isize_unsuffixed => isize,
Alex Crichton69385662018-11-08 06:30:04 -0800803 }
804
Alex Crichtona914a612018-04-04 07:48:44 -0700805 pub fn f32_unsuffixed(f: f32) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700806 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800807 Literal::Compiler(proc_macro::Literal::f32_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700808 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800809 Literal::Fallback(fallback::Literal::f32_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700810 }
Alex Crichtona914a612018-04-04 07:48:44 -0700811 }
812
813 pub fn f64_unsuffixed(f: f64) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700814 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800815 Literal::Compiler(proc_macro::Literal::f64_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700816 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800817 Literal::Fallback(fallback::Literal::f64_unsuffixed(f))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700818 }
Alex Crichtona914a612018-04-04 07:48:44 -0700819 }
820
Alex Crichtona914a612018-04-04 07:48:44 -0700821 pub fn string(t: &str) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700822 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800823 Literal::Compiler(proc_macro::Literal::string(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700824 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800825 Literal::Fallback(fallback::Literal::string(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700826 }
Alex Crichtona914a612018-04-04 07:48:44 -0700827 }
828
829 pub fn character(t: char) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700830 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800831 Literal::Compiler(proc_macro::Literal::character(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700832 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800833 Literal::Fallback(fallback::Literal::character(t))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700834 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700835 }
836
837 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700838 if nightly_works() {
David Tolnay9204bac2019-01-19 19:26:17 -0800839 Literal::Compiler(proc_macro::Literal::byte_string(bytes))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700840 } else {
David Tolnay9204bac2019-01-19 19:26:17 -0800841 Literal::Fallback(fallback::Literal::byte_string(bytes))
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700842 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700843 }
844
Alex Crichtonb2c94622018-04-04 07:36:41 -0700845 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700846 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800847 Literal::Compiler(lit) => Span::Compiler(lit.span()),
848 Literal::Fallback(lit) => Span::Fallback(lit.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700849 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700850 }
851
Alex Crichtonb2c94622018-04-04 07:36:41 -0700852 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700853 match (self, span) {
David Tolnay9204bac2019-01-19 19:26:17 -0800854 (Literal::Compiler(lit), Span::Compiler(s)) => lit.set_span(s),
855 (Literal::Fallback(lit), Span::Fallback(s)) => lit.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700856 _ => mismatch(),
857 }
858 }
859
860 fn unwrap_nightly(self) -> proc_macro::Literal {
861 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800862 Literal::Compiler(s) => s,
863 Literal::Fallback(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700864 }
865 }
866}
867
David Tolnayaef075b2019-01-16 16:29:18 -0800868impl From<fallback::Literal> for Literal {
869 fn from(s: fallback::Literal) -> Literal {
David Tolnay9204bac2019-01-19 19:26:17 -0800870 Literal::Fallback(s)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700871 }
872}
873
874impl fmt::Display for Literal {
875 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700876 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800877 Literal::Compiler(t) => t.fmt(f),
878 Literal::Fallback(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700879 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700880 }
881}
882
883impl fmt::Debug for Literal {
884 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700885 match self {
David Tolnay9204bac2019-01-19 19:26:17 -0800886 Literal::Compiler(t) => t.fmt(f),
887 Literal::Fallback(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700888 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700889 }
890}