blob: d15b9da630ee91f8c47a9679dceb9d1d31d038cf [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 Tolnay3d9d6ad2018-05-18 10:51:55 -07006use std::str::FromStr;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -07007
8use proc_macro;
Alex Crichton30a4e9e2018-04-27 17:02:19 -07009use stable;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070010
David Tolnayf14813f2018-09-08 17:14:07 -070011use {Delimiter, Punct, Spacing, TokenTree};
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070012
13#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -070014pub enum TokenStream {
15 Nightly(proc_macro::TokenStream),
16 Stable(stable::TokenStream),
17}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070018
Alex Crichton30a4e9e2018-04-27 17:02:19 -070019pub enum LexError {
20 Nightly(proc_macro::LexError),
21 Stable(stable::LexError),
22}
23
24fn nightly_works() -> bool {
25 use std::sync::atomic::*;
David Tolnay2ed15d52018-09-01 08:46:12 -070026 use std::sync::Once;
27
Alex Crichton30a4e9e2018-04-27 17:02:19 -070028 static WORKS: AtomicUsize = ATOMIC_USIZE_INIT;
David Tolnay2ed15d52018-09-01 08:46:12 -070029 static INIT: Once = Once::new();
Alex Crichton30a4e9e2018-04-27 17:02:19 -070030
31 match WORKS.load(Ordering::SeqCst) {
32 1 => return false,
33 2 => return true,
34 _ => {}
35 }
David Tolnay2ed15d52018-09-01 08:46:12 -070036
37 // Swap in a null panic hook to avoid printing "thread panicked" to stderr,
38 // then use catch_unwind to determine whether the compiler's proc_macro is
39 // working. When proc-macro2 is used from outside of a procedural macro all
40 // of the proc_macro crate's APIs currently panic.
41 //
42 // The Once is to prevent the possibility of this ordering:
43 //
44 // thread 1 calls take_hook, gets the user's original hook
45 // thread 1 calls set_hook with the null hook
46 // thread 2 calls take_hook, thinks null hook is the original hook
47 // thread 2 calls set_hook with the null hook
48 // thread 1 calls set_hook with the actual original hook
49 // thread 2 calls set_hook with what it thinks is the original hook
50 //
51 // in which the user's hook has been lost.
52 //
53 // There is still a race condition where a panic in a different thread can
54 // happen during the interval that the user's original panic hook is
55 // unregistered such that their hook is incorrectly not called. This is
56 // sufficiently unlikely and less bad than printing panic messages to stderr
57 // on correct use of this crate. Maybe there is a libstd feature request
58 // here. For now, if a user needs to guarantee that this failure mode does
59 // not occur, they need to call e.g. `proc_macro2::Span::call_site()` from
60 // the main thread before launching any other threads.
61 INIT.call_once(|| {
David Tolnay78ef7732018-09-01 19:20:23 -070062 type PanicHook = Fn(&PanicInfo) + Sync + Send + 'static;
63
64 let null_hook: Box<PanicHook> = Box::new(|_panic_info| { /* ignore */ });
65 let sanity_check = &*null_hook as *const PanicHook;
David Tolnay2ed15d52018-09-01 08:46:12 -070066 let original_hook = panic::take_hook();
David Tolnay78ef7732018-09-01 19:20:23 -070067 panic::set_hook(null_hook);
68
David Tolnay2ed15d52018-09-01 08:46:12 -070069 let works = panic::catch_unwind(|| proc_macro::Span::call_site()).is_ok();
70 WORKS.store(works as usize + 1, Ordering::SeqCst);
David Tolnay78ef7732018-09-01 19:20:23 -070071
72 let hopefully_null_hook = panic::take_hook();
David Tolnay2ed15d52018-09-01 08:46:12 -070073 panic::set_hook(original_hook);
David Tolnay78ef7732018-09-01 19:20:23 -070074 if sanity_check != &*hopefully_null_hook {
75 panic!("observed race condition in proc_macro2::nightly_works");
76 }
David Tolnay2ed15d52018-09-01 08:46:12 -070077 });
78 nightly_works()
Alex Crichton30a4e9e2018-04-27 17:02:19 -070079}
80
81fn mismatch() -> ! {
82 panic!("stable/nightly mismatch")
83}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070084
85impl TokenStream {
David Tolnayc3bb4592018-05-28 20:09:44 -070086 pub fn new() -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070087 if nightly_works() {
David Tolnayc3bb4592018-05-28 20:09:44 -070088 TokenStream::Nightly(proc_macro::TokenStream::new())
Alex Crichton30a4e9e2018-04-27 17:02:19 -070089 } else {
David Tolnayc3bb4592018-05-28 20:09:44 -070090 TokenStream::Stable(stable::TokenStream::new())
Alex Crichton30a4e9e2018-04-27 17:02:19 -070091 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070092 }
93
94 pub fn is_empty(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070095 match self {
96 TokenStream::Nightly(tts) => tts.is_empty(),
97 TokenStream::Stable(tts) => tts.is_empty(),
98 }
99 }
100
101 fn unwrap_nightly(self) -> proc_macro::TokenStream {
102 match self {
103 TokenStream::Nightly(s) => s,
104 TokenStream::Stable(_) => mismatch(),
105 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700106 }
David Tolnay5c58c532018-08-13 11:33:51 -0700107
108 fn unwrap_stable(self) -> stable::TokenStream {
109 match self {
110 TokenStream::Nightly(_) => mismatch(),
111 TokenStream::Stable(s) => s,
112 }
113 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700114}
115
116impl FromStr for TokenStream {
117 type Err = LexError;
118
119 fn from_str(src: &str) -> Result<TokenStream, LexError> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700120 if nightly_works() {
121 Ok(TokenStream::Nightly(src.parse()?))
122 } else {
123 Ok(TokenStream::Stable(src.parse()?))
124 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700125 }
126}
127
128impl fmt::Display for TokenStream {
129 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700130 match self {
131 TokenStream::Nightly(tts) => tts.fmt(f),
132 TokenStream::Stable(tts) => tts.fmt(f),
133 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700134 }
135}
136
137impl From<proc_macro::TokenStream> for TokenStream {
138 fn from(inner: proc_macro::TokenStream) -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700139 TokenStream::Nightly(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700140 }
141}
142
143impl From<TokenStream> for proc_macro::TokenStream {
144 fn from(inner: TokenStream) -> proc_macro::TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700145 match inner {
146 TokenStream::Nightly(inner) => inner,
147 TokenStream::Stable(inner) => inner.to_string().parse().unwrap(),
148 }
149 }
150}
151
152impl From<stable::TokenStream> for TokenStream {
153 fn from(inner: stable::TokenStream) -> TokenStream {
154 TokenStream::Stable(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700155 }
156}
157
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700158impl From<TokenTree> for TokenStream {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700159 fn from(token: TokenTree) -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700160 if !nightly_works() {
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700161 return TokenStream::Stable(token.into());
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700162 }
Alex Crichton9cd80a62018-04-05 17:46:58 -0700163 let tt: proc_macro::TokenTree = match token {
David Tolnayf14813f2018-09-08 17:14:07 -0700164 TokenTree::Group(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichtonf3888432018-05-16 09:11:05 -0700165 TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700166 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700167 Spacing::Joint => proc_macro::Spacing::Joint,
168 Spacing::Alone => proc_macro::Spacing::Alone,
169 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700170 let mut op = proc_macro::Punct::new(tt.as_char(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700171 op.set_span(tt.span().inner.unwrap_nightly());
Alex Crichton9cd80a62018-04-05 17:46:58 -0700172 op.into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700173 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700174 TokenTree::Ident(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700175 TokenTree::Literal(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700176 };
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700177 TokenStream::Nightly(tt.into())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700178 }
179}
180
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700181impl iter::FromIterator<TokenTree> for TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700182 fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self {
183 if nightly_works() {
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700184 let trees = trees
185 .into_iter()
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700186 .map(TokenStream::from)
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700187 .flat_map(|t| match t {
188 TokenStream::Nightly(s) => s,
189 TokenStream::Stable(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700190 });
191 TokenStream::Nightly(trees.collect())
192 } else {
193 TokenStream::Stable(trees.into_iter().collect())
194 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700195 }
196}
197
Alex Crichton53b00672018-09-06 17:16:10 -0700198impl iter::FromIterator<TokenStream> for TokenStream {
199 fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self {
200 let mut streams = streams.into_iter();
201 match streams.next() {
202 #[cfg(slow_extend)]
203 Some(TokenStream::Nightly(first)) => {
204 let stream = iter::once(first).chain(streams.map(|s| {
205 match s {
206 TokenStream::Nightly(s) => s,
207 TokenStream::Stable(_) => mismatch(),
208 }
209 })).collect();
210 TokenStream::Nightly(stream)
211 }
212 #[cfg(not(slow_extend))]
213 Some(TokenStream::Nightly(mut first)) => {
214 first.extend(streams.map(|s| {
215 match s {
216 TokenStream::Nightly(s) => s,
217 TokenStream::Stable(_) => mismatch(),
218 }
219 }));
220 TokenStream::Nightly(first)
221 }
222 Some(TokenStream::Stable(mut first)) => {
223 first.extend(streams.map(|s| {
224 match s {
225 TokenStream::Stable(s) => s,
226 TokenStream::Nightly(_) => mismatch(),
227 }
228 }));
229 TokenStream::Stable(first)
230 }
231 None => TokenStream::new(),
232
233 }
234 }
235}
236
Alex Crichtonf3888432018-05-16 09:11:05 -0700237impl Extend<TokenTree> for TokenStream {
238 fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
239 match self {
240 TokenStream::Nightly(tts) => {
David Tolnaye839e4f2018-09-06 09:36:43 -0700241 #[cfg(not(slow_extend))]
242 {
243 tts.extend(
244 streams
245 .into_iter()
246 .map(|t| TokenStream::from(t).unwrap_nightly()),
247 );
248 }
249 #[cfg(slow_extend)]
250 {
251 *tts = tts
252 .clone()
David Tolnay40d4ffd2018-08-12 13:49:09 -0700253 .into_iter()
David Tolnaye839e4f2018-09-06 09:36:43 -0700254 .chain(
255 streams
256 .into_iter()
257 .map(TokenStream::from)
258 .flat_map(|t| match t {
259 TokenStream::Nightly(tts) => tts.into_iter(),
260 _ => mismatch(),
261 }),
262 ).collect();
263 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700264 }
265 TokenStream::Stable(tts) => tts.extend(streams),
266 }
267 }
268}
269
David Tolnay5c58c532018-08-13 11:33:51 -0700270impl Extend<TokenStream> for TokenStream {
271 fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
272 match self {
273 TokenStream::Nightly(tts) => {
David Tolnaye839e4f2018-09-06 09:36:43 -0700274 #[cfg(not(slow_extend))]
275 {
276 tts.extend(streams.into_iter().map(|stream| stream.unwrap_nightly()));
277 }
278 #[cfg(slow_extend)]
279 {
280 *tts = tts
281 .clone()
282 .into_iter()
283 .chain(
284 streams
285 .into_iter()
286 .flat_map(|t| match t {
287 TokenStream::Nightly(tts) => tts.into_iter(),
288 _ => mismatch(),
289 }),
290 ).collect();
291 }
David Tolnay5c58c532018-08-13 11:33:51 -0700292 }
293 TokenStream::Stable(tts) => {
294 tts.extend(streams.into_iter().map(|stream| stream.unwrap_stable()))
295 }
296 }
297 }
298}
299
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700300impl fmt::Debug for TokenStream {
301 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700302 match self {
303 TokenStream::Nightly(tts) => tts.fmt(f),
304 TokenStream::Stable(tts) => tts.fmt(f),
305 }
306 }
307}
308
309impl From<proc_macro::LexError> for LexError {
310 fn from(e: proc_macro::LexError) -> LexError {
311 LexError::Nightly(e)
312 }
313}
314
315impl From<stable::LexError> for LexError {
316 fn from(e: stable::LexError) -> LexError {
317 LexError::Stable(e)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700318 }
319}
320
321impl fmt::Debug for LexError {
322 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700323 match self {
324 LexError::Nightly(e) => e.fmt(f),
325 LexError::Stable(e) => e.fmt(f),
326 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700327 }
328}
329
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700330pub enum TokenTreeIter {
331 Nightly(proc_macro::token_stream::IntoIter),
332 Stable(stable::TokenTreeIter),
333}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700334
335impl IntoIterator for TokenStream {
336 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700337 type IntoIter = TokenTreeIter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700338
Alex Crichton1a7f7622017-07-05 17:47:15 -0700339 fn into_iter(self) -> TokenTreeIter {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700340 match self {
341 TokenStream::Nightly(tts) => TokenTreeIter::Nightly(tts.into_iter()),
342 TokenStream::Stable(tts) => TokenTreeIter::Stable(tts.into_iter()),
343 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700344 }
345}
346
Alex Crichton1a7f7622017-07-05 17:47:15 -0700347impl Iterator for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700348 type Item = TokenTree;
349
350 fn next(&mut self) -> Option<TokenTree> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700351 let token = match self {
352 TokenTreeIter::Nightly(iter) => iter.next()?,
353 TokenTreeIter::Stable(iter) => return iter.next(),
354 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700355 Some(match token {
David Tolnayf14813f2018-09-08 17:14:07 -0700356 proc_macro::TokenTree::Group(tt) => ::Group::_new(Group::Nightly(tt)).into(),
Alex Crichtonf3888432018-05-16 09:11:05 -0700357 proc_macro::TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700358 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700359 proc_macro::Spacing::Joint => Spacing::Joint,
360 proc_macro::Spacing::Alone => Spacing::Alone,
361 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700362 let mut o = Punct::new(tt.as_char(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700363 o.set_span(::Span::_new(Span::Nightly(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700364 o.into()
365 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700366 proc_macro::TokenTree::Ident(s) => ::Ident::_new(Ident::Nightly(s)).into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700367 proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Nightly(l)).into(),
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700368 })
369 }
370
371 fn size_hint(&self) -> (usize, Option<usize>) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700372 match self {
373 TokenTreeIter::Nightly(tts) => tts.size_hint(),
374 TokenTreeIter::Stable(tts) => tts.size_hint(),
375 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700376 }
377}
378
Alex Crichton1a7f7622017-07-05 17:47:15 -0700379impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700380 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700381 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700382 }
383}
384
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700385pub use stable::FileName;
Nika Layzellb35a9a32017-12-30 14:34:35 -0500386
387// NOTE: We have to generate our own filename object here because we can't wrap
388// the one provided by proc_macro.
389#[derive(Clone, PartialEq, Eq)]
Alex Crichtonce0904d2018-08-27 17:29:49 -0700390#[cfg(super_unstable)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700391pub enum SourceFile {
392 Nightly(proc_macro::SourceFile, FileName),
393 Stable(stable::SourceFile),
394}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500395
Alex Crichtonce0904d2018-08-27 17:29:49 -0700396#[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500397impl SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700398 fn nightly(sf: proc_macro::SourceFile) -> Self {
Alex Crichton9f0a28a2018-07-21 18:53:35 -0700399 let filename = stable::file_name(sf.path().display().to_string());
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700400 SourceFile::Nightly(sf, filename)
Nika Layzellb35a9a32017-12-30 14:34:35 -0500401 }
402
Nika Layzellf8d5f212017-12-11 14:07:02 -0500403 /// Get the path to this source file as a string.
Nika Layzellb35a9a32017-12-30 14:34:35 -0500404 pub fn path(&self) -> &FileName {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700405 match self {
406 SourceFile::Nightly(_, f) => f,
407 SourceFile::Stable(a) => a.path(),
408 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500409 }
410
411 pub fn is_real(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700412 match self {
413 SourceFile::Nightly(a, _) => a.is_real(),
414 SourceFile::Stable(a) => a.is_real(),
415 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500416 }
417}
418
Alex Crichtonce0904d2018-08-27 17:29:49 -0700419#[cfg(super_unstable)]
Nika Layzellb35a9a32017-12-30 14:34:35 -0500420impl AsRef<FileName> for SourceFile {
421 fn as_ref(&self) -> &FileName {
422 self.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500423 }
424}
425
Alex Crichtonce0904d2018-08-27 17:29:49 -0700426#[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500427impl fmt::Debug for SourceFile {
428 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700429 match self {
430 SourceFile::Nightly(a, _) => a.fmt(f),
431 SourceFile::Stable(a) => a.fmt(f),
432 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500433 }
434}
435
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500436pub struct LineColumn {
437 pub line: usize,
438 pub column: usize,
439}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500440
Alex Crichton9cd80a62018-04-05 17:46:58 -0700441#[derive(Copy, Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700442pub enum Span {
443 Nightly(proc_macro::Span),
444 Stable(stable::Span),
Sergio Benitez13805082018-01-04 01:25:45 -0800445}
446
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700447impl Span {
448 pub fn call_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700449 if nightly_works() {
450 Span::Nightly(proc_macro::Span::call_site())
451 } else {
452 Span::Stable(stable::Span::call_site())
453 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700454 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700455
Alex Crichtonce0904d2018-08-27 17:29:49 -0700456 #[cfg(super_unstable)]
Alex Crichtone6085b72017-11-21 07:24:25 -0800457 pub fn def_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700458 if nightly_works() {
459 Span::Nightly(proc_macro::Span::def_site())
460 } else {
461 Span::Stable(stable::Span::def_site())
462 }
Alex Crichton998f6422017-11-19 08:06:27 -0800463 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500464
Alex Crichtonce0904d2018-08-27 17:29:49 -0700465 #[cfg(super_unstable)]
David Tolnay4e8e3972018-01-05 18:10:22 -0800466 pub fn resolved_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700467 match (self, other) {
468 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.resolved_at(b)),
469 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.resolved_at(b)),
470 _ => mismatch(),
471 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800472 }
473
Alex Crichtonce0904d2018-08-27 17:29:49 -0700474 #[cfg(super_unstable)]
David Tolnay4e8e3972018-01-05 18:10:22 -0800475 pub fn located_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700476 match (self, other) {
477 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.located_at(b)),
478 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.located_at(b)),
479 _ => mismatch(),
480 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800481 }
482
David Tolnay16a17202017-12-31 10:47:24 -0500483 pub fn unstable(self) -> proc_macro::Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700484 match self {
485 Span::Nightly(s) => s,
486 Span::Stable(_) => mismatch(),
487 }
David Tolnay16a17202017-12-31 10:47:24 -0500488 }
489
Alex Crichtonce0904d2018-08-27 17:29:49 -0700490 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500491 pub fn source_file(&self) -> SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700492 match self {
493 Span::Nightly(s) => SourceFile::nightly(s.source_file()),
494 Span::Stable(s) => SourceFile::Stable(s.source_file()),
495 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500496 }
497
Alex Crichtonce0904d2018-08-27 17:29:49 -0700498 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500499 pub fn start(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700500 match self {
501 Span::Nightly(s) => {
502 let proc_macro::LineColumn { line, column } = s.start();
503 LineColumn { line, column }
504 }
505 Span::Stable(s) => {
506 let stable::LineColumn { line, column } = s.start();
507 LineColumn { line, column }
508 }
509 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500510 }
511
Alex Crichtonce0904d2018-08-27 17:29:49 -0700512 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500513 pub fn end(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700514 match self {
515 Span::Nightly(s) => {
516 let proc_macro::LineColumn { line, column } = s.end();
517 LineColumn { line, column }
518 }
519 Span::Stable(s) => {
520 let stable::LineColumn { line, column } = s.end();
521 LineColumn { line, column }
522 }
523 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500524 }
525
Alex Crichtonce0904d2018-08-27 17:29:49 -0700526 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500527 pub fn join(&self, other: Span) -> Option<Span> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700528 let ret = match (self, other) {
529 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.join(b)?),
530 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.join(b)?),
531 _ => return None,
532 };
533 Some(ret)
Nika Layzellf8d5f212017-12-11 14:07:02 -0500534 }
Alex Crichtone24f7342018-04-05 17:58:11 -0700535
Alex Crichtonce0904d2018-08-27 17:29:49 -0700536 #[cfg(super_unstable)]
Alex Crichtone24f7342018-04-05 17:58:11 -0700537 pub fn eq(&self, other: &Span) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700538 match (self, other) {
539 (Span::Nightly(a), Span::Nightly(b)) => a.eq(b),
540 (Span::Stable(a), Span::Stable(b)) => a.eq(b),
541 _ => false,
542 }
543 }
544
545 fn unwrap_nightly(self) -> proc_macro::Span {
546 match self {
547 Span::Nightly(s) => s,
548 Span::Stable(_) => mismatch(),
549 }
550 }
551}
552
553impl From<proc_macro::Span> for ::Span {
554 fn from(proc_span: proc_macro::Span) -> ::Span {
555 ::Span::_new(Span::Nightly(proc_span))
556 }
557}
558
559impl From<stable::Span> for Span {
560 fn from(inner: stable::Span) -> Span {
561 Span::Stable(inner)
Alex Crichtone24f7342018-04-05 17:58:11 -0700562 }
Alex Crichton998f6422017-11-19 08:06:27 -0800563}
564
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700565impl fmt::Debug for Span {
566 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700567 match self {
568 Span::Nightly(s) => s.fmt(f),
569 Span::Stable(s) => s.fmt(f),
570 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700571 }
572}
573
Alex Crichtonf3888432018-05-16 09:11:05 -0700574#[derive(Clone)]
David Tolnayf14813f2018-09-08 17:14:07 -0700575pub enum Group {
576 Nightly(proc_macro::Group),
577 Stable(stable::Group),
578}
579
580impl Group {
581 pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
582 match stream {
583 TokenStream::Nightly(stream) => {
584 let delimiter = match delimiter {
585 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
586 Delimiter::Bracket => proc_macro::Delimiter::Bracket,
587 Delimiter::Brace => proc_macro::Delimiter::Brace,
588 Delimiter::None => proc_macro::Delimiter::None,
589 };
590 Group::Nightly(proc_macro::Group::new(delimiter, stream))
591 }
592 TokenStream::Stable(stream) => {
593 Group::Stable(stable::Group::new(delimiter, stream))
594 }
595 }
596 }
597
598 pub fn delimiter(&self) -> Delimiter {
599 match self {
600 Group::Nightly(g) => match g.delimiter() {
601 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
602 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
603 proc_macro::Delimiter::Brace => Delimiter::Brace,
604 proc_macro::Delimiter::None => Delimiter::None,
605 }
606 Group::Stable(g) => g.delimiter(),
607 }
608 }
609
610 pub fn stream(&self) -> TokenStream {
611 match self {
612 Group::Nightly(g) => TokenStream::Nightly(g.stream()),
613 Group::Stable(g) => TokenStream::Stable(g.stream()),
614 }
615 }
616
617 pub fn span(&self) -> Span {
618 match self {
619 Group::Nightly(g) => Span::Nightly(g.span()),
620 Group::Stable(g) => Span::Stable(g.span()),
621 }
622 }
623
624 #[cfg(super_unstable)]
625 pub fn span_open(&self) -> Span {
626 match self {
627 Group::Nightly(g) => Span::Nightly(g.span_open()),
628 Group::Stable(g) => Span::Stable(g.span_open()),
629 }
630 }
631
632 #[cfg(super_unstable)]
633 pub fn span_close(&self) -> Span {
634 match self {
635 Group::Nightly(g) => Span::Nightly(g.span_close()),
636 Group::Stable(g) => Span::Stable(g.span_close()),
637 }
638 }
639
640 pub fn set_span(&mut self, span: Span) {
641 match (self, span) {
642 (Group::Nightly(g), Span::Nightly(s)) => g.set_span(s),
643 (Group::Stable(g), Span::Stable(s)) => g.set_span(s),
644 _ => mismatch(),
645 }
646 }
647
648 fn unwrap_nightly(self) -> proc_macro::Group {
649 match self {
650 Group::Nightly(g) => g,
651 Group::Stable(_) => mismatch(),
652 }
653 }
654}
655
656impl From<stable::Group> for Group {
657 fn from(g: stable::Group) -> Self {
658 Group::Stable(g)
659 }
660}
661
662impl fmt::Display for Group {
663 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
664 match self {
665 Group::Nightly(group) => group.fmt(formatter),
666 Group::Stable(group) => group.fmt(formatter),
667 }
668 }
669}
670
671impl fmt::Debug for Group {
672 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
673 match self {
674 Group::Nightly(group) => group.fmt(formatter),
675 Group::Stable(group) => group.fmt(formatter),
676 }
677 }
678}
679
680#[derive(Clone)]
Alex Crichtonf3888432018-05-16 09:11:05 -0700681pub enum Ident {
682 Nightly(proc_macro::Ident),
683 Stable(stable::Ident),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700684}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700685
Alex Crichtonf3888432018-05-16 09:11:05 -0700686impl Ident {
687 pub fn new(string: &str, span: Span) -> Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700688 match span {
Alex Crichtonf3888432018-05-16 09:11:05 -0700689 Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new(string, s)),
690 Span::Stable(s) => Ident::Stable(stable::Ident::new(string, s)),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700691 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700692 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700693
Alex Crichtonf3888432018-05-16 09:11:05 -0700694 pub fn new_raw(string: &str, span: Span) -> Ident {
695 match span {
Alex Crichtonce0904d2018-08-27 17:29:49 -0700696 Span::Nightly(s) => {
697 let p: proc_macro::TokenStream = string.parse().unwrap();
698 let ident = match p.into_iter().next() {
699 Some(proc_macro::TokenTree::Ident(mut i)) => {
700 i.set_span(s);
701 i
702 }
703 _ => panic!(),
704 };
705 Ident::Nightly(ident)
706 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700707 Span::Stable(s) => Ident::Stable(stable::Ident::new_raw(string, s)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700708 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700709 }
710
711 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700712 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700713 Ident::Nightly(t) => Span::Nightly(t.span()),
714 Ident::Stable(t) => Span::Stable(t.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700715 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700716 }
717
718 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700719 match (self, span) {
Alex Crichtonf3888432018-05-16 09:11:05 -0700720 (Ident::Nightly(t), Span::Nightly(s)) => t.set_span(s),
721 (Ident::Stable(t), Span::Stable(s)) => t.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700722 _ => mismatch(),
723 }
724 }
725
Alex Crichtonf3888432018-05-16 09:11:05 -0700726 fn unwrap_nightly(self) -> proc_macro::Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700727 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700728 Ident::Nightly(s) => s,
729 Ident::Stable(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700730 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700731 }
732}
733
David Tolnayc0b0f2e2018-09-02 17:56:08 -0700734impl PartialEq for Ident {
735 fn eq(&self, other: &Ident) -> bool {
736 match (self, other) {
737 (Ident::Nightly(t), Ident::Nightly(o)) => t.to_string() == o.to_string(),
738 (Ident::Stable(t), Ident::Stable(o)) => t == o,
739 _ => mismatch(),
740 }
741 }
742}
743
744impl<T> PartialEq<T> for Ident
745where
746 T: ?Sized + AsRef<str>,
747{
748 fn eq(&self, other: &T) -> bool {
749 let other = other.as_ref();
750 match self {
751 Ident::Nightly(t) => t.to_string() == other,
752 Ident::Stable(t) => t == other,
753 }
754 }
755}
756
Alex Crichtonf3888432018-05-16 09:11:05 -0700757impl fmt::Display for Ident {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700758 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700759 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700760 Ident::Nightly(t) => t.fmt(f),
761 Ident::Stable(t) => t.fmt(f),
762 }
763 }
764}
765
766impl fmt::Debug for Ident {
767 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
768 match self {
769 Ident::Nightly(t) => t.fmt(f),
770 Ident::Stable(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700771 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700772 }
773}
774
775#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700776pub enum Literal {
777 Nightly(proc_macro::Literal),
778 Stable(stable::Literal),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700779}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700780
Alex Crichtona914a612018-04-04 07:48:44 -0700781macro_rules! suffixed_numbers {
782 ($($name:ident => $kind:ident,)*) => ($(
783 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700784 if nightly_works() {
785 Literal::Nightly(proc_macro::Literal::$name(n))
786 } else {
787 Literal::Stable(stable::Literal::$name(n))
788 }
Alex Crichtona914a612018-04-04 07:48:44 -0700789 }
790 )*)
791}
792
793macro_rules! unsuffixed_integers {
794 ($($name:ident => $kind:ident,)*) => ($(
795 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700796 if nightly_works() {
797 Literal::Nightly(proc_macro::Literal::$name(n))
798 } else {
799 Literal::Stable(stable::Literal::$name(n))
800 }
Alex Crichtona914a612018-04-04 07:48:44 -0700801 }
802 )*)
803}
804
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700805impl Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700806 suffixed_numbers! {
807 u8_suffixed => u8,
808 u16_suffixed => u16,
809 u32_suffixed => u32,
810 u64_suffixed => u64,
811 usize_suffixed => usize,
812 i8_suffixed => i8,
813 i16_suffixed => i16,
814 i32_suffixed => i32,
815 i64_suffixed => i64,
816 isize_suffixed => isize,
817
818 f32_suffixed => f32,
819 f64_suffixed => f64,
820 }
821
Alex Crichton69385662018-11-08 06:30:04 -0800822 #[cfg(u128)]
823 suffixed_numbers! {
824 i128_suffixed => i128,
825 u128_suffixed => u128,
826 }
827
Alex Crichtona914a612018-04-04 07:48:44 -0700828 unsuffixed_integers! {
829 u8_unsuffixed => u8,
830 u16_unsuffixed => u16,
831 u32_unsuffixed => u32,
832 u64_unsuffixed => u64,
833 usize_unsuffixed => usize,
834 i8_unsuffixed => i8,
835 i16_unsuffixed => i16,
836 i32_unsuffixed => i32,
837 i64_unsuffixed => i64,
838 isize_unsuffixed => isize,
839 }
840
Alex Crichton69385662018-11-08 06:30:04 -0800841 #[cfg(u128)]
842 unsuffixed_integers! {
843 i128_unsuffixed => i128,
844 u128_unsuffixed => u128,
845 }
846
Alex Crichtona914a612018-04-04 07:48:44 -0700847 pub fn f32_unsuffixed(f: f32) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700848 if nightly_works() {
849 Literal::Nightly(proc_macro::Literal::f32_unsuffixed(f))
850 } else {
851 Literal::Stable(stable::Literal::f32_unsuffixed(f))
852 }
Alex Crichtona914a612018-04-04 07:48:44 -0700853 }
854
855 pub fn f64_unsuffixed(f: f64) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700856 if nightly_works() {
857 Literal::Nightly(proc_macro::Literal::f64_unsuffixed(f))
858 } else {
859 Literal::Stable(stable::Literal::f64_unsuffixed(f))
860 }
Alex Crichtona914a612018-04-04 07:48:44 -0700861 }
862
Alex Crichtona914a612018-04-04 07:48:44 -0700863 pub fn string(t: &str) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700864 if nightly_works() {
865 Literal::Nightly(proc_macro::Literal::string(t))
866 } else {
867 Literal::Stable(stable::Literal::string(t))
868 }
Alex Crichtona914a612018-04-04 07:48:44 -0700869 }
870
871 pub fn character(t: char) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700872 if nightly_works() {
873 Literal::Nightly(proc_macro::Literal::character(t))
874 } else {
875 Literal::Stable(stable::Literal::character(t))
876 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700877 }
878
879 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700880 if nightly_works() {
881 Literal::Nightly(proc_macro::Literal::byte_string(bytes))
882 } else {
883 Literal::Stable(stable::Literal::byte_string(bytes))
884 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700885 }
886
Alex Crichtonb2c94622018-04-04 07:36:41 -0700887 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700888 match self {
889 Literal::Nightly(lit) => Span::Nightly(lit.span()),
890 Literal::Stable(lit) => Span::Stable(lit.span()),
891 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700892 }
893
Alex Crichtonb2c94622018-04-04 07:36:41 -0700894 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700895 match (self, span) {
896 (Literal::Nightly(lit), Span::Nightly(s)) => lit.set_span(s),
897 (Literal::Stable(lit), Span::Stable(s)) => lit.set_span(s),
898 _ => mismatch(),
899 }
900 }
901
902 fn unwrap_nightly(self) -> proc_macro::Literal {
903 match self {
904 Literal::Nightly(s) => s,
905 Literal::Stable(_) => mismatch(),
906 }
907 }
908}
909
910impl From<stable::Literal> for Literal {
911 fn from(s: stable::Literal) -> Literal {
912 Literal::Stable(s)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700913 }
914}
915
916impl fmt::Display for Literal {
917 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700918 match self {
919 Literal::Nightly(t) => t.fmt(f),
920 Literal::Stable(t) => t.fmt(f),
921 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700922 }
923}
924
925impl fmt::Debug for Literal {
926 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700927 match self {
928 Literal::Nightly(t) => t.fmt(f),
929 Literal::Stable(t) => t.fmt(f),
930 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700931 }
932}