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 | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 170 | *tts = tts |
| 171 | .clone() |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 172 | .into_iter() |
| 173 | .chain( |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 174 | streams |
| 175 | .into_iter() |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 176 | .map(TokenStream::from) |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 177 | .flat_map(|t| match t { |
| 178 | TokenStream::Nightly(tts) => tts.into_iter(), |
| 179 | _ => panic!(), |
| 180 | }), |
David Tolnay | 5a556cf | 2018-08-12 13:49:39 -0700 | [diff] [blame] | 181 | ).collect(); |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 182 | } |
| 183 | TokenStream::Stable(tts) => tts.extend(streams), |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
David Tolnay | 5c58c53 | 2018-08-13 11:33:51 -0700 | [diff] [blame^] | 188 | impl 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 Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 201 | impl fmt::Debug for TokenStream { |
| 202 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 203 | match self { |
| 204 | TokenStream::Nightly(tts) => tts.fmt(f), |
| 205 | TokenStream::Stable(tts) => tts.fmt(f), |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | impl From<proc_macro::LexError> for LexError { |
| 211 | fn from(e: proc_macro::LexError) -> LexError { |
| 212 | LexError::Nightly(e) |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | impl From<stable::LexError> for LexError { |
| 217 | fn from(e: stable::LexError) -> LexError { |
| 218 | LexError::Stable(e) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | impl fmt::Debug for LexError { |
| 223 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 224 | match self { |
| 225 | LexError::Nightly(e) => e.fmt(f), |
| 226 | LexError::Stable(e) => e.fmt(f), |
| 227 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 231 | pub enum TokenTreeIter { |
| 232 | Nightly(proc_macro::token_stream::IntoIter), |
| 233 | Stable(stable::TokenTreeIter), |
| 234 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 235 | |
| 236 | impl IntoIterator for TokenStream { |
| 237 | type Item = TokenTree; |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 238 | type IntoIter = TokenTreeIter; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 239 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 240 | fn into_iter(self) -> TokenTreeIter { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 241 | match self { |
| 242 | TokenStream::Nightly(tts) => TokenTreeIter::Nightly(tts.into_iter()), |
| 243 | TokenStream::Stable(tts) => TokenTreeIter::Stable(tts.into_iter()), |
| 244 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 248 | impl Iterator for TokenTreeIter { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 249 | type Item = TokenTree; |
| 250 | |
| 251 | fn next(&mut self) -> Option<TokenTree> { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 252 | let token = match self { |
| 253 | TokenTreeIter::Nightly(iter) => iter.next()?, |
| 254 | TokenTreeIter::Stable(iter) => return iter.next(), |
| 255 | }; |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 256 | Some(match token { |
| 257 | proc_macro::TokenTree::Group(tt) => { |
| 258 | let delim = match tt.delimiter() { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 259 | 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 Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 264 | let stream = ::TokenStream::_new(TokenStream::Nightly(tt.stream())); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 265 | let mut g = Group::new(delim, stream); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 266 | g.set_span(::Span::_new(Span::Nightly(tt.span()))); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 267 | g.into() |
| 268 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 269 | proc_macro::TokenTree::Punct(tt) => { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 270 | let spacing = match tt.spacing() { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 271 | proc_macro::Spacing::Joint => Spacing::Joint, |
| 272 | proc_macro::Spacing::Alone => Spacing::Alone, |
| 273 | }; |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 274 | let mut o = Punct::new(tt.as_char(), spacing); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 275 | o.set_span(::Span::_new(Span::Nightly(tt.span()))); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 276 | o.into() |
| 277 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 278 | proc_macro::TokenTree::Ident(s) => ::Ident::_new(Ident::Nightly(s)).into(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 279 | proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Nightly(l)).into(), |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 280 | }) |
| 281 | } |
| 282 | |
| 283 | fn size_hint(&self) -> (usize, Option<usize>) { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 284 | match self { |
| 285 | TokenTreeIter::Nightly(tts) => tts.size_hint(), |
| 286 | TokenTreeIter::Stable(tts) => tts.size_hint(), |
| 287 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 291 | impl fmt::Debug for TokenTreeIter { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 292 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 293 | f.debug_struct("TokenTreeIter").finish() |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 297 | pub use stable::FileName; |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 298 | |
| 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 Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 302 | pub enum SourceFile { |
| 303 | Nightly(proc_macro::SourceFile, FileName), |
| 304 | Stable(stable::SourceFile), |
| 305 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 306 | |
| 307 | impl SourceFile { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 308 | fn nightly(sf: proc_macro::SourceFile) -> Self { |
Alex Crichton | 9f0a28a | 2018-07-21 18:53:35 -0700 | [diff] [blame] | 309 | let filename = stable::file_name(sf.path().display().to_string()); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 310 | SourceFile::Nightly(sf, filename) |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 311 | } |
| 312 | |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 313 | /// Get the path to this source file as a string. |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 314 | pub fn path(&self) -> &FileName { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 315 | match self { |
| 316 | SourceFile::Nightly(_, f) => f, |
| 317 | SourceFile::Stable(a) => a.path(), |
| 318 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | pub fn is_real(&self) -> bool { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 322 | match self { |
| 323 | SourceFile::Nightly(a, _) => a.is_real(), |
| 324 | SourceFile::Stable(a) => a.is_real(), |
| 325 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 329 | impl AsRef<FileName> for SourceFile { |
| 330 | fn as_ref(&self) -> &FileName { |
| 331 | self.path() |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | impl fmt::Debug for SourceFile { |
| 336 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 337 | match self { |
| 338 | SourceFile::Nightly(a, _) => a.fmt(f), |
| 339 | SourceFile::Stable(a) => a.fmt(f), |
| 340 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
Nika Layzell | 1ecb6ce | 2017-12-30 14:34:05 -0500 | [diff] [blame] | 344 | pub struct LineColumn { |
| 345 | pub line: usize, |
| 346 | pub column: usize, |
| 347 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 348 | |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 349 | #[derive(Copy, Clone)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 350 | pub enum Span { |
| 351 | Nightly(proc_macro::Span), |
| 352 | Stable(stable::Span), |
Sergio Benitez | 1380508 | 2018-01-04 01:25:45 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 355 | impl Span { |
| 356 | pub fn call_site() -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 357 | if nightly_works() { |
| 358 | Span::Nightly(proc_macro::Span::call_site()) |
| 359 | } else { |
| 360 | Span::Stable(stable::Span::call_site()) |
| 361 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 362 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 363 | |
Alex Crichton | e6085b7 | 2017-11-21 07:24:25 -0800 | [diff] [blame] | 364 | pub fn def_site() -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 365 | if nightly_works() { |
| 366 | Span::Nightly(proc_macro::Span::def_site()) |
| 367 | } else { |
| 368 | Span::Stable(stable::Span::def_site()) |
| 369 | } |
Alex Crichton | 998f642 | 2017-11-19 08:06:27 -0800 | [diff] [blame] | 370 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 371 | |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 372 | pub fn resolved_at(&self, other: Span) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 373 | 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 Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | pub fn located_at(&self, other: Span) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 381 | 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 Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 386 | } |
| 387 | |
David Tolnay | 16a1720 | 2017-12-31 10:47:24 -0500 | [diff] [blame] | 388 | pub fn unstable(self) -> proc_macro::Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 389 | match self { |
| 390 | Span::Nightly(s) => s, |
| 391 | Span::Stable(_) => mismatch(), |
| 392 | } |
David Tolnay | 16a1720 | 2017-12-31 10:47:24 -0500 | [diff] [blame] | 393 | } |
| 394 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 395 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 396 | pub fn source_file(&self) -> SourceFile { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 397 | match self { |
| 398 | Span::Nightly(s) => SourceFile::nightly(s.source_file()), |
| 399 | Span::Stable(s) => SourceFile::Stable(s.source_file()), |
| 400 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 401 | } |
| 402 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 403 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 404 | pub fn start(&self) -> LineColumn { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 405 | 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 Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 415 | } |
| 416 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 417 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 418 | pub fn end(&self) -> LineColumn { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 419 | 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 Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 429 | } |
| 430 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 431 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 432 | pub fn join(&self, other: Span) -> Option<Span> { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 433 | 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 Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 439 | } |
Alex Crichton | e24f734 | 2018-04-05 17:58:11 -0700 | [diff] [blame] | 440 | |
| 441 | pub fn eq(&self, other: &Span) -> bool { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 442 | 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 | |
| 457 | impl 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 | |
| 463 | impl From<stable::Span> for Span { |
| 464 | fn from(inner: stable::Span) -> Span { |
| 465 | Span::Stable(inner) |
Alex Crichton | e24f734 | 2018-04-05 17:58:11 -0700 | [diff] [blame] | 466 | } |
Alex Crichton | 998f642 | 2017-11-19 08:06:27 -0800 | [diff] [blame] | 467 | } |
| 468 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 469 | impl fmt::Debug for Span { |
| 470 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 471 | match self { |
| 472 | Span::Nightly(s) => s.fmt(f), |
| 473 | Span::Stable(s) => s.fmt(f), |
| 474 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 478 | #[derive(Clone)] |
| 479 | pub enum Ident { |
| 480 | Nightly(proc_macro::Ident), |
| 481 | Stable(stable::Ident), |
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 | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 484 | impl Ident { |
| 485 | pub fn new(string: &str, span: Span) -> Ident { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 486 | match span { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 487 | Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new(string, s)), |
| 488 | Span::Stable(s) => Ident::Stable(stable::Ident::new(string, s)), |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 489 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 490 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 491 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 492 | 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 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 span(&self) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 500 | match self { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 501 | Ident::Nightly(t) => Span::Nightly(t.span()), |
| 502 | Ident::Stable(t) => Span::Stable(t.span()), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 503 | } |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 507 | match (self, span) { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 508 | (Ident::Nightly(t), Span::Nightly(s)) => t.set_span(s), |
| 509 | (Ident::Stable(t), Span::Stable(s)) => t.set_span(s), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 510 | _ => mismatch(), |
| 511 | } |
| 512 | } |
| 513 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 514 | fn unwrap_nightly(self) -> proc_macro::Ident { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 515 | match self { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 516 | Ident::Nightly(s) => s, |
| 517 | Ident::Stable(_) => mismatch(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 518 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 522 | impl fmt::Display for Ident { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 523 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 524 | match self { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 525 | Ident::Nightly(t) => t.fmt(f), |
| 526 | Ident::Stable(t) => t.fmt(f), |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | impl 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 Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 536 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
| 540 | #[derive(Clone)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 541 | pub enum Literal { |
| 542 | Nightly(proc_macro::Literal), |
| 543 | Stable(stable::Literal), |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 544 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 545 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 546 | macro_rules! suffixed_numbers { |
| 547 | ($($name:ident => $kind:ident,)*) => ($( |
| 548 | pub fn $name(n: $kind) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 549 | if nightly_works() { |
| 550 | Literal::Nightly(proc_macro::Literal::$name(n)) |
| 551 | } else { |
| 552 | Literal::Stable(stable::Literal::$name(n)) |
| 553 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 554 | } |
| 555 | )*) |
| 556 | } |
| 557 | |
| 558 | macro_rules! unsuffixed_integers { |
| 559 | ($($name:ident => $kind:ident,)*) => ($( |
| 560 | pub fn $name(n: $kind) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 561 | if nightly_works() { |
| 562 | Literal::Nightly(proc_macro::Literal::$name(n)) |
| 563 | } else { |
| 564 | Literal::Stable(stable::Literal::$name(n)) |
| 565 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 566 | } |
| 567 | )*) |
| 568 | } |
| 569 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 570 | impl Literal { |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 571 | 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 Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 601 | if nightly_works() { |
| 602 | Literal::Nightly(proc_macro::Literal::f32_unsuffixed(f)) |
| 603 | } else { |
| 604 | Literal::Stable(stable::Literal::f32_unsuffixed(f)) |
| 605 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | pub fn f64_unsuffixed(f: f64) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 609 | if nightly_works() { |
| 610 | Literal::Nightly(proc_macro::Literal::f64_unsuffixed(f)) |
| 611 | } else { |
| 612 | Literal::Stable(stable::Literal::f64_unsuffixed(f)) |
| 613 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 616 | pub fn string(t: &str) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 617 | if nightly_works() { |
| 618 | Literal::Nightly(proc_macro::Literal::string(t)) |
| 619 | } else { |
| 620 | Literal::Stable(stable::Literal::string(t)) |
| 621 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | pub fn character(t: char) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 625 | if nightly_works() { |
| 626 | Literal::Nightly(proc_macro::Literal::character(t)) |
| 627 | } else { |
| 628 | Literal::Stable(stable::Literal::character(t)) |
| 629 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | pub fn byte_string(bytes: &[u8]) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 633 | if nightly_works() { |
| 634 | Literal::Nightly(proc_macro::Literal::byte_string(bytes)) |
| 635 | } else { |
| 636 | Literal::Stable(stable::Literal::byte_string(bytes)) |
| 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 span(&self) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 641 | match self { |
| 642 | Literal::Nightly(lit) => Span::Nightly(lit.span()), |
| 643 | Literal::Stable(lit) => Span::Stable(lit.span()), |
| 644 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 647 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 648 | 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 | |
| 663 | impl From<stable::Literal> for Literal { |
| 664 | fn from(s: stable::Literal) -> Literal { |
| 665 | Literal::Stable(s) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | |
| 669 | impl fmt::Display for Literal { |
| 670 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 671 | match self { |
| 672 | Literal::Nightly(t) => t.fmt(f), |
| 673 | Literal::Stable(t) => t.fmt(f), |
| 674 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
| 678 | impl fmt::Debug for Literal { |
| 679 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 680 | match self { |
| 681 | Literal::Nightly(t) => t.fmt(f), |
| 682 | Literal::Stable(t) => t.fmt(f), |
| 683 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 684 | } |
| 685 | } |