Skip to content

Commit 364a3be

Browse files
committed
Put TokenStream stuff in a sensible order.
I.e. the type definition, then a single inherent `impl` block, then the trait `impl` blocks. The lack of sensible ordering here has bugged me for some time.
1 parent 16b5ac1 commit 364a3be

File tree

1 file changed

+65
-67
lines changed

1 file changed

+65
-67
lines changed

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 65 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,6 @@ impl TokenTree {
9090
}
9191
}
9292

93-
impl<CTX> HashStable<CTX> for TokenStream
94-
where
95-
CTX: crate::HashStableContext,
96-
{
97-
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
98-
for sub_tt in self.iter() {
99-
sub_tt.hash_stable(hcx, hasher);
100-
}
101-
}
102-
}
103-
10493
/// A lazy version of [`AttrTokenStream`], which defers creation of an actual
10594
/// `AttrTokenStream` until it is needed.
10695
#[derive(Clone)]
@@ -547,10 +536,6 @@ pub struct AttrsTarget {
547536
pub tokens: LazyAttrTokenStream,
548537
}
549538

550-
/// A `TokenStream` is an abstract sequence of tokens, organized into [`TokenTree`]s.
551-
#[derive(Clone, Debug, Default, Encodable, Decodable)]
552-
pub struct TokenStream(pub(crate) Arc<Vec<TokenTree>>);
553-
554539
/// Indicates whether a token can join with the following token to form a
555540
/// compound token. Used for conversions to `proc_macro::Spacing`. Also used to
556541
/// guide pretty-printing, which is where the `JointHidden` value (which isn't
@@ -611,58 +596,9 @@ pub enum Spacing {
611596
JointHidden,
612597
}
613598

614-
impl TokenStream {
615-
/// Given a `TokenStream` with a `Stream` of only two arguments, return a new `TokenStream`
616-
/// separating the two arguments with a comma for diagnostic suggestions.
617-
pub fn add_comma(&self) -> Option<(TokenStream, Span)> {
618-
// Used to suggest if a user writes `foo!(a b);`
619-
let mut suggestion = None;
620-
let mut iter = self.0.iter().enumerate().peekable();
621-
while let Some((pos, ts)) = iter.next() {
622-
if let Some((_, next)) = iter.peek() {
623-
let sp = match (&ts, &next) {
624-
(_, TokenTree::Token(Token { kind: token::Comma, .. }, _)) => continue,
625-
(
626-
TokenTree::Token(token_left, Spacing::Alone),
627-
TokenTree::Token(token_right, _),
628-
) if (token_left.is_non_reserved_ident() || token_left.is_lit())
629-
&& (token_right.is_non_reserved_ident() || token_right.is_lit()) =>
630-
{
631-
token_left.span
632-
}
633-
(TokenTree::Delimited(sp, ..), _) => sp.entire(),
634-
_ => continue,
635-
};
636-
let sp = sp.shrink_to_hi();
637-
let comma = TokenTree::token_alone(token::Comma, sp);
638-
suggestion = Some((pos, comma, sp));
639-
}
640-
}
641-
if let Some((pos, comma, sp)) = suggestion {
642-
let mut new_stream = Vec::with_capacity(self.0.len() + 1);
643-
let parts = self.0.split_at(pos + 1);
644-
new_stream.extend_from_slice(parts.0);
645-
new_stream.push(comma);
646-
new_stream.extend_from_slice(parts.1);
647-
return Some((TokenStream::new(new_stream), sp));
648-
}
649-
None
650-
}
651-
}
652-
653-
impl FromIterator<TokenTree> for TokenStream {
654-
fn from_iter<I: IntoIterator<Item = TokenTree>>(iter: I) -> Self {
655-
TokenStream::new(iter.into_iter().collect::<Vec<TokenTree>>())
656-
}
657-
}
658-
659-
impl Eq for TokenStream {}
660-
661-
impl PartialEq<TokenStream> for TokenStream {
662-
fn eq(&self, other: &TokenStream) -> bool {
663-
self.iter().eq(other.iter())
664-
}
665-
}
599+
/// A `TokenStream` is an abstract sequence of tokens, organized into [`TokenTree`]s.
600+
#[derive(Clone, Debug, Default, Encodable, Decodable)]
601+
pub struct TokenStream(pub(crate) Arc<Vec<TokenTree>>);
666602

667603
impl TokenStream {
668604
pub fn new(tts: Vec<TokenTree>) -> TokenStream {
@@ -838,6 +774,68 @@ impl TokenStream {
838774
}
839775
}
840776
}
777+
778+
/// Given a `TokenStream` with a `Stream` of only two arguments, return a new `TokenStream`
779+
/// separating the two arguments with a comma for diagnostic suggestions.
780+
pub fn add_comma(&self) -> Option<(TokenStream, Span)> {
781+
// Used to suggest if a user writes `foo!(a b);`
782+
let mut suggestion = None;
783+
let mut iter = self.0.iter().enumerate().peekable();
784+
while let Some((pos, ts)) = iter.next() {
785+
if let Some((_, next)) = iter.peek() {
786+
let sp = match (&ts, &next) {
787+
(_, TokenTree::Token(Token { kind: token::Comma, .. }, _)) => continue,
788+
(
789+
TokenTree::Token(token_left, Spacing::Alone),
790+
TokenTree::Token(token_right, _),
791+
) if (token_left.is_non_reserved_ident() || token_left.is_lit())
792+
&& (token_right.is_non_reserved_ident() || token_right.is_lit()) =>
793+
{
794+
token_left.span
795+
}
796+
(TokenTree::Delimited(sp, ..), _) => sp.entire(),
797+
_ => continue,
798+
};
799+
let sp = sp.shrink_to_hi();
800+
let comma = TokenTree::token_alone(token::Comma, sp);
801+
suggestion = Some((pos, comma, sp));
802+
}
803+
}
804+
if let Some((pos, comma, sp)) = suggestion {
805+
let mut new_stream = Vec::with_capacity(self.0.len() + 1);
806+
let parts = self.0.split_at(pos + 1);
807+
new_stream.extend_from_slice(parts.0);
808+
new_stream.push(comma);
809+
new_stream.extend_from_slice(parts.1);
810+
return Some((TokenStream::new(new_stream), sp));
811+
}
812+
None
813+
}
814+
}
815+
816+
impl PartialEq<TokenStream> for TokenStream {
817+
fn eq(&self, other: &TokenStream) -> bool {
818+
self.iter().eq(other.iter())
819+
}
820+
}
821+
822+
impl Eq for TokenStream {}
823+
824+
impl FromIterator<TokenTree> for TokenStream {
825+
fn from_iter<I: IntoIterator<Item = TokenTree>>(iter: I) -> Self {
826+
TokenStream::new(iter.into_iter().collect::<Vec<TokenTree>>())
827+
}
828+
}
829+
830+
impl<CTX> HashStable<CTX> for TokenStream
831+
where
832+
CTX: crate::HashStableContext,
833+
{
834+
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
835+
for sub_tt in self.iter() {
836+
sub_tt.hash_stable(hcx, hasher);
837+
}
838+
}
841839
}
842840

843841
#[derive(Clone)]

0 commit comments

Comments
 (0)