blob: d709f889a116571b0f836feecdceec9360dba8d1 [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 Crichtoncbec8ec2017-06-02 13:19:33 -07005use std::str::FromStr;
Alex Crichton30a4e9e2018-04-27 17:02:19 -07006use std::panic;
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 Tolnayb28f38a2018-03-31 22:02:29 +020011use {Delimiter, Group, Op, 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() {
111 return TokenStream::Stable(token.into())
112 }
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 }
126 TokenTree::Op(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 Crichton9cd80a62018-04-05 17:46:58 -0700131 let mut op = proc_macro::Op::new(tt.op(), 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 Crichton30a4e9e2018-04-27 17:02:19 -0700135 TokenTree::Term(tt) => tt.inner.unwrap_nightly().into(),
136 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() {
145 let trees = trees.into_iter()
146 .map(TokenStream::from)
147 .flat_map(|t| {
148 match t {
149 TokenStream::Nightly(s) => s,
150 TokenStream::Stable(_) => mismatch(),
151 }
152 });
153 TokenStream::Nightly(trees.collect())
154 } else {
155 TokenStream::Stable(trees.into_iter().collect())
156 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700157 }
158}
159
160impl fmt::Debug for TokenStream {
161 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700162 match self {
163 TokenStream::Nightly(tts) => tts.fmt(f),
164 TokenStream::Stable(tts) => tts.fmt(f),
165 }
166 }
167}
168
169impl From<proc_macro::LexError> for LexError {
170 fn from(e: proc_macro::LexError) -> LexError {
171 LexError::Nightly(e)
172 }
173}
174
175impl From<stable::LexError> for LexError {
176 fn from(e: stable::LexError) -> LexError {
177 LexError::Stable(e)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700178 }
179}
180
181impl fmt::Debug for LexError {
182 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700183 match self {
184 LexError::Nightly(e) => e.fmt(f),
185 LexError::Stable(e) => e.fmt(f),
186 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700187 }
188}
189
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700190pub enum TokenTreeIter {
191 Nightly(proc_macro::token_stream::IntoIter),
192 Stable(stable::TokenTreeIter),
193}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700194
195impl IntoIterator for TokenStream {
196 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700197 type IntoIter = TokenTreeIter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700198
Alex Crichton1a7f7622017-07-05 17:47:15 -0700199 fn into_iter(self) -> TokenTreeIter {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700200 match self {
201 TokenStream::Nightly(tts) => TokenTreeIter::Nightly(tts.into_iter()),
202 TokenStream::Stable(tts) => TokenTreeIter::Stable(tts.into_iter()),
203 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700204 }
205}
206
Alex Crichton1a7f7622017-07-05 17:47:15 -0700207impl Iterator for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700208 type Item = TokenTree;
209
210 fn next(&mut self) -> Option<TokenTree> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700211 let token = match self {
212 TokenTreeIter::Nightly(iter) => iter.next()?,
213 TokenTreeIter::Stable(iter) => return iter.next(),
214 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700215 Some(match token {
216 proc_macro::TokenTree::Group(tt) => {
217 let delim = match tt.delimiter() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700218 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
219 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
220 proc_macro::Delimiter::Brace => Delimiter::Brace,
221 proc_macro::Delimiter::None => Delimiter::None,
222 };
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700223 let stream = ::TokenStream::_new(TokenStream::Nightly(tt.stream()));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700224 let mut g = Group::new(delim, stream);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700225 g.set_span(::Span::_new(Span::Nightly(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700226 g.into()
227 }
Alex Crichton9cd80a62018-04-05 17:46:58 -0700228 proc_macro::TokenTree::Op(tt) => {
229 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700230 proc_macro::Spacing::Joint => Spacing::Joint,
231 proc_macro::Spacing::Alone => Spacing::Alone,
232 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700233 let mut o = Op::new(tt.op(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700234 o.set_span(::Span::_new(Span::Nightly(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700235 o.into()
236 }
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700237 proc_macro::TokenTree::Term(s) => ::Term::_new(Term::Nightly(s)).into(),
238 proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Nightly(l)).into(),
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700239 })
240 }
241
242 fn size_hint(&self) -> (usize, Option<usize>) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700243 match self {
244 TokenTreeIter::Nightly(tts) => tts.size_hint(),
245 TokenTreeIter::Stable(tts) => tts.size_hint(),
246 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700247 }
248}
249
Alex Crichton1a7f7622017-07-05 17:47:15 -0700250impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700251 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700252 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700253 }
254}
255
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700256pub use stable::FileName;
Nika Layzellb35a9a32017-12-30 14:34:35 -0500257
258// NOTE: We have to generate our own filename object here because we can't wrap
259// the one provided by proc_macro.
260#[derive(Clone, PartialEq, Eq)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700261pub enum SourceFile {
262 Nightly(proc_macro::SourceFile, FileName),
263 Stable(stable::SourceFile),
264}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500265
266impl SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700267 fn nightly(sf: proc_macro::SourceFile) -> Self {
268 let filename = stable::file_name(sf.path().to_string());
269 SourceFile::Nightly(sf, filename)
Nika Layzellb35a9a32017-12-30 14:34:35 -0500270 }
271
Nika Layzellf8d5f212017-12-11 14:07:02 -0500272 /// Get the path to this source file as a string.
Nika Layzellb35a9a32017-12-30 14:34:35 -0500273 pub fn path(&self) -> &FileName {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700274 match self {
275 SourceFile::Nightly(_, f) => f,
276 SourceFile::Stable(a) => a.path(),
277 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500278 }
279
280 pub fn is_real(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700281 match self {
282 SourceFile::Nightly(a, _) => a.is_real(),
283 SourceFile::Stable(a) => a.is_real(),
284 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500285 }
286}
287
Nika Layzellb35a9a32017-12-30 14:34:35 -0500288impl AsRef<FileName> for SourceFile {
289 fn as_ref(&self) -> &FileName {
290 self.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500291 }
292}
293
294impl fmt::Debug for SourceFile {
295 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700296 match self {
297 SourceFile::Nightly(a, _) => a.fmt(f),
298 SourceFile::Stable(a) => a.fmt(f),
299 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500300 }
301}
302
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500303pub struct LineColumn {
304 pub line: usize,
305 pub column: usize,
306}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500307
Alex Crichton9cd80a62018-04-05 17:46:58 -0700308#[derive(Copy, Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700309pub enum Span {
310 Nightly(proc_macro::Span),
311 Stable(stable::Span),
Sergio Benitez13805082018-01-04 01:25:45 -0800312}
313
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700314impl Span {
315 pub fn call_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700316 if nightly_works() {
317 Span::Nightly(proc_macro::Span::call_site())
318 } else {
319 Span::Stable(stable::Span::call_site())
320 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700321 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700322
Alex Crichtone6085b72017-11-21 07:24:25 -0800323 pub fn def_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700324 if nightly_works() {
325 Span::Nightly(proc_macro::Span::def_site())
326 } else {
327 Span::Stable(stable::Span::def_site())
328 }
Alex Crichton998f6422017-11-19 08:06:27 -0800329 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500330
David Tolnay4e8e3972018-01-05 18:10:22 -0800331 pub fn resolved_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700332 match (self, other) {
333 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.resolved_at(b)),
334 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.resolved_at(b)),
335 _ => mismatch(),
336 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800337 }
338
339 pub fn located_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700340 match (self, other) {
341 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.located_at(b)),
342 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.located_at(b)),
343 _ => mismatch(),
344 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800345 }
346
David Tolnay16a17202017-12-31 10:47:24 -0500347 pub fn unstable(self) -> proc_macro::Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700348 match self {
349 Span::Nightly(s) => s,
350 Span::Stable(_) => mismatch(),
351 }
David Tolnay16a17202017-12-31 10:47:24 -0500352 }
353
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700354 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500355 pub fn source_file(&self) -> SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700356 match self {
357 Span::Nightly(s) => SourceFile::nightly(s.source_file()),
358 Span::Stable(s) => SourceFile::Stable(s.source_file()),
359 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500360 }
361
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700362 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500363 pub fn start(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700364 match self {
365 Span::Nightly(s) => {
366 let proc_macro::LineColumn { line, column } = s.start();
367 LineColumn { line, column }
368 }
369 Span::Stable(s) => {
370 let stable::LineColumn { line, column } = s.start();
371 LineColumn { line, column }
372 }
373 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500374 }
375
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700376 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500377 pub fn end(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700378 match self {
379 Span::Nightly(s) => {
380 let proc_macro::LineColumn { line, column } = s.end();
381 LineColumn { line, column }
382 }
383 Span::Stable(s) => {
384 let stable::LineColumn { line, column } = s.end();
385 LineColumn { line, column }
386 }
387 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500388 }
389
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700390 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500391 pub fn join(&self, other: Span) -> Option<Span> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700392 let ret = match (self, other) {
393 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.join(b)?),
394 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.join(b)?),
395 _ => return None,
396 };
397 Some(ret)
Nika Layzellf8d5f212017-12-11 14:07:02 -0500398 }
Alex Crichtone24f7342018-04-05 17:58:11 -0700399
400 pub fn eq(&self, other: &Span) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700401 match (self, other) {
402 (Span::Nightly(a), Span::Nightly(b)) => a.eq(b),
403 (Span::Stable(a), Span::Stable(b)) => a.eq(b),
404 _ => false,
405 }
406 }
407
408 fn unwrap_nightly(self) -> proc_macro::Span {
409 match self {
410 Span::Nightly(s) => s,
411 Span::Stable(_) => mismatch(),
412 }
413 }
414}
415
416impl From<proc_macro::Span> for ::Span {
417 fn from(proc_span: proc_macro::Span) -> ::Span {
418 ::Span::_new(Span::Nightly(proc_span))
419 }
420}
421
422impl From<stable::Span> for Span {
423 fn from(inner: stable::Span) -> Span {
424 Span::Stable(inner)
Alex Crichtone24f7342018-04-05 17:58:11 -0700425 }
Alex Crichton998f6422017-11-19 08:06:27 -0800426}
427
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700428impl fmt::Debug for Span {
429 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700430 match self {
431 Span::Nightly(s) => s.fmt(f),
432 Span::Stable(s) => s.fmt(f),
433 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700434 }
435}
436
437#[derive(Copy, Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700438pub enum Term {
439 Nightly(proc_macro::Term),
440 Stable(stable::Term),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700441}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700442
David Tolnay10effeb2018-01-06 11:07:49 -0800443impl Term {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700444 pub fn new(string: &str, span: Span) -> Term {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700445 match span {
446 Span::Nightly(s) => Term::Nightly(proc_macro::Term::new(string, s)),
447 Span::Stable(s) => Term::Stable(stable::Term::new(string, s)),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700448 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700449 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700450
David Tolnay10effeb2018-01-06 11:07:49 -0800451 pub fn as_str(&self) -> &str {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700452 match self {
453 Term::Nightly(t) => t.as_str(),
454 Term::Stable(t) => t.as_str(),
455 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700456 }
457
458 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700459 match self {
460 Term::Nightly(t) => Span::Nightly(t.span()),
461 Term::Stable(t) => Span::Stable(t.span()),
462 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700463 }
464
465 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700466 match (self, span) {
467 (Term::Nightly(t), Span::Nightly(s)) => t.set_span(s),
468 (Term::Stable(t), Span::Stable(s)) => t.set_span(s),
469 _ => mismatch(),
470 }
471 }
472
473 fn unwrap_nightly(self) -> proc_macro::Term {
474 match self {
475 Term::Nightly(s) => s,
476 Term::Stable(_) => mismatch(),
477 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700478 }
479}
480
Alex Crichton1a7f7622017-07-05 17:47:15 -0700481impl fmt::Debug for Term {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700482 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700483 match self {
484 Term::Nightly(t) => t.fmt(f),
485 Term::Stable(t) => t.fmt(f),
486 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700487 }
488}
489
490#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700491pub enum Literal {
492 Nightly(proc_macro::Literal),
493 Stable(stable::Literal),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700494}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700495
Alex Crichtona914a612018-04-04 07:48:44 -0700496macro_rules! suffixed_numbers {
497 ($($name:ident => $kind:ident,)*) => ($(
498 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700499 if nightly_works() {
500 Literal::Nightly(proc_macro::Literal::$name(n))
501 } else {
502 Literal::Stable(stable::Literal::$name(n))
503 }
Alex Crichtona914a612018-04-04 07:48:44 -0700504 }
505 )*)
506}
507
508macro_rules! unsuffixed_integers {
509 ($($name:ident => $kind:ident,)*) => ($(
510 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700511 if nightly_works() {
512 Literal::Nightly(proc_macro::Literal::$name(n))
513 } else {
514 Literal::Stable(stable::Literal::$name(n))
515 }
Alex Crichtona914a612018-04-04 07:48:44 -0700516 }
517 )*)
518}
519
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700520impl Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700521 suffixed_numbers! {
522 u8_suffixed => u8,
523 u16_suffixed => u16,
524 u32_suffixed => u32,
525 u64_suffixed => u64,
526 usize_suffixed => usize,
527 i8_suffixed => i8,
528 i16_suffixed => i16,
529 i32_suffixed => i32,
530 i64_suffixed => i64,
531 isize_suffixed => isize,
532
533 f32_suffixed => f32,
534 f64_suffixed => f64,
535 }
536
537 unsuffixed_integers! {
538 u8_unsuffixed => u8,
539 u16_unsuffixed => u16,
540 u32_unsuffixed => u32,
541 u64_unsuffixed => u64,
542 usize_unsuffixed => usize,
543 i8_unsuffixed => i8,
544 i16_unsuffixed => i16,
545 i32_unsuffixed => i32,
546 i64_unsuffixed => i64,
547 isize_unsuffixed => isize,
548 }
549
550 pub fn f32_unsuffixed(f: f32) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700551 if nightly_works() {
552 Literal::Nightly(proc_macro::Literal::f32_unsuffixed(f))
553 } else {
554 Literal::Stable(stable::Literal::f32_unsuffixed(f))
555 }
Alex Crichtona914a612018-04-04 07:48:44 -0700556 }
557
558 pub fn f64_unsuffixed(f: f64) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700559 if nightly_works() {
560 Literal::Nightly(proc_macro::Literal::f64_unsuffixed(f))
561 } else {
562 Literal::Stable(stable::Literal::f64_unsuffixed(f))
563 }
Alex Crichtona914a612018-04-04 07:48:44 -0700564 }
565
Alex Crichtona914a612018-04-04 07:48:44 -0700566 pub fn string(t: &str) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700567 if nightly_works() {
568 Literal::Nightly(proc_macro::Literal::string(t))
569 } else {
570 Literal::Stable(stable::Literal::string(t))
571 }
Alex Crichtona914a612018-04-04 07:48:44 -0700572 }
573
574 pub fn character(t: char) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700575 if nightly_works() {
576 Literal::Nightly(proc_macro::Literal::character(t))
577 } else {
578 Literal::Stable(stable::Literal::character(t))
579 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700580 }
581
582 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700583 if nightly_works() {
584 Literal::Nightly(proc_macro::Literal::byte_string(bytes))
585 } else {
586 Literal::Stable(stable::Literal::byte_string(bytes))
587 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700588 }
589
Alex Crichtonb2c94622018-04-04 07:36:41 -0700590 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700591 match self {
592 Literal::Nightly(lit) => Span::Nightly(lit.span()),
593 Literal::Stable(lit) => Span::Stable(lit.span()),
594 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700595 }
596
Alex Crichtonb2c94622018-04-04 07:36:41 -0700597 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700598 match (self, span) {
599 (Literal::Nightly(lit), Span::Nightly(s)) => lit.set_span(s),
600 (Literal::Stable(lit), Span::Stable(s)) => lit.set_span(s),
601 _ => mismatch(),
602 }
603 }
604
605 fn unwrap_nightly(self) -> proc_macro::Literal {
606 match self {
607 Literal::Nightly(s) => s,
608 Literal::Stable(_) => mismatch(),
609 }
610 }
611}
612
613impl From<stable::Literal> for Literal {
614 fn from(s: stable::Literal) -> Literal {
615 Literal::Stable(s)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700616 }
617}
618
619impl fmt::Display for Literal {
620 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700621 match self {
622 Literal::Nightly(t) => t.fmt(f),
623 Literal::Stable(t) => t.fmt(f),
624 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700625 }
626}
627
628impl fmt::Debug for Literal {
629 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700630 match self {
631 Literal::Nightly(t) => t.fmt(f),
632 Literal::Stable(t) => t.fmt(f),
633 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700634 }
635}