Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/proc-macro2/src/wrapper.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / proc-macro2 / src / wrapper.rs` 987 lines ยท 31198 bytes Raw Blame History 1 use crate::detection::inside_proc_macro; 2 use crate::fallback::{self, FromStr2 as _}; 3 #[cfg(span_locations)] 4 use crate::location::LineColumn; 5 #[cfg(proc_macro_span)] 6 use crate::probe::proc_macro_span; 7 #[cfg(all(span_locations, proc_macro_span_file))] 8 use crate::probe::proc_macro_span_file; 9 #[cfg(all(span_locations, proc_macro_span_location))] 10 use crate::probe::proc_macro_span_location; 11 use crate::{Delimiter, Punct, Spacing, TokenTree}; 12 #[cfg(all(span_locations, not(proc_macro_span_file)))] 13 use alloc::borrow::ToOwned as _; 14 use alloc::string::{String, ToString as _}; 15 use alloc::vec::Vec; 16 use core::ffi::CStr; 17 use core::fmt::{self, Debug, Display}; 18 #[cfg(span_locations)] 19 use core::ops::Range; 20 use core::ops::RangeBounds; 21 #[cfg(span_locations)] 22 use std::path::PathBuf; 23 24 #[derive(Clone)] 25 pub(crate) enum TokenStream { 26 Compiler(DeferredTokenStream), 27 Fallback(fallback::TokenStream), 28 } 29 30 // Work around https://github.com/rust-lang/rust/issues/65080. 31 // In `impl Extend for TokenStream` which is used heavily by quote, 32 // we hold on to the appended tokens and do proc_macro::TokenStream::extend as 33 // late as possible to batch together consecutive uses of the Extend impl. 34 #[derive(Clone)] 35 pub(crate) struct DeferredTokenStream { 36 stream: proc_macro::TokenStream, 37 extra: Vec, 38 } 39 40 pub(crate) enum LexError { 41 Compiler(proc_macro::LexError), 42 Fallback(fallback::LexError), 43 44 // Rustc was supposed to return a LexError, but it panicked instead. 45 // https://github.com/rust-lang/rust/issues/58736 46 CompilerPanic, 47 } 48 49 #[cold] 50 fn mismatch(line: u32) -> ! { 51 #[cfg(procmacro2_backtrace)] 52 { 53 let backtrace = std::backtrace::Backtrace::force_capture(); 54 panic!("compiler/fallback mismatch L{}\n\n{}", line, backtrace) 55 } 56 #[cfg(not(procmacro2_backtrace))] 57 { 58 panic!("compiler/fallback mismatch L{}", line) 59 } 60 } 61 62 impl DeferredTokenStream { 63 fn new(stream: proc_macro::TokenStream) -> Self { 64 DeferredTokenStream { 65 stream, 66 extra: Vec::new(), 67 } 68 } 69 70 fn is_empty(&self) -> bool { 71 self.stream.is_empty() && self.extra.is_empty() 72 } 73 74 fn evaluate_now(&mut self) { 75 // If-check provides a fast short circuit for the common case of `extra` 76 // being empty, which saves a round trip over the proc macro bridge. 77 // Improves macro expansion time in winrt by 6% in debug mode. 78 if !self.extra.is_empty() { 79 self.stream.extend(self.extra.drain(..)); 80 } 81 } 82 83 fn into_token_stream(mut self) -> proc_macro::TokenStream { 84 self.evaluate_now(); 85 self.stream 86 } 87 } 88 89 impl TokenStream { 90 pub(crate) fn new() -> Self { 91 if inside_proc_macro() { 92 TokenStream::Compiler(DeferredTokenStream::new(proc_macro::TokenStream::new())) 93 } else { 94 TokenStream::Fallback(fallback::TokenStream::new()) 95 } 96 } 97 98 pub(crate) fn from_str_checked(src: &str) -> Result { 99 if inside_proc_macro() { 100 Ok(TokenStream::Compiler(DeferredTokenStream::new( 101 proc_macro::TokenStream::from_str_checked(src)?, 102 ))) 103 } else { 104 Ok(TokenStream::Fallback( 105 fallback::TokenStream::from_str_checked(src)?, 106 )) 107 } 108 } 109 110 pub(crate) fn is_empty(&self) -> bool { 111 match self { 112 TokenStream::Compiler(tts) => tts.is_empty(), 113 TokenStream::Fallback(tts) => tts.is_empty(), 114 } 115 } 116 117 fn unwrap_nightly(self) -> proc_macro::TokenStream { 118 match self { 119 TokenStream::Compiler(s) => s.into_token_stream(), 120 TokenStream::Fallback(_) => mismatch(line!()), 121 } 122 } 123 124 fn unwrap_stable(self) -> fallback::TokenStream { 125 match self { 126 TokenStream::Compiler(_) => mismatch(line!()), 127 TokenStream::Fallback(s) => s, 128 } 129 } 130 } 131 132 impl Display for TokenStream { 133 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 134 match self { 135 TokenStream::Compiler(tts) => Display::fmt(&tts.clone().into_token_stream(), f), 136 TokenStream::Fallback(tts) => Display::fmt(tts, f), 137 } 138 } 139 } 140 141 impl From for TokenStream { 142 fn from(inner: proc_macro::TokenStream) -> Self { 143 TokenStream::Compiler(DeferredTokenStream::new(inner)) 144 } 145 } 146 147 impl From for proc_macro::TokenStream { 148 fn from(inner: TokenStream) -> Self { 149 match inner { 150 TokenStream::Compiler(inner) => inner.into_token_stream(), 151 TokenStream::Fallback(inner) => { 152 proc_macro::TokenStream::from_str_unchecked(&inner.to_string()) 153 } 154 } 155 } 156 } 157 158 impl From for TokenStream { 159 fn from(inner: fallback::TokenStream) -> Self { 160 TokenStream::Fallback(inner) 161 } 162 } 163 164 // Assumes inside_proc_macro(). 165 fn into_compiler_token(token: TokenTree) -> proc_macro::TokenTree { 166 match token { 167 TokenTree::Group(tt) => proc_macro::TokenTree::Group(tt.inner.unwrap_nightly()), 168 TokenTree::Punct(tt) => { 169 let spacing = match tt.spacing() { 170 Spacing::Joint => proc_macro::Spacing::Joint, 171 Spacing::Alone => proc_macro::Spacing::Alone, 172 }; 173 let mut punct = proc_macro::Punct::new(tt.as_char(), spacing); 174 punct.set_span(tt.span().inner.unwrap_nightly()); 175 proc_macro::TokenTree::Punct(punct) 176 } 177 TokenTree::Ident(tt) => proc_macro::TokenTree::Ident(tt.inner.unwrap_nightly()), 178 TokenTree::Literal(tt) => proc_macro::TokenTree::Literal(tt.inner.unwrap_nightly()), 179 } 180 } 181 182 impl From for TokenStream { 183 fn from(token: TokenTree) -> Self { 184 if inside_proc_macro() { 185 TokenStream::Compiler(DeferredTokenStream::new(proc_macro::TokenStream::from( 186 into_compiler_token(token), 187 ))) 188 } else { 189 TokenStream::Fallback(fallback::TokenStream::from(token)) 190 } 191 } 192 } 193 194 impl FromIterator for TokenStream { 195 fn from_iter>(tokens: I) -> Self { 196 if inside_proc_macro() { 197 TokenStream::Compiler(DeferredTokenStream::new( 198 tokens.into_iter().map(into_compiler_token).collect(), 199 )) 200 } else { 201 TokenStream::Fallback(tokens.into_iter().collect()) 202 } 203 } 204 } 205 206 impl FromIterator for TokenStream { 207 fn from_iter>(streams: I) -> Self { 208 let mut streams = streams.into_iter(); 209 match streams.next() { 210 Some(TokenStream::Compiler(mut first)) => { 211 first.evaluate_now(); 212 first.stream.extend(streams.map(|s| match s { 213 TokenStream::Compiler(s) => s.into_token_stream(), 214 TokenStream::Fallback(_) => mismatch(line!()), 215 })); 216 TokenStream::Compiler(first) 217 } 218 Some(TokenStream::Fallback(mut first)) => { 219 first.extend(streams.map(|s| match s { 220 TokenStream::Fallback(s) => s, 221 TokenStream::Compiler(_) => mismatch(line!()), 222 })); 223 TokenStream::Fallback(first) 224 } 225 None => TokenStream::new(), 226 } 227 } 228 } 229 230 impl Extend for TokenStream { 231 fn extend>(&mut self, tokens: I) { 232 match self { 233 TokenStream::Compiler(tts) => { 234 // Here is the reason for DeferredTokenStream. 235 for token in tokens { 236 tts.extra.push(into_compiler_token(token)); 237 } 238 } 239 TokenStream::Fallback(tts) => tts.extend(tokens), 240 } 241 } 242 } 243 244 impl Extend for TokenStream { 245 fn extend>(&mut self, streams: I) { 246 match self { 247 TokenStream::Compiler(tts) => { 248 tts.evaluate_now(); 249 tts.stream 250 .extend(streams.into_iter().map(TokenStream::unwrap_nightly)); 251 } 252 TokenStream::Fallback(tts) => { 253 tts.extend(streams.into_iter().map(TokenStream::unwrap_stable)); 254 } 255 } 256 } 257 } 258 259 impl Debug for TokenStream { 260 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 261 match self { 262 TokenStream::Compiler(tts) => Debug::fmt(&tts.clone().into_token_stream(), f), 263 TokenStream::Fallback(tts) => Debug::fmt(tts, f), 264 } 265 } 266 } 267 268 impl LexError { 269 pub(crate) fn span(&self) -> Span { 270 match self { 271 LexError::Compiler(_) | LexError::CompilerPanic => Span::call_site(), 272 LexError::Fallback(e) => Span::Fallback(e.span()), 273 } 274 } 275 } 276 277 impl From for LexError { 278 fn from(e: proc_macro::LexError) -> Self { 279 LexError::Compiler(e) 280 } 281 } 282 283 impl From for LexError { 284 fn from(e: fallback::LexError) -> Self { 285 LexError::Fallback(e) 286 } 287 } 288 289 impl Debug for LexError { 290 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 291 match self { 292 LexError::Compiler(e) => Debug::fmt(e, f), 293 LexError::Fallback(e) => Debug::fmt(e, f), 294 LexError::CompilerPanic => { 295 let fallback = fallback::LexError::call_site(); 296 Debug::fmt(&fallback, f) 297 } 298 } 299 } 300 } 301 302 impl Display for LexError { 303 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 304 match self { 305 LexError::Compiler(e) => Display::fmt(e, f), 306 LexError::Fallback(e) => Display::fmt(e, f), 307 LexError::CompilerPanic => { 308 let fallback = fallback::LexError::call_site(); 309 Display::fmt(&fallback, f) 310 } 311 } 312 } 313 } 314 315 #[derive(Clone)] 316 pub(crate) enum TokenTreeIter { 317 Compiler(proc_macro::token_stream::IntoIter), 318 Fallback(fallback::TokenTreeIter), 319 } 320 321 impl IntoIterator for TokenStream { 322 type Item = TokenTree; 323 type IntoIter = TokenTreeIter; 324 325 fn into_iter(self) -> TokenTreeIter { 326 match self { 327 TokenStream::Compiler(tts) => { 328 TokenTreeIter::Compiler(tts.into_token_stream().into_iter()) 329 } 330 TokenStream::Fallback(tts) => TokenTreeIter::Fallback(tts.into_iter()), 331 } 332 } 333 } 334 335 impl Iterator for TokenTreeIter { 336 type Item = TokenTree; 337 338 fn next(&mut self) -> Option { 339 let token = match self { 340 TokenTreeIter::Compiler(iter) => iter.next()?, 341 TokenTreeIter::Fallback(iter) => return iter.next(), 342 }; 343 Some(match token { 344 proc_macro::TokenTree::Group(tt) => { 345 TokenTree::Group(crate::Group::_new(Group::Compiler(tt))) 346 } 347 proc_macro::TokenTree::Punct(tt) => { 348 let spacing = match tt.spacing() { 349 proc_macro::Spacing::Joint => Spacing::Joint, 350 proc_macro::Spacing::Alone => Spacing::Alone, 351 }; 352 let mut o = Punct::new(tt.as_char(), spacing); 353 o.set_span(crate::Span::_new(Span::Compiler(tt.span()))); 354 TokenTree::Punct(o) 355 } 356 proc_macro::TokenTree::Ident(s) => { 357 TokenTree::Ident(crate::Ident::_new(Ident::Compiler(s))) 358 } 359 proc_macro::TokenTree::Literal(l) => { 360 TokenTree::Literal(crate::Literal::_new(Literal::Compiler(l))) 361 } 362 }) 363 } 364 365 fn size_hint(&self) -> (usize, Option) { 366 match self { 367 TokenTreeIter::Compiler(tts) => tts.size_hint(), 368 TokenTreeIter::Fallback(tts) => tts.size_hint(), 369 } 370 } 371 } 372 373 #[derive(Copy, Clone)] 374 pub(crate) enum Span { 375 Compiler(proc_macro::Span), 376 Fallback(fallback::Span), 377 } 378 379 impl Span { 380 pub(crate) fn call_site() -> Self { 381 if inside_proc_macro() { 382 Span::Compiler(proc_macro::Span::call_site()) 383 } else { 384 Span::Fallback(fallback::Span::call_site()) 385 } 386 } 387 388 pub(crate) fn mixed_site() -> Self { 389 if inside_proc_macro() { 390 Span::Compiler(proc_macro::Span::mixed_site()) 391 } else { 392 Span::Fallback(fallback::Span::mixed_site()) 393 } 394 } 395 396 #[cfg(super_unstable)] 397 pub(crate) fn def_site() -> Self { 398 if inside_proc_macro() { 399 Span::Compiler(proc_macro::Span::def_site()) 400 } else { 401 Span::Fallback(fallback::Span::def_site()) 402 } 403 } 404 405 pub(crate) fn resolved_at(&self, other: Span) -> Span { 406 match (self, other) { 407 (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.resolved_at(b)), 408 (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.resolved_at(b)), 409 (Span::Compiler(_), Span::Fallback(_)) => mismatch(line!()), 410 (Span::Fallback(_), Span::Compiler(_)) => mismatch(line!()), 411 } 412 } 413 414 pub(crate) fn located_at(&self, other: Span) -> Span { 415 match (self, other) { 416 (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.located_at(b)), 417 (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.located_at(b)), 418 (Span::Compiler(_), Span::Fallback(_)) => mismatch(line!()), 419 (Span::Fallback(_), Span::Compiler(_)) => mismatch(line!()), 420 } 421 } 422 423 pub(crate) fn unwrap(self) -> proc_macro::Span { 424 match self { 425 Span::Compiler(s) => s, 426 Span::Fallback(_) => panic!("proc_macro::Span is only available in procedural macros"), 427 } 428 } 429 430 #[cfg(span_locations)] 431 pub(crate) fn byte_range(&self) -> Range { 432 match self { 433 #[cfg(proc_macro_span)] 434 Span::Compiler(s) => proc_macro_span::byte_range(s), 435 #[cfg(not(proc_macro_span))] 436 Span::Compiler(_) => 0..0, 437 Span::Fallback(s) => s.byte_range(), 438 } 439 } 440 441 #[cfg(span_locations)] 442 pub(crate) fn start(&self) -> LineColumn { 443 match self { 444 #[cfg(proc_macro_span_location)] 445 Span::Compiler(s) => LineColumn { 446 line: proc_macro_span_location::line(s), 447 column: proc_macro_span_location::column(s).saturating_sub(1), 448 }, 449 #[cfg(not(proc_macro_span_location))] 450 Span::Compiler(_) => LineColumn { line: 0, column: 0 }, 451 Span::Fallback(s) => s.start(), 452 } 453 } 454 455 #[cfg(span_locations)] 456 pub(crate) fn end(&self) -> LineColumn { 457 match self { 458 #[cfg(proc_macro_span_location)] 459 Span::Compiler(s) => { 460 let end = proc_macro_span_location::end(s); 461 LineColumn { 462 line: proc_macro_span_location::line(&end), 463 column: proc_macro_span_location::column(&end).saturating_sub(1), 464 } 465 } 466 #[cfg(not(proc_macro_span_location))] 467 Span::Compiler(_) => LineColumn { line: 0, column: 0 }, 468 Span::Fallback(s) => s.end(), 469 } 470 } 471 472 #[cfg(span_locations)] 473 pub(crate) fn file(&self) -> String { 474 match self { 475 #[cfg(proc_macro_span_file)] 476 Span::Compiler(s) => proc_macro_span_file::file(s), 477 #[cfg(not(proc_macro_span_file))] 478 Span::Compiler(_) => "".to_owned(), 479 Span::Fallback(s) => s.file(), 480 } 481 } 482 483 #[cfg(span_locations)] 484 pub(crate) fn local_file(&self) -> Option { 485 match self { 486 #[cfg(proc_macro_span_file)] 487 Span::Compiler(s) => proc_macro_span_file::local_file(s), 488 #[cfg(not(proc_macro_span_file))] 489 Span::Compiler(_) => None, 490 Span::Fallback(s) => s.local_file(), 491 } 492 } 493 494 pub(crate) fn join(&self, other: Span) -> Option { 495 let ret = match (self, other) { 496 #[cfg(proc_macro_span)] 497 (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(proc_macro_span::join(a, b)?), 498 (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.join(b)?), 499 _ => return None, 500 }; 501 Some(ret) 502 } 503 504 #[cfg(super_unstable)] 505 pub(crate) fn eq(&self, other: &Span) -> bool { 506 match (self, other) { 507 (Span::Compiler(a), Span::Compiler(b)) => a.eq(b), 508 (Span::Fallback(a), Span::Fallback(b)) => a.eq(b), 509 _ => false, 510 } 511 } 512 513 pub(crate) fn source_text(&self) -> Option { 514 match self { 515 #[cfg(not(no_source_text))] 516 Span::Compiler(s) => s.source_text(), 517 #[cfg(no_source_text)] 518 Span::Compiler(_) => None, 519 Span::Fallback(s) => s.source_text(), 520 } 521 } 522 523 fn unwrap_nightly(self) -> proc_macro::Span { 524 match self { 525 Span::Compiler(s) => s, 526 Span::Fallback(_) => mismatch(line!()), 527 } 528 } 529 } 530 531 impl From for crate::Span { 532 fn from(proc_span: proc_macro::Span) -> Self { 533 crate::Span::_new(Span::Compiler(proc_span)) 534 } 535 } 536 537 impl From for Span { 538 fn from(inner: fallback::Span) -> Self { 539 Span::Fallback(inner) 540 } 541 } 542 543 impl Debug for Span { 544 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 545 match self { 546 Span::Compiler(s) => Debug::fmt(s, f), 547 Span::Fallback(s) => Debug::fmt(s, f), 548 } 549 } 550 } 551 552 pub(crate) fn debug_span_field_if_nontrivial(debug: &mut fmt::DebugStruct, span: Span) { 553 match span { 554 Span::Compiler(s) => { 555 debug.field("span", &s); 556 } 557 Span::Fallback(s) => fallback::debug_span_field_if_nontrivial(debug, s), 558 } 559 } 560 561 #[derive(Clone)] 562 pub(crate) enum Group { 563 Compiler(proc_macro::Group), 564 Fallback(fallback::Group), 565 } 566 567 impl Group { 568 pub(crate) fn new(delimiter: Delimiter, stream: TokenStream) -> Self { 569 match stream { 570 TokenStream::Compiler(tts) => { 571 let delimiter = match delimiter { 572 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis, 573 Delimiter::Bracket => proc_macro::Delimiter::Bracket, 574 Delimiter::Brace => proc_macro::Delimiter::Brace, 575 Delimiter::None => proc_macro::Delimiter::None, 576 }; 577 Group::Compiler(proc_macro::Group::new(delimiter, tts.into_token_stream())) 578 } 579 TokenStream::Fallback(stream) => { 580 Group::Fallback(fallback::Group::new(delimiter, stream)) 581 } 582 } 583 } 584 585 pub(crate) fn delimiter(&self) -> Delimiter { 586 match self { 587 Group::Compiler(g) => match g.delimiter() { 588 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis, 589 proc_macro::Delimiter::Bracket => Delimiter::Bracket, 590 proc_macro::Delimiter::Brace => Delimiter::Brace, 591 proc_macro::Delimiter::None => Delimiter::None, 592 }, 593 Group::Fallback(g) => g.delimiter(), 594 } 595 } 596 597 pub(crate) fn stream(&self) -> TokenStream { 598 match self { 599 Group::Compiler(g) => TokenStream::Compiler(DeferredTokenStream::new(g.stream())), 600 Group::Fallback(g) => TokenStream::Fallback(g.stream()), 601 } 602 } 603 604 pub(crate) fn span(&self) -> Span { 605 match self { 606 Group::Compiler(g) => Span::Compiler(g.span()), 607 Group::Fallback(g) => Span::Fallback(g.span()), 608 } 609 } 610 611 pub(crate) fn span_open(&self) -> Span { 612 match self { 613 Group::Compiler(g) => Span::Compiler(g.span_open()), 614 Group::Fallback(g) => Span::Fallback(g.span_open()), 615 } 616 } 617 618 pub(crate) fn span_close(&self) -> Span { 619 match self { 620 Group::Compiler(g) => Span::Compiler(g.span_close()), 621 Group::Fallback(g) => Span::Fallback(g.span_close()), 622 } 623 } 624 625 pub(crate) fn set_span(&mut self, span: Span) { 626 match (self, span) { 627 (Group::Compiler(g), Span::Compiler(s)) => g.set_span(s), 628 (Group::Fallback(g), Span::Fallback(s)) => g.set_span(s), 629 (Group::Compiler(_), Span::Fallback(_)) => mismatch(line!()), 630 (Group::Fallback(_), Span::Compiler(_)) => mismatch(line!()), 631 } 632 } 633 634 fn unwrap_nightly(self) -> proc_macro::Group { 635 match self { 636 Group::Compiler(g) => g, 637 Group::Fallback(_) => mismatch(line!()), 638 } 639 } 640 } 641 642 impl From for Group { 643 fn from(g: fallback::Group) -> Self { 644 Group::Fallback(g) 645 } 646 } 647 648 impl Display for Group { 649 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 650 match self { 651 Group::Compiler(group) => Display::fmt(group, formatter), 652 Group::Fallback(group) => Display::fmt(group, formatter), 653 } 654 } 655 } 656 657 impl Debug for Group { 658 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 659 match self { 660 Group::Compiler(group) => Debug::fmt(group, formatter), 661 Group::Fallback(group) => Debug::fmt(group, formatter), 662 } 663 } 664 } 665 666 #[derive(Clone)] 667 pub(crate) enum Ident { 668 Compiler(proc_macro::Ident), 669 Fallback(fallback::Ident), 670 } 671 672 impl Ident { 673 #[track_caller] 674 pub(crate) fn new_checked(string: &str, span: Span) -> Self { 675 match span { 676 Span::Compiler(s) => Ident::Compiler(proc_macro::Ident::new(string, s)), 677 Span::Fallback(s) => Ident::Fallback(fallback::Ident::new_checked(string, s)), 678 } 679 } 680 681 #[track_caller] 682 pub(crate) fn new_raw_checked(string: &str, span: Span) -> Self { 683 match span { 684 Span::Compiler(s) => Ident::Compiler(proc_macro::Ident::new_raw(string, s)), 685 Span::Fallback(s) => Ident::Fallback(fallback::Ident::new_raw_checked(string, s)), 686 } 687 } 688 689 pub(crate) fn span(&self) -> Span { 690 match self { 691 Ident::Compiler(t) => Span::Compiler(t.span()), 692 Ident::Fallback(t) => Span::Fallback(t.span()), 693 } 694 } 695 696 pub(crate) fn set_span(&mut self, span: Span) { 697 match (self, span) { 698 (Ident::Compiler(t), Span::Compiler(s)) => t.set_span(s), 699 (Ident::Fallback(t), Span::Fallback(s)) => t.set_span(s), 700 (Ident::Compiler(_), Span::Fallback(_)) => mismatch(line!()), 701 (Ident::Fallback(_), Span::Compiler(_)) => mismatch(line!()), 702 } 703 } 704 705 fn unwrap_nightly(self) -> proc_macro::Ident { 706 match self { 707 Ident::Compiler(s) => s, 708 Ident::Fallback(_) => mismatch(line!()), 709 } 710 } 711 } 712 713 impl From for Ident { 714 fn from(inner: fallback::Ident) -> Self { 715 Ident::Fallback(inner) 716 } 717 } 718 719 impl PartialEq for Ident { 720 fn eq(&self, other: &Ident) -> bool { 721 match (self, other) { 722 (Ident::Compiler(t), Ident::Compiler(o)) => t.to_string() == o.to_string(), 723 (Ident::Fallback(t), Ident::Fallback(o)) => t == o, 724 (Ident::Compiler(_), Ident::Fallback(_)) => mismatch(line!()), 725 (Ident::Fallback(_), Ident::Compiler(_)) => mismatch(line!()), 726 } 727 } 728 } 729 730 impl PartialEq for Ident 731 where 732 T: ?Sized + AsRef, 733 { 734 fn eq(&self, other: &T) -> bool { 735 let other = other.as_ref(); 736 match self { 737 Ident::Compiler(t) => t.to_string() == other, 738 Ident::Fallback(t) => t == other, 739 } 740 } 741 } 742 743 impl Display for Ident { 744 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 745 match self { 746 Ident::Compiler(t) => Display::fmt(t, f), 747 Ident::Fallback(t) => Display::fmt(t, f), 748 } 749 } 750 } 751 752 impl Debug for Ident { 753 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 754 match self { 755 Ident::Compiler(t) => Debug::fmt(t, f), 756 Ident::Fallback(t) => Debug::fmt(t, f), 757 } 758 } 759 } 760 761 #[derive(Clone)] 762 pub(crate) enum Literal { 763 Compiler(proc_macro::Literal), 764 Fallback(fallback::Literal), 765 } 766 767 macro_rules! suffixed_numbers { 768 ($($name:ident => $kind:ident,)*) => ($( 769 pub(crate) fn $name(n: $kind) -> Literal { 770 if inside_proc_macro() { 771 Literal::Compiler(proc_macro::Literal::$name(n)) 772 } else { 773 Literal::Fallback(fallback::Literal::$name(n)) 774 } 775 } 776 )*) 777 } 778 779 macro_rules! unsuffixed_integers { 780 ($($name:ident => $kind:ident,)*) => ($( 781 pub(crate) fn $name(n: $kind) -> Literal { 782 if inside_proc_macro() { 783 Literal::Compiler(proc_macro::Literal::$name(n)) 784 } else { 785 Literal::Fallback(fallback::Literal::$name(n)) 786 } 787 } 788 )*) 789 } 790 791 impl Literal { 792 pub(crate) fn from_str_checked(repr: &str) -> Result { 793 if inside_proc_macro() { 794 let literal = proc_macro::Literal::from_str_checked(repr)?; 795 Ok(Literal::Compiler(literal)) 796 } else { 797 let literal = fallback::Literal::from_str_checked(repr)?; 798 Ok(Literal::Fallback(literal)) 799 } 800 } 801 802 pub(crate) unsafe fn from_str_unchecked(repr: &str) -> Self { 803 if inside_proc_macro() { 804 Literal::Compiler(proc_macro::Literal::from_str_unchecked(repr)) 805 } else { 806 Literal::Fallback(unsafe { fallback::Literal::from_str_unchecked(repr) }) 807 } 808 } 809 810 suffixed_numbers! { 811 u8_suffixed => u8, 812 u16_suffixed => u16, 813 u32_suffixed => u32, 814 u64_suffixed => u64, 815 u128_suffixed => u128, 816 usize_suffixed => usize, 817 i8_suffixed => i8, 818 i16_suffixed => i16, 819 i32_suffixed => i32, 820 i64_suffixed => i64, 821 i128_suffixed => i128, 822 isize_suffixed => isize, 823 824 f32_suffixed => f32, 825 f64_suffixed => f64, 826 } 827 828 unsuffixed_integers! { 829 u8_unsuffixed => u8, 830 u16_unsuffixed => u16, 831 u32_unsuffixed => u32, 832 u64_unsuffixed => u64, 833 u128_unsuffixed => u128, 834 usize_unsuffixed => usize, 835 i8_unsuffixed => i8, 836 i16_unsuffixed => i16, 837 i32_unsuffixed => i32, 838 i64_unsuffixed => i64, 839 i128_unsuffixed => i128, 840 isize_unsuffixed => isize, 841 } 842 843 pub(crate) fn f32_unsuffixed(f: f32) -> Literal { 844 if inside_proc_macro() { 845 Literal::Compiler(proc_macro::Literal::f32_unsuffixed(f)) 846 } else { 847 Literal::Fallback(fallback::Literal::f32_unsuffixed(f)) 848 } 849 } 850 851 pub(crate) fn f64_unsuffixed(f: f64) -> Literal { 852 if inside_proc_macro() { 853 Literal::Compiler(proc_macro::Literal::f64_unsuffixed(f)) 854 } else { 855 Literal::Fallback(fallback::Literal::f64_unsuffixed(f)) 856 } 857 } 858 859 pub(crate) fn string(string: &str) -> Literal { 860 if inside_proc_macro() { 861 Literal::Compiler(proc_macro::Literal::string(string)) 862 } else { 863 Literal::Fallback(fallback::Literal::string(string)) 864 } 865 } 866 867 pub(crate) fn character(ch: char) -> Literal { 868 if inside_proc_macro() { 869 Literal::Compiler(proc_macro::Literal::character(ch)) 870 } else { 871 Literal::Fallback(fallback::Literal::character(ch)) 872 } 873 } 874 875 pub(crate) fn byte_character(byte: u8) -> Literal { 876 if inside_proc_macro() { 877 Literal::Compiler({ 878 #[cfg(not(no_literal_byte_character))] 879 { 880 proc_macro::Literal::byte_character(byte) 881 } 882 883 #[cfg(no_literal_byte_character)] 884 { 885 let fallback = fallback::Literal::byte_character(byte); 886 proc_macro::Literal::from_str_unchecked(&fallback.repr) 887 } 888 }) 889 } else { 890 Literal::Fallback(fallback::Literal::byte_character(byte)) 891 } 892 } 893 894 pub(crate) fn byte_string(bytes: &[u8]) -> Literal { 895 if inside_proc_macro() { 896 Literal::Compiler(proc_macro::Literal::byte_string(bytes)) 897 } else { 898 Literal::Fallback(fallback::Literal::byte_string(bytes)) 899 } 900 } 901 902 pub(crate) fn c_string(string: &CStr) -> Literal { 903 if inside_proc_macro() { 904 Literal::Compiler({ 905 #[cfg(not(no_literal_c_string))] 906 { 907 proc_macro::Literal::c_string(string) 908 } 909 910 #[cfg(no_literal_c_string)] 911 { 912 let fallback = fallback::Literal::c_string(string); 913 proc_macro::Literal::from_str_unchecked(&fallback.repr) 914 } 915 }) 916 } else { 917 Literal::Fallback(fallback::Literal::c_string(string)) 918 } 919 } 920 921 pub(crate) fn span(&self) -> Span { 922 match self { 923 Literal::Compiler(lit) => Span::Compiler(lit.span()), 924 Literal::Fallback(lit) => Span::Fallback(lit.span()), 925 } 926 } 927 928 pub(crate) fn set_span(&mut self, span: Span) { 929 match (self, span) { 930 (Literal::Compiler(lit), Span::Compiler(s)) => lit.set_span(s), 931 (Literal::Fallback(lit), Span::Fallback(s)) => lit.set_span(s), 932 (Literal::Compiler(_), Span::Fallback(_)) => mismatch(line!()), 933 (Literal::Fallback(_), Span::Compiler(_)) => mismatch(line!()), 934 } 935 } 936 937 pub(crate) fn subspan>(&self, range: R) -> Option { 938 match self { 939 #[cfg(proc_macro_span)] 940 Literal::Compiler(lit) => proc_macro_span::subspan(lit, range).map(Span::Compiler), 941 #[cfg(not(proc_macro_span))] 942 Literal::Compiler(_lit) => None, 943 Literal::Fallback(lit) => lit.subspan(range).map(Span::Fallback), 944 } 945 } 946 947 fn unwrap_nightly(self) -> proc_macro::Literal { 948 match self { 949 Literal::Compiler(s) => s, 950 Literal::Fallback(_) => mismatch(line!()), 951 } 952 } 953 } 954 955 impl From for Literal { 956 fn from(s: fallback::Literal) -> Self { 957 Literal::Fallback(s) 958 } 959 } 960 961 impl Display for Literal { 962 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 963 match self { 964 Literal::Compiler(t) => Display::fmt(t, f), 965 Literal::Fallback(t) => Display::fmt(t, f), 966 } 967 } 968 } 969 970 impl Debug for Literal { 971 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 972 match self { 973 Literal::Compiler(t) => Debug::fmt(t, f), 974 Literal::Fallback(t) => Debug::fmt(t, f), 975 } 976 } 977 } 978 979 #[cfg(span_locations)] 980 pub(crate) fn invalidate_current_thread_spans() { 981 if inside_proc_macro() { 982 panic!( 983 "proc_macro2::extra::invalidate_current_thread_spans is not available in procedural macros" 984 ); 985 } 986 crate::fallback::invalidate_current_thread_spans(); 987 }