blob: 57488ddbdb54f20da1a66746552ebd569eaf590e [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,
David Tolnayc0425cd2019-01-16 12:13:15 -0800476 Span::Stable(_) => {
477 panic!("proc_macro::Span is only available in procedural macros")
478 }
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700479 }
David Tolnay16a17202017-12-31 10:47:24 -0500480 }
481
Alex Crichtonce0904d2018-08-27 17:29:49 -0700482 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500483 pub fn source_file(&self) -> SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700484 match self {
485 Span::Nightly(s) => SourceFile::nightly(s.source_file()),
486 Span::Stable(s) => SourceFile::Stable(s.source_file()),
487 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500488 }
489
Alex Crichtonce0904d2018-08-27 17:29:49 -0700490 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500491 pub fn start(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700492 match self {
493 Span::Nightly(s) => {
494 let proc_macro::LineColumn { line, column } = s.start();
495 LineColumn { line, column }
496 }
497 Span::Stable(s) => {
498 let stable::LineColumn { line, column } = s.start();
499 LineColumn { line, column }
500 }
501 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500502 }
503
Alex Crichtonce0904d2018-08-27 17:29:49 -0700504 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500505 pub fn end(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700506 match self {
507 Span::Nightly(s) => {
508 let proc_macro::LineColumn { line, column } = s.end();
509 LineColumn { line, column }
510 }
511 Span::Stable(s) => {
512 let stable::LineColumn { line, column } = s.end();
513 LineColumn { line, column }
514 }
515 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500516 }
517
Alex Crichtonce0904d2018-08-27 17:29:49 -0700518 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500519 pub fn join(&self, other: Span) -> Option<Span> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700520 let ret = match (self, other) {
521 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.join(b)?),
522 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.join(b)?),
523 _ => return None,
524 };
525 Some(ret)
Nika Layzellf8d5f212017-12-11 14:07:02 -0500526 }
Alex Crichtone24f7342018-04-05 17:58:11 -0700527
Alex Crichtonce0904d2018-08-27 17:29:49 -0700528 #[cfg(super_unstable)]
Alex Crichtone24f7342018-04-05 17:58:11 -0700529 pub fn eq(&self, other: &Span) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700530 match (self, other) {
531 (Span::Nightly(a), Span::Nightly(b)) => a.eq(b),
532 (Span::Stable(a), Span::Stable(b)) => a.eq(b),
533 _ => false,
534 }
535 }
536
537 fn unwrap_nightly(self) -> proc_macro::Span {
538 match self {
539 Span::Nightly(s) => s,
540 Span::Stable(_) => mismatch(),
541 }
542 }
543}
544
545impl From<proc_macro::Span> for ::Span {
546 fn from(proc_span: proc_macro::Span) -> ::Span {
547 ::Span::_new(Span::Nightly(proc_span))
548 }
549}
550
551impl From<stable::Span> for Span {
552 fn from(inner: stable::Span) -> Span {
553 Span::Stable(inner)
Alex Crichtone24f7342018-04-05 17:58:11 -0700554 }
Alex Crichton998f6422017-11-19 08:06:27 -0800555}
556
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700557impl fmt::Debug for Span {
558 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700559 match self {
560 Span::Nightly(s) => s.fmt(f),
561 Span::Stable(s) => s.fmt(f),
562 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700563 }
564}
565
Alex Crichtonf3888432018-05-16 09:11:05 -0700566#[derive(Clone)]
David Tolnayf14813f2018-09-08 17:14:07 -0700567pub enum Group {
568 Nightly(proc_macro::Group),
569 Stable(stable::Group),
570}
571
572impl Group {
573 pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
574 match stream {
575 TokenStream::Nightly(stream) => {
576 let delimiter = match delimiter {
577 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
578 Delimiter::Bracket => proc_macro::Delimiter::Bracket,
579 Delimiter::Brace => proc_macro::Delimiter::Brace,
580 Delimiter::None => proc_macro::Delimiter::None,
581 };
582 Group::Nightly(proc_macro::Group::new(delimiter, stream))
583 }
584 TokenStream::Stable(stream) => {
585 Group::Stable(stable::Group::new(delimiter, stream))
586 }
587 }
588 }
589
590 pub fn delimiter(&self) -> Delimiter {
591 match self {
592 Group::Nightly(g) => match g.delimiter() {
593 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
594 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
595 proc_macro::Delimiter::Brace => Delimiter::Brace,
596 proc_macro::Delimiter::None => Delimiter::None,
597 }
598 Group::Stable(g) => g.delimiter(),
599 }
600 }
601
602 pub fn stream(&self) -> TokenStream {
603 match self {
604 Group::Nightly(g) => TokenStream::Nightly(g.stream()),
605 Group::Stable(g) => TokenStream::Stable(g.stream()),
606 }
607 }
608
609 pub fn span(&self) -> Span {
610 match self {
611 Group::Nightly(g) => Span::Nightly(g.span()),
612 Group::Stable(g) => Span::Stable(g.span()),
613 }
614 }
615
616 #[cfg(super_unstable)]
617 pub fn span_open(&self) -> Span {
618 match self {
619 Group::Nightly(g) => Span::Nightly(g.span_open()),
620 Group::Stable(g) => Span::Stable(g.span_open()),
621 }
622 }
623
624 #[cfg(super_unstable)]
625 pub fn span_close(&self) -> Span {
626 match self {
627 Group::Nightly(g) => Span::Nightly(g.span_close()),
628 Group::Stable(g) => Span::Stable(g.span_close()),
629 }
630 }
631
632 pub fn set_span(&mut self, span: Span) {
633 match (self, span) {
634 (Group::Nightly(g), Span::Nightly(s)) => g.set_span(s),
635 (Group::Stable(g), Span::Stable(s)) => g.set_span(s),
636 _ => mismatch(),
637 }
638 }
639
640 fn unwrap_nightly(self) -> proc_macro::Group {
641 match self {
642 Group::Nightly(g) => g,
643 Group::Stable(_) => mismatch(),
644 }
645 }
646}
647
648impl From<stable::Group> for Group {
649 fn from(g: stable::Group) -> Self {
650 Group::Stable(g)
651 }
652}
653
654impl fmt::Display for Group {
655 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
656 match self {
657 Group::Nightly(group) => group.fmt(formatter),
658 Group::Stable(group) => group.fmt(formatter),
659 }
660 }
661}
662
663impl fmt::Debug for Group {
664 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
665 match self {
666 Group::Nightly(group) => group.fmt(formatter),
667 Group::Stable(group) => group.fmt(formatter),
668 }
669 }
670}
671
672#[derive(Clone)]
Alex Crichtonf3888432018-05-16 09:11:05 -0700673pub enum Ident {
674 Nightly(proc_macro::Ident),
675 Stable(stable::Ident),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700676}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700677
Alex Crichtonf3888432018-05-16 09:11:05 -0700678impl Ident {
679 pub fn new(string: &str, span: Span) -> Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700680 match span {
Alex Crichtonf3888432018-05-16 09:11:05 -0700681 Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new(string, s)),
682 Span::Stable(s) => Ident::Stable(stable::Ident::new(string, s)),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700683 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700684 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700685
Alex Crichtonf3888432018-05-16 09:11:05 -0700686 pub fn new_raw(string: &str, span: Span) -> Ident {
687 match span {
Alex Crichtonce0904d2018-08-27 17:29:49 -0700688 Span::Nightly(s) => {
689 let p: proc_macro::TokenStream = string.parse().unwrap();
690 let ident = match p.into_iter().next() {
691 Some(proc_macro::TokenTree::Ident(mut i)) => {
692 i.set_span(s);
693 i
694 }
695 _ => panic!(),
696 };
697 Ident::Nightly(ident)
698 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700699 Span::Stable(s) => Ident::Stable(stable::Ident::new_raw(string, s)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700700 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700701 }
702
703 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700704 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700705 Ident::Nightly(t) => Span::Nightly(t.span()),
706 Ident::Stable(t) => Span::Stable(t.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700707 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700708 }
709
710 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700711 match (self, span) {
Alex Crichtonf3888432018-05-16 09:11:05 -0700712 (Ident::Nightly(t), Span::Nightly(s)) => t.set_span(s),
713 (Ident::Stable(t), Span::Stable(s)) => t.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700714 _ => mismatch(),
715 }
716 }
717
Alex Crichtonf3888432018-05-16 09:11:05 -0700718 fn unwrap_nightly(self) -> proc_macro::Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700719 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700720 Ident::Nightly(s) => s,
721 Ident::Stable(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700722 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700723 }
724}
725
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700726impl PartialEq for Ident {
727 fn eq(&self, other: &Ident) -> bool {
728 match (self, other) {
729 (Ident::Nightly(t), Ident::Nightly(o)) => t.to_string() == o.to_string(),
730 (Ident::Stable(t), Ident::Stable(o)) => t == o,
731 _ => mismatch(),
732 }
733 }
734}
735
736impl<T> PartialEq<T> for Ident
737where
738 T: ?Sized + AsRef<str>,
739{
740 fn eq(&self, other: &T) -> bool {
741 let other = other.as_ref();
742 match self {
743 Ident::Nightly(t) => t.to_string() == other,
744 Ident::Stable(t) => t == other,
745 }
746 }
747}
748
Alex Crichtonf3888432018-05-16 09:11:05 -0700749impl fmt::Display for Ident {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700750 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700751 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700752 Ident::Nightly(t) => t.fmt(f),
753 Ident::Stable(t) => t.fmt(f),
754 }
755 }
756}
757
758impl fmt::Debug for Ident {
759 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
760 match self {
761 Ident::Nightly(t) => t.fmt(f),
762 Ident::Stable(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700763 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700764 }
765}
766
767#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700768pub enum Literal {
769 Nightly(proc_macro::Literal),
770 Stable(stable::Literal),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700771}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700772
Alex Crichtona914a612018-04-04 07:48:44 -0700773macro_rules! suffixed_numbers {
774 ($($name:ident => $kind:ident,)*) => ($(
775 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700776 if nightly_works() {
777 Literal::Nightly(proc_macro::Literal::$name(n))
778 } else {
779 Literal::Stable(stable::Literal::$name(n))
780 }
Alex Crichtona914a612018-04-04 07:48:44 -0700781 }
782 )*)
783}
784
785macro_rules! unsuffixed_integers {
786 ($($name:ident => $kind:ident,)*) => ($(
787 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700788 if nightly_works() {
789 Literal::Nightly(proc_macro::Literal::$name(n))
790 } else {
791 Literal::Stable(stable::Literal::$name(n))
792 }
Alex Crichtona914a612018-04-04 07:48:44 -0700793 }
794 )*)
795}
796
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700797impl Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700798 suffixed_numbers! {
799 u8_suffixed => u8,
800 u16_suffixed => u16,
801 u32_suffixed => u32,
802 u64_suffixed => u64,
803 usize_suffixed => usize,
804 i8_suffixed => i8,
805 i16_suffixed => i16,
806 i32_suffixed => i32,
807 i64_suffixed => i64,
808 isize_suffixed => isize,
809
810 f32_suffixed => f32,
811 f64_suffixed => f64,
812 }
813
Alex Crichton69385662018-11-08 06:30:04 -0800814 #[cfg(u128)]
815 suffixed_numbers! {
816 i128_suffixed => i128,
817 u128_suffixed => u128,
818 }
819
Alex Crichtona914a612018-04-04 07:48:44 -0700820 unsuffixed_integers! {
821 u8_unsuffixed => u8,
822 u16_unsuffixed => u16,
823 u32_unsuffixed => u32,
824 u64_unsuffixed => u64,
825 usize_unsuffixed => usize,
826 i8_unsuffixed => i8,
827 i16_unsuffixed => i16,
828 i32_unsuffixed => i32,
829 i64_unsuffixed => i64,
830 isize_unsuffixed => isize,
831 }
832
Alex Crichton69385662018-11-08 06:30:04 -0800833 #[cfg(u128)]
834 unsuffixed_integers! {
835 i128_unsuffixed => i128,
836 u128_unsuffixed => u128,
837 }
838
Alex Crichtona914a612018-04-04 07:48:44 -0700839 pub fn f32_unsuffixed(f: f32) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700840 if nightly_works() {
841 Literal::Nightly(proc_macro::Literal::f32_unsuffixed(f))
842 } else {
843 Literal::Stable(stable::Literal::f32_unsuffixed(f))
844 }
Alex Crichtona914a612018-04-04 07:48:44 -0700845 }
846
847 pub fn f64_unsuffixed(f: f64) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700848 if nightly_works() {
849 Literal::Nightly(proc_macro::Literal::f64_unsuffixed(f))
850 } else {
851 Literal::Stable(stable::Literal::f64_unsuffixed(f))
852 }
Alex Crichtona914a612018-04-04 07:48:44 -0700853 }
854
Alex Crichtona914a612018-04-04 07:48:44 -0700855 pub fn string(t: &str) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700856 if nightly_works() {
857 Literal::Nightly(proc_macro::Literal::string(t))
858 } else {
859 Literal::Stable(stable::Literal::string(t))
860 }
Alex Crichtona914a612018-04-04 07:48:44 -0700861 }
862
863 pub fn character(t: char) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700864 if nightly_works() {
865 Literal::Nightly(proc_macro::Literal::character(t))
866 } else {
867 Literal::Stable(stable::Literal::character(t))
868 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700869 }
870
871 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700872 if nightly_works() {
873 Literal::Nightly(proc_macro::Literal::byte_string(bytes))
874 } else {
875 Literal::Stable(stable::Literal::byte_string(bytes))
876 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700877 }
878
Alex Crichtonb2c94622018-04-04 07:36:41 -0700879 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700880 match self {
881 Literal::Nightly(lit) => Span::Nightly(lit.span()),
882 Literal::Stable(lit) => Span::Stable(lit.span()),
883 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700884 }
885
Alex Crichtonb2c94622018-04-04 07:36:41 -0700886 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700887 match (self, span) {
888 (Literal::Nightly(lit), Span::Nightly(s)) => lit.set_span(s),
889 (Literal::Stable(lit), Span::Stable(s)) => lit.set_span(s),
890 _ => mismatch(),
891 }
892 }
893
894 fn unwrap_nightly(self) -> proc_macro::Literal {
895 match self {
896 Literal::Nightly(s) => s,
897 Literal::Stable(_) => mismatch(),
898 }
899 }
900}
901
902impl From<stable::Literal> for Literal {
903 fn from(s: stable::Literal) -> Literal {
904 Literal::Stable(s)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700905 }
906}
907
908impl fmt::Display for Literal {
909 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700910 match self {
911 Literal::Nightly(t) => t.fmt(f),
912 Literal::Stable(t) => t.fmt(f),
913 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700914 }
915}
916
917impl fmt::Debug for Literal {
918 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700919 match self {
920 Literal::Nightly(t) => t.fmt(f),
921 Literal::Stable(t) => t.fmt(f),
922 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700923 }
924}