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 | } |
David Tolnay | 5c58c53 | 2018-08-13 11:33:51 -0700 | [diff] [blame] | 64 | |
| 65 | fn unwrap_stable(self) -> stable::TokenStream { |
| 66 | match self { |
| 67 | TokenStream::Nightly(_) => mismatch(), |
| 68 | TokenStream::Stable(s) => s, |
| 69 | } |
| 70 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | impl FromStr for TokenStream { |
| 74 | type Err = LexError; |
| 75 | |
| 76 | fn from_str(src: &str) -> Result<TokenStream, LexError> { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 77 | if nightly_works() { |
| 78 | Ok(TokenStream::Nightly(src.parse()?)) |
| 79 | } else { |
| 80 | Ok(TokenStream::Stable(src.parse()?)) |
| 81 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | impl fmt::Display for TokenStream { |
| 86 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 87 | match self { |
| 88 | TokenStream::Nightly(tts) => tts.fmt(f), |
| 89 | TokenStream::Stable(tts) => tts.fmt(f), |
| 90 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | impl From<proc_macro::TokenStream> for TokenStream { |
| 95 | fn from(inner: proc_macro::TokenStream) -> TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 96 | TokenStream::Nightly(inner) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | impl From<TokenStream> for proc_macro::TokenStream { |
| 101 | fn from(inner: TokenStream) -> proc_macro::TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 102 | match inner { |
| 103 | TokenStream::Nightly(inner) => inner, |
| 104 | TokenStream::Stable(inner) => inner.to_string().parse().unwrap(), |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | impl From<stable::TokenStream> for TokenStream { |
| 110 | fn from(inner: stable::TokenStream) -> TokenStream { |
| 111 | TokenStream::Stable(inner) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 115 | impl From<TokenTree> for TokenStream { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 116 | fn from(token: TokenTree) -> TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 117 | if !nightly_works() { |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 118 | return TokenStream::Stable(token.into()); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 119 | } |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 120 | let tt: proc_macro::TokenTree = match token { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 121 | 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 Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 128 | let span = tt.span(); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 129 | let mut group = proc_macro::Group::new(delim, tt.stream.inner.unwrap_nightly()); |
| 130 | group.set_span(span.inner.unwrap_nightly()); |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 131 | group.into() |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 132 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 133 | TokenTree::Punct(tt) => { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 134 | let spacing = match tt.spacing() { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 135 | Spacing::Joint => proc_macro::Spacing::Joint, |
| 136 | Spacing::Alone => proc_macro::Spacing::Alone, |
| 137 | }; |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 138 | let mut op = proc_macro::Punct::new(tt.as_char(), spacing); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 139 | op.set_span(tt.span().inner.unwrap_nightly()); |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 140 | op.into() |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 141 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 142 | TokenTree::Ident(tt) => tt.inner.unwrap_nightly().into(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 143 | TokenTree::Literal(tt) => tt.inner.unwrap_nightly().into(), |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 144 | }; |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 145 | TokenStream::Nightly(tt.into()) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 149 | impl iter::FromIterator<TokenTree> for TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 150 | fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self { |
| 151 | if nightly_works() { |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 152 | let trees = trees |
| 153 | .into_iter() |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 154 | .map(TokenStream::from) |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 155 | .flat_map(|t| match t { |
| 156 | TokenStream::Nightly(s) => s, |
| 157 | TokenStream::Stable(_) => mismatch(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 158 | }); |
| 159 | TokenStream::Nightly(trees.collect()) |
| 160 | } else { |
| 161 | TokenStream::Stable(trees.into_iter().collect()) |
| 162 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 166 | impl Extend<TokenTree> for TokenStream { |
| 167 | fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) { |
| 168 | match self { |
| 169 | TokenStream::Nightly(tts) => { |
David Tolnay | 40d4ffd | 2018-08-12 13:49:09 -0700 | [diff] [blame] | 170 | tts.extend( |
| 171 | streams |
| 172 | .into_iter() |
| 173 | .map(|t| TokenStream::from(t).unwrap_nightly()), |
| 174 | ); |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 175 | } |
| 176 | TokenStream::Stable(tts) => tts.extend(streams), |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
David Tolnay | 5c58c53 | 2018-08-13 11:33:51 -0700 | [diff] [blame] | 181 | impl Extend<TokenStream> for TokenStream { |
| 182 | fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) { |
| 183 | match self { |
| 184 | TokenStream::Nightly(tts) => { |
| 185 | tts.extend(streams.into_iter().map(|stream| stream.unwrap_nightly())) |
| 186 | } |
| 187 | TokenStream::Stable(tts) => { |
| 188 | tts.extend(streams.into_iter().map(|stream| stream.unwrap_stable())) |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 194 | impl fmt::Debug for TokenStream { |
| 195 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 196 | match self { |
| 197 | TokenStream::Nightly(tts) => tts.fmt(f), |
| 198 | TokenStream::Stable(tts) => tts.fmt(f), |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | impl From<proc_macro::LexError> for LexError { |
| 204 | fn from(e: proc_macro::LexError) -> LexError { |
| 205 | LexError::Nightly(e) |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | impl From<stable::LexError> for LexError { |
| 210 | fn from(e: stable::LexError) -> LexError { |
| 211 | LexError::Stable(e) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | impl fmt::Debug for LexError { |
| 216 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 217 | match self { |
| 218 | LexError::Nightly(e) => e.fmt(f), |
| 219 | LexError::Stable(e) => e.fmt(f), |
| 220 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 224 | pub enum TokenTreeIter { |
| 225 | Nightly(proc_macro::token_stream::IntoIter), |
| 226 | Stable(stable::TokenTreeIter), |
| 227 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 228 | |
| 229 | impl IntoIterator for TokenStream { |
| 230 | type Item = TokenTree; |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 231 | type IntoIter = TokenTreeIter; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 232 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 233 | fn into_iter(self) -> TokenTreeIter { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 234 | match self { |
| 235 | TokenStream::Nightly(tts) => TokenTreeIter::Nightly(tts.into_iter()), |
| 236 | TokenStream::Stable(tts) => TokenTreeIter::Stable(tts.into_iter()), |
| 237 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 241 | impl Iterator for TokenTreeIter { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 242 | type Item = TokenTree; |
| 243 | |
| 244 | fn next(&mut self) -> Option<TokenTree> { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 245 | let token = match self { |
| 246 | TokenTreeIter::Nightly(iter) => iter.next()?, |
| 247 | TokenTreeIter::Stable(iter) => return iter.next(), |
| 248 | }; |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 249 | Some(match token { |
| 250 | proc_macro::TokenTree::Group(tt) => { |
| 251 | let delim = match tt.delimiter() { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 252 | proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis, |
| 253 | proc_macro::Delimiter::Bracket => Delimiter::Bracket, |
| 254 | proc_macro::Delimiter::Brace => Delimiter::Brace, |
| 255 | proc_macro::Delimiter::None => Delimiter::None, |
| 256 | }; |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 257 | let stream = ::TokenStream::_new(TokenStream::Nightly(tt.stream())); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 258 | let mut g = Group::new(delim, stream); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 259 | g.set_span(::Span::_new(Span::Nightly(tt.span()))); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 260 | g.into() |
| 261 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 262 | proc_macro::TokenTree::Punct(tt) => { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 263 | let spacing = match tt.spacing() { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 264 | proc_macro::Spacing::Joint => Spacing::Joint, |
| 265 | proc_macro::Spacing::Alone => Spacing::Alone, |
| 266 | }; |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 267 | let mut o = Punct::new(tt.as_char(), spacing); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 268 | o.set_span(::Span::_new(Span::Nightly(tt.span()))); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 269 | o.into() |
| 270 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 271 | proc_macro::TokenTree::Ident(s) => ::Ident::_new(Ident::Nightly(s)).into(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 272 | proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Nightly(l)).into(), |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 273 | }) |
| 274 | } |
| 275 | |
| 276 | fn size_hint(&self) -> (usize, Option<usize>) { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 277 | match self { |
| 278 | TokenTreeIter::Nightly(tts) => tts.size_hint(), |
| 279 | TokenTreeIter::Stable(tts) => tts.size_hint(), |
| 280 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 284 | impl fmt::Debug for TokenTreeIter { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 285 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 286 | f.debug_struct("TokenTreeIter").finish() |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 290 | pub use stable::FileName; |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 291 | |
| 292 | // NOTE: We have to generate our own filename object here because we can't wrap |
| 293 | // the one provided by proc_macro. |
| 294 | #[derive(Clone, PartialEq, Eq)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 295 | pub enum SourceFile { |
| 296 | Nightly(proc_macro::SourceFile, FileName), |
| 297 | Stable(stable::SourceFile), |
| 298 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 299 | |
| 300 | impl SourceFile { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 301 | fn nightly(sf: proc_macro::SourceFile) -> Self { |
Alex Crichton | 9f0a28a | 2018-07-21 18:53:35 -0700 | [diff] [blame] | 302 | let filename = stable::file_name(sf.path().display().to_string()); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 303 | SourceFile::Nightly(sf, filename) |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 304 | } |
| 305 | |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 306 | /// Get the path to this source file as a string. |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 307 | pub fn path(&self) -> &FileName { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 308 | match self { |
| 309 | SourceFile::Nightly(_, f) => f, |
| 310 | SourceFile::Stable(a) => a.path(), |
| 311 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | pub fn is_real(&self) -> bool { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 315 | match self { |
| 316 | SourceFile::Nightly(a, _) => a.is_real(), |
| 317 | SourceFile::Stable(a) => a.is_real(), |
| 318 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 322 | impl AsRef<FileName> for SourceFile { |
| 323 | fn as_ref(&self) -> &FileName { |
| 324 | self.path() |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
| 328 | impl fmt::Debug for SourceFile { |
| 329 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 330 | match self { |
| 331 | SourceFile::Nightly(a, _) => a.fmt(f), |
| 332 | SourceFile::Stable(a) => a.fmt(f), |
| 333 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Nika Layzell | 1ecb6ce | 2017-12-30 14:34:05 -0500 | [diff] [blame] | 337 | pub struct LineColumn { |
| 338 | pub line: usize, |
| 339 | pub column: usize, |
| 340 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 341 | |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 342 | #[derive(Copy, Clone)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 343 | pub enum Span { |
| 344 | Nightly(proc_macro::Span), |
| 345 | Stable(stable::Span), |
Sergio Benitez | 1380508 | 2018-01-04 01:25:45 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 348 | impl Span { |
| 349 | pub fn call_site() -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 350 | if nightly_works() { |
| 351 | Span::Nightly(proc_macro::Span::call_site()) |
| 352 | } else { |
| 353 | Span::Stable(stable::Span::call_site()) |
| 354 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 355 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 356 | |
Alex Crichton | e6085b7 | 2017-11-21 07:24:25 -0800 | [diff] [blame] | 357 | pub fn def_site() -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 358 | if nightly_works() { |
| 359 | Span::Nightly(proc_macro::Span::def_site()) |
| 360 | } else { |
| 361 | Span::Stable(stable::Span::def_site()) |
| 362 | } |
Alex Crichton | 998f642 | 2017-11-19 08:06:27 -0800 | [diff] [blame] | 363 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 364 | |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 365 | pub fn resolved_at(&self, other: Span) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 366 | match (self, other) { |
| 367 | (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.resolved_at(b)), |
| 368 | (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.resolved_at(b)), |
| 369 | _ => mismatch(), |
| 370 | } |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | pub fn located_at(&self, other: Span) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 374 | match (self, other) { |
| 375 | (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.located_at(b)), |
| 376 | (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.located_at(b)), |
| 377 | _ => mismatch(), |
| 378 | } |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 379 | } |
| 380 | |
David Tolnay | 16a1720 | 2017-12-31 10:47:24 -0500 | [diff] [blame] | 381 | pub fn unstable(self) -> proc_macro::Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 382 | match self { |
| 383 | Span::Nightly(s) => s, |
| 384 | Span::Stable(_) => mismatch(), |
| 385 | } |
David Tolnay | 16a1720 | 2017-12-31 10:47:24 -0500 | [diff] [blame] | 386 | } |
| 387 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 388 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 389 | pub fn source_file(&self) -> SourceFile { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 390 | match self { |
| 391 | Span::Nightly(s) => SourceFile::nightly(s.source_file()), |
| 392 | Span::Stable(s) => SourceFile::Stable(s.source_file()), |
| 393 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 394 | } |
| 395 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 396 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 397 | pub fn start(&self) -> LineColumn { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 398 | match self { |
| 399 | Span::Nightly(s) => { |
| 400 | let proc_macro::LineColumn { line, column } = s.start(); |
| 401 | LineColumn { line, column } |
| 402 | } |
| 403 | Span::Stable(s) => { |
| 404 | let stable::LineColumn { line, column } = s.start(); |
| 405 | LineColumn { line, column } |
| 406 | } |
| 407 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 408 | } |
| 409 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 410 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 411 | pub fn end(&self) -> LineColumn { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 412 | match self { |
| 413 | Span::Nightly(s) => { |
| 414 | let proc_macro::LineColumn { line, column } = s.end(); |
| 415 | LineColumn { line, column } |
| 416 | } |
| 417 | Span::Stable(s) => { |
| 418 | let stable::LineColumn { line, column } = s.end(); |
| 419 | LineColumn { line, column } |
| 420 | } |
| 421 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 422 | } |
| 423 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 424 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 425 | pub fn join(&self, other: Span) -> Option<Span> { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 426 | let ret = match (self, other) { |
| 427 | (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.join(b)?), |
| 428 | (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.join(b)?), |
| 429 | _ => return None, |
| 430 | }; |
| 431 | Some(ret) |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 432 | } |
Alex Crichton | e24f734 | 2018-04-05 17:58:11 -0700 | [diff] [blame] | 433 | |
| 434 | pub fn eq(&self, other: &Span) -> bool { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 435 | match (self, other) { |
| 436 | (Span::Nightly(a), Span::Nightly(b)) => a.eq(b), |
| 437 | (Span::Stable(a), Span::Stable(b)) => a.eq(b), |
| 438 | _ => false, |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | fn unwrap_nightly(self) -> proc_macro::Span { |
| 443 | match self { |
| 444 | Span::Nightly(s) => s, |
| 445 | Span::Stable(_) => mismatch(), |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | impl From<proc_macro::Span> for ::Span { |
| 451 | fn from(proc_span: proc_macro::Span) -> ::Span { |
| 452 | ::Span::_new(Span::Nightly(proc_span)) |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | impl From<stable::Span> for Span { |
| 457 | fn from(inner: stable::Span) -> Span { |
| 458 | Span::Stable(inner) |
Alex Crichton | e24f734 | 2018-04-05 17:58:11 -0700 | [diff] [blame] | 459 | } |
Alex Crichton | 998f642 | 2017-11-19 08:06:27 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 462 | impl fmt::Debug for Span { |
| 463 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 464 | match self { |
| 465 | Span::Nightly(s) => s.fmt(f), |
| 466 | Span::Stable(s) => s.fmt(f), |
| 467 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 471 | #[derive(Clone)] |
| 472 | pub enum Ident { |
| 473 | Nightly(proc_macro::Ident), |
| 474 | Stable(stable::Ident), |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 475 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 476 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 477 | impl Ident { |
| 478 | pub fn new(string: &str, span: Span) -> Ident { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 479 | match span { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 480 | Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new(string, s)), |
| 481 | Span::Stable(s) => Ident::Stable(stable::Ident::new(string, s)), |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 482 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 483 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 484 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 485 | pub fn new_raw(string: &str, span: Span) -> Ident { |
| 486 | match span { |
| 487 | Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new_raw(string, s)), |
| 488 | Span::Stable(s) => Ident::Stable(stable::Ident::new_raw(string, s)), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 489 | } |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | pub fn span(&self) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 493 | match self { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 494 | Ident::Nightly(t) => Span::Nightly(t.span()), |
| 495 | Ident::Stable(t) => Span::Stable(t.span()), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 496 | } |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 500 | match (self, span) { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 501 | (Ident::Nightly(t), Span::Nightly(s)) => t.set_span(s), |
| 502 | (Ident::Stable(t), Span::Stable(s)) => t.set_span(s), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 503 | _ => mismatch(), |
| 504 | } |
| 505 | } |
| 506 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 507 | fn unwrap_nightly(self) -> proc_macro::Ident { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 508 | match self { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 509 | Ident::Nightly(s) => s, |
| 510 | Ident::Stable(_) => mismatch(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 511 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 515 | impl fmt::Display for Ident { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 516 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 517 | match self { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 518 | Ident::Nightly(t) => t.fmt(f), |
| 519 | Ident::Stable(t) => t.fmt(f), |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | impl fmt::Debug for Ident { |
| 525 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 526 | match self { |
| 527 | Ident::Nightly(t) => t.fmt(f), |
| 528 | Ident::Stable(t) => t.fmt(f), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 529 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
| 533 | #[derive(Clone)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 534 | pub enum Literal { |
| 535 | Nightly(proc_macro::Literal), |
| 536 | Stable(stable::Literal), |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 537 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 538 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 539 | macro_rules! suffixed_numbers { |
| 540 | ($($name:ident => $kind:ident,)*) => ($( |
| 541 | pub fn $name(n: $kind) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 542 | if nightly_works() { |
| 543 | Literal::Nightly(proc_macro::Literal::$name(n)) |
| 544 | } else { |
| 545 | Literal::Stable(stable::Literal::$name(n)) |
| 546 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 547 | } |
| 548 | )*) |
| 549 | } |
| 550 | |
| 551 | macro_rules! unsuffixed_integers { |
| 552 | ($($name:ident => $kind:ident,)*) => ($( |
| 553 | pub fn $name(n: $kind) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 554 | if nightly_works() { |
| 555 | Literal::Nightly(proc_macro::Literal::$name(n)) |
| 556 | } else { |
| 557 | Literal::Stable(stable::Literal::$name(n)) |
| 558 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 559 | } |
| 560 | )*) |
| 561 | } |
| 562 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 563 | impl Literal { |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 564 | suffixed_numbers! { |
| 565 | u8_suffixed => u8, |
| 566 | u16_suffixed => u16, |
| 567 | u32_suffixed => u32, |
| 568 | u64_suffixed => u64, |
| 569 | usize_suffixed => usize, |
| 570 | i8_suffixed => i8, |
| 571 | i16_suffixed => i16, |
| 572 | i32_suffixed => i32, |
| 573 | i64_suffixed => i64, |
| 574 | isize_suffixed => isize, |
| 575 | |
| 576 | f32_suffixed => f32, |
| 577 | f64_suffixed => f64, |
| 578 | } |
| 579 | |
| 580 | unsuffixed_integers! { |
| 581 | u8_unsuffixed => u8, |
| 582 | u16_unsuffixed => u16, |
| 583 | u32_unsuffixed => u32, |
| 584 | u64_unsuffixed => u64, |
| 585 | usize_unsuffixed => usize, |
| 586 | i8_unsuffixed => i8, |
| 587 | i16_unsuffixed => i16, |
| 588 | i32_unsuffixed => i32, |
| 589 | i64_unsuffixed => i64, |
| 590 | isize_unsuffixed => isize, |
| 591 | } |
| 592 | |
| 593 | pub fn f32_unsuffixed(f: f32) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 594 | if nightly_works() { |
| 595 | Literal::Nightly(proc_macro::Literal::f32_unsuffixed(f)) |
| 596 | } else { |
| 597 | Literal::Stable(stable::Literal::f32_unsuffixed(f)) |
| 598 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | pub fn f64_unsuffixed(f: f64) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 602 | if nightly_works() { |
| 603 | Literal::Nightly(proc_macro::Literal::f64_unsuffixed(f)) |
| 604 | } else { |
| 605 | Literal::Stable(stable::Literal::f64_unsuffixed(f)) |
| 606 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 609 | pub fn string(t: &str) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 610 | if nightly_works() { |
| 611 | Literal::Nightly(proc_macro::Literal::string(t)) |
| 612 | } else { |
| 613 | Literal::Stable(stable::Literal::string(t)) |
| 614 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | pub fn character(t: char) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 618 | if nightly_works() { |
| 619 | Literal::Nightly(proc_macro::Literal::character(t)) |
| 620 | } else { |
| 621 | Literal::Stable(stable::Literal::character(t)) |
| 622 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | pub fn byte_string(bytes: &[u8]) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 626 | if nightly_works() { |
| 627 | Literal::Nightly(proc_macro::Literal::byte_string(bytes)) |
| 628 | } else { |
| 629 | Literal::Stable(stable::Literal::byte_string(bytes)) |
| 630 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 633 | pub fn span(&self) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 634 | match self { |
| 635 | Literal::Nightly(lit) => Span::Nightly(lit.span()), |
| 636 | Literal::Stable(lit) => Span::Stable(lit.span()), |
| 637 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 640 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 641 | match (self, span) { |
| 642 | (Literal::Nightly(lit), Span::Nightly(s)) => lit.set_span(s), |
| 643 | (Literal::Stable(lit), Span::Stable(s)) => lit.set_span(s), |
| 644 | _ => mismatch(), |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | fn unwrap_nightly(self) -> proc_macro::Literal { |
| 649 | match self { |
| 650 | Literal::Nightly(s) => s, |
| 651 | Literal::Stable(_) => mismatch(), |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | impl From<stable::Literal> for Literal { |
| 657 | fn from(s: stable::Literal) -> Literal { |
| 658 | Literal::Stable(s) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
| 662 | impl fmt::Display for Literal { |
| 663 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 664 | match self { |
| 665 | Literal::Nightly(t) => t.fmt(f), |
| 666 | Literal::Stable(t) => t.fmt(f), |
| 667 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
| 671 | impl fmt::Debug for Literal { |
| 672 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 673 | match self { |
| 674 | Literal::Nightly(t) => t.fmt(f), |
| 675 | Literal::Stable(t) => t.fmt(f), |
| 676 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 677 | } |
| 678 | } |