diff options
author | Jeff Vander Stoep <jeffv@google.com> | 2022-12-13 13:07:23 +0100 |
---|---|---|
committer | Jeff Vander Stoep <jeffv@google.com> | 2022-12-13 13:08:00 +0100 |
commit | 4a8e734e53eff9b3f001ee924358db0db596c6a5 (patch) | |
tree | fd37219d0d16282ddb9369941bbb611ad44a1900 /src/wrapper.rs | |
parent | 1401d56e2bb01a28cb43e394311992213d9ff057 (diff) | |
download | proc-macro2-4a8e734e53eff9b3f001ee924358db0db596c6a5.tar.gz |
Upgrade proc-macro2 to 1.0.47
This project was upgraded with external_updater.
Usage: tools/external_updater/updater.sh update rust/crates/proc-macro2
For more info, check https://cs.android.com/android/platform/superproject/+/master:tools/external_updater/README.md
Test: TreeHugger
Change-Id: Ie945a24b3e0d36cd8b4110031ace1b70e847e6ff
Diffstat (limited to 'src/wrapper.rs')
-rw-r--r-- | src/wrapper.rs | 56 |
1 files changed, 38 insertions, 18 deletions
diff --git a/src/wrapper.rs b/src/wrapper.rs index 2ba76cc..47d1494 100644 --- a/src/wrapper.rs +++ b/src/wrapper.rs @@ -1,12 +1,12 @@ use crate::detection::inside_proc_macro; use crate::{fallback, Delimiter, Punct, Spacing, TokenTree}; -use std::fmt::{self, Debug, Display}; -use std::iter::FromIterator; -use std::ops::RangeBounds; +use core::fmt::{self, Debug, Display}; +use core::iter::FromIterator; +use core::ops::RangeBounds; +use core::str::FromStr; use std::panic; #[cfg(super_unstable)] use std::path::PathBuf; -use std::str::FromStr; #[derive(Clone)] pub(crate) enum TokenStream { @@ -350,12 +350,6 @@ impl Iterator for TokenTreeIter { } } -impl Debug for TokenTreeIter { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("TokenTreeIter").finish() - } -} - #[derive(Clone, PartialEq, Eq)] #[cfg(super_unstable)] pub(crate) enum SourceFile { @@ -511,6 +505,22 @@ impl Span { } } + #[cfg(super_unstable)] + pub fn before(&self) -> Span { + match self { + Span::Compiler(s) => Span::Compiler(s.before()), + Span::Fallback(s) => Span::Fallback(s.before()), + } + } + + #[cfg(super_unstable)] + pub fn after(&self) -> Span { + match self { + Span::Compiler(s) => Span::Compiler(s.after()), + Span::Fallback(s) => Span::Fallback(s.after()), + } + } + pub fn join(&self, other: Span) -> Option<Span> { let ret = match (self, other) { #[cfg(proc_macro_span)] @@ -694,16 +704,26 @@ impl Ident { pub fn new_raw(string: &str, span: Span) -> Self { match span { + #[cfg(not(no_ident_new_raw))] + Span::Compiler(s) => Ident::Compiler(proc_macro::Ident::new_raw(string, s)), + #[cfg(no_ident_new_raw)] Span::Compiler(s) => { - let p: proc_macro::TokenStream = string.parse().unwrap(); - let ident = match p.into_iter().next() { - Some(proc_macro::TokenTree::Ident(mut i)) => { - i.set_span(s); - i + let _ = proc_macro::Ident::new(string, s); + // At this point the un-r#-prefixed string is known to be a + // valid identifier. Try to produce a valid raw identifier by + // running the `TokenStream` parser, and unwrapping the first + // token as an `Ident`. + let raw_prefixed = format!("r#{}", string); + if let Ok(ts) = raw_prefixed.parse::<proc_macro::TokenStream>() { + let mut iter = ts.into_iter(); + if let (Some(proc_macro::TokenTree::Ident(mut id)), None) = + (iter.next(), iter.next()) + { + id.set_span(s); + return Ident::Compiler(id); } - _ => panic!(), - }; - Ident::Compiler(ident) + } + panic!("not allowed as a raw identifier: `{}`", raw_prefixed) } Span::Fallback(s) => Ident::Fallback(fallback::Ident::new_raw(string, s)), } |