1
Fork 0

review changes

longer names for RPC generics and reduced dependency on macros in the server.
This commit is contained in:
Nika Layzell 2022-06-27 20:03:56 -04:00
parent f28dfdf1c7
commit 64a7d57046
3 changed files with 127 additions and 131 deletions

View file

@ -55,8 +55,10 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)>
fn from_internal((stream, rustc): (TokenStream, &mut Rustc<'_, '_>)) -> Self { fn from_internal((stream, rustc): (TokenStream, &mut Rustc<'_, '_>)) -> Self {
use rustc_ast::token::*; use rustc_ast::token::*;
// Estimate the capacity as `stream.len()` rounded up to the next power
// of two to limit the number of required reallocations.
let mut trees = Vec::with_capacity(stream.len().next_power_of_two());
let mut cursor = stream.into_trees(); let mut cursor = stream.into_trees();
let mut trees = Vec::new();
while let Some((tree, spacing)) = cursor.next_with_spacing() { while let Some((tree, spacing)) = cursor.next_with_spacing() {
let joint = spacing == Joint; let joint = spacing == Joint;
@ -77,95 +79,78 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)>
tokenstream::TokenTree::Token(token) => token, tokenstream::TokenTree::Token(token) => token,
}; };
macro_rules! tt { let mut op = |s: &str| {
($ty:ident { $($field:ident $(: $value:expr)*),+ $(,)? }) => ( assert!(s.is_ascii());
trees.push(TokenTree::$ty(self::$ty { trees.extend(s.as_bytes().iter().enumerate().map(|(idx, &ch)| {
$($field $(: $value)*,)+ TokenTree::Punct(Punct { ch, joint: joint || idx != s.len() - 1, span })
span, }));
})) };
);
($ty:ident::$method:ident($($value:expr),*)) => (
trees.push(TokenTree::$ty(self::$ty::$method($($value,)* span)))
);
}
macro_rules! op {
($a:expr) => {{
tt!(Punct { ch: $a, joint });
}};
($a:expr, $b:expr) => {{
tt!(Punct { ch: $a, joint: true });
tt!(Punct { ch: $b, joint });
}};
($a:expr, $b:expr, $c:expr) => {{
tt!(Punct { ch: $a, joint: true });
tt!(Punct { ch: $b, joint: true });
tt!(Punct { ch: $c, joint });
}};
}
match kind { match kind {
Eq => op!('='), Eq => op("="),
Lt => op!('<'), Lt => op("<"),
Le => op!('<', '='), Le => op("<="),
EqEq => op!('=', '='), EqEq => op("=="),
Ne => op!('!', '='), Ne => op("!="),
Ge => op!('>', '='), Ge => op(">="),
Gt => op!('>'), Gt => op(">"),
AndAnd => op!('&', '&'), AndAnd => op("&&"),
OrOr => op!('|', '|'), OrOr => op("||"),
Not => op!('!'), Not => op("!"),
Tilde => op!('~'), Tilde => op("~"),
BinOp(Plus) => op!('+'), BinOp(Plus) => op("+"),
BinOp(Minus) => op!('-'), BinOp(Minus) => op("-"),
BinOp(Star) => op!('*'), BinOp(Star) => op("*"),
BinOp(Slash) => op!('/'), BinOp(Slash) => op("/"),
BinOp(Percent) => op!('%'), BinOp(Percent) => op("%"),
BinOp(Caret) => op!('^'), BinOp(Caret) => op("^"),
BinOp(And) => op!('&'), BinOp(And) => op("&"),
BinOp(Or) => op!('|'), BinOp(Or) => op("|"),
BinOp(Shl) => op!('<', '<'), BinOp(Shl) => op("<<"),
BinOp(Shr) => op!('>', '>'), BinOp(Shr) => op(">>"),
BinOpEq(Plus) => op!('+', '='), BinOpEq(Plus) => op("+="),
BinOpEq(Minus) => op!('-', '='), BinOpEq(Minus) => op("-="),
BinOpEq(Star) => op!('*', '='), BinOpEq(Star) => op("*="),
BinOpEq(Slash) => op!('/', '='), BinOpEq(Slash) => op("/="),
BinOpEq(Percent) => op!('%', '='), BinOpEq(Percent) => op("%="),
BinOpEq(Caret) => op!('^', '='), BinOpEq(Caret) => op("^="),
BinOpEq(And) => op!('&', '='), BinOpEq(And) => op("&="),
BinOpEq(Or) => op!('|', '='), BinOpEq(Or) => op("|="),
BinOpEq(Shl) => op!('<', '<', '='), BinOpEq(Shl) => op("<<="),
BinOpEq(Shr) => op!('>', '>', '='), BinOpEq(Shr) => op(">>="),
At => op!('@'), At => op("@"),
Dot => op!('.'), Dot => op("."),
DotDot => op!('.', '.'), DotDot => op(".."),
DotDotDot => op!('.', '.', '.'), DotDotDot => op("..."),
DotDotEq => op!('.', '.', '='), DotDotEq => op("..="),
Comma => op!(','), Comma => op(","),
Semi => op!(';'), Semi => op(";"),
Colon => op!(':'), Colon => op(":"),
ModSep => op!(':', ':'), ModSep => op("::"),
RArrow => op!('-', '>'), RArrow => op("->"),
LArrow => op!('<', '-'), LArrow => op("<-"),
FatArrow => op!('=', '>'), FatArrow => op("=>"),
Pound => op!('#'), Pound => op("#"),
Dollar => op!('$'), Dollar => op("$"),
Question => op!('?'), Question => op("?"),
SingleQuote => op!('\''), SingleQuote => op("'"),
Ident(name, false) if name == kw::DollarCrate => tt!(Ident::dollar_crate()), Ident(name, false) if name == kw::DollarCrate => trees.push(TokenTree::Ident(Ident::dollar_crate(span))),
Ident(name, is_raw) => tt!(Ident::new(rustc.sess(), name, is_raw)), Ident(name, is_raw) => trees.push(TokenTree::Ident(Ident::new(rustc.sess(), name, is_raw, span))),
Lifetime(name) => { Lifetime(name) => {
let ident = symbol::Ident::new(name, span).without_first_quote(); let ident = symbol::Ident::new(name, span).without_first_quote();
tt!(Punct { ch: '\'', joint: true }); trees.extend([
tt!(Ident::new(rustc.sess(), ident.name, false)); TokenTree::Punct(Punct { ch: b'\'', joint: true, span }),
TokenTree::Ident(Ident::new(rustc.sess(), ident.name, false, span)),
]);
} }
Literal(lit) => tt!(Literal { lit }), Literal(lit) => trees.push(TokenTree::Literal(self::Literal { lit, span })),
DocComment(_, attr_style, data) => { DocComment(_, attr_style, data) => {
let mut escaped = String::new(); let mut escaped = String::new();
for ch in data.as_str().chars() { for ch in data.as_str().chars() {
escaped.extend(ch.escape_debug()); escaped.extend(ch.escape_debug());
} }
let stream = vec![ let stream = [
Ident(sym::doc, false), Ident(sym::doc, false),
Eq, Eq,
TokenKind::lit(token::Str, Symbol::intern(&escaped), None), TokenKind::lit(token::Str, Symbol::intern(&escaped), None),
@ -173,9 +158,9 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)>
.into_iter() .into_iter()
.map(|kind| tokenstream::TokenTree::token(kind, span)) .map(|kind| tokenstream::TokenTree::token(kind, span))
.collect(); .collect();
tt!(Punct { ch: '#', joint: false }); trees.push(TokenTree::Punct(Punct { ch: b'#', joint: false, span }));
if attr_style == ast::AttrStyle::Inner { if attr_style == ast::AttrStyle::Inner {
tt!(Punct { ch: '!', joint: false }); trees.push(TokenTree::Punct(Punct { ch: b'!', joint: false, span }));
} }
trees.push(TokenTree::Group(Group { trees.push(TokenTree::Group(Group {
delimiter: pm::Delimiter::Bracket, delimiter: pm::Delimiter::Bracket,
@ -190,6 +175,12 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)>
Interpolated(nt) => { Interpolated(nt) => {
let stream = TokenStream::from_nonterminal_ast(&nt); let stream = TokenStream::from_nonterminal_ast(&nt);
// A hack used to pass AST fragments to attribute and derive
// macros as a single nonterminal token instead of a token
// stream. Such token needs to be "unwrapped" and not
// represented as a delimited group.
// FIXME: It needs to be removed, but there are some
// compatibility issues (see #73345).
if crate::base::nt_pretty_printing_compatibility_hack(&nt, rustc.sess()) { if crate::base::nt_pretty_printing_compatibility_hack(&nt, rustc.sess()) {
trees.extend(Self::from_internal((stream, rustc))); trees.extend(Self::from_internal((stream, rustc)));
} else { } else {
@ -254,28 +245,28 @@ impl ToInternal<TokenStream> for TokenTree<TokenStream, Span, Ident, Literal> {
}; };
let kind = match ch { let kind = match ch {
'=' => Eq, b'=' => Eq,
'<' => Lt, b'<' => Lt,
'>' => Gt, b'>' => Gt,
'!' => Not, b'!' => Not,
'~' => Tilde, b'~' => Tilde,
'+' => BinOp(Plus), b'+' => BinOp(Plus),
'-' => BinOp(Minus), b'-' => BinOp(Minus),
'*' => BinOp(Star), b'*' => BinOp(Star),
'/' => BinOp(Slash), b'/' => BinOp(Slash),
'%' => BinOp(Percent), b'%' => BinOp(Percent),
'^' => BinOp(Caret), b'^' => BinOp(Caret),
'&' => BinOp(And), b'&' => BinOp(And),
'|' => BinOp(Or), b'|' => BinOp(Or),
'@' => At, b'@' => At,
'.' => Dot, b'.' => Dot,
',' => Comma, b',' => Comma,
';' => Semi, b';' => Semi,
':' => Colon, b':' => Colon,
'#' => Pound, b'#' => Pound,
'$' => Dollar, b'$' => Dollar,
'?' => Question, b'?' => Question,
'\'' => SingleQuote, b'\'' => SingleQuote,
_ => unreachable!(), _ => unreachable!(),
}; };

View file

@ -325,6 +325,7 @@ mark_noop! {
&'_ [u8], &'_ [u8],
&'_ str, &'_ str,
String, String,
u8,
usize, usize,
Delimiter, Delimiter,
Level, Level,
@ -431,48 +432,48 @@ compound_traits!(
); );
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct DelimSpan<S> { pub struct DelimSpan<Span> {
pub open: S, pub open: Span,
pub close: S, pub close: Span,
pub entire: S, pub entire: Span,
} }
impl<S: Copy> DelimSpan<S> { impl<Span: Copy> DelimSpan<Span> {
pub fn from_single(span: S) -> Self { pub fn from_single(span: Span) -> Self {
DelimSpan { open: span, close: span, entire: span } DelimSpan { open: span, close: span, entire: span }
} }
} }
compound_traits!(struct DelimSpan<Sp> { open, close, entire }); compound_traits!(struct DelimSpan<Span> { open, close, entire });
#[derive(Clone)] #[derive(Clone)]
pub struct Group<T, S> { pub struct Group<TokenStream, Span> {
pub delimiter: Delimiter, pub delimiter: Delimiter,
pub stream: Option<T>, pub stream: Option<TokenStream>,
pub span: DelimSpan<S>, pub span: DelimSpan<Span>,
} }
compound_traits!(struct Group<T, Sp> { delimiter, stream, span }); compound_traits!(struct Group<TokenStream, Span> { delimiter, stream, span });
#[derive(Clone)] #[derive(Clone)]
pub struct Punct<S> { pub struct Punct<Span> {
pub ch: char, pub ch: u8,
pub joint: bool, pub joint: bool,
pub span: S, pub span: Span,
} }
compound_traits!(struct Punct<Sp> { ch, joint, span }); compound_traits!(struct Punct<Span> { ch, joint, span });
#[derive(Clone)] #[derive(Clone)]
pub enum TokenTree<T, S, I, L> { pub enum TokenTree<TokenStream, Span, Ident, Literal> {
Group(Group<T, S>), Group(Group<TokenStream, Span>),
Punct(Punct<S>), Punct(Punct<Span>),
Ident(I), Ident(Ident),
Literal(L), Literal(Literal),
} }
compound_traits!( compound_traits!(
enum TokenTree<T, Sp, I, L> { enum TokenTree<TokenStream, Span, Ident, Literal> {
Group(tt), Group(tt),
Punct(tt), Punct(tt),
Ident(tt), Ident(tt),
@ -483,12 +484,12 @@ compound_traits!(
/// Globals provided alongside the initial inputs for a macro expansion. /// Globals provided alongside the initial inputs for a macro expansion.
/// Provides values such as spans which are used frequently to avoid RPC. /// Provides values such as spans which are used frequently to avoid RPC.
#[derive(Clone)] #[derive(Clone)]
pub struct ExpnGlobals<S> { pub struct ExpnGlobals<Span> {
pub def_site: S, pub def_site: Span,
pub call_site: S, pub call_site: Span,
pub mixed_site: S, pub mixed_site: Span,
} }
compound_traits!( compound_traits!(
struct ExpnGlobals<Sp> { def_site, call_site, mixed_site } struct ExpnGlobals<Span> { def_site, call_site, mixed_site }
); );

View file

@ -969,13 +969,17 @@ impl Punct {
if !LEGAL_CHARS.contains(&ch) { if !LEGAL_CHARS.contains(&ch) {
panic!("unsupported character `{:?}`", ch); panic!("unsupported character `{:?}`", ch);
} }
Punct(bridge::Punct { ch, joint: spacing == Spacing::Joint, span: Span::call_site().0 }) Punct(bridge::Punct {
ch: ch as u8,
joint: spacing == Spacing::Joint,
span: Span::call_site().0,
})
} }
/// Returns the value of this punctuation character as `char`. /// Returns the value of this punctuation character as `char`.
#[stable(feature = "proc_macro_lib2", since = "1.29.0")] #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn as_char(&self) -> char { pub fn as_char(&self) -> char {
self.0.ch self.0.ch as char
} }
/// Returns the spacing of this punctuation character, indicating whether it's immediately /// Returns the spacing of this punctuation character, indicating whether it's immediately