blob: 4706ca7165515e75b4d4059adfa051a1f73bf6a9 [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;
Alex Crichton30a4e9e2018-04-27 17:02:19 -07005use std::panic;
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
Alex Crichtonf3888432018-05-16 09:11:05 -070011use {Delimiter, Group, 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(|| {
62 let original_hook = panic::take_hook();
63 panic::set_hook(Box::new(|_panic_info| { /* ignore */ }));
64 let works = panic::catch_unwind(|| proc_macro::Span::call_site()).is_ok();
65 WORKS.store(works as usize + 1, Ordering::SeqCst);
66 panic::set_hook(original_hook);
67 });
68 nightly_works()
Alex Crichton30a4e9e2018-04-27 17:02:19 -070069}
70
71fn mismatch() -> ! {
72 panic!("stable/nightly mismatch")
73}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070074
75impl TokenStream {
David Tolnayc3bb4592018-05-28 20:09:44 -070076 pub fn new() -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070077 if nightly_works() {
David Tolnayc3bb4592018-05-28 20:09:44 -070078 TokenStream::Nightly(proc_macro::TokenStream::new())
Alex Crichton30a4e9e2018-04-27 17:02:19 -070079 } else {
David Tolnayc3bb4592018-05-28 20:09:44 -070080 TokenStream::Stable(stable::TokenStream::new())
Alex Crichton30a4e9e2018-04-27 17:02:19 -070081 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070082 }
83
84 pub fn is_empty(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070085 match self {
86 TokenStream::Nightly(tts) => tts.is_empty(),
87 TokenStream::Stable(tts) => tts.is_empty(),
88 }
89 }
90
91 fn unwrap_nightly(self) -> proc_macro::TokenStream {
92 match self {
93 TokenStream::Nightly(s) => s,
94 TokenStream::Stable(_) => mismatch(),
95 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070096 }
David Tolnay5c58c532018-08-13 11:33:51 -070097
98 fn unwrap_stable(self) -> stable::TokenStream {
99 match self {
100 TokenStream::Nightly(_) => mismatch(),
101 TokenStream::Stable(s) => s,
102 }
103 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700104}
105
106impl FromStr for TokenStream {
107 type Err = LexError;
108
109 fn from_str(src: &str) -> Result<TokenStream, LexError> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700110 if nightly_works() {
111 Ok(TokenStream::Nightly(src.parse()?))
112 } else {
113 Ok(TokenStream::Stable(src.parse()?))
114 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700115 }
116}
117
118impl fmt::Display for TokenStream {
119 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700120 match self {
121 TokenStream::Nightly(tts) => tts.fmt(f),
122 TokenStream::Stable(tts) => tts.fmt(f),
123 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700124 }
125}
126
127impl From<proc_macro::TokenStream> for TokenStream {
128 fn from(inner: proc_macro::TokenStream) -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700129 TokenStream::Nightly(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700130 }
131}
132
133impl From<TokenStream> for proc_macro::TokenStream {
134 fn from(inner: TokenStream) -> proc_macro::TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700135 match inner {
136 TokenStream::Nightly(inner) => inner,
137 TokenStream::Stable(inner) => inner.to_string().parse().unwrap(),
138 }
139 }
140}
141
142impl From<stable::TokenStream> for TokenStream {
143 fn from(inner: stable::TokenStream) -> TokenStream {
144 TokenStream::Stable(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700145 }
146}
147
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700148impl From<TokenTree> for TokenStream {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700149 fn from(token: TokenTree) -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700150 if !nightly_works() {
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700151 return TokenStream::Stable(token.into());
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700152 }
Alex Crichton9cd80a62018-04-05 17:46:58 -0700153 let tt: proc_macro::TokenTree = match token {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700154 TokenTree::Group(tt) => {
155 let delim = match tt.delimiter() {
156 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
157 Delimiter::Bracket => proc_macro::Delimiter::Bracket,
158 Delimiter::Brace => proc_macro::Delimiter::Brace,
159 Delimiter::None => proc_macro::Delimiter::None,
160 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700161 let span = tt.span();
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700162 let mut group = proc_macro::Group::new(delim, tt.stream.inner.unwrap_nightly());
163 group.set_span(span.inner.unwrap_nightly());
Alex Crichton9cd80a62018-04-05 17:46:58 -0700164 group.into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700165 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700166 TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700167 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700168 Spacing::Joint => proc_macro::Spacing::Joint,
169 Spacing::Alone => proc_macro::Spacing::Alone,
170 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700171 let mut op = proc_macro::Punct::new(tt.as_char(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700172 op.set_span(tt.span().inner.unwrap_nightly());
Alex Crichton9cd80a62018-04-05 17:46:58 -0700173 op.into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700174 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700175 TokenTree::Ident(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700176 TokenTree::Literal(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700177 };
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700178 TokenStream::Nightly(tt.into())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700179 }
180}
181
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700182impl iter::FromIterator<TokenTree> for TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700183 fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self {
184 if nightly_works() {
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700185 let trees = trees
186 .into_iter()
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700187 .map(TokenStream::from)
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700188 .flat_map(|t| match t {
189 TokenStream::Nightly(s) => s,
190 TokenStream::Stable(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700191 });
192 TokenStream::Nightly(trees.collect())
193 } else {
194 TokenStream::Stable(trees.into_iter().collect())
195 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700196 }
197}
198
Alex Crichtonf3888432018-05-16 09:11:05 -0700199impl Extend<TokenTree> for TokenStream {
200 fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
201 match self {
202 TokenStream::Nightly(tts) => {
David Tolnay40d4ffd2018-08-12 13:49:09 -0700203 tts.extend(
204 streams
205 .into_iter()
206 .map(|t| TokenStream::from(t).unwrap_nightly()),
207 );
Alex Crichtonf3888432018-05-16 09:11:05 -0700208 }
209 TokenStream::Stable(tts) => tts.extend(streams),
210 }
211 }
212}
213
David Tolnay5c58c532018-08-13 11:33:51 -0700214impl Extend<TokenStream> for TokenStream {
215 fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
216 match self {
217 TokenStream::Nightly(tts) => {
218 tts.extend(streams.into_iter().map(|stream| stream.unwrap_nightly()))
219 }
220 TokenStream::Stable(tts) => {
221 tts.extend(streams.into_iter().map(|stream| stream.unwrap_stable()))
222 }
223 }
224 }
225}
226
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700227impl fmt::Debug for TokenStream {
228 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700229 match self {
230 TokenStream::Nightly(tts) => tts.fmt(f),
231 TokenStream::Stable(tts) => tts.fmt(f),
232 }
233 }
234}
235
236impl From<proc_macro::LexError> for LexError {
237 fn from(e: proc_macro::LexError) -> LexError {
238 LexError::Nightly(e)
239 }
240}
241
242impl From<stable::LexError> for LexError {
243 fn from(e: stable::LexError) -> LexError {
244 LexError::Stable(e)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700245 }
246}
247
248impl fmt::Debug for LexError {
249 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700250 match self {
251 LexError::Nightly(e) => e.fmt(f),
252 LexError::Stable(e) => e.fmt(f),
253 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700254 }
255}
256
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700257pub enum TokenTreeIter {
258 Nightly(proc_macro::token_stream::IntoIter),
259 Stable(stable::TokenTreeIter),
260}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700261
262impl IntoIterator for TokenStream {
263 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700264 type IntoIter = TokenTreeIter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700265
Alex Crichton1a7f7622017-07-05 17:47:15 -0700266 fn into_iter(self) -> TokenTreeIter {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700267 match self {
268 TokenStream::Nightly(tts) => TokenTreeIter::Nightly(tts.into_iter()),
269 TokenStream::Stable(tts) => TokenTreeIter::Stable(tts.into_iter()),
270 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700271 }
272}
273
Alex Crichton1a7f7622017-07-05 17:47:15 -0700274impl Iterator for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700275 type Item = TokenTree;
276
277 fn next(&mut self) -> Option<TokenTree> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700278 let token = match self {
279 TokenTreeIter::Nightly(iter) => iter.next()?,
280 TokenTreeIter::Stable(iter) => return iter.next(),
281 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700282 Some(match token {
283 proc_macro::TokenTree::Group(tt) => {
284 let delim = match tt.delimiter() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700285 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
286 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
287 proc_macro::Delimiter::Brace => Delimiter::Brace,
288 proc_macro::Delimiter::None => Delimiter::None,
289 };
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700290 let stream = ::TokenStream::_new(TokenStream::Nightly(tt.stream()));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700291 let mut g = Group::new(delim, stream);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700292 g.set_span(::Span::_new(Span::Nightly(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700293 g.into()
294 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700295 proc_macro::TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700296 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700297 proc_macro::Spacing::Joint => Spacing::Joint,
298 proc_macro::Spacing::Alone => Spacing::Alone,
299 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700300 let mut o = Punct::new(tt.as_char(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700301 o.set_span(::Span::_new(Span::Nightly(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700302 o.into()
303 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700304 proc_macro::TokenTree::Ident(s) => ::Ident::_new(Ident::Nightly(s)).into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700305 proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Nightly(l)).into(),
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700306 })
307 }
308
309 fn size_hint(&self) -> (usize, Option<usize>) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700310 match self {
311 TokenTreeIter::Nightly(tts) => tts.size_hint(),
312 TokenTreeIter::Stable(tts) => tts.size_hint(),
313 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700314 }
315}
316
Alex Crichton1a7f7622017-07-05 17:47:15 -0700317impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700318 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700319 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700320 }
321}
322
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700323pub use stable::FileName;
Nika Layzellb35a9a32017-12-30 14:34:35 -0500324
325// NOTE: We have to generate our own filename object here because we can't wrap
326// the one provided by proc_macro.
327#[derive(Clone, PartialEq, Eq)]
Alex Crichtonce0904d2018-08-27 17:29:49 -0700328#[cfg(super_unstable)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700329pub enum SourceFile {
330 Nightly(proc_macro::SourceFile, FileName),
331 Stable(stable::SourceFile),
332}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500333
Alex Crichtonce0904d2018-08-27 17:29:49 -0700334#[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500335impl SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700336 fn nightly(sf: proc_macro::SourceFile) -> Self {
Alex Crichton9f0a28a2018-07-21 18:53:35 -0700337 let filename = stable::file_name(sf.path().display().to_string());
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700338 SourceFile::Nightly(sf, filename)
Nika Layzellb35a9a32017-12-30 14:34:35 -0500339 }
340
Nika Layzellf8d5f212017-12-11 14:07:02 -0500341 /// Get the path to this source file as a string.
Nika Layzellb35a9a32017-12-30 14:34:35 -0500342 pub fn path(&self) -> &FileName {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700343 match self {
344 SourceFile::Nightly(_, f) => f,
345 SourceFile::Stable(a) => a.path(),
346 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500347 }
348
349 pub fn is_real(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700350 match self {
351 SourceFile::Nightly(a, _) => a.is_real(),
352 SourceFile::Stable(a) => a.is_real(),
353 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500354 }
355}
356
Alex Crichtonce0904d2018-08-27 17:29:49 -0700357#[cfg(super_unstable)]
Nika Layzellb35a9a32017-12-30 14:34:35 -0500358impl AsRef<FileName> for SourceFile {
359 fn as_ref(&self) -> &FileName {
360 self.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500361 }
362}
363
Alex Crichtonce0904d2018-08-27 17:29:49 -0700364#[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500365impl fmt::Debug for SourceFile {
366 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700367 match self {
368 SourceFile::Nightly(a, _) => a.fmt(f),
369 SourceFile::Stable(a) => a.fmt(f),
370 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500371 }
372}
373
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500374pub struct LineColumn {
375 pub line: usize,
376 pub column: usize,
377}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500378
Alex Crichton9cd80a62018-04-05 17:46:58 -0700379#[derive(Copy, Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700380pub enum Span {
381 Nightly(proc_macro::Span),
382 Stable(stable::Span),
Sergio Benitez13805082018-01-04 01:25:45 -0800383}
384
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700385impl Span {
386 pub fn call_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700387 if nightly_works() {
388 Span::Nightly(proc_macro::Span::call_site())
389 } else {
390 Span::Stable(stable::Span::call_site())
391 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700392 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700393
Alex Crichtonce0904d2018-08-27 17:29:49 -0700394 #[cfg(super_unstable)]
Alex Crichtone6085b72017-11-21 07:24:25 -0800395 pub fn def_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700396 if nightly_works() {
397 Span::Nightly(proc_macro::Span::def_site())
398 } else {
399 Span::Stable(stable::Span::def_site())
400 }
Alex Crichton998f6422017-11-19 08:06:27 -0800401 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500402
Alex Crichtonce0904d2018-08-27 17:29:49 -0700403 #[cfg(super_unstable)]
David Tolnay4e8e3972018-01-05 18:10:22 -0800404 pub fn resolved_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700405 match (self, other) {
406 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.resolved_at(b)),
407 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.resolved_at(b)),
408 _ => mismatch(),
409 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800410 }
411
Alex Crichtonce0904d2018-08-27 17:29:49 -0700412 #[cfg(super_unstable)]
David Tolnay4e8e3972018-01-05 18:10:22 -0800413 pub fn located_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700414 match (self, other) {
415 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.located_at(b)),
416 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.located_at(b)),
417 _ => mismatch(),
418 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800419 }
420
David Tolnay16a17202017-12-31 10:47:24 -0500421 pub fn unstable(self) -> proc_macro::Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700422 match self {
423 Span::Nightly(s) => s,
424 Span::Stable(_) => mismatch(),
425 }
David Tolnay16a17202017-12-31 10:47:24 -0500426 }
427
Alex Crichtonce0904d2018-08-27 17:29:49 -0700428 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500429 pub fn source_file(&self) -> SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700430 match self {
431 Span::Nightly(s) => SourceFile::nightly(s.source_file()),
432 Span::Stable(s) => SourceFile::Stable(s.source_file()),
433 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500434 }
435
Alex Crichtonce0904d2018-08-27 17:29:49 -0700436 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500437 pub fn start(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700438 match self {
439 Span::Nightly(s) => {
440 let proc_macro::LineColumn { line, column } = s.start();
441 LineColumn { line, column }
442 }
443 Span::Stable(s) => {
444 let stable::LineColumn { line, column } = s.start();
445 LineColumn { line, column }
446 }
447 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500448 }
449
Alex Crichtonce0904d2018-08-27 17:29:49 -0700450 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500451 pub fn end(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700452 match self {
453 Span::Nightly(s) => {
454 let proc_macro::LineColumn { line, column } = s.end();
455 LineColumn { line, column }
456 }
457 Span::Stable(s) => {
458 let stable::LineColumn { line, column } = s.end();
459 LineColumn { line, column }
460 }
461 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500462 }
463
Alex Crichtonce0904d2018-08-27 17:29:49 -0700464 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500465 pub fn join(&self, other: Span) -> Option<Span> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700466 let ret = match (self, other) {
467 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.join(b)?),
468 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.join(b)?),
469 _ => return None,
470 };
471 Some(ret)
Nika Layzellf8d5f212017-12-11 14:07:02 -0500472 }
Alex Crichtone24f7342018-04-05 17:58:11 -0700473
Alex Crichtonce0904d2018-08-27 17:29:49 -0700474 #[cfg(super_unstable)]
Alex Crichtone24f7342018-04-05 17:58:11 -0700475 pub fn eq(&self, other: &Span) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700476 match (self, other) {
477 (Span::Nightly(a), Span::Nightly(b)) => a.eq(b),
478 (Span::Stable(a), Span::Stable(b)) => a.eq(b),
479 _ => false,
480 }
481 }
482
483 fn unwrap_nightly(self) -> proc_macro::Span {
484 match self {
485 Span::Nightly(s) => s,
486 Span::Stable(_) => mismatch(),
487 }
488 }
489}
490
491impl From<proc_macro::Span> for ::Span {
492 fn from(proc_span: proc_macro::Span) -> ::Span {
493 ::Span::_new(Span::Nightly(proc_span))
494 }
495}
496
497impl From<stable::Span> for Span {
498 fn from(inner: stable::Span) -> Span {
499 Span::Stable(inner)
Alex Crichtone24f7342018-04-05 17:58:11 -0700500 }
Alex Crichton998f6422017-11-19 08:06:27 -0800501}
502
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700503impl fmt::Debug for Span {
504 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700505 match self {
506 Span::Nightly(s) => s.fmt(f),
507 Span::Stable(s) => s.fmt(f),
508 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700509 }
510}
511
Alex Crichtonf3888432018-05-16 09:11:05 -0700512#[derive(Clone)]
513pub enum Ident {
514 Nightly(proc_macro::Ident),
515 Stable(stable::Ident),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700516}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700517
Alex Crichtonf3888432018-05-16 09:11:05 -0700518impl Ident {
519 pub fn new(string: &str, span: Span) -> Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700520 match span {
Alex Crichtonf3888432018-05-16 09:11:05 -0700521 Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new(string, s)),
522 Span::Stable(s) => Ident::Stable(stable::Ident::new(string, s)),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700523 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700524 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700525
Alex Crichtonf3888432018-05-16 09:11:05 -0700526 pub fn new_raw(string: &str, span: Span) -> Ident {
527 match span {
Alex Crichtonce0904d2018-08-27 17:29:49 -0700528 Span::Nightly(s) => {
529 let p: proc_macro::TokenStream = string.parse().unwrap();
530 let ident = match p.into_iter().next() {
531 Some(proc_macro::TokenTree::Ident(mut i)) => {
532 i.set_span(s);
533 i
534 }
535 _ => panic!(),
536 };
537 Ident::Nightly(ident)
538 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700539 Span::Stable(s) => Ident::Stable(stable::Ident::new_raw(string, s)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700540 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700541 }
542
543 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700544 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700545 Ident::Nightly(t) => Span::Nightly(t.span()),
546 Ident::Stable(t) => Span::Stable(t.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700547 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700548 }
549
550 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700551 match (self, span) {
Alex Crichtonf3888432018-05-16 09:11:05 -0700552 (Ident::Nightly(t), Span::Nightly(s)) => t.set_span(s),
553 (Ident::Stable(t), Span::Stable(s)) => t.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700554 _ => mismatch(),
555 }
556 }
557
Alex Crichtonf3888432018-05-16 09:11:05 -0700558 fn unwrap_nightly(self) -> proc_macro::Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700559 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700560 Ident::Nightly(s) => s,
561 Ident::Stable(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700562 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700563 }
564}
565
Alex Crichtonf3888432018-05-16 09:11:05 -0700566impl fmt::Display for Ident {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700567 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700568 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700569 Ident::Nightly(t) => t.fmt(f),
570 Ident::Stable(t) => t.fmt(f),
571 }
572 }
573}
574
575impl fmt::Debug for Ident {
576 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
577 match self {
578 Ident::Nightly(t) => t.fmt(f),
579 Ident::Stable(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700580 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700581 }
582}
583
584#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700585pub enum Literal {
586 Nightly(proc_macro::Literal),
587 Stable(stable::Literal),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700588}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700589
Alex Crichtona914a612018-04-04 07:48:44 -0700590macro_rules! suffixed_numbers {
591 ($($name:ident => $kind:ident,)*) => ($(
592 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700593 if nightly_works() {
594 Literal::Nightly(proc_macro::Literal::$name(n))
595 } else {
596 Literal::Stable(stable::Literal::$name(n))
597 }
Alex Crichtona914a612018-04-04 07:48:44 -0700598 }
599 )*)
600}
601
602macro_rules! unsuffixed_integers {
603 ($($name:ident => $kind:ident,)*) => ($(
604 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700605 if nightly_works() {
606 Literal::Nightly(proc_macro::Literal::$name(n))
607 } else {
608 Literal::Stable(stable::Literal::$name(n))
609 }
Alex Crichtona914a612018-04-04 07:48:44 -0700610 }
611 )*)
612}
613
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700614impl Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700615 suffixed_numbers! {
616 u8_suffixed => u8,
617 u16_suffixed => u16,
618 u32_suffixed => u32,
619 u64_suffixed => u64,
620 usize_suffixed => usize,
621 i8_suffixed => i8,
622 i16_suffixed => i16,
623 i32_suffixed => i32,
624 i64_suffixed => i64,
625 isize_suffixed => isize,
626
627 f32_suffixed => f32,
628 f64_suffixed => f64,
629 }
630
631 unsuffixed_integers! {
632 u8_unsuffixed => u8,
633 u16_unsuffixed => u16,
634 u32_unsuffixed => u32,
635 u64_unsuffixed => u64,
636 usize_unsuffixed => usize,
637 i8_unsuffixed => i8,
638 i16_unsuffixed => i16,
639 i32_unsuffixed => i32,
640 i64_unsuffixed => i64,
641 isize_unsuffixed => isize,
642 }
643
644 pub fn f32_unsuffixed(f: f32) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700645 if nightly_works() {
646 Literal::Nightly(proc_macro::Literal::f32_unsuffixed(f))
647 } else {
648 Literal::Stable(stable::Literal::f32_unsuffixed(f))
649 }
Alex Crichtona914a612018-04-04 07:48:44 -0700650 }
651
652 pub fn f64_unsuffixed(f: f64) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700653 if nightly_works() {
654 Literal::Nightly(proc_macro::Literal::f64_unsuffixed(f))
655 } else {
656 Literal::Stable(stable::Literal::f64_unsuffixed(f))
657 }
Alex Crichtona914a612018-04-04 07:48:44 -0700658 }
659
Alex Crichtona914a612018-04-04 07:48:44 -0700660 pub fn string(t: &str) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700661 if nightly_works() {
662 Literal::Nightly(proc_macro::Literal::string(t))
663 } else {
664 Literal::Stable(stable::Literal::string(t))
665 }
Alex Crichtona914a612018-04-04 07:48:44 -0700666 }
667
668 pub fn character(t: char) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700669 if nightly_works() {
670 Literal::Nightly(proc_macro::Literal::character(t))
671 } else {
672 Literal::Stable(stable::Literal::character(t))
673 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700674 }
675
676 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700677 if nightly_works() {
678 Literal::Nightly(proc_macro::Literal::byte_string(bytes))
679 } else {
680 Literal::Stable(stable::Literal::byte_string(bytes))
681 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700682 }
683
Alex Crichtonb2c94622018-04-04 07:36:41 -0700684 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700685 match self {
686 Literal::Nightly(lit) => Span::Nightly(lit.span()),
687 Literal::Stable(lit) => Span::Stable(lit.span()),
688 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700689 }
690
Alex Crichtonb2c94622018-04-04 07:36:41 -0700691 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700692 match (self, span) {
693 (Literal::Nightly(lit), Span::Nightly(s)) => lit.set_span(s),
694 (Literal::Stable(lit), Span::Stable(s)) => lit.set_span(s),
695 _ => mismatch(),
696 }
697 }
698
699 fn unwrap_nightly(self) -> proc_macro::Literal {
700 match self {
701 Literal::Nightly(s) => s,
702 Literal::Stable(_) => mismatch(),
703 }
704 }
705}
706
707impl From<stable::Literal> for Literal {
708 fn from(s: stable::Literal) -> Literal {
709 Literal::Stable(s)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700710 }
711}
712
713impl fmt::Display for Literal {
714 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700715 match self {
716 Literal::Nightly(t) => t.fmt(f),
717 Literal::Stable(t) => t.fmt(f),
718 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700719 }
720}
721
722impl fmt::Debug for Literal {
723 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700724 match self {
725 Literal::Nightly(t) => t.fmt(f),
726 Literal::Stable(t) => t.fmt(f),
727 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700728 }
729}