Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename glue and refactor push_tree #106481

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion compiler/rustc_ast/src/token.rs
Expand Up @@ -710,7 +710,11 @@ impl Token {
}
}

pub fn glue(&self, joint: &Token) -> Option<Token> {
/// Checks if the current token is a multiple-character token (or glued like `==`, `<=`, `...`)
/// and returns the corresponding `TokenKind` if it is.
///
/// if the current tokens is already complete, returns `None`.
Comment on lines +713 to +716
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this comment explains what this method is doing properly.

What this is doing is checking if the current token can be combined with the joint token to turn into a multiple-character token.

pub fn check_is_multiple_char_token(&self, joint: &Token) -> Option<Token> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the name glue. Adding a doc comment is fine, but the new name doesn't really explain that we're trying to combine self and joint into a new "glued"/merged/combined/joined token.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly it doesn't seem very practical. Thank you for your feedback.

let kind = match self.kind {
Eq => match joint.kind {
Eq => EqEq,
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_ast/src/tokenstream.rs
Expand Up @@ -511,7 +511,7 @@ impl TokenStream {
fn try_glue_to_last(vec: &mut Vec<TokenTree>, tt: &TokenTree) -> bool {
if let Some(TokenTree::Token(last_tok, Spacing::Joint)) = vec.last()
&& let TokenTree::Token(tok, spacing) = tt
&& let Some(glued_tok) = last_tok.glue(tok)
&& let Some(glued_tok) = last_tok.check_is_multiple_char_token(tok)
{
// ...then overwrite the last token tree in `vec` with the
// glued token, and skip the first token tree from `stream`.
Expand All @@ -527,9 +527,7 @@ impl TokenStream {
pub fn push_tree(&mut self, tt: TokenTree) {
let vec_mut = Lrc::make_mut(&mut self.0);

if Self::try_glue_to_last(vec_mut, &tt) {
// nothing else to do
} else {
if !Self::try_glue_to_last(vec_mut, &tt) {
vec_mut.push(tt);
}
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_parse/src/lexer/tokentrees.rs
Expand Up @@ -71,7 +71,8 @@ impl<'a> TokenTreesReader<'a> {
let (next_tok, is_next_tok_preceded_by_whitespace) =
self.string_reader.next_token();
if !is_next_tok_preceded_by_whitespace {
if let Some(glued) = self.token.glue(&next_tok) {
if let Some(glued) = self.token.check_is_multiple_char_token(&next_tok)
{
self.token = glued;
} else {
let this_spacing =
Expand Down