blob: d8fe6f29ca5e0d2971543d178617a48e3f7471d1 [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;
Alex Crichton30a4e9e2018-04-27 17:02:19 -070011use stable;
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 {
17 Nightly(proc_macro::TokenStream),
18 Stable(stable::TokenStream),
19}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070020
Alex Crichton30a4e9e2018-04-27 17:02:19 -070021pub enum LexError {
22 Nightly(proc_macro::LexError),
23 Stable(stable::LexError),
24}
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 Tolnayc3bb4592018-05-28 20:09:44 -070090 TokenStream::Nightly(proc_macro::TokenStream::new())
Alex Crichton30a4e9e2018-04-27 17:02:19 -070091 } else {
David Tolnayc3bb4592018-05-28 20:09:44 -070092 TokenStream::Stable(stable::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 {
98 TokenStream::Nightly(tts) => tts.is_empty(),
99 TokenStream::Stable(tts) => tts.is_empty(),
100 }
101 }
102
103 fn unwrap_nightly(self) -> proc_macro::TokenStream {
104 match self {
105 TokenStream::Nightly(s) => s,
106 TokenStream::Stable(_) => mismatch(),
107 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700108 }
David Tolnay5c58c532018-08-13 11:33:51 -0700109
110 fn unwrap_stable(self) -> stable::TokenStream {
111 match self {
112 TokenStream::Nightly(_) => mismatch(),
113 TokenStream::Stable(s) => s,
114 }
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() {
123 Ok(TokenStream::Nightly(src.parse()?))
124 } else {
125 Ok(TokenStream::Stable(src.parse()?))
126 }
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 {
133 TokenStream::Nightly(tts) => tts.fmt(f),
134 TokenStream::Stable(tts) => tts.fmt(f),
135 }
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 {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700141 TokenStream::Nightly(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 {
148 TokenStream::Nightly(inner) => inner,
149 TokenStream::Stable(inner) => inner.to_string().parse().unwrap(),
150 }
151 }
152}
153
154impl From<stable::TokenStream> for TokenStream {
155 fn from(inner: stable::TokenStream) -> TokenStream {
156 TokenStream::Stable(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 Tolnay3d9d6ad2018-05-18 10:51:55 -0700163 return TokenStream::Stable(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 };
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700179 TokenStream::Nightly(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 {
190 TokenStream::Nightly(s) => s,
191 TokenStream::Stable(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700192 });
193 TokenStream::Nightly(trees.collect())
194 } else {
195 TokenStream::Stable(trees.into_iter().collect())
196 }
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)]
205 Some(TokenStream::Nightly(first)) => {
David Tolnay4453fcc2019-01-16 12:15:01 -0800206 let stream = iter::once(first)
207 .chain(streams.map(|s| match s {
Alex Crichton53b00672018-09-06 17:16:10 -0700208 TokenStream::Nightly(s) => s,
209 TokenStream::Stable(_) => mismatch(),
David Tolnay4453fcc2019-01-16 12:15:01 -0800210 }))
211 .collect();
Alex Crichton53b00672018-09-06 17:16:10 -0700212 TokenStream::Nightly(stream)
213 }
214 #[cfg(not(slow_extend))]
215 Some(TokenStream::Nightly(mut first)) => {
David Tolnay4453fcc2019-01-16 12:15:01 -0800216 first.extend(streams.map(|s| match s {
217 TokenStream::Nightly(s) => s,
218 TokenStream::Stable(_) => mismatch(),
Alex Crichton53b00672018-09-06 17:16:10 -0700219 }));
220 TokenStream::Nightly(first)
221 }
222 Some(TokenStream::Stable(mut first)) => {
David Tolnay4453fcc2019-01-16 12:15:01 -0800223 first.extend(streams.map(|s| match s {
224 TokenStream::Stable(s) => s,
225 TokenStream::Nightly(_) => mismatch(),
Alex Crichton53b00672018-09-06 17:16:10 -0700226 }));
227 TokenStream::Stable(first)
228 }
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 {
237 TokenStream::Nightly(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 Tolnaye839e4f2018-09-06 09:36:43 -0700253 TokenStream::Nightly(tts) => tts.into_iter(),
254 _ => 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 }
260 TokenStream::Stable(tts) => tts.extend(streams),
261 }
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 {
268 TokenStream::Nightly(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 {
279 TokenStream::Nightly(tts) => tts.into_iter(),
280 _ => mismatch(),
281 }))
282 .collect();
David Tolnaye839e4f2018-09-06 09:36:43 -0700283 }
David Tolnay5c58c532018-08-13 11:33:51 -0700284 }
285 TokenStream::Stable(tts) => {
286 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 {
295 TokenStream::Nightly(tts) => tts.fmt(f),
296 TokenStream::Stable(tts) => tts.fmt(f),
297 }
298 }
299}
300
301impl From<proc_macro::LexError> for LexError {
302 fn from(e: proc_macro::LexError) -> LexError {
303 LexError::Nightly(e)
304 }
305}
306
307impl From<stable::LexError> for LexError {
308 fn from(e: stable::LexError) -> LexError {
309 LexError::Stable(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 {
316 LexError::Nightly(e) => e.fmt(f),
317 LexError::Stable(e) => e.fmt(f),
318 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700319 }
320}
321
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700322pub enum TokenTreeIter {
323 Nightly(proc_macro::token_stream::IntoIter),
324 Stable(stable::TokenTreeIter),
325}
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 {
333 TokenStream::Nightly(tts) => TokenTreeIter::Nightly(tts.into_iter()),
334 TokenStream::Stable(tts) => TokenTreeIter::Stable(tts.into_iter()),
335 }
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 {
344 TokenTreeIter::Nightly(iter) => iter.next()?,
345 TokenTreeIter::Stable(iter) => return iter.next(),
346 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700347 Some(match token {
David Tolnayf14813f2018-09-08 17:14:07 -0700348 proc_macro::TokenTree::Group(tt) => ::Group::_new(Group::Nightly(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);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700355 o.set_span(::Span::_new(Span::Nightly(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700356 o.into()
357 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700358 proc_macro::TokenTree::Ident(s) => ::Ident::_new(Ident::Nightly(s)).into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700359 proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Nightly(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 {
365 TokenTreeIter::Nightly(tts) => tts.size_hint(),
366 TokenTreeIter::Stable(tts) => tts.size_hint(),
367 }
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 Tolnay9cd3b4c2018-11-11 16:47:32 -0800380 Nightly(proc_macro::SourceFile),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700381 Stable(stable::SourceFile),
382}
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 Tolnay9cd3b4c2018-11-11 16:47:32 -0800387 SourceFile::Nightly(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 Tolnay9cd3b4c2018-11-11 16:47:32 -0800393 SourceFile::Nightly(a) => a.path(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700394 SourceFile::Stable(a) => a.path(),
395 }
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 Tolnay9cd3b4c2018-11-11 16:47:32 -0800400 SourceFile::Nightly(a) => a.is_real(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700401 SourceFile::Stable(a) => a.is_real(),
402 }
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 Tolnay9cd3b4c2018-11-11 16:47:32 -0800410 SourceFile::Nightly(a) => a.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700411 SourceFile::Stable(a) => a.fmt(f),
412 }
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 {
423 Nightly(proc_macro::Span),
424 Stable(stable::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() {
430 Span::Nightly(proc_macro::Span::call_site())
431 } else {
432 Span::Stable(stable::Span::call_site())
433 }
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() {
439 Span::Nightly(proc_macro::Span::def_site())
440 } else {
441 Span::Stable(stable::Span::def_site())
442 }
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) {
448 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.resolved_at(b)),
449 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.resolved_at(b)),
450 _ => 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) {
457 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.located_at(b)),
458 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.located_at(b)),
459 _ => 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 {
465 Span::Nightly(s) => s,
David Tolnay4453fcc2019-01-16 12:15:01 -0800466 Span::Stable(_) => 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 {
473 Span::Nightly(s) => SourceFile::nightly(s.source_file()),
474 Span::Stable(s) => SourceFile::Stable(s.source_file()),
475 }
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 {
481 Span::Nightly(s) => {
482 let proc_macro::LineColumn { line, column } = s.start();
483 LineColumn { line, column }
484 }
485 Span::Stable(s) => {
486 let stable::LineColumn { line, column } = s.start();
487 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 {
495 Span::Nightly(s) => {
496 let proc_macro::LineColumn { line, column } = s.end();
497 LineColumn { line, column }
498 }
499 Span::Stable(s) => {
500 let stable::LineColumn { line, column } = s.end();
501 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) {
509 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.join(b)?),
510 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.join(b)?),
511 _ => 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) {
519 (Span::Nightly(a), Span::Nightly(b)) => a.eq(b),
520 (Span::Stable(a), Span::Stable(b)) => a.eq(b),
521 _ => false,
522 }
523 }
524
525 fn unwrap_nightly(self) -> proc_macro::Span {
526 match self {
527 Span::Nightly(s) => s,
528 Span::Stable(_) => mismatch(),
529 }
530 }
531}
532
533impl From<proc_macro::Span> for ::Span {
534 fn from(proc_span: proc_macro::Span) -> ::Span {
535 ::Span::_new(Span::Nightly(proc_span))
536 }
537}
538
539impl From<stable::Span> for Span {
540 fn from(inner: stable::Span) -> Span {
541 Span::Stable(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 {
548 Span::Nightly(s) => s.fmt(f),
549 Span::Stable(s) => s.fmt(f),
550 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700551 }
552}
553
Alex Crichtonf3888432018-05-16 09:11:05 -0700554#[derive(Clone)]
David Tolnayf14813f2018-09-08 17:14:07 -0700555pub enum Group {
556 Nightly(proc_macro::Group),
557 Stable(stable::Group),
558}
559
560impl Group {
561 pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
562 match stream {
563 TokenStream::Nightly(stream) => {
564 let delimiter = match delimiter {
565 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
566 Delimiter::Bracket => proc_macro::Delimiter::Bracket,
567 Delimiter::Brace => proc_macro::Delimiter::Brace,
568 Delimiter::None => proc_macro::Delimiter::None,
569 };
570 Group::Nightly(proc_macro::Group::new(delimiter, stream))
571 }
David Tolnay4453fcc2019-01-16 12:15:01 -0800572 TokenStream::Stable(stream) => Group::Stable(stable::Group::new(delimiter, stream)),
David Tolnayf14813f2018-09-08 17:14:07 -0700573 }
574 }
575
576 pub fn delimiter(&self) -> Delimiter {
577 match self {
578 Group::Nightly(g) => match g.delimiter() {
579 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
580 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
581 proc_macro::Delimiter::Brace => Delimiter::Brace,
582 proc_macro::Delimiter::None => Delimiter::None,
David Tolnay4453fcc2019-01-16 12:15:01 -0800583 },
David Tolnayf14813f2018-09-08 17:14:07 -0700584 Group::Stable(g) => g.delimiter(),
585 }
586 }
587
588 pub fn stream(&self) -> TokenStream {
589 match self {
590 Group::Nightly(g) => TokenStream::Nightly(g.stream()),
591 Group::Stable(g) => TokenStream::Stable(g.stream()),
592 }
593 }
594
595 pub fn span(&self) -> Span {
596 match self {
597 Group::Nightly(g) => Span::Nightly(g.span()),
598 Group::Stable(g) => Span::Stable(g.span()),
599 }
600 }
601
602 #[cfg(super_unstable)]
603 pub fn span_open(&self) -> Span {
604 match self {
605 Group::Nightly(g) => Span::Nightly(g.span_open()),
606 Group::Stable(g) => Span::Stable(g.span_open()),
607 }
608 }
609
610 #[cfg(super_unstable)]
611 pub fn span_close(&self) -> Span {
612 match self {
613 Group::Nightly(g) => Span::Nightly(g.span_close()),
614 Group::Stable(g) => Span::Stable(g.span_close()),
615 }
616 }
617
618 pub fn set_span(&mut self, span: Span) {
619 match (self, span) {
620 (Group::Nightly(g), Span::Nightly(s)) => g.set_span(s),
621 (Group::Stable(g), Span::Stable(s)) => g.set_span(s),
622 _ => mismatch(),
623 }
624 }
625
626 fn unwrap_nightly(self) -> proc_macro::Group {
627 match self {
628 Group::Nightly(g) => g,
629 Group::Stable(_) => mismatch(),
630 }
631 }
632}
633
634impl From<stable::Group> for Group {
635 fn from(g: stable::Group) -> Self {
636 Group::Stable(g)
637 }
638}
639
640impl fmt::Display for Group {
641 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
642 match self {
643 Group::Nightly(group) => group.fmt(formatter),
644 Group::Stable(group) => group.fmt(formatter),
645 }
646 }
647}
648
649impl fmt::Debug for Group {
650 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
651 match self {
652 Group::Nightly(group) => group.fmt(formatter),
653 Group::Stable(group) => group.fmt(formatter),
654 }
655 }
656}
657
658#[derive(Clone)]
Alex Crichtonf3888432018-05-16 09:11:05 -0700659pub enum Ident {
660 Nightly(proc_macro::Ident),
661 Stable(stable::Ident),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700662}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700663
Alex Crichtonf3888432018-05-16 09:11:05 -0700664impl Ident {
665 pub fn new(string: &str, span: Span) -> Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700666 match span {
Alex Crichtonf3888432018-05-16 09:11:05 -0700667 Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new(string, s)),
668 Span::Stable(s) => Ident::Stable(stable::Ident::new(string, s)),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700669 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700670 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700671
Alex Crichtonf3888432018-05-16 09:11:05 -0700672 pub fn new_raw(string: &str, span: Span) -> Ident {
673 match span {
Alex Crichtonce0904d2018-08-27 17:29:49 -0700674 Span::Nightly(s) => {
675 let p: proc_macro::TokenStream = string.parse().unwrap();
676 let ident = match p.into_iter().next() {
677 Some(proc_macro::TokenTree::Ident(mut i)) => {
678 i.set_span(s);
679 i
680 }
681 _ => panic!(),
682 };
683 Ident::Nightly(ident)
684 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700685 Span::Stable(s) => Ident::Stable(stable::Ident::new_raw(string, s)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700686 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700687 }
688
689 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700690 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700691 Ident::Nightly(t) => Span::Nightly(t.span()),
692 Ident::Stable(t) => Span::Stable(t.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700693 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700694 }
695
696 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700697 match (self, span) {
Alex Crichtonf3888432018-05-16 09:11:05 -0700698 (Ident::Nightly(t), Span::Nightly(s)) => t.set_span(s),
699 (Ident::Stable(t), Span::Stable(s)) => t.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700700 _ => mismatch(),
701 }
702 }
703
Alex Crichtonf3888432018-05-16 09:11:05 -0700704 fn unwrap_nightly(self) -> proc_macro::Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700705 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700706 Ident::Nightly(s) => s,
707 Ident::Stable(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700708 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700709 }
710}
711
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700712impl PartialEq for Ident {
713 fn eq(&self, other: &Ident) -> bool {
714 match (self, other) {
715 (Ident::Nightly(t), Ident::Nightly(o)) => t.to_string() == o.to_string(),
716 (Ident::Stable(t), Ident::Stable(o)) => t == o,
717 _ => mismatch(),
718 }
719 }
720}
721
722impl<T> PartialEq<T> for Ident
723where
724 T: ?Sized + AsRef<str>,
725{
726 fn eq(&self, other: &T) -> bool {
727 let other = other.as_ref();
728 match self {
729 Ident::Nightly(t) => t.to_string() == other,
730 Ident::Stable(t) => t == other,
731 }
732 }
733}
734
Alex Crichtonf3888432018-05-16 09:11:05 -0700735impl fmt::Display for Ident {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700736 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700737 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700738 Ident::Nightly(t) => t.fmt(f),
739 Ident::Stable(t) => t.fmt(f),
740 }
741 }
742}
743
744impl fmt::Debug for Ident {
745 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
746 match self {
747 Ident::Nightly(t) => t.fmt(f),
748 Ident::Stable(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700749 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700750 }
751}
752
753#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700754pub enum Literal {
755 Nightly(proc_macro::Literal),
756 Stable(stable::Literal),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700757}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700758
Alex Crichtona914a612018-04-04 07:48:44 -0700759macro_rules! suffixed_numbers {
760 ($($name:ident => $kind:ident,)*) => ($(
761 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700762 if nightly_works() {
763 Literal::Nightly(proc_macro::Literal::$name(n))
764 } else {
765 Literal::Stable(stable::Literal::$name(n))
766 }
Alex Crichtona914a612018-04-04 07:48:44 -0700767 }
768 )*)
769}
770
771macro_rules! unsuffixed_integers {
772 ($($name:ident => $kind:ident,)*) => ($(
773 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700774 if nightly_works() {
775 Literal::Nightly(proc_macro::Literal::$name(n))
776 } else {
777 Literal::Stable(stable::Literal::$name(n))
778 }
Alex Crichtona914a612018-04-04 07:48:44 -0700779 }
780 )*)
781}
782
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700783impl Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700784 suffixed_numbers! {
785 u8_suffixed => u8,
786 u16_suffixed => u16,
787 u32_suffixed => u32,
788 u64_suffixed => u64,
789 usize_suffixed => usize,
790 i8_suffixed => i8,
791 i16_suffixed => i16,
792 i32_suffixed => i32,
793 i64_suffixed => i64,
794 isize_suffixed => isize,
795
796 f32_suffixed => f32,
797 f64_suffixed => f64,
798 }
799
Alex Crichton69385662018-11-08 06:30:04 -0800800 #[cfg(u128)]
801 suffixed_numbers! {
802 i128_suffixed => i128,
803 u128_suffixed => u128,
804 }
805
Alex Crichtona914a612018-04-04 07:48:44 -0700806 unsuffixed_integers! {
807 u8_unsuffixed => u8,
808 u16_unsuffixed => u16,
809 u32_unsuffixed => u32,
810 u64_unsuffixed => u64,
811 usize_unsuffixed => usize,
812 i8_unsuffixed => i8,
813 i16_unsuffixed => i16,
814 i32_unsuffixed => i32,
815 i64_unsuffixed => i64,
816 isize_unsuffixed => isize,
817 }
818
Alex Crichton69385662018-11-08 06:30:04 -0800819 #[cfg(u128)]
820 unsuffixed_integers! {
821 i128_unsuffixed => i128,
822 u128_unsuffixed => u128,
823 }
824
Alex Crichtona914a612018-04-04 07:48:44 -0700825 pub fn f32_unsuffixed(f: f32) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700826 if nightly_works() {
827 Literal::Nightly(proc_macro::Literal::f32_unsuffixed(f))
828 } else {
829 Literal::Stable(stable::Literal::f32_unsuffixed(f))
830 }
Alex Crichtona914a612018-04-04 07:48:44 -0700831 }
832
833 pub fn f64_unsuffixed(f: f64) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700834 if nightly_works() {
835 Literal::Nightly(proc_macro::Literal::f64_unsuffixed(f))
836 } else {
837 Literal::Stable(stable::Literal::f64_unsuffixed(f))
838 }
Alex Crichtona914a612018-04-04 07:48:44 -0700839 }
840
Alex Crichtona914a612018-04-04 07:48:44 -0700841 pub fn string(t: &str) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700842 if nightly_works() {
843 Literal::Nightly(proc_macro::Literal::string(t))
844 } else {
845 Literal::Stable(stable::Literal::string(t))
846 }
Alex Crichtona914a612018-04-04 07:48:44 -0700847 }
848
849 pub fn character(t: char) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700850 if nightly_works() {
851 Literal::Nightly(proc_macro::Literal::character(t))
852 } else {
853 Literal::Stable(stable::Literal::character(t))
854 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700855 }
856
857 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700858 if nightly_works() {
859 Literal::Nightly(proc_macro::Literal::byte_string(bytes))
860 } else {
861 Literal::Stable(stable::Literal::byte_string(bytes))
862 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700863 }
864
Alex Crichtonb2c94622018-04-04 07:36:41 -0700865 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700866 match self {
867 Literal::Nightly(lit) => Span::Nightly(lit.span()),
868 Literal::Stable(lit) => Span::Stable(lit.span()),
869 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700870 }
871
Alex Crichtonb2c94622018-04-04 07:36:41 -0700872 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700873 match (self, span) {
874 (Literal::Nightly(lit), Span::Nightly(s)) => lit.set_span(s),
875 (Literal::Stable(lit), Span::Stable(s)) => lit.set_span(s),
876 _ => mismatch(),
877 }
878 }
879
880 fn unwrap_nightly(self) -> proc_macro::Literal {
881 match self {
882 Literal::Nightly(s) => s,
883 Literal::Stable(_) => mismatch(),
884 }
885 }
886}
887
888impl From<stable::Literal> for Literal {
889 fn from(s: stable::Literal) -> Literal {
890 Literal::Stable(s)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700891 }
892}
893
894impl fmt::Display for Literal {
895 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700896 match self {
897 Literal::Nightly(t) => t.fmt(f),
898 Literal::Stable(t) => t.fmt(f),
899 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700900 }
901}
902
903impl fmt::Debug for Literal {
904 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700905 match self {
906 Literal::Nightly(t) => t.fmt(f),
907 Literal::Stable(t) => t.fmt(f),
908 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700909 }
910}