blob: e024205aa7549c21a0bf116468627c4139493c43 [file] [log] [blame]
Alex Crichtona914a612018-04-04 07:48:44 -07001#![cfg_attr(not(procmacro2_semver_exempt), 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::*;
26 static WORKS: AtomicUsize = ATOMIC_USIZE_INIT;
27
28 match WORKS.load(Ordering::SeqCst) {
29 1 => return false,
30 2 => return true,
31 _ => {}
32 }
33 let works = panic::catch_unwind(|| proc_macro::Span::call_site()).is_ok();
34 WORKS.store(works as usize + 1, Ordering::SeqCst);
35 works
36}
37
38fn mismatch() -> ! {
39 panic!("stable/nightly mismatch")
40}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070041
42impl TokenStream {
David Tolnayc3bb4592018-05-28 20:09:44 -070043 pub fn new() -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070044 if nightly_works() {
David Tolnayc3bb4592018-05-28 20:09:44 -070045 TokenStream::Nightly(proc_macro::TokenStream::new())
Alex Crichton30a4e9e2018-04-27 17:02:19 -070046 } else {
David Tolnayc3bb4592018-05-28 20:09:44 -070047 TokenStream::Stable(stable::TokenStream::new())
Alex Crichton30a4e9e2018-04-27 17:02:19 -070048 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070049 }
50
51 pub fn is_empty(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070052 match self {
53 TokenStream::Nightly(tts) => tts.is_empty(),
54 TokenStream::Stable(tts) => tts.is_empty(),
55 }
56 }
57
58 fn unwrap_nightly(self) -> proc_macro::TokenStream {
59 match self {
60 TokenStream::Nightly(s) => s,
61 TokenStream::Stable(_) => mismatch(),
62 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070063 }
64}
65
66impl FromStr for TokenStream {
67 type Err = LexError;
68
69 fn from_str(src: &str) -> Result<TokenStream, LexError> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070070 if nightly_works() {
71 Ok(TokenStream::Nightly(src.parse()?))
72 } else {
73 Ok(TokenStream::Stable(src.parse()?))
74 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070075 }
76}
77
78impl fmt::Display for TokenStream {
79 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070080 match self {
81 TokenStream::Nightly(tts) => tts.fmt(f),
82 TokenStream::Stable(tts) => tts.fmt(f),
83 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070084 }
85}
86
87impl From<proc_macro::TokenStream> for TokenStream {
88 fn from(inner: proc_macro::TokenStream) -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070089 TokenStream::Nightly(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070090 }
91}
92
93impl From<TokenStream> for proc_macro::TokenStream {
94 fn from(inner: TokenStream) -> proc_macro::TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070095 match inner {
96 TokenStream::Nightly(inner) => inner,
97 TokenStream::Stable(inner) => inner.to_string().parse().unwrap(),
98 }
99 }
100}
101
102impl From<stable::TokenStream> for TokenStream {
103 fn from(inner: stable::TokenStream) -> TokenStream {
104 TokenStream::Stable(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700105 }
106}
107
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700108impl From<TokenTree> for TokenStream {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700109 fn from(token: TokenTree) -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700110 if !nightly_works() {
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700111 return TokenStream::Stable(token.into());
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700112 }
Alex Crichton9cd80a62018-04-05 17:46:58 -0700113 let tt: proc_macro::TokenTree = match token {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700114 TokenTree::Group(tt) => {
115 let delim = match tt.delimiter() {
116 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
117 Delimiter::Bracket => proc_macro::Delimiter::Bracket,
118 Delimiter::Brace => proc_macro::Delimiter::Brace,
119 Delimiter::None => proc_macro::Delimiter::None,
120 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700121 let span = tt.span();
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700122 let mut group = proc_macro::Group::new(delim, tt.stream.inner.unwrap_nightly());
123 group.set_span(span.inner.unwrap_nightly());
Alex Crichton9cd80a62018-04-05 17:46:58 -0700124 group.into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700125 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700126 TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700127 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700128 Spacing::Joint => proc_macro::Spacing::Joint,
129 Spacing::Alone => proc_macro::Spacing::Alone,
130 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700131 let mut op = proc_macro::Punct::new(tt.as_char(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700132 op.set_span(tt.span().inner.unwrap_nightly());
Alex Crichton9cd80a62018-04-05 17:46:58 -0700133 op.into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700134 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700135 TokenTree::Ident(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700136 TokenTree::Literal(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700137 };
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700138 TokenStream::Nightly(tt.into())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700139 }
140}
141
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700142impl iter::FromIterator<TokenTree> for TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700143 fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self {
144 if nightly_works() {
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700145 let trees = trees
146 .into_iter()
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700147 .map(TokenStream::from)
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700148 .flat_map(|t| match t {
149 TokenStream::Nightly(s) => s,
150 TokenStream::Stable(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700151 });
152 TokenStream::Nightly(trees.collect())
153 } else {
154 TokenStream::Stable(trees.into_iter().collect())
155 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700156 }
157}
158
Alex Crichtonf3888432018-05-16 09:11:05 -0700159impl Extend<TokenTree> for TokenStream {
160 fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
161 match self {
162 TokenStream::Nightly(tts) => {
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700163 *tts = tts
164 .clone()
Alex Crichtonf3888432018-05-16 09:11:05 -0700165 .into_iter()
166 .chain(
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700167 streams
168 .into_iter()
Alex Crichtonf3888432018-05-16 09:11:05 -0700169 .map(TokenStream::from)
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700170 .flat_map(|t| match t {
171 TokenStream::Nightly(tts) => tts.into_iter(),
172 _ => panic!(),
173 }),
David Tolnay5a556cf2018-08-12 13:49:39 -0700174 ).collect();
Alex Crichtonf3888432018-05-16 09:11:05 -0700175 }
176 TokenStream::Stable(tts) => tts.extend(streams),
177 }
178 }
179}
180
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700181impl fmt::Debug for TokenStream {
182 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700183 match self {
184 TokenStream::Nightly(tts) => tts.fmt(f),
185 TokenStream::Stable(tts) => tts.fmt(f),
186 }
187 }
188}
189
190impl From<proc_macro::LexError> for LexError {
191 fn from(e: proc_macro::LexError) -> LexError {
192 LexError::Nightly(e)
193 }
194}
195
196impl From<stable::LexError> for LexError {
197 fn from(e: stable::LexError) -> LexError {
198 LexError::Stable(e)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700199 }
200}
201
202impl fmt::Debug for LexError {
203 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700204 match self {
205 LexError::Nightly(e) => e.fmt(f),
206 LexError::Stable(e) => e.fmt(f),
207 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700208 }
209}
210
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700211pub enum TokenTreeIter {
212 Nightly(proc_macro::token_stream::IntoIter),
213 Stable(stable::TokenTreeIter),
214}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700215
216impl IntoIterator for TokenStream {
217 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700218 type IntoIter = TokenTreeIter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700219
Alex Crichton1a7f7622017-07-05 17:47:15 -0700220 fn into_iter(self) -> TokenTreeIter {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700221 match self {
222 TokenStream::Nightly(tts) => TokenTreeIter::Nightly(tts.into_iter()),
223 TokenStream::Stable(tts) => TokenTreeIter::Stable(tts.into_iter()),
224 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700225 }
226}
227
Alex Crichton1a7f7622017-07-05 17:47:15 -0700228impl Iterator for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700229 type Item = TokenTree;
230
231 fn next(&mut self) -> Option<TokenTree> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700232 let token = match self {
233 TokenTreeIter::Nightly(iter) => iter.next()?,
234 TokenTreeIter::Stable(iter) => return iter.next(),
235 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700236 Some(match token {
237 proc_macro::TokenTree::Group(tt) => {
238 let delim = match tt.delimiter() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700239 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
240 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
241 proc_macro::Delimiter::Brace => Delimiter::Brace,
242 proc_macro::Delimiter::None => Delimiter::None,
243 };
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700244 let stream = ::TokenStream::_new(TokenStream::Nightly(tt.stream()));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700245 let mut g = Group::new(delim, stream);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700246 g.set_span(::Span::_new(Span::Nightly(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700247 g.into()
248 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700249 proc_macro::TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700250 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700251 proc_macro::Spacing::Joint => Spacing::Joint,
252 proc_macro::Spacing::Alone => Spacing::Alone,
253 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700254 let mut o = Punct::new(tt.as_char(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700255 o.set_span(::Span::_new(Span::Nightly(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700256 o.into()
257 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700258 proc_macro::TokenTree::Ident(s) => ::Ident::_new(Ident::Nightly(s)).into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700259 proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Nightly(l)).into(),
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700260 })
261 }
262
263 fn size_hint(&self) -> (usize, Option<usize>) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700264 match self {
265 TokenTreeIter::Nightly(tts) => tts.size_hint(),
266 TokenTreeIter::Stable(tts) => tts.size_hint(),
267 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700268 }
269}
270
Alex Crichton1a7f7622017-07-05 17:47:15 -0700271impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700272 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700273 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700274 }
275}
276
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700277pub use stable::FileName;
Nika Layzellb35a9a32017-12-30 14:34:35 -0500278
279// NOTE: We have to generate our own filename object here because we can't wrap
280// the one provided by proc_macro.
281#[derive(Clone, PartialEq, Eq)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700282pub enum SourceFile {
283 Nightly(proc_macro::SourceFile, FileName),
284 Stable(stable::SourceFile),
285}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500286
287impl SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700288 fn nightly(sf: proc_macro::SourceFile) -> Self {
Alex Crichton9f0a28a2018-07-21 18:53:35 -0700289 let filename = stable::file_name(sf.path().display().to_string());
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700290 SourceFile::Nightly(sf, filename)
Nika Layzellb35a9a32017-12-30 14:34:35 -0500291 }
292
Nika Layzellf8d5f212017-12-11 14:07:02 -0500293 /// Get the path to this source file as a string.
Nika Layzellb35a9a32017-12-30 14:34:35 -0500294 pub fn path(&self) -> &FileName {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700295 match self {
296 SourceFile::Nightly(_, f) => f,
297 SourceFile::Stable(a) => a.path(),
298 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500299 }
300
301 pub fn is_real(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700302 match self {
303 SourceFile::Nightly(a, _) => a.is_real(),
304 SourceFile::Stable(a) => a.is_real(),
305 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500306 }
307}
308
Nika Layzellb35a9a32017-12-30 14:34:35 -0500309impl AsRef<FileName> for SourceFile {
310 fn as_ref(&self) -> &FileName {
311 self.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500312 }
313}
314
315impl fmt::Debug for SourceFile {
316 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700317 match self {
318 SourceFile::Nightly(a, _) => a.fmt(f),
319 SourceFile::Stable(a) => a.fmt(f),
320 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500321 }
322}
323
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500324pub struct LineColumn {
325 pub line: usize,
326 pub column: usize,
327}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500328
Alex Crichton9cd80a62018-04-05 17:46:58 -0700329#[derive(Copy, Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700330pub enum Span {
331 Nightly(proc_macro::Span),
332 Stable(stable::Span),
Sergio Benitez13805082018-01-04 01:25:45 -0800333}
334
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700335impl Span {
336 pub fn call_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700337 if nightly_works() {
338 Span::Nightly(proc_macro::Span::call_site())
339 } else {
340 Span::Stable(stable::Span::call_site())
341 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700342 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700343
Alex Crichtone6085b72017-11-21 07:24:25 -0800344 pub fn def_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700345 if nightly_works() {
346 Span::Nightly(proc_macro::Span::def_site())
347 } else {
348 Span::Stable(stable::Span::def_site())
349 }
Alex Crichton998f6422017-11-19 08:06:27 -0800350 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500351
David Tolnay4e8e3972018-01-05 18:10:22 -0800352 pub fn resolved_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700353 match (self, other) {
354 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.resolved_at(b)),
355 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.resolved_at(b)),
356 _ => mismatch(),
357 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800358 }
359
360 pub fn located_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700361 match (self, other) {
362 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.located_at(b)),
363 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.located_at(b)),
364 _ => mismatch(),
365 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800366 }
367
David Tolnay16a17202017-12-31 10:47:24 -0500368 pub fn unstable(self) -> proc_macro::Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700369 match self {
370 Span::Nightly(s) => s,
371 Span::Stable(_) => mismatch(),
372 }
David Tolnay16a17202017-12-31 10:47:24 -0500373 }
374
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700375 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500376 pub fn source_file(&self) -> SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700377 match self {
378 Span::Nightly(s) => SourceFile::nightly(s.source_file()),
379 Span::Stable(s) => SourceFile::Stable(s.source_file()),
380 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500381 }
382
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700383 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500384 pub fn start(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700385 match self {
386 Span::Nightly(s) => {
387 let proc_macro::LineColumn { line, column } = s.start();
388 LineColumn { line, column }
389 }
390 Span::Stable(s) => {
391 let stable::LineColumn { line, column } = s.start();
392 LineColumn { line, column }
393 }
394 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500395 }
396
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700397 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500398 pub fn end(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700399 match self {
400 Span::Nightly(s) => {
401 let proc_macro::LineColumn { line, column } = s.end();
402 LineColumn { line, column }
403 }
404 Span::Stable(s) => {
405 let stable::LineColumn { line, column } = s.end();
406 LineColumn { line, column }
407 }
408 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500409 }
410
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700411 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500412 pub fn join(&self, other: Span) -> Option<Span> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700413 let ret = match (self, other) {
414 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.join(b)?),
415 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.join(b)?),
416 _ => return None,
417 };
418 Some(ret)
Nika Layzellf8d5f212017-12-11 14:07:02 -0500419 }
Alex Crichtone24f7342018-04-05 17:58:11 -0700420
421 pub fn eq(&self, other: &Span) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700422 match (self, other) {
423 (Span::Nightly(a), Span::Nightly(b)) => a.eq(b),
424 (Span::Stable(a), Span::Stable(b)) => a.eq(b),
425 _ => false,
426 }
427 }
428
429 fn unwrap_nightly(self) -> proc_macro::Span {
430 match self {
431 Span::Nightly(s) => s,
432 Span::Stable(_) => mismatch(),
433 }
434 }
435}
436
437impl From<proc_macro::Span> for ::Span {
438 fn from(proc_span: proc_macro::Span) -> ::Span {
439 ::Span::_new(Span::Nightly(proc_span))
440 }
441}
442
443impl From<stable::Span> for Span {
444 fn from(inner: stable::Span) -> Span {
445 Span::Stable(inner)
Alex Crichtone24f7342018-04-05 17:58:11 -0700446 }
Alex Crichton998f6422017-11-19 08:06:27 -0800447}
448
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700449impl fmt::Debug for Span {
450 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700451 match self {
452 Span::Nightly(s) => s.fmt(f),
453 Span::Stable(s) => s.fmt(f),
454 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700455 }
456}
457
Alex Crichtonf3888432018-05-16 09:11:05 -0700458#[derive(Clone)]
459pub enum Ident {
460 Nightly(proc_macro::Ident),
461 Stable(stable::Ident),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700462}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700463
Alex Crichtonf3888432018-05-16 09:11:05 -0700464impl Ident {
465 pub fn new(string: &str, span: Span) -> Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700466 match span {
Alex Crichtonf3888432018-05-16 09:11:05 -0700467 Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new(string, s)),
468 Span::Stable(s) => Ident::Stable(stable::Ident::new(string, s)),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700469 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700470 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700471
Alex Crichtonf3888432018-05-16 09:11:05 -0700472 pub fn new_raw(string: &str, span: Span) -> Ident {
473 match span {
474 Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new_raw(string, s)),
475 Span::Stable(s) => Ident::Stable(stable::Ident::new_raw(string, s)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700476 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700477 }
478
479 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700480 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700481 Ident::Nightly(t) => Span::Nightly(t.span()),
482 Ident::Stable(t) => Span::Stable(t.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700483 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700484 }
485
486 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700487 match (self, span) {
Alex Crichtonf3888432018-05-16 09:11:05 -0700488 (Ident::Nightly(t), Span::Nightly(s)) => t.set_span(s),
489 (Ident::Stable(t), Span::Stable(s)) => t.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700490 _ => mismatch(),
491 }
492 }
493
Alex Crichtonf3888432018-05-16 09:11:05 -0700494 fn unwrap_nightly(self) -> proc_macro::Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700495 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700496 Ident::Nightly(s) => s,
497 Ident::Stable(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700498 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700499 }
500}
501
Alex Crichtonf3888432018-05-16 09:11:05 -0700502impl fmt::Display for Ident {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700503 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700504 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700505 Ident::Nightly(t) => t.fmt(f),
506 Ident::Stable(t) => t.fmt(f),
507 }
508 }
509}
510
511impl fmt::Debug for Ident {
512 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
513 match self {
514 Ident::Nightly(t) => t.fmt(f),
515 Ident::Stable(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700516 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700517 }
518}
519
520#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700521pub enum Literal {
522 Nightly(proc_macro::Literal),
523 Stable(stable::Literal),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700524}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700525
Alex Crichtona914a612018-04-04 07:48:44 -0700526macro_rules! suffixed_numbers {
527 ($($name:ident => $kind:ident,)*) => ($(
528 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700529 if nightly_works() {
530 Literal::Nightly(proc_macro::Literal::$name(n))
531 } else {
532 Literal::Stable(stable::Literal::$name(n))
533 }
Alex Crichtona914a612018-04-04 07:48:44 -0700534 }
535 )*)
536}
537
538macro_rules! unsuffixed_integers {
539 ($($name:ident => $kind:ident,)*) => ($(
540 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700541 if nightly_works() {
542 Literal::Nightly(proc_macro::Literal::$name(n))
543 } else {
544 Literal::Stable(stable::Literal::$name(n))
545 }
Alex Crichtona914a612018-04-04 07:48:44 -0700546 }
547 )*)
548}
549
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700550impl Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700551 suffixed_numbers! {
552 u8_suffixed => u8,
553 u16_suffixed => u16,
554 u32_suffixed => u32,
555 u64_suffixed => u64,
556 usize_suffixed => usize,
557 i8_suffixed => i8,
558 i16_suffixed => i16,
559 i32_suffixed => i32,
560 i64_suffixed => i64,
561 isize_suffixed => isize,
562
563 f32_suffixed => f32,
564 f64_suffixed => f64,
565 }
566
567 unsuffixed_integers! {
568 u8_unsuffixed => u8,
569 u16_unsuffixed => u16,
570 u32_unsuffixed => u32,
571 u64_unsuffixed => u64,
572 usize_unsuffixed => usize,
573 i8_unsuffixed => i8,
574 i16_unsuffixed => i16,
575 i32_unsuffixed => i32,
576 i64_unsuffixed => i64,
577 isize_unsuffixed => isize,
578 }
579
580 pub fn f32_unsuffixed(f: f32) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700581 if nightly_works() {
582 Literal::Nightly(proc_macro::Literal::f32_unsuffixed(f))
583 } else {
584 Literal::Stable(stable::Literal::f32_unsuffixed(f))
585 }
Alex Crichtona914a612018-04-04 07:48:44 -0700586 }
587
588 pub fn f64_unsuffixed(f: f64) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700589 if nightly_works() {
590 Literal::Nightly(proc_macro::Literal::f64_unsuffixed(f))
591 } else {
592 Literal::Stable(stable::Literal::f64_unsuffixed(f))
593 }
Alex Crichtona914a612018-04-04 07:48:44 -0700594 }
595
Alex Crichtona914a612018-04-04 07:48:44 -0700596 pub fn string(t: &str) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700597 if nightly_works() {
598 Literal::Nightly(proc_macro::Literal::string(t))
599 } else {
600 Literal::Stable(stable::Literal::string(t))
601 }
Alex Crichtona914a612018-04-04 07:48:44 -0700602 }
603
604 pub fn character(t: char) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700605 if nightly_works() {
606 Literal::Nightly(proc_macro::Literal::character(t))
607 } else {
608 Literal::Stable(stable::Literal::character(t))
609 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700610 }
611
612 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700613 if nightly_works() {
614 Literal::Nightly(proc_macro::Literal::byte_string(bytes))
615 } else {
616 Literal::Stable(stable::Literal::byte_string(bytes))
617 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700618 }
619
Alex Crichtonb2c94622018-04-04 07:36:41 -0700620 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700621 match self {
622 Literal::Nightly(lit) => Span::Nightly(lit.span()),
623 Literal::Stable(lit) => Span::Stable(lit.span()),
624 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700625 }
626
Alex Crichtonb2c94622018-04-04 07:36:41 -0700627 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700628 match (self, span) {
629 (Literal::Nightly(lit), Span::Nightly(s)) => lit.set_span(s),
630 (Literal::Stable(lit), Span::Stable(s)) => lit.set_span(s),
631 _ => mismatch(),
632 }
633 }
634
635 fn unwrap_nightly(self) -> proc_macro::Literal {
636 match self {
637 Literal::Nightly(s) => s,
638 Literal::Stable(_) => mismatch(),
639 }
640 }
641}
642
643impl From<stable::Literal> for Literal {
644 fn from(s: stable::Literal) -> Literal {
645 Literal::Stable(s)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700646 }
647}
648
649impl fmt::Display for Literal {
650 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700651 match self {
652 Literal::Nightly(t) => t.fmt(f),
653 Literal::Stable(t) => t.fmt(f),
654 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700655 }
656}
657
658impl fmt::Debug for Literal {
659 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700660 match self {
661 Literal::Nightly(t) => t.fmt(f),
662 Literal::Stable(t) => t.fmt(f),
663 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700664 }
665}