Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 1 | #![cfg_attr(not(procmacro2_semver_exempt), allow(dead_code))] |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 2 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 3 | use std::fmt; |
| 4 | use std::iter; |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 5 | use std::panic; |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 6 | use std::str::FromStr; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 7 | |
| 8 | use proc_macro; |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 9 | use stable; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 10 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 11 | use {Delimiter, Group, Punct, Spacing, TokenTree}; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 12 | |
| 13 | #[derive(Clone)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 14 | pub enum TokenStream { |
| 15 | Nightly(proc_macro::TokenStream), |
| 16 | Stable(stable::TokenStream), |
| 17 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 18 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 19 | pub enum LexError { |
| 20 | Nightly(proc_macro::LexError), |
| 21 | Stable(stable::LexError), |
| 22 | } |
| 23 | |
| 24 | fn 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 | |
| 38 | fn mismatch() -> ! { |
| 39 | panic!("stable/nightly mismatch") |
| 40 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 41 | |
| 42 | impl TokenStream { |
David Tolnay | c3bb459 | 2018-05-28 20:09:44 -0700 | [diff] [blame] | 43 | pub fn new() -> TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 44 | if nightly_works() { |
David Tolnay | c3bb459 | 2018-05-28 20:09:44 -0700 | [diff] [blame] | 45 | TokenStream::Nightly(proc_macro::TokenStream::new()) |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 46 | } else { |
David Tolnay | c3bb459 | 2018-05-28 20:09:44 -0700 | [diff] [blame] | 47 | TokenStream::Stable(stable::TokenStream::new()) |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 48 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | pub fn is_empty(&self) -> bool { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 52 | 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 Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | impl FromStr for TokenStream { |
| 67 | type Err = LexError; |
| 68 | |
| 69 | fn from_str(src: &str) -> Result<TokenStream, LexError> { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 70 | if nightly_works() { |
| 71 | Ok(TokenStream::Nightly(src.parse()?)) |
| 72 | } else { |
| 73 | Ok(TokenStream::Stable(src.parse()?)) |
| 74 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | impl fmt::Display for TokenStream { |
| 79 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 80 | match self { |
| 81 | TokenStream::Nightly(tts) => tts.fmt(f), |
| 82 | TokenStream::Stable(tts) => tts.fmt(f), |
| 83 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | impl From<proc_macro::TokenStream> for TokenStream { |
| 88 | fn from(inner: proc_macro::TokenStream) -> TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 89 | TokenStream::Nightly(inner) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | impl From<TokenStream> for proc_macro::TokenStream { |
| 94 | fn from(inner: TokenStream) -> proc_macro::TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 95 | match inner { |
| 96 | TokenStream::Nightly(inner) => inner, |
| 97 | TokenStream::Stable(inner) => inner.to_string().parse().unwrap(), |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | impl From<stable::TokenStream> for TokenStream { |
| 103 | fn from(inner: stable::TokenStream) -> TokenStream { |
| 104 | TokenStream::Stable(inner) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 108 | impl From<TokenTree> for TokenStream { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 109 | fn from(token: TokenTree) -> TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 110 | if !nightly_works() { |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 111 | return TokenStream::Stable(token.into()); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 112 | } |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 113 | let tt: proc_macro::TokenTree = match token { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 114 | 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 Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 121 | let span = tt.span(); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 122 | let mut group = proc_macro::Group::new(delim, tt.stream.inner.unwrap_nightly()); |
| 123 | group.set_span(span.inner.unwrap_nightly()); |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 124 | group.into() |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 125 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 126 | TokenTree::Punct(tt) => { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 127 | let spacing = match tt.spacing() { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 128 | Spacing::Joint => proc_macro::Spacing::Joint, |
| 129 | Spacing::Alone => proc_macro::Spacing::Alone, |
| 130 | }; |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 131 | let mut op = proc_macro::Punct::new(tt.as_char(), spacing); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 132 | op.set_span(tt.span().inner.unwrap_nightly()); |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 133 | op.into() |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 134 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 135 | TokenTree::Ident(tt) => tt.inner.unwrap_nightly().into(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 136 | TokenTree::Literal(tt) => tt.inner.unwrap_nightly().into(), |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 137 | }; |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 138 | TokenStream::Nightly(tt.into()) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 142 | impl iter::FromIterator<TokenTree> for TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 143 | fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self { |
| 144 | if nightly_works() { |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 145 | let trees = trees |
| 146 | .into_iter() |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 147 | .map(TokenStream::from) |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 148 | .flat_map(|t| match t { |
| 149 | TokenStream::Nightly(s) => s, |
| 150 | TokenStream::Stable(_) => mismatch(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 151 | }); |
| 152 | TokenStream::Nightly(trees.collect()) |
| 153 | } else { |
| 154 | TokenStream::Stable(trees.into_iter().collect()) |
| 155 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 159 | impl Extend<TokenTree> for TokenStream { |
| 160 | fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) { |
| 161 | match self { |
| 162 | TokenStream::Nightly(tts) => { |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 163 | *tts = tts |
| 164 | .clone() |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 165 | .into_iter() |
| 166 | .chain( |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 167 | streams |
| 168 | .into_iter() |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 169 | .map(TokenStream::from) |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 170 | .flat_map(|t| match t { |
| 171 | TokenStream::Nightly(tts) => tts.into_iter(), |
| 172 | _ => panic!(), |
| 173 | }), |
David Tolnay | 5a556cf | 2018-08-12 13:49:39 -0700 | [diff] [blame] | 174 | ).collect(); |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 175 | } |
| 176 | TokenStream::Stable(tts) => tts.extend(streams), |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 181 | impl fmt::Debug for TokenStream { |
| 182 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 183 | match self { |
| 184 | TokenStream::Nightly(tts) => tts.fmt(f), |
| 185 | TokenStream::Stable(tts) => tts.fmt(f), |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | impl From<proc_macro::LexError> for LexError { |
| 191 | fn from(e: proc_macro::LexError) -> LexError { |
| 192 | LexError::Nightly(e) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | impl From<stable::LexError> for LexError { |
| 197 | fn from(e: stable::LexError) -> LexError { |
| 198 | LexError::Stable(e) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | impl fmt::Debug for LexError { |
| 203 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 204 | match self { |
| 205 | LexError::Nightly(e) => e.fmt(f), |
| 206 | LexError::Stable(e) => e.fmt(f), |
| 207 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 211 | pub enum TokenTreeIter { |
| 212 | Nightly(proc_macro::token_stream::IntoIter), |
| 213 | Stable(stable::TokenTreeIter), |
| 214 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 215 | |
| 216 | impl IntoIterator for TokenStream { |
| 217 | type Item = TokenTree; |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 218 | type IntoIter = TokenTreeIter; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 219 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 220 | fn into_iter(self) -> TokenTreeIter { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 221 | match self { |
| 222 | TokenStream::Nightly(tts) => TokenTreeIter::Nightly(tts.into_iter()), |
| 223 | TokenStream::Stable(tts) => TokenTreeIter::Stable(tts.into_iter()), |
| 224 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 228 | impl Iterator for TokenTreeIter { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 229 | type Item = TokenTree; |
| 230 | |
| 231 | fn next(&mut self) -> Option<TokenTree> { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 232 | let token = match self { |
| 233 | TokenTreeIter::Nightly(iter) => iter.next()?, |
| 234 | TokenTreeIter::Stable(iter) => return iter.next(), |
| 235 | }; |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 236 | Some(match token { |
| 237 | proc_macro::TokenTree::Group(tt) => { |
| 238 | let delim = match tt.delimiter() { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 239 | 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 Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 244 | let stream = ::TokenStream::_new(TokenStream::Nightly(tt.stream())); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 245 | let mut g = Group::new(delim, stream); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 246 | g.set_span(::Span::_new(Span::Nightly(tt.span()))); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 247 | g.into() |
| 248 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 249 | proc_macro::TokenTree::Punct(tt) => { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 250 | let spacing = match tt.spacing() { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 251 | proc_macro::Spacing::Joint => Spacing::Joint, |
| 252 | proc_macro::Spacing::Alone => Spacing::Alone, |
| 253 | }; |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 254 | let mut o = Punct::new(tt.as_char(), spacing); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 255 | o.set_span(::Span::_new(Span::Nightly(tt.span()))); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 256 | o.into() |
| 257 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 258 | proc_macro::TokenTree::Ident(s) => ::Ident::_new(Ident::Nightly(s)).into(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 259 | proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Nightly(l)).into(), |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 260 | }) |
| 261 | } |
| 262 | |
| 263 | fn size_hint(&self) -> (usize, Option<usize>) { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 264 | match self { |
| 265 | TokenTreeIter::Nightly(tts) => tts.size_hint(), |
| 266 | TokenTreeIter::Stable(tts) => tts.size_hint(), |
| 267 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 271 | impl fmt::Debug for TokenTreeIter { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 272 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 273 | f.debug_struct("TokenTreeIter").finish() |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 277 | pub use stable::FileName; |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 278 | |
| 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 Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 282 | pub enum SourceFile { |
| 283 | Nightly(proc_macro::SourceFile, FileName), |
| 284 | Stable(stable::SourceFile), |
| 285 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 286 | |
| 287 | impl SourceFile { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 288 | fn nightly(sf: proc_macro::SourceFile) -> Self { |
Alex Crichton | 9f0a28a | 2018-07-21 18:53:35 -0700 | [diff] [blame] | 289 | let filename = stable::file_name(sf.path().display().to_string()); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 290 | SourceFile::Nightly(sf, filename) |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 291 | } |
| 292 | |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 293 | /// Get the path to this source file as a string. |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 294 | pub fn path(&self) -> &FileName { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 295 | match self { |
| 296 | SourceFile::Nightly(_, f) => f, |
| 297 | SourceFile::Stable(a) => a.path(), |
| 298 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | pub fn is_real(&self) -> bool { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 302 | match self { |
| 303 | SourceFile::Nightly(a, _) => a.is_real(), |
| 304 | SourceFile::Stable(a) => a.is_real(), |
| 305 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 309 | impl AsRef<FileName> for SourceFile { |
| 310 | fn as_ref(&self) -> &FileName { |
| 311 | self.path() |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | impl fmt::Debug for SourceFile { |
| 316 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 317 | match self { |
| 318 | SourceFile::Nightly(a, _) => a.fmt(f), |
| 319 | SourceFile::Stable(a) => a.fmt(f), |
| 320 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Nika Layzell | 1ecb6ce | 2017-12-30 14:34:05 -0500 | [diff] [blame] | 324 | pub struct LineColumn { |
| 325 | pub line: usize, |
| 326 | pub column: usize, |
| 327 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 328 | |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 329 | #[derive(Copy, Clone)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 330 | pub enum Span { |
| 331 | Nightly(proc_macro::Span), |
| 332 | Stable(stable::Span), |
Sergio Benitez | 1380508 | 2018-01-04 01:25:45 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 335 | impl Span { |
| 336 | pub fn call_site() -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 337 | if nightly_works() { |
| 338 | Span::Nightly(proc_macro::Span::call_site()) |
| 339 | } else { |
| 340 | Span::Stable(stable::Span::call_site()) |
| 341 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 342 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 343 | |
Alex Crichton | e6085b7 | 2017-11-21 07:24:25 -0800 | [diff] [blame] | 344 | pub fn def_site() -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 345 | if nightly_works() { |
| 346 | Span::Nightly(proc_macro::Span::def_site()) |
| 347 | } else { |
| 348 | Span::Stable(stable::Span::def_site()) |
| 349 | } |
Alex Crichton | 998f642 | 2017-11-19 08:06:27 -0800 | [diff] [blame] | 350 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 351 | |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 352 | pub fn resolved_at(&self, other: Span) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 353 | 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 Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | pub fn located_at(&self, other: Span) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 361 | 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 Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 366 | } |
| 367 | |
David Tolnay | 16a1720 | 2017-12-31 10:47:24 -0500 | [diff] [blame] | 368 | pub fn unstable(self) -> proc_macro::Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 369 | match self { |
| 370 | Span::Nightly(s) => s, |
| 371 | Span::Stable(_) => mismatch(), |
| 372 | } |
David Tolnay | 16a1720 | 2017-12-31 10:47:24 -0500 | [diff] [blame] | 373 | } |
| 374 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 375 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 376 | pub fn source_file(&self) -> SourceFile { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 377 | match self { |
| 378 | Span::Nightly(s) => SourceFile::nightly(s.source_file()), |
| 379 | Span::Stable(s) => SourceFile::Stable(s.source_file()), |
| 380 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 381 | } |
| 382 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 383 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 384 | pub fn start(&self) -> LineColumn { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 385 | 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 Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 395 | } |
| 396 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 397 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 398 | pub fn end(&self) -> LineColumn { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 399 | 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 Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 409 | } |
| 410 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 411 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 412 | pub fn join(&self, other: Span) -> Option<Span> { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 413 | 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 Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 419 | } |
Alex Crichton | e24f734 | 2018-04-05 17:58:11 -0700 | [diff] [blame] | 420 | |
| 421 | pub fn eq(&self, other: &Span) -> bool { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 422 | 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 | |
| 437 | impl 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 | |
| 443 | impl From<stable::Span> for Span { |
| 444 | fn from(inner: stable::Span) -> Span { |
| 445 | Span::Stable(inner) |
Alex Crichton | e24f734 | 2018-04-05 17:58:11 -0700 | [diff] [blame] | 446 | } |
Alex Crichton | 998f642 | 2017-11-19 08:06:27 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 449 | impl fmt::Debug for Span { |
| 450 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 451 | match self { |
| 452 | Span::Nightly(s) => s.fmt(f), |
| 453 | Span::Stable(s) => s.fmt(f), |
| 454 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 458 | #[derive(Clone)] |
| 459 | pub enum Ident { |
| 460 | Nightly(proc_macro::Ident), |
| 461 | Stable(stable::Ident), |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 462 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 463 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 464 | impl Ident { |
| 465 | pub fn new(string: &str, span: Span) -> Ident { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 466 | match span { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 467 | Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new(string, s)), |
| 468 | Span::Stable(s) => Ident::Stable(stable::Ident::new(string, s)), |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 469 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 470 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 471 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 472 | 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 Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 476 | } |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | pub fn span(&self) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 480 | match self { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 481 | Ident::Nightly(t) => Span::Nightly(t.span()), |
| 482 | Ident::Stable(t) => Span::Stable(t.span()), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 483 | } |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 487 | match (self, span) { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 488 | (Ident::Nightly(t), Span::Nightly(s)) => t.set_span(s), |
| 489 | (Ident::Stable(t), Span::Stable(s)) => t.set_span(s), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 490 | _ => mismatch(), |
| 491 | } |
| 492 | } |
| 493 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 494 | fn unwrap_nightly(self) -> proc_macro::Ident { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 495 | match self { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 496 | Ident::Nightly(s) => s, |
| 497 | Ident::Stable(_) => mismatch(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 498 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 502 | impl fmt::Display for Ident { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 503 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 504 | match self { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 505 | Ident::Nightly(t) => t.fmt(f), |
| 506 | Ident::Stable(t) => t.fmt(f), |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | impl 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 Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 516 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
| 520 | #[derive(Clone)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 521 | pub enum Literal { |
| 522 | Nightly(proc_macro::Literal), |
| 523 | Stable(stable::Literal), |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 524 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 525 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 526 | macro_rules! suffixed_numbers { |
| 527 | ($($name:ident => $kind:ident,)*) => ($( |
| 528 | pub fn $name(n: $kind) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 529 | if nightly_works() { |
| 530 | Literal::Nightly(proc_macro::Literal::$name(n)) |
| 531 | } else { |
| 532 | Literal::Stable(stable::Literal::$name(n)) |
| 533 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 534 | } |
| 535 | )*) |
| 536 | } |
| 537 | |
| 538 | macro_rules! unsuffixed_integers { |
| 539 | ($($name:ident => $kind:ident,)*) => ($( |
| 540 | pub fn $name(n: $kind) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 541 | if nightly_works() { |
| 542 | Literal::Nightly(proc_macro::Literal::$name(n)) |
| 543 | } else { |
| 544 | Literal::Stable(stable::Literal::$name(n)) |
| 545 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 546 | } |
| 547 | )*) |
| 548 | } |
| 549 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 550 | impl Literal { |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 551 | 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 Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 581 | if nightly_works() { |
| 582 | Literal::Nightly(proc_macro::Literal::f32_unsuffixed(f)) |
| 583 | } else { |
| 584 | Literal::Stable(stable::Literal::f32_unsuffixed(f)) |
| 585 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | pub fn f64_unsuffixed(f: f64) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 589 | if nightly_works() { |
| 590 | Literal::Nightly(proc_macro::Literal::f64_unsuffixed(f)) |
| 591 | } else { |
| 592 | Literal::Stable(stable::Literal::f64_unsuffixed(f)) |
| 593 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 596 | pub fn string(t: &str) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 597 | if nightly_works() { |
| 598 | Literal::Nightly(proc_macro::Literal::string(t)) |
| 599 | } else { |
| 600 | Literal::Stable(stable::Literal::string(t)) |
| 601 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | pub fn character(t: char) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 605 | if nightly_works() { |
| 606 | Literal::Nightly(proc_macro::Literal::character(t)) |
| 607 | } else { |
| 608 | Literal::Stable(stable::Literal::character(t)) |
| 609 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | pub fn byte_string(bytes: &[u8]) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 613 | if nightly_works() { |
| 614 | Literal::Nightly(proc_macro::Literal::byte_string(bytes)) |
| 615 | } else { |
| 616 | Literal::Stable(stable::Literal::byte_string(bytes)) |
| 617 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 620 | pub fn span(&self) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 621 | match self { |
| 622 | Literal::Nightly(lit) => Span::Nightly(lit.span()), |
| 623 | Literal::Stable(lit) => Span::Stable(lit.span()), |
| 624 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 627 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 628 | 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 | |
| 643 | impl From<stable::Literal> for Literal { |
| 644 | fn from(s: stable::Literal) -> Literal { |
| 645 | Literal::Stable(s) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
| 649 | impl fmt::Display for Literal { |
| 650 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 651 | match self { |
| 652 | Literal::Nightly(t) => t.fmt(f), |
| 653 | Literal::Stable(t) => t.fmt(f), |
| 654 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
| 658 | impl fmt::Debug for Literal { |
| 659 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 660 | match self { |
| 661 | Literal::Nightly(t) => t.fmt(f), |
| 662 | Literal::Stable(t) => t.fmt(f), |
| 663 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 664 | } |
| 665 | } |