blob: a5a48ad50247303282e481e436aeae7c18a4b3ef [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 }
David Tolnay5c58c532018-08-13 11:33:51 -070064
65 fn unwrap_stable(self) -> stable::TokenStream {
66 match self {
67 TokenStream::Nightly(_) => mismatch(),
68 TokenStream::Stable(s) => s,
69 }
70 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070071}
72
73impl FromStr for TokenStream {
74 type Err = LexError;
75
76 fn from_str(src: &str) -> Result<TokenStream, LexError> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070077 if nightly_works() {
78 Ok(TokenStream::Nightly(src.parse()?))
79 } else {
80 Ok(TokenStream::Stable(src.parse()?))
81 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070082 }
83}
84
85impl fmt::Display for TokenStream {
86 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070087 match self {
88 TokenStream::Nightly(tts) => tts.fmt(f),
89 TokenStream::Stable(tts) => tts.fmt(f),
90 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070091 }
92}
93
94impl From<proc_macro::TokenStream> for TokenStream {
95 fn from(inner: proc_macro::TokenStream) -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070096 TokenStream::Nightly(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070097 }
98}
99
100impl From<TokenStream> for proc_macro::TokenStream {
101 fn from(inner: TokenStream) -> proc_macro::TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700102 match inner {
103 TokenStream::Nightly(inner) => inner,
104 TokenStream::Stable(inner) => inner.to_string().parse().unwrap(),
105 }
106 }
107}
108
109impl From<stable::TokenStream> for TokenStream {
110 fn from(inner: stable::TokenStream) -> TokenStream {
111 TokenStream::Stable(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700112 }
113}
114
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700115impl From<TokenTree> for TokenStream {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700116 fn from(token: TokenTree) -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700117 if !nightly_works() {
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700118 return TokenStream::Stable(token.into());
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700119 }
Alex Crichton9cd80a62018-04-05 17:46:58 -0700120 let tt: proc_macro::TokenTree = match token {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700121 TokenTree::Group(tt) => {
122 let delim = match tt.delimiter() {
123 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
124 Delimiter::Bracket => proc_macro::Delimiter::Bracket,
125 Delimiter::Brace => proc_macro::Delimiter::Brace,
126 Delimiter::None => proc_macro::Delimiter::None,
127 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700128 let span = tt.span();
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700129 let mut group = proc_macro::Group::new(delim, tt.stream.inner.unwrap_nightly());
130 group.set_span(span.inner.unwrap_nightly());
Alex Crichton9cd80a62018-04-05 17:46:58 -0700131 group.into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700132 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700133 TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700134 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700135 Spacing::Joint => proc_macro::Spacing::Joint,
136 Spacing::Alone => proc_macro::Spacing::Alone,
137 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700138 let mut op = proc_macro::Punct::new(tt.as_char(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700139 op.set_span(tt.span().inner.unwrap_nightly());
Alex Crichton9cd80a62018-04-05 17:46:58 -0700140 op.into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700141 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700142 TokenTree::Ident(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700143 TokenTree::Literal(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700144 };
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700145 TokenStream::Nightly(tt.into())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700146 }
147}
148
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700149impl iter::FromIterator<TokenTree> for TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700150 fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self {
151 if nightly_works() {
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700152 let trees = trees
153 .into_iter()
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700154 .map(TokenStream::from)
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700155 .flat_map(|t| match t {
156 TokenStream::Nightly(s) => s,
157 TokenStream::Stable(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700158 });
159 TokenStream::Nightly(trees.collect())
160 } else {
161 TokenStream::Stable(trees.into_iter().collect())
162 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700163 }
164}
165
Alex Crichtonf3888432018-05-16 09:11:05 -0700166impl Extend<TokenTree> for TokenStream {
167 fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
168 match self {
169 TokenStream::Nightly(tts) => {
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700170 *tts = tts
171 .clone()
Alex Crichtonf3888432018-05-16 09:11:05 -0700172 .into_iter()
173 .chain(
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700174 streams
175 .into_iter()
Alex Crichtonf3888432018-05-16 09:11:05 -0700176 .map(TokenStream::from)
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700177 .flat_map(|t| match t {
178 TokenStream::Nightly(tts) => tts.into_iter(),
179 _ => panic!(),
180 }),
David Tolnay5a556cf2018-08-12 13:49:39 -0700181 ).collect();
Alex Crichtonf3888432018-05-16 09:11:05 -0700182 }
183 TokenStream::Stable(tts) => tts.extend(streams),
184 }
185 }
186}
187
David Tolnay5c58c532018-08-13 11:33:51 -0700188impl Extend<TokenStream> for TokenStream {
189 fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
190 match self {
191 TokenStream::Nightly(tts) => {
192 tts.extend(streams.into_iter().map(|stream| stream.unwrap_nightly()))
193 }
194 TokenStream::Stable(tts) => {
195 tts.extend(streams.into_iter().map(|stream| stream.unwrap_stable()))
196 }
197 }
198 }
199}
200
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700201impl fmt::Debug for TokenStream {
202 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700203 match self {
204 TokenStream::Nightly(tts) => tts.fmt(f),
205 TokenStream::Stable(tts) => tts.fmt(f),
206 }
207 }
208}
209
210impl From<proc_macro::LexError> for LexError {
211 fn from(e: proc_macro::LexError) -> LexError {
212 LexError::Nightly(e)
213 }
214}
215
216impl From<stable::LexError> for LexError {
217 fn from(e: stable::LexError) -> LexError {
218 LexError::Stable(e)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700219 }
220}
221
222impl fmt::Debug for LexError {
223 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700224 match self {
225 LexError::Nightly(e) => e.fmt(f),
226 LexError::Stable(e) => e.fmt(f),
227 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700228 }
229}
230
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700231pub enum TokenTreeIter {
232 Nightly(proc_macro::token_stream::IntoIter),
233 Stable(stable::TokenTreeIter),
234}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700235
236impl IntoIterator for TokenStream {
237 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700238 type IntoIter = TokenTreeIter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700239
Alex Crichton1a7f7622017-07-05 17:47:15 -0700240 fn into_iter(self) -> TokenTreeIter {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700241 match self {
242 TokenStream::Nightly(tts) => TokenTreeIter::Nightly(tts.into_iter()),
243 TokenStream::Stable(tts) => TokenTreeIter::Stable(tts.into_iter()),
244 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700245 }
246}
247
Alex Crichton1a7f7622017-07-05 17:47:15 -0700248impl Iterator for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700249 type Item = TokenTree;
250
251 fn next(&mut self) -> Option<TokenTree> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700252 let token = match self {
253 TokenTreeIter::Nightly(iter) => iter.next()?,
254 TokenTreeIter::Stable(iter) => return iter.next(),
255 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700256 Some(match token {
257 proc_macro::TokenTree::Group(tt) => {
258 let delim = match tt.delimiter() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700259 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
260 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
261 proc_macro::Delimiter::Brace => Delimiter::Brace,
262 proc_macro::Delimiter::None => Delimiter::None,
263 };
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700264 let stream = ::TokenStream::_new(TokenStream::Nightly(tt.stream()));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700265 let mut g = Group::new(delim, stream);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700266 g.set_span(::Span::_new(Span::Nightly(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700267 g.into()
268 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700269 proc_macro::TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700270 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700271 proc_macro::Spacing::Joint => Spacing::Joint,
272 proc_macro::Spacing::Alone => Spacing::Alone,
273 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700274 let mut o = Punct::new(tt.as_char(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700275 o.set_span(::Span::_new(Span::Nightly(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700276 o.into()
277 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700278 proc_macro::TokenTree::Ident(s) => ::Ident::_new(Ident::Nightly(s)).into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700279 proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Nightly(l)).into(),
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700280 })
281 }
282
283 fn size_hint(&self) -> (usize, Option<usize>) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700284 match self {
285 TokenTreeIter::Nightly(tts) => tts.size_hint(),
286 TokenTreeIter::Stable(tts) => tts.size_hint(),
287 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700288 }
289}
290
Alex Crichton1a7f7622017-07-05 17:47:15 -0700291impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700292 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700293 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700294 }
295}
296
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700297pub use stable::FileName;
Nika Layzellb35a9a32017-12-30 14:34:35 -0500298
299// NOTE: We have to generate our own filename object here because we can't wrap
300// the one provided by proc_macro.
301#[derive(Clone, PartialEq, Eq)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700302pub enum SourceFile {
303 Nightly(proc_macro::SourceFile, FileName),
304 Stable(stable::SourceFile),
305}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500306
307impl SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700308 fn nightly(sf: proc_macro::SourceFile) -> Self {
Alex Crichton9f0a28a2018-07-21 18:53:35 -0700309 let filename = stable::file_name(sf.path().display().to_string());
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700310 SourceFile::Nightly(sf, filename)
Nika Layzellb35a9a32017-12-30 14:34:35 -0500311 }
312
Nika Layzellf8d5f212017-12-11 14:07:02 -0500313 /// Get the path to this source file as a string.
Nika Layzellb35a9a32017-12-30 14:34:35 -0500314 pub fn path(&self) -> &FileName {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700315 match self {
316 SourceFile::Nightly(_, f) => f,
317 SourceFile::Stable(a) => a.path(),
318 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500319 }
320
321 pub fn is_real(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700322 match self {
323 SourceFile::Nightly(a, _) => a.is_real(),
324 SourceFile::Stable(a) => a.is_real(),
325 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500326 }
327}
328
Nika Layzellb35a9a32017-12-30 14:34:35 -0500329impl AsRef<FileName> for SourceFile {
330 fn as_ref(&self) -> &FileName {
331 self.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500332 }
333}
334
335impl fmt::Debug for SourceFile {
336 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700337 match self {
338 SourceFile::Nightly(a, _) => a.fmt(f),
339 SourceFile::Stable(a) => a.fmt(f),
340 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500341 }
342}
343
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500344pub struct LineColumn {
345 pub line: usize,
346 pub column: usize,
347}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500348
Alex Crichton9cd80a62018-04-05 17:46:58 -0700349#[derive(Copy, Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700350pub enum Span {
351 Nightly(proc_macro::Span),
352 Stable(stable::Span),
Sergio Benitez13805082018-01-04 01:25:45 -0800353}
354
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700355impl Span {
356 pub fn call_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700357 if nightly_works() {
358 Span::Nightly(proc_macro::Span::call_site())
359 } else {
360 Span::Stable(stable::Span::call_site())
361 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700362 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700363
Alex Crichtone6085b72017-11-21 07:24:25 -0800364 pub fn def_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700365 if nightly_works() {
366 Span::Nightly(proc_macro::Span::def_site())
367 } else {
368 Span::Stable(stable::Span::def_site())
369 }
Alex Crichton998f6422017-11-19 08:06:27 -0800370 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500371
David Tolnay4e8e3972018-01-05 18:10:22 -0800372 pub fn resolved_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700373 match (self, other) {
374 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.resolved_at(b)),
375 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.resolved_at(b)),
376 _ => mismatch(),
377 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800378 }
379
380 pub fn located_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700381 match (self, other) {
382 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.located_at(b)),
383 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.located_at(b)),
384 _ => mismatch(),
385 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800386 }
387
David Tolnay16a17202017-12-31 10:47:24 -0500388 pub fn unstable(self) -> proc_macro::Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700389 match self {
390 Span::Nightly(s) => s,
391 Span::Stable(_) => mismatch(),
392 }
David Tolnay16a17202017-12-31 10:47:24 -0500393 }
394
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700395 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500396 pub fn source_file(&self) -> SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700397 match self {
398 Span::Nightly(s) => SourceFile::nightly(s.source_file()),
399 Span::Stable(s) => SourceFile::Stable(s.source_file()),
400 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500401 }
402
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700403 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500404 pub fn start(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700405 match self {
406 Span::Nightly(s) => {
407 let proc_macro::LineColumn { line, column } = s.start();
408 LineColumn { line, column }
409 }
410 Span::Stable(s) => {
411 let stable::LineColumn { line, column } = s.start();
412 LineColumn { line, column }
413 }
414 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500415 }
416
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700417 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500418 pub fn end(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700419 match self {
420 Span::Nightly(s) => {
421 let proc_macro::LineColumn { line, column } = s.end();
422 LineColumn { line, column }
423 }
424 Span::Stable(s) => {
425 let stable::LineColumn { line, column } = s.end();
426 LineColumn { line, column }
427 }
428 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500429 }
430
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700431 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500432 pub fn join(&self, other: Span) -> Option<Span> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700433 let ret = match (self, other) {
434 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.join(b)?),
435 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.join(b)?),
436 _ => return None,
437 };
438 Some(ret)
Nika Layzellf8d5f212017-12-11 14:07:02 -0500439 }
Alex Crichtone24f7342018-04-05 17:58:11 -0700440
441 pub fn eq(&self, other: &Span) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700442 match (self, other) {
443 (Span::Nightly(a), Span::Nightly(b)) => a.eq(b),
444 (Span::Stable(a), Span::Stable(b)) => a.eq(b),
445 _ => false,
446 }
447 }
448
449 fn unwrap_nightly(self) -> proc_macro::Span {
450 match self {
451 Span::Nightly(s) => s,
452 Span::Stable(_) => mismatch(),
453 }
454 }
455}
456
457impl From<proc_macro::Span> for ::Span {
458 fn from(proc_span: proc_macro::Span) -> ::Span {
459 ::Span::_new(Span::Nightly(proc_span))
460 }
461}
462
463impl From<stable::Span> for Span {
464 fn from(inner: stable::Span) -> Span {
465 Span::Stable(inner)
Alex Crichtone24f7342018-04-05 17:58:11 -0700466 }
Alex Crichton998f6422017-11-19 08:06:27 -0800467}
468
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700469impl fmt::Debug for Span {
470 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700471 match self {
472 Span::Nightly(s) => s.fmt(f),
473 Span::Stable(s) => s.fmt(f),
474 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700475 }
476}
477
Alex Crichtonf3888432018-05-16 09:11:05 -0700478#[derive(Clone)]
479pub enum Ident {
480 Nightly(proc_macro::Ident),
481 Stable(stable::Ident),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700482}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700483
Alex Crichtonf3888432018-05-16 09:11:05 -0700484impl Ident {
485 pub fn new(string: &str, span: Span) -> Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700486 match span {
Alex Crichtonf3888432018-05-16 09:11:05 -0700487 Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new(string, s)),
488 Span::Stable(s) => Ident::Stable(stable::Ident::new(string, s)),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700489 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700490 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700491
Alex Crichtonf3888432018-05-16 09:11:05 -0700492 pub fn new_raw(string: &str, span: Span) -> Ident {
493 match span {
494 Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new_raw(string, s)),
495 Span::Stable(s) => Ident::Stable(stable::Ident::new_raw(string, s)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700496 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700497 }
498
499 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700500 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700501 Ident::Nightly(t) => Span::Nightly(t.span()),
502 Ident::Stable(t) => Span::Stable(t.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700503 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700504 }
505
506 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700507 match (self, span) {
Alex Crichtonf3888432018-05-16 09:11:05 -0700508 (Ident::Nightly(t), Span::Nightly(s)) => t.set_span(s),
509 (Ident::Stable(t), Span::Stable(s)) => t.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700510 _ => mismatch(),
511 }
512 }
513
Alex Crichtonf3888432018-05-16 09:11:05 -0700514 fn unwrap_nightly(self) -> proc_macro::Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700515 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700516 Ident::Nightly(s) => s,
517 Ident::Stable(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700518 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700519 }
520}
521
Alex Crichtonf3888432018-05-16 09:11:05 -0700522impl fmt::Display for Ident {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700523 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700524 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700525 Ident::Nightly(t) => t.fmt(f),
526 Ident::Stable(t) => t.fmt(f),
527 }
528 }
529}
530
531impl fmt::Debug for Ident {
532 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
533 match self {
534 Ident::Nightly(t) => t.fmt(f),
535 Ident::Stable(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700536 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700537 }
538}
539
540#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700541pub enum Literal {
542 Nightly(proc_macro::Literal),
543 Stable(stable::Literal),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700544}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700545
Alex Crichtona914a612018-04-04 07:48:44 -0700546macro_rules! suffixed_numbers {
547 ($($name:ident => $kind:ident,)*) => ($(
548 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700549 if nightly_works() {
550 Literal::Nightly(proc_macro::Literal::$name(n))
551 } else {
552 Literal::Stable(stable::Literal::$name(n))
553 }
Alex Crichtona914a612018-04-04 07:48:44 -0700554 }
555 )*)
556}
557
558macro_rules! unsuffixed_integers {
559 ($($name:ident => $kind:ident,)*) => ($(
560 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700561 if nightly_works() {
562 Literal::Nightly(proc_macro::Literal::$name(n))
563 } else {
564 Literal::Stable(stable::Literal::$name(n))
565 }
Alex Crichtona914a612018-04-04 07:48:44 -0700566 }
567 )*)
568}
569
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700570impl Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700571 suffixed_numbers! {
572 u8_suffixed => u8,
573 u16_suffixed => u16,
574 u32_suffixed => u32,
575 u64_suffixed => u64,
576 usize_suffixed => usize,
577 i8_suffixed => i8,
578 i16_suffixed => i16,
579 i32_suffixed => i32,
580 i64_suffixed => i64,
581 isize_suffixed => isize,
582
583 f32_suffixed => f32,
584 f64_suffixed => f64,
585 }
586
587 unsuffixed_integers! {
588 u8_unsuffixed => u8,
589 u16_unsuffixed => u16,
590 u32_unsuffixed => u32,
591 u64_unsuffixed => u64,
592 usize_unsuffixed => usize,
593 i8_unsuffixed => i8,
594 i16_unsuffixed => i16,
595 i32_unsuffixed => i32,
596 i64_unsuffixed => i64,
597 isize_unsuffixed => isize,
598 }
599
600 pub fn f32_unsuffixed(f: f32) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700601 if nightly_works() {
602 Literal::Nightly(proc_macro::Literal::f32_unsuffixed(f))
603 } else {
604 Literal::Stable(stable::Literal::f32_unsuffixed(f))
605 }
Alex Crichtona914a612018-04-04 07:48:44 -0700606 }
607
608 pub fn f64_unsuffixed(f: f64) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700609 if nightly_works() {
610 Literal::Nightly(proc_macro::Literal::f64_unsuffixed(f))
611 } else {
612 Literal::Stable(stable::Literal::f64_unsuffixed(f))
613 }
Alex Crichtona914a612018-04-04 07:48:44 -0700614 }
615
Alex Crichtona914a612018-04-04 07:48:44 -0700616 pub fn string(t: &str) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700617 if nightly_works() {
618 Literal::Nightly(proc_macro::Literal::string(t))
619 } else {
620 Literal::Stable(stable::Literal::string(t))
621 }
Alex Crichtona914a612018-04-04 07:48:44 -0700622 }
623
624 pub fn character(t: char) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700625 if nightly_works() {
626 Literal::Nightly(proc_macro::Literal::character(t))
627 } else {
628 Literal::Stable(stable::Literal::character(t))
629 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700630 }
631
632 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700633 if nightly_works() {
634 Literal::Nightly(proc_macro::Literal::byte_string(bytes))
635 } else {
636 Literal::Stable(stable::Literal::byte_string(bytes))
637 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700638 }
639
Alex Crichtonb2c94622018-04-04 07:36:41 -0700640 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700641 match self {
642 Literal::Nightly(lit) => Span::Nightly(lit.span()),
643 Literal::Stable(lit) => Span::Stable(lit.span()),
644 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700645 }
646
Alex Crichtonb2c94622018-04-04 07:36:41 -0700647 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700648 match (self, span) {
649 (Literal::Nightly(lit), Span::Nightly(s)) => lit.set_span(s),
650 (Literal::Stable(lit), Span::Stable(s)) => lit.set_span(s),
651 _ => mismatch(),
652 }
653 }
654
655 fn unwrap_nightly(self) -> proc_macro::Literal {
656 match self {
657 Literal::Nightly(s) => s,
658 Literal::Stable(_) => mismatch(),
659 }
660 }
661}
662
663impl From<stable::Literal> for Literal {
664 fn from(s: stable::Literal) -> Literal {
665 Literal::Stable(s)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700666 }
667}
668
669impl fmt::Display for Literal {
670 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700671 match self {
672 Literal::Nightly(t) => t.fmt(f),
673 Literal::Stable(t) => t.fmt(f),
674 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700675 }
676}
677
678impl fmt::Debug for Literal {
679 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700680 match self {
681 Literal::Nightly(t) => t.fmt(f),
682 Literal::Stable(t) => t.fmt(f),
683 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700684 }
685}