blob: c4cf11a8da5f191bf1751e2872318b3e284dffe5 [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)) => {
206 let stream = iter::once(first).chain(streams.map(|s| {
207 match s {
208 TokenStream::Nightly(s) => s,
209 TokenStream::Stable(_) => mismatch(),
210 }
211 })).collect();
212 TokenStream::Nightly(stream)
213 }
214 #[cfg(not(slow_extend))]
215 Some(TokenStream::Nightly(mut first)) => {
216 first.extend(streams.map(|s| {
217 match s {
218 TokenStream::Nightly(s) => s,
219 TokenStream::Stable(_) => mismatch(),
220 }
221 }));
222 TokenStream::Nightly(first)
223 }
224 Some(TokenStream::Stable(mut first)) => {
225 first.extend(streams.map(|s| {
226 match s {
227 TokenStream::Stable(s) => s,
228 TokenStream::Nightly(_) => mismatch(),
229 }
230 }));
231 TokenStream::Stable(first)
232 }
233 None => TokenStream::new(),
234
235 }
236 }
237}
238
Alex Crichtonf3888432018-05-16 09:11:05 -0700239impl Extend<TokenTree> for TokenStream {
240 fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
241 match self {
242 TokenStream::Nightly(tts) => {
David Tolnaye839e4f2018-09-06 09:36:43 -0700243 #[cfg(not(slow_extend))]
244 {
245 tts.extend(
246 streams
247 .into_iter()
248 .map(|t| TokenStream::from(t).unwrap_nightly()),
249 );
250 }
251 #[cfg(slow_extend)]
252 {
253 *tts = tts
254 .clone()
David Tolnay40d4ffd2018-08-12 13:49:09 -0700255 .into_iter()
David Tolnaye839e4f2018-09-06 09:36:43 -0700256 .chain(
257 streams
258 .into_iter()
259 .map(TokenStream::from)
260 .flat_map(|t| match t {
261 TokenStream::Nightly(tts) => tts.into_iter(),
262 _ => mismatch(),
263 }),
264 ).collect();
265 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700266 }
267 TokenStream::Stable(tts) => tts.extend(streams),
268 }
269 }
270}
271
David Tolnay5c58c532018-08-13 11:33:51 -0700272impl Extend<TokenStream> for TokenStream {
273 fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
274 match self {
275 TokenStream::Nightly(tts) => {
David Tolnaye839e4f2018-09-06 09:36:43 -0700276 #[cfg(not(slow_extend))]
277 {
278 tts.extend(streams.into_iter().map(|stream| stream.unwrap_nightly()));
279 }
280 #[cfg(slow_extend)]
281 {
282 *tts = tts
283 .clone()
284 .into_iter()
285 .chain(
286 streams
287 .into_iter()
288 .flat_map(|t| match t {
289 TokenStream::Nightly(tts) => tts.into_iter(),
290 _ => mismatch(),
291 }),
292 ).collect();
293 }
David Tolnay5c58c532018-08-13 11:33:51 -0700294 }
295 TokenStream::Stable(tts) => {
296 tts.extend(streams.into_iter().map(|stream| stream.unwrap_stable()))
297 }
298 }
299 }
300}
301
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700302impl fmt::Debug for TokenStream {
303 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700304 match self {
305 TokenStream::Nightly(tts) => tts.fmt(f),
306 TokenStream::Stable(tts) => tts.fmt(f),
307 }
308 }
309}
310
311impl From<proc_macro::LexError> for LexError {
312 fn from(e: proc_macro::LexError) -> LexError {
313 LexError::Nightly(e)
314 }
315}
316
317impl From<stable::LexError> for LexError {
318 fn from(e: stable::LexError) -> LexError {
319 LexError::Stable(e)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700320 }
321}
322
323impl fmt::Debug for LexError {
324 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700325 match self {
326 LexError::Nightly(e) => e.fmt(f),
327 LexError::Stable(e) => e.fmt(f),
328 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700329 }
330}
331
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700332pub enum TokenTreeIter {
333 Nightly(proc_macro::token_stream::IntoIter),
334 Stable(stable::TokenTreeIter),
335}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700336
337impl IntoIterator for TokenStream {
338 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700339 type IntoIter = TokenTreeIter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700340
Alex Crichton1a7f7622017-07-05 17:47:15 -0700341 fn into_iter(self) -> TokenTreeIter {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700342 match self {
343 TokenStream::Nightly(tts) => TokenTreeIter::Nightly(tts.into_iter()),
344 TokenStream::Stable(tts) => TokenTreeIter::Stable(tts.into_iter()),
345 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700346 }
347}
348
Alex Crichton1a7f7622017-07-05 17:47:15 -0700349impl Iterator for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700350 type Item = TokenTree;
351
352 fn next(&mut self) -> Option<TokenTree> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700353 let token = match self {
354 TokenTreeIter::Nightly(iter) => iter.next()?,
355 TokenTreeIter::Stable(iter) => return iter.next(),
356 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700357 Some(match token {
David Tolnayf14813f2018-09-08 17:14:07 -0700358 proc_macro::TokenTree::Group(tt) => ::Group::_new(Group::Nightly(tt)).into(),
Alex Crichtonf3888432018-05-16 09:11:05 -0700359 proc_macro::TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700360 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700361 proc_macro::Spacing::Joint => Spacing::Joint,
362 proc_macro::Spacing::Alone => Spacing::Alone,
363 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700364 let mut o = Punct::new(tt.as_char(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700365 o.set_span(::Span::_new(Span::Nightly(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700366 o.into()
367 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700368 proc_macro::TokenTree::Ident(s) => ::Ident::_new(Ident::Nightly(s)).into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700369 proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Nightly(l)).into(),
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700370 })
371 }
372
373 fn size_hint(&self) -> (usize, Option<usize>) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700374 match self {
375 TokenTreeIter::Nightly(tts) => tts.size_hint(),
376 TokenTreeIter::Stable(tts) => tts.size_hint(),
377 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700378 }
379}
380
Alex Crichton1a7f7622017-07-05 17:47:15 -0700381impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700382 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700383 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700384 }
385}
386
Nika Layzellb35a9a32017-12-30 14:34:35 -0500387#[derive(Clone, PartialEq, Eq)]
Alex Crichtonce0904d2018-08-27 17:29:49 -0700388#[cfg(super_unstable)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700389pub enum SourceFile {
David Tolnay9cd3b4c2018-11-11 16:47:32 -0800390 Nightly(proc_macro::SourceFile),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700391 Stable(stable::SourceFile),
392}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500393
Alex Crichtonce0904d2018-08-27 17:29:49 -0700394#[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500395impl SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700396 fn nightly(sf: proc_macro::SourceFile) -> Self {
David Tolnay9cd3b4c2018-11-11 16:47:32 -0800397 SourceFile::Nightly(sf)
Nika Layzellb35a9a32017-12-30 14:34:35 -0500398 }
399
Nika Layzellf8d5f212017-12-11 14:07:02 -0500400 /// Get the path to this source file as a string.
David Tolnay9cd3b4c2018-11-11 16:47:32 -0800401 pub fn path(&self) -> PathBuf {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700402 match self {
David Tolnay9cd3b4c2018-11-11 16:47:32 -0800403 SourceFile::Nightly(a) => a.path(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700404 SourceFile::Stable(a) => a.path(),
405 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500406 }
407
408 pub fn is_real(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700409 match self {
David Tolnay9cd3b4c2018-11-11 16:47:32 -0800410 SourceFile::Nightly(a) => a.is_real(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700411 SourceFile::Stable(a) => a.is_real(),
412 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500413 }
414}
415
Alex Crichtonce0904d2018-08-27 17:29:49 -0700416#[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500417impl fmt::Debug for SourceFile {
418 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700419 match self {
David Tolnay9cd3b4c2018-11-11 16:47:32 -0800420 SourceFile::Nightly(a) => a.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700421 SourceFile::Stable(a) => a.fmt(f),
422 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500423 }
424}
425
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500426pub struct LineColumn {
427 pub line: usize,
428 pub column: usize,
429}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500430
Alex Crichton9cd80a62018-04-05 17:46:58 -0700431#[derive(Copy, Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700432pub enum Span {
433 Nightly(proc_macro::Span),
434 Stable(stable::Span),
Sergio Benitez13805082018-01-04 01:25:45 -0800435}
436
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700437impl Span {
438 pub fn call_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700439 if nightly_works() {
440 Span::Nightly(proc_macro::Span::call_site())
441 } else {
442 Span::Stable(stable::Span::call_site())
443 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700444 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700445
Alex Crichtonce0904d2018-08-27 17:29:49 -0700446 #[cfg(super_unstable)]
Alex Crichtone6085b72017-11-21 07:24:25 -0800447 pub fn def_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700448 if nightly_works() {
449 Span::Nightly(proc_macro::Span::def_site())
450 } else {
451 Span::Stable(stable::Span::def_site())
452 }
Alex Crichton998f6422017-11-19 08:06:27 -0800453 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500454
Alex Crichtonce0904d2018-08-27 17:29:49 -0700455 #[cfg(super_unstable)]
David Tolnay4e8e3972018-01-05 18:10:22 -0800456 pub fn resolved_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700457 match (self, other) {
458 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.resolved_at(b)),
459 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.resolved_at(b)),
460 _ => mismatch(),
461 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800462 }
463
Alex Crichtonce0904d2018-08-27 17:29:49 -0700464 #[cfg(super_unstable)]
David Tolnay4e8e3972018-01-05 18:10:22 -0800465 pub fn located_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700466 match (self, other) {
467 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.located_at(b)),
468 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.located_at(b)),
469 _ => mismatch(),
470 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800471 }
472
David Tolnay16a17202017-12-31 10:47:24 -0500473 pub fn unstable(self) -> proc_macro::Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700474 match self {
475 Span::Nightly(s) => s,
476 Span::Stable(_) => mismatch(),
477 }
David Tolnay16a17202017-12-31 10:47:24 -0500478 }
479
Alex Crichtonce0904d2018-08-27 17:29:49 -0700480 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500481 pub fn source_file(&self) -> SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700482 match self {
483 Span::Nightly(s) => SourceFile::nightly(s.source_file()),
484 Span::Stable(s) => SourceFile::Stable(s.source_file()),
485 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500486 }
487
Alex Crichtonce0904d2018-08-27 17:29:49 -0700488 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500489 pub fn start(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700490 match self {
491 Span::Nightly(s) => {
492 let proc_macro::LineColumn { line, column } = s.start();
493 LineColumn { line, column }
494 }
495 Span::Stable(s) => {
496 let stable::LineColumn { line, column } = s.start();
497 LineColumn { line, column }
498 }
499 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500500 }
501
Alex Crichtonce0904d2018-08-27 17:29:49 -0700502 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500503 pub fn end(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700504 match self {
505 Span::Nightly(s) => {
506 let proc_macro::LineColumn { line, column } = s.end();
507 LineColumn { line, column }
508 }
509 Span::Stable(s) => {
510 let stable::LineColumn { line, column } = s.end();
511 LineColumn { line, column }
512 }
513 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500514 }
515
Alex Crichtonce0904d2018-08-27 17:29:49 -0700516 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500517 pub fn join(&self, other: Span) -> Option<Span> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700518 let ret = match (self, other) {
519 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.join(b)?),
520 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.join(b)?),
521 _ => return None,
522 };
523 Some(ret)
Nika Layzellf8d5f212017-12-11 14:07:02 -0500524 }
Alex Crichtone24f7342018-04-05 17:58:11 -0700525
Alex Crichtonce0904d2018-08-27 17:29:49 -0700526 #[cfg(super_unstable)]
Alex Crichtone24f7342018-04-05 17:58:11 -0700527 pub fn eq(&self, other: &Span) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700528 match (self, other) {
529 (Span::Nightly(a), Span::Nightly(b)) => a.eq(b),
530 (Span::Stable(a), Span::Stable(b)) => a.eq(b),
531 _ => false,
532 }
533 }
534
535 fn unwrap_nightly(self) -> proc_macro::Span {
536 match self {
537 Span::Nightly(s) => s,
538 Span::Stable(_) => mismatch(),
539 }
540 }
541}
542
543impl From<proc_macro::Span> for ::Span {
544 fn from(proc_span: proc_macro::Span) -> ::Span {
545 ::Span::_new(Span::Nightly(proc_span))
546 }
547}
548
549impl From<stable::Span> for Span {
550 fn from(inner: stable::Span) -> Span {
551 Span::Stable(inner)
Alex Crichtone24f7342018-04-05 17:58:11 -0700552 }
Alex Crichton998f6422017-11-19 08:06:27 -0800553}
554
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700555impl fmt::Debug for Span {
556 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700557 match self {
558 Span::Nightly(s) => s.fmt(f),
559 Span::Stable(s) => s.fmt(f),
560 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700561 }
562}
563
Alex Crichtonf3888432018-05-16 09:11:05 -0700564#[derive(Clone)]
David Tolnayf14813f2018-09-08 17:14:07 -0700565pub enum Group {
566 Nightly(proc_macro::Group),
567 Stable(stable::Group),
568}
569
570impl Group {
571 pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
572 match stream {
573 TokenStream::Nightly(stream) => {
574 let delimiter = match delimiter {
575 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
576 Delimiter::Bracket => proc_macro::Delimiter::Bracket,
577 Delimiter::Brace => proc_macro::Delimiter::Brace,
578 Delimiter::None => proc_macro::Delimiter::None,
579 };
580 Group::Nightly(proc_macro::Group::new(delimiter, stream))
581 }
582 TokenStream::Stable(stream) => {
583 Group::Stable(stable::Group::new(delimiter, stream))
584 }
585 }
586 }
587
588 pub fn delimiter(&self) -> Delimiter {
589 match self {
590 Group::Nightly(g) => match g.delimiter() {
591 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
592 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
593 proc_macro::Delimiter::Brace => Delimiter::Brace,
594 proc_macro::Delimiter::None => Delimiter::None,
595 }
596 Group::Stable(g) => g.delimiter(),
597 }
598 }
599
600 pub fn stream(&self) -> TokenStream {
601 match self {
602 Group::Nightly(g) => TokenStream::Nightly(g.stream()),
603 Group::Stable(g) => TokenStream::Stable(g.stream()),
604 }
605 }
606
607 pub fn span(&self) -> Span {
608 match self {
609 Group::Nightly(g) => Span::Nightly(g.span()),
610 Group::Stable(g) => Span::Stable(g.span()),
611 }
612 }
613
614 #[cfg(super_unstable)]
615 pub fn span_open(&self) -> Span {
616 match self {
617 Group::Nightly(g) => Span::Nightly(g.span_open()),
618 Group::Stable(g) => Span::Stable(g.span_open()),
619 }
620 }
621
622 #[cfg(super_unstable)]
623 pub fn span_close(&self) -> Span {
624 match self {
625 Group::Nightly(g) => Span::Nightly(g.span_close()),
626 Group::Stable(g) => Span::Stable(g.span_close()),
627 }
628 }
629
630 pub fn set_span(&mut self, span: Span) {
631 match (self, span) {
632 (Group::Nightly(g), Span::Nightly(s)) => g.set_span(s),
633 (Group::Stable(g), Span::Stable(s)) => g.set_span(s),
634 _ => mismatch(),
635 }
636 }
637
638 fn unwrap_nightly(self) -> proc_macro::Group {
639 match self {
640 Group::Nightly(g) => g,
641 Group::Stable(_) => mismatch(),
642 }
643 }
644}
645
646impl From<stable::Group> for Group {
647 fn from(g: stable::Group) -> Self {
648 Group::Stable(g)
649 }
650}
651
652impl fmt::Display for Group {
653 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
654 match self {
655 Group::Nightly(group) => group.fmt(formatter),
656 Group::Stable(group) => group.fmt(formatter),
657 }
658 }
659}
660
661impl fmt::Debug for Group {
662 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
663 match self {
664 Group::Nightly(group) => group.fmt(formatter),
665 Group::Stable(group) => group.fmt(formatter),
666 }
667 }
668}
669
670#[derive(Clone)]
Alex Crichtonf3888432018-05-16 09:11:05 -0700671pub enum Ident {
672 Nightly(proc_macro::Ident),
673 Stable(stable::Ident),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700674}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700675
Alex Crichtonf3888432018-05-16 09:11:05 -0700676impl Ident {
677 pub fn new(string: &str, span: Span) -> Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700678 match span {
Alex Crichtonf3888432018-05-16 09:11:05 -0700679 Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new(string, s)),
680 Span::Stable(s) => Ident::Stable(stable::Ident::new(string, s)),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700681 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700682 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700683
Alex Crichtonf3888432018-05-16 09:11:05 -0700684 pub fn new_raw(string: &str, span: Span) -> Ident {
685 match span {
Alex Crichtonce0904d2018-08-27 17:29:49 -0700686 Span::Nightly(s) => {
687 let p: proc_macro::TokenStream = string.parse().unwrap();
688 let ident = match p.into_iter().next() {
689 Some(proc_macro::TokenTree::Ident(mut i)) => {
690 i.set_span(s);
691 i
692 }
693 _ => panic!(),
694 };
695 Ident::Nightly(ident)
696 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700697 Span::Stable(s) => Ident::Stable(stable::Ident::new_raw(string, s)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700698 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700699 }
700
701 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700702 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700703 Ident::Nightly(t) => Span::Nightly(t.span()),
704 Ident::Stable(t) => Span::Stable(t.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700705 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700706 }
707
708 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700709 match (self, span) {
Alex Crichtonf3888432018-05-16 09:11:05 -0700710 (Ident::Nightly(t), Span::Nightly(s)) => t.set_span(s),
711 (Ident::Stable(t), Span::Stable(s)) => t.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700712 _ => mismatch(),
713 }
714 }
715
Alex Crichtonf3888432018-05-16 09:11:05 -0700716 fn unwrap_nightly(self) -> proc_macro::Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700717 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700718 Ident::Nightly(s) => s,
719 Ident::Stable(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700720 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700721 }
722}
723
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700724impl PartialEq for Ident {
725 fn eq(&self, other: &Ident) -> bool {
726 match (self, other) {
727 (Ident::Nightly(t), Ident::Nightly(o)) => t.to_string() == o.to_string(),
728 (Ident::Stable(t), Ident::Stable(o)) => t == o,
729 _ => mismatch(),
730 }
731 }
732}
733
734impl<T> PartialEq<T> for Ident
735where
736 T: ?Sized + AsRef<str>,
737{
738 fn eq(&self, other: &T) -> bool {
739 let other = other.as_ref();
740 match self {
741 Ident::Nightly(t) => t.to_string() == other,
742 Ident::Stable(t) => t == other,
743 }
744 }
745}
746
Alex Crichtonf3888432018-05-16 09:11:05 -0700747impl fmt::Display for Ident {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700748 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700749 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700750 Ident::Nightly(t) => t.fmt(f),
751 Ident::Stable(t) => t.fmt(f),
752 }
753 }
754}
755
756impl fmt::Debug for Ident {
757 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
758 match self {
759 Ident::Nightly(t) => t.fmt(f),
760 Ident::Stable(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700761 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700762 }
763}
764
765#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700766pub enum Literal {
767 Nightly(proc_macro::Literal),
768 Stable(stable::Literal),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700769}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700770
Alex Crichtona914a612018-04-04 07:48:44 -0700771macro_rules! suffixed_numbers {
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
783macro_rules! unsuffixed_integers {
784 ($($name:ident => $kind:ident,)*) => ($(
785 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700786 if nightly_works() {
787 Literal::Nightly(proc_macro::Literal::$name(n))
788 } else {
789 Literal::Stable(stable::Literal::$name(n))
790 }
Alex Crichtona914a612018-04-04 07:48:44 -0700791 }
792 )*)
793}
794
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700795impl Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700796 suffixed_numbers! {
797 u8_suffixed => u8,
798 u16_suffixed => u16,
799 u32_suffixed => u32,
800 u64_suffixed => u64,
801 usize_suffixed => usize,
802 i8_suffixed => i8,
803 i16_suffixed => i16,
804 i32_suffixed => i32,
805 i64_suffixed => i64,
806 isize_suffixed => isize,
807
808 f32_suffixed => f32,
809 f64_suffixed => f64,
810 }
811
Alex Crichton69385662018-11-08 06:30:04 -0800812 #[cfg(u128)]
813 suffixed_numbers! {
814 i128_suffixed => i128,
815 u128_suffixed => u128,
816 }
817
Alex Crichtona914a612018-04-04 07:48:44 -0700818 unsuffixed_integers! {
819 u8_unsuffixed => u8,
820 u16_unsuffixed => u16,
821 u32_unsuffixed => u32,
822 u64_unsuffixed => u64,
823 usize_unsuffixed => usize,
824 i8_unsuffixed => i8,
825 i16_unsuffixed => i16,
826 i32_unsuffixed => i32,
827 i64_unsuffixed => i64,
828 isize_unsuffixed => isize,
829 }
830
Alex Crichton69385662018-11-08 06:30:04 -0800831 #[cfg(u128)]
832 unsuffixed_integers! {
833 i128_unsuffixed => i128,
834 u128_unsuffixed => u128,
835 }
836
Alex Crichtona914a612018-04-04 07:48:44 -0700837 pub fn f32_unsuffixed(f: f32) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700838 if nightly_works() {
839 Literal::Nightly(proc_macro::Literal::f32_unsuffixed(f))
840 } else {
841 Literal::Stable(stable::Literal::f32_unsuffixed(f))
842 }
Alex Crichtona914a612018-04-04 07:48:44 -0700843 }
844
845 pub fn f64_unsuffixed(f: f64) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700846 if nightly_works() {
847 Literal::Nightly(proc_macro::Literal::f64_unsuffixed(f))
848 } else {
849 Literal::Stable(stable::Literal::f64_unsuffixed(f))
850 }
Alex Crichtona914a612018-04-04 07:48:44 -0700851 }
852
Alex Crichtona914a612018-04-04 07:48:44 -0700853 pub fn string(t: &str) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700854 if nightly_works() {
855 Literal::Nightly(proc_macro::Literal::string(t))
856 } else {
857 Literal::Stable(stable::Literal::string(t))
858 }
Alex Crichtona914a612018-04-04 07:48:44 -0700859 }
860
861 pub fn character(t: char) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700862 if nightly_works() {
863 Literal::Nightly(proc_macro::Literal::character(t))
864 } else {
865 Literal::Stable(stable::Literal::character(t))
866 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700867 }
868
869 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700870 if nightly_works() {
871 Literal::Nightly(proc_macro::Literal::byte_string(bytes))
872 } else {
873 Literal::Stable(stable::Literal::byte_string(bytes))
874 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700875 }
876
Alex Crichtonb2c94622018-04-04 07:36:41 -0700877 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700878 match self {
879 Literal::Nightly(lit) => Span::Nightly(lit.span()),
880 Literal::Stable(lit) => Span::Stable(lit.span()),
881 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700882 }
883
Alex Crichtonb2c94622018-04-04 07:36:41 -0700884 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700885 match (self, span) {
886 (Literal::Nightly(lit), Span::Nightly(s)) => lit.set_span(s),
887 (Literal::Stable(lit), Span::Stable(s)) => lit.set_span(s),
888 _ => mismatch(),
889 }
890 }
891
892 fn unwrap_nightly(self) -> proc_macro::Literal {
893 match self {
894 Literal::Nightly(s) => s,
895 Literal::Stable(_) => mismatch(),
896 }
897 }
898}
899
900impl From<stable::Literal> for Literal {
901 fn from(s: stable::Literal) -> Literal {
902 Literal::Stable(s)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700903 }
904}
905
906impl fmt::Display for Literal {
907 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700908 match self {
909 Literal::Nightly(t) => t.fmt(f),
910 Literal::Stable(t) => t.fmt(f),
911 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700912 }
913}
914
915impl fmt::Debug for Literal {
916 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700917 match self {
918 Literal::Nightly(t) => t.fmt(f),
919 Literal::Stable(t) => t.fmt(f),
920 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700921 }
922}