blob: 2ddc939a9076133efbc97058b85930636bd1bb02 [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 {
43 pub fn empty() -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070044 if nightly_works() {
45 TokenStream::Nightly(proc_macro::TokenStream::empty())
46 } else {
47 TokenStream::Stable(stable::TokenStream::empty())
48 }
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 }),
Alex Crichtonf3888432018-05-16 09:11:05 -0700174 )
175 .collect();
176 }
177 TokenStream::Stable(tts) => tts.extend(streams),
178 }
179 }
180}
181
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700182impl fmt::Debug for TokenStream {
183 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700184 match self {
185 TokenStream::Nightly(tts) => tts.fmt(f),
186 TokenStream::Stable(tts) => tts.fmt(f),
187 }
188 }
189}
190
191impl From<proc_macro::LexError> for LexError {
192 fn from(e: proc_macro::LexError) -> LexError {
193 LexError::Nightly(e)
194 }
195}
196
197impl From<stable::LexError> for LexError {
198 fn from(e: stable::LexError) -> LexError {
199 LexError::Stable(e)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700200 }
201}
202
203impl fmt::Debug for LexError {
204 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700205 match self {
206 LexError::Nightly(e) => e.fmt(f),
207 LexError::Stable(e) => e.fmt(f),
208 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700209 }
210}
211
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700212pub enum TokenTreeIter {
213 Nightly(proc_macro::token_stream::IntoIter),
214 Stable(stable::TokenTreeIter),
215}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700216
217impl IntoIterator for TokenStream {
218 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700219 type IntoIter = TokenTreeIter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700220
Alex Crichton1a7f7622017-07-05 17:47:15 -0700221 fn into_iter(self) -> TokenTreeIter {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700222 match self {
223 TokenStream::Nightly(tts) => TokenTreeIter::Nightly(tts.into_iter()),
224 TokenStream::Stable(tts) => TokenTreeIter::Stable(tts.into_iter()),
225 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700226 }
227}
228
Alex Crichton1a7f7622017-07-05 17:47:15 -0700229impl Iterator for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700230 type Item = TokenTree;
231
232 fn next(&mut self) -> Option<TokenTree> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700233 let token = match self {
234 TokenTreeIter::Nightly(iter) => iter.next()?,
235 TokenTreeIter::Stable(iter) => return iter.next(),
236 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700237 Some(match token {
238 proc_macro::TokenTree::Group(tt) => {
239 let delim = match tt.delimiter() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700240 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
241 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
242 proc_macro::Delimiter::Brace => Delimiter::Brace,
243 proc_macro::Delimiter::None => Delimiter::None,
244 };
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700245 let stream = ::TokenStream::_new(TokenStream::Nightly(tt.stream()));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700246 let mut g = Group::new(delim, stream);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700247 g.set_span(::Span::_new(Span::Nightly(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700248 g.into()
249 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700250 proc_macro::TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700251 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700252 proc_macro::Spacing::Joint => Spacing::Joint,
253 proc_macro::Spacing::Alone => Spacing::Alone,
254 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700255 let mut o = Punct::new(tt.as_char(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700256 o.set_span(::Span::_new(Span::Nightly(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700257 o.into()
258 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700259 proc_macro::TokenTree::Ident(s) => ::Ident::_new(Ident::Nightly(s)).into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700260 proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Nightly(l)).into(),
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700261 })
262 }
263
264 fn size_hint(&self) -> (usize, Option<usize>) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700265 match self {
266 TokenTreeIter::Nightly(tts) => tts.size_hint(),
267 TokenTreeIter::Stable(tts) => tts.size_hint(),
268 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700269 }
270}
271
Alex Crichton1a7f7622017-07-05 17:47:15 -0700272impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700273 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700274 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700275 }
276}
277
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700278pub use stable::FileName;
Nika Layzellb35a9a32017-12-30 14:34:35 -0500279
280// NOTE: We have to generate our own filename object here because we can't wrap
281// the one provided by proc_macro.
282#[derive(Clone, PartialEq, Eq)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700283pub enum SourceFile {
284 Nightly(proc_macro::SourceFile, FileName),
285 Stable(stable::SourceFile),
286}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500287
288impl SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700289 fn nightly(sf: proc_macro::SourceFile) -> Self {
290 let filename = stable::file_name(sf.path().to_string());
291 SourceFile::Nightly(sf, filename)
Nika Layzellb35a9a32017-12-30 14:34:35 -0500292 }
293
Nika Layzellf8d5f212017-12-11 14:07:02 -0500294 /// Get the path to this source file as a string.
Nika Layzellb35a9a32017-12-30 14:34:35 -0500295 pub fn path(&self) -> &FileName {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700296 match self {
297 SourceFile::Nightly(_, f) => f,
298 SourceFile::Stable(a) => a.path(),
299 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500300 }
301
302 pub fn is_real(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700303 match self {
304 SourceFile::Nightly(a, _) => a.is_real(),
305 SourceFile::Stable(a) => a.is_real(),
306 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500307 }
308}
309
Nika Layzellb35a9a32017-12-30 14:34:35 -0500310impl AsRef<FileName> for SourceFile {
311 fn as_ref(&self) -> &FileName {
312 self.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500313 }
314}
315
316impl fmt::Debug for SourceFile {
317 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700318 match self {
319 SourceFile::Nightly(a, _) => a.fmt(f),
320 SourceFile::Stable(a) => a.fmt(f),
321 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500322 }
323}
324
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500325pub struct LineColumn {
326 pub line: usize,
327 pub column: usize,
328}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500329
Alex Crichton9cd80a62018-04-05 17:46:58 -0700330#[derive(Copy, Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700331pub enum Span {
332 Nightly(proc_macro::Span),
333 Stable(stable::Span),
Sergio Benitez13805082018-01-04 01:25:45 -0800334}
335
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700336impl Span {
337 pub fn call_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700338 if nightly_works() {
339 Span::Nightly(proc_macro::Span::call_site())
340 } else {
341 Span::Stable(stable::Span::call_site())
342 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700343 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700344
Alex Crichtone6085b72017-11-21 07:24:25 -0800345 pub fn def_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700346 if nightly_works() {
347 Span::Nightly(proc_macro::Span::def_site())
348 } else {
349 Span::Stable(stable::Span::def_site())
350 }
Alex Crichton998f6422017-11-19 08:06:27 -0800351 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500352
David Tolnay4e8e3972018-01-05 18:10:22 -0800353 pub fn resolved_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700354 match (self, other) {
355 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.resolved_at(b)),
356 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.resolved_at(b)),
357 _ => mismatch(),
358 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800359 }
360
361 pub fn located_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700362 match (self, other) {
363 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.located_at(b)),
364 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.located_at(b)),
365 _ => mismatch(),
366 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800367 }
368
David Tolnay16a17202017-12-31 10:47:24 -0500369 pub fn unstable(self) -> proc_macro::Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700370 match self {
371 Span::Nightly(s) => s,
372 Span::Stable(_) => mismatch(),
373 }
David Tolnay16a17202017-12-31 10:47:24 -0500374 }
375
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700376 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500377 pub fn source_file(&self) -> SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700378 match self {
379 Span::Nightly(s) => SourceFile::nightly(s.source_file()),
380 Span::Stable(s) => SourceFile::Stable(s.source_file()),
381 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500382 }
383
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700384 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500385 pub fn start(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700386 match self {
387 Span::Nightly(s) => {
388 let proc_macro::LineColumn { line, column } = s.start();
389 LineColumn { line, column }
390 }
391 Span::Stable(s) => {
392 let stable::LineColumn { line, column } = s.start();
393 LineColumn { line, column }
394 }
395 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500396 }
397
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700398 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500399 pub fn end(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700400 match self {
401 Span::Nightly(s) => {
402 let proc_macro::LineColumn { line, column } = s.end();
403 LineColumn { line, column }
404 }
405 Span::Stable(s) => {
406 let stable::LineColumn { line, column } = s.end();
407 LineColumn { line, column }
408 }
409 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500410 }
411
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700412 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500413 pub fn join(&self, other: Span) -> Option<Span> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700414 let ret = match (self, other) {
415 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.join(b)?),
416 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.join(b)?),
417 _ => return None,
418 };
419 Some(ret)
Nika Layzellf8d5f212017-12-11 14:07:02 -0500420 }
Alex Crichtone24f7342018-04-05 17:58:11 -0700421
422 pub fn eq(&self, other: &Span) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700423 match (self, other) {
424 (Span::Nightly(a), Span::Nightly(b)) => a.eq(b),
425 (Span::Stable(a), Span::Stable(b)) => a.eq(b),
426 _ => false,
427 }
428 }
429
430 fn unwrap_nightly(self) -> proc_macro::Span {
431 match self {
432 Span::Nightly(s) => s,
433 Span::Stable(_) => mismatch(),
434 }
435 }
436}
437
438impl From<proc_macro::Span> for ::Span {
439 fn from(proc_span: proc_macro::Span) -> ::Span {
440 ::Span::_new(Span::Nightly(proc_span))
441 }
442}
443
444impl From<stable::Span> for Span {
445 fn from(inner: stable::Span) -> Span {
446 Span::Stable(inner)
Alex Crichtone24f7342018-04-05 17:58:11 -0700447 }
Alex Crichton998f6422017-11-19 08:06:27 -0800448}
449
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700450impl fmt::Debug for Span {
451 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700452 match self {
453 Span::Nightly(s) => s.fmt(f),
454 Span::Stable(s) => s.fmt(f),
455 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700456 }
457}
458
Alex Crichtonf3888432018-05-16 09:11:05 -0700459#[derive(Clone)]
460pub enum Ident {
461 Nightly(proc_macro::Ident),
462 Stable(stable::Ident),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700463}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700464
Alex Crichtonf3888432018-05-16 09:11:05 -0700465impl Ident {
466 pub fn new(string: &str, span: Span) -> Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700467 match span {
Alex Crichtonf3888432018-05-16 09:11:05 -0700468 Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new(string, s)),
469 Span::Stable(s) => Ident::Stable(stable::Ident::new(string, s)),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700470 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700471 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700472
Alex Crichtonf3888432018-05-16 09:11:05 -0700473 pub fn new_raw(string: &str, span: Span) -> Ident {
474 match span {
475 Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new_raw(string, s)),
476 Span::Stable(s) => Ident::Stable(stable::Ident::new_raw(string, s)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700477 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700478 }
479
480 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700481 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700482 Ident::Nightly(t) => Span::Nightly(t.span()),
483 Ident::Stable(t) => Span::Stable(t.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700484 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700485 }
486
487 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700488 match (self, span) {
Alex Crichtonf3888432018-05-16 09:11:05 -0700489 (Ident::Nightly(t), Span::Nightly(s)) => t.set_span(s),
490 (Ident::Stable(t), Span::Stable(s)) => t.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700491 _ => mismatch(),
492 }
493 }
494
Alex Crichtonf3888432018-05-16 09:11:05 -0700495 fn unwrap_nightly(self) -> proc_macro::Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700496 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700497 Ident::Nightly(s) => s,
498 Ident::Stable(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700499 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700500 }
501}
502
Alex Crichtonf3888432018-05-16 09:11:05 -0700503impl fmt::Display for Ident {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700504 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700505 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700506 Ident::Nightly(t) => t.fmt(f),
507 Ident::Stable(t) => t.fmt(f),
508 }
509 }
510}
511
512impl fmt::Debug for Ident {
513 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
514 match self {
515 Ident::Nightly(t) => t.fmt(f),
516 Ident::Stable(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700517 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700518 }
519}
520
521#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700522pub enum Literal {
523 Nightly(proc_macro::Literal),
524 Stable(stable::Literal),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700525}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700526
Alex Crichtona914a612018-04-04 07:48:44 -0700527macro_rules! suffixed_numbers {
528 ($($name:ident => $kind:ident,)*) => ($(
529 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700530 if nightly_works() {
531 Literal::Nightly(proc_macro::Literal::$name(n))
532 } else {
533 Literal::Stable(stable::Literal::$name(n))
534 }
Alex Crichtona914a612018-04-04 07:48:44 -0700535 }
536 )*)
537}
538
539macro_rules! unsuffixed_integers {
540 ($($name:ident => $kind:ident,)*) => ($(
541 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700542 if nightly_works() {
543 Literal::Nightly(proc_macro::Literal::$name(n))
544 } else {
545 Literal::Stable(stable::Literal::$name(n))
546 }
Alex Crichtona914a612018-04-04 07:48:44 -0700547 }
548 )*)
549}
550
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700551impl Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700552 suffixed_numbers! {
553 u8_suffixed => u8,
554 u16_suffixed => u16,
555 u32_suffixed => u32,
556 u64_suffixed => u64,
557 usize_suffixed => usize,
558 i8_suffixed => i8,
559 i16_suffixed => i16,
560 i32_suffixed => i32,
561 i64_suffixed => i64,
562 isize_suffixed => isize,
563
564 f32_suffixed => f32,
565 f64_suffixed => f64,
566 }
567
568 unsuffixed_integers! {
569 u8_unsuffixed => u8,
570 u16_unsuffixed => u16,
571 u32_unsuffixed => u32,
572 u64_unsuffixed => u64,
573 usize_unsuffixed => usize,
574 i8_unsuffixed => i8,
575 i16_unsuffixed => i16,
576 i32_unsuffixed => i32,
577 i64_unsuffixed => i64,
578 isize_unsuffixed => isize,
579 }
580
581 pub fn f32_unsuffixed(f: f32) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700582 if nightly_works() {
583 Literal::Nightly(proc_macro::Literal::f32_unsuffixed(f))
584 } else {
585 Literal::Stable(stable::Literal::f32_unsuffixed(f))
586 }
Alex Crichtona914a612018-04-04 07:48:44 -0700587 }
588
589 pub fn f64_unsuffixed(f: f64) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700590 if nightly_works() {
591 Literal::Nightly(proc_macro::Literal::f64_unsuffixed(f))
592 } else {
593 Literal::Stable(stable::Literal::f64_unsuffixed(f))
594 }
Alex Crichtona914a612018-04-04 07:48:44 -0700595 }
596
Alex Crichtona914a612018-04-04 07:48:44 -0700597 pub fn string(t: &str) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700598 if nightly_works() {
599 Literal::Nightly(proc_macro::Literal::string(t))
600 } else {
601 Literal::Stable(stable::Literal::string(t))
602 }
Alex Crichtona914a612018-04-04 07:48:44 -0700603 }
604
605 pub fn character(t: char) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700606 if nightly_works() {
607 Literal::Nightly(proc_macro::Literal::character(t))
608 } else {
609 Literal::Stable(stable::Literal::character(t))
610 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700611 }
612
613 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700614 if nightly_works() {
615 Literal::Nightly(proc_macro::Literal::byte_string(bytes))
616 } else {
617 Literal::Stable(stable::Literal::byte_string(bytes))
618 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700619 }
620
Alex Crichtonb2c94622018-04-04 07:36:41 -0700621 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700622 match self {
623 Literal::Nightly(lit) => Span::Nightly(lit.span()),
624 Literal::Stable(lit) => Span::Stable(lit.span()),
625 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700626 }
627
Alex Crichtonb2c94622018-04-04 07:36:41 -0700628 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700629 match (self, span) {
630 (Literal::Nightly(lit), Span::Nightly(s)) => lit.set_span(s),
631 (Literal::Stable(lit), Span::Stable(s)) => lit.set_span(s),
632 _ => mismatch(),
633 }
634 }
635
636 fn unwrap_nightly(self) -> proc_macro::Literal {
637 match self {
638 Literal::Nightly(s) => s,
639 Literal::Stable(_) => mismatch(),
640 }
641 }
642}
643
644impl From<stable::Literal> for Literal {
645 fn from(s: stable::Literal) -> Literal {
646 Literal::Stable(s)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700647 }
648}
649
650impl fmt::Display for Literal {
651 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700652 match self {
653 Literal::Nightly(t) => t.fmt(f),
654 Literal::Stable(t) => t.fmt(f),
655 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700656 }
657}
658
659impl fmt::Debug for Literal {
660 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700661 match self {
662 Literal::Nightly(t) => t.fmt(f),
663 Literal::Stable(t) => t.fmt(f),
664 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700665 }
666}