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

Remove TrailingToken. #127842

Merged
merged 1 commit into from
Jul 18, 2024
Merged

Remove TrailingToken. #127842

merged 1 commit into from
Jul 18, 2024

Conversation

nnethercote
Copy link
Contributor

It's used in Parser::collect_tokens_trailing_token to decide whether to capture a trailing token. But the callers actually know whether to capture a trailing token, so it's simpler for them to just pass in a bool.

Also, the TrailingToken::Gt case was weird, because it didn't result in a trailing token being captured. It could have been subsumed by the TrailingToken::MaybeComma case, and it effectively is in the new code.

r? @petrochenkov

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 17, 2024
@@ -3262,7 +3262,7 @@ impl<'a> Parser<'a> {
id: DUMMY_NODE_ID,
is_placeholder: false,
},
TrailingToken::None,
false,
Copy link
Contributor

Choose a reason for hiding this comment

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

I would expect a next_token == token::Comma here, because match arms are separated by commas.

I suspect this may cause issues with configured out arms inside derive/cfg_eval.

#[cfg_eval]
#[proc_macro_that_tries_to_parse_it_again] // will fail with a syntax error because the comma
                                           // is not removed from the token representation?
fn foo() {
  match 10 {
    #[cfg(FALSE)]
    10 => true,
     _ => false,
  }
}

compiler/rustc_parse/src/parser/expr.rs Outdated Show resolved Hide resolved
// We just ate the comma, so no need to use `TrailingToken`
Ok((param, TrailingToken::None))
// We just ate the comma, so no need to capture the trailing token.
Ok((param, false))
Copy link
Contributor

Choose a reason for hiding this comment

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

Including commas into the node's tokens in node1, node2, node3 doesn't feel right, but maybe it's ok if proc macro attributes are not supported on this kind of nodes, only cfgs.
That's assuming a proc macro should only see node's tokens without comma, and cfg(FALSE) should remove tokens including tokens.

On the other hand, proc macros probably should see the trailing comma as a part of their input, so they could correctly expand into nothing if necessary, like cfg(FALSE).
That would be a change in behavior, but it should only affect unstable proc macros (?).
That would eliminate the need in TrailingToken::MaybeComma entirely, because the commas would always be included and would never be trailing.

@petrochenkov
Copy link
Contributor

petrochenkov commented Jul 17, 2024

Okay, maybe I'm a bit confused about the way in which the current machinery works, because it's not very consistent.
Maybe it depends on the parser code specifics whether capture_trailing should be set to true, not on node kinds.

If we parse like this

collect_tokens(parse_expr);
eat_comma();

then we should pass capture_trailing = true to collect_tokens so it picks up the comma even if it's outside of collect_tokens.

If we parse like this

collect_tokens(parse_expr_and_comma);

then we should pass capture_trailing = false to collect_tokens because the comma is already inside collect_tokens and is collected.

Either way the comma will be collected, just through slightly different means, and will be included into the token stream for the expression.

I'm actually not sure anymore, will the commas appear in the token streams that my_proc_macro sees in

[#[my_proc_macro] a, #[my_proc_macro] b, #[my_proc_macro] c];

in the first two invocations?

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 17, 2024
@nnethercote
Copy link
Contributor Author

I'm not sure the answers to your questions. This is meant to be a refactoring that just relies on local code reasoning.

E.g. for this code:

            TrailingToken::Semi => {
                assert_eq!(self.token.kind, token::Semi);
                true
            }

It's a bit silly to assert that self.token.kind equals token::Semi, because all the call sites that pass TrailingToken::Semi do so after checking that self.token.kind == token::Semi. That's the basic observation that is the starting point for this PR.

@petrochenkov
Copy link
Contributor

As a refactoring this looks good to me, even if the pre-existing behavior is somewhat questionable.
r=me after fixing #127842 (comment).

@bors
Copy link
Contributor

bors commented Jul 18, 2024

☔ The latest upstream changes (presumably #127892) made this pull request unmergeable. Please resolve the merge conflicts.

It's used in `Parser::collect_tokens_trailing_token` to decide whether
to capture a trailing token. But the callers actually know whether to
capture a trailing token, so it's simpler for them to just pass in a
bool.

Also, the `TrailingToken::Gt` case was weird, because it didn't result
in a trailing token being captured. It could have been subsumed by the
`TrailingToken::MaybeComma` case, and it effectively is in the new code.
@nnethercote
Copy link
Contributor Author

I fixed the mistaken case and rebased.

@bors r=petrochenkov rollup

@bors
Copy link
Contributor

bors commented Jul 18, 2024

📌 Commit 487802d has been approved by petrochenkov

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 18, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 18, 2024
Rollup of 7 pull requests

Successful merges:

 - rust-lang#127491 (Migrate 8 very similar FFI `run-make` tests to rmake)
 - rust-lang#127687 (Const-to-pattern-to-MIR cleanup)
 - rust-lang#127822 (Migrate `issue-85401-static-mir`, `missing-crate-dependency` and `unstable-flag-required` `run-make` tests to rmake)
 - rust-lang#127842 (Remove `TrailingToken`.)
 - rust-lang#127864 (cleanup: remove support for 3DNow! cpu features)
 - rust-lang#127899 (Mark myself as on leave)
 - rust-lang#127901 (Add missing GHA group for building `llvm-bitcode-linker`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit e2e0681 into rust-lang:master Jul 18, 2024
6 checks passed
@rustbot rustbot added this to the 1.81.0 milestone Jul 18, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jul 18, 2024
Rollup merge of rust-lang#127842 - nnethercote:rm-TrailingToken, r=petrochenkov

Remove `TrailingToken`.

It's used in `Parser::collect_tokens_trailing_token` to decide whether to capture a trailing token. But the callers actually know whether to capture a trailing token, so it's simpler for them to just pass in a bool.

Also, the `TrailingToken::Gt` case was weird, because it didn't result in a trailing token being captured. It could have been subsumed by the `TrailingToken::MaybeComma` case, and it effectively is in the new code.

r? `@petrochenkov`
@nnethercote nnethercote deleted the rm-TrailingToken branch July 18, 2024 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants