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

Properly calculate best failure in macro matching #105570

Merged
merged 1 commit into from
Dec 29, 2022

Conversation

Noratrieb
Copy link
Member

Previously, we used spans. This was not good. Sometimes, the span of the token that failed to match may come from a position later in the file which has been transcribed into a token stream way earlier in the file. If precisely this token fails to match, we think that it was the best match because its span is so high, even though other arms might have gotten further in the token stream.

We now try to properly use the location in the token stream.

This needs a little cleanup as the best_failure field is getting out of hand but it should be mostly good to go. I hope I didn't violate too many abstraction boundaries..

@rustbot
Copy link
Collaborator

rustbot commented Dec 11, 2022

r? @davidtwco

(rustbot has picked a reviewer for you, use r? to override)

@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 Dec 11, 2022
@Noratrieb
Copy link
Member Author

Noratrieb commented Dec 11, 2022

For reference, this is the current terrible error for the added UI test, which caught my attention as someone was confused and asking about this on the community discord server:

macro_rules! number {
    (neg false, $self:ident) => { $self };
    ($signed:tt => $ty:ty;) => {
        number!(neg $signed, $self);
        //~^ ERROR no rules expected the token `$`
    };
}

number! { false => u8; }

fn main() {}
error: no rules expected the token `false`
 --> src/main.rs:9:11
  |
1 | macro_rules! number {
  | ------------------- when calling this macro
...
9 | number! { false => u8; }
  |           ^^^^^ no rules expected this token in macro call
  |
note: while trying to match `=>`
 --> src/main.rs:3:17
  |
3 |     ($signed:tt => $ty:ty;) => {
  |                 ^^

@rust-log-analyzer

This comment has been minimized.

Previously, we used spans. This was not good. Sometimes, the span of the
token that failed to match may come from a position later in the file
which has been transcribed into a token stream way earlier in the file.
If precisely this token fails to match, we think that it was the best
match because its span is so high, even though other arms might have
gotten further in the token stream.

We now try to properly use the location in the token stream.
@Noratrieb Noratrieb marked this pull request as ready for review December 12, 2022 17:21
@Noratrieb
Copy link
Member Author

r? compiler

@rustbot rustbot assigned nagisa and unassigned davidtwco Dec 23, 2022
@compiler-errors
Copy link
Member

Thanks, this explanation...

the span of the token that failed to match may come from a position later in the file which has been transcribed into a token stream way earlier in the file

... elucidates the error. Gotta love macro variables.

@bors r+

@bors
Copy link
Contributor

bors commented Dec 28, 2022

📌 Commit d72a0c4 has been approved by compiler-errors

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-review Status: Awaiting review from the assignee but also interested parties. labels Dec 28, 2022
}

impl BestFailure {
fn is_better_position(&self, position: usize) -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

The polarity of this fn is a bit confusing -- it reads as it's asking the question "is self better than position", but it's actually "is position better than self"?

LL | macro_rules! number {
| ------------------- when calling this macro
...
LL | number!(neg $signed, $self);
Copy link
Member

Choose a reason for hiding this comment

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

kind of a shame that we highlight the whole $self metavariable rather than just the $ token given the primary message of this diagnostic, but w/e

bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 28, 2022
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#104402 (Move `ReentrantMutex` to `std::sync`)
 - rust-lang#104493 (available_parallelism: Gracefully handle zero value cfs_period_us)
 - rust-lang#105359 (Make sentinel value configurable in `library/std/src/sys_common/thread_local_key.rs`)
 - rust-lang#105497 (Clarify `catch_unwind` docs about panic hooks)
 - rust-lang#105570 (Properly calculate best failure in macro matching)
 - rust-lang#105702 (Format only modified files)
 - rust-lang#105998 (adjust message on non-unwinding panic)
 - rust-lang#106161 (Iterator::find: link to Iterator::position in docs for discoverability)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit c52d58f into rust-lang:master Dec 29, 2022
@rustbot rustbot added this to the 1.68.0 milestone Dec 29, 2022
@Noratrieb Noratrieb deleted the actual-best-failure branch January 4, 2023 18:04
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 5, 2023
…-errors

Shrink `ParseResult` in the hot path.

rust-lang#105570 increased the size, which caused regressions. This uses the existing generic infrastructure to differentiate between the hot path and the diagnostics path.
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.

7 participants