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

Fix DebugParser. #127273

Merged
merged 3 commits into from
Jul 14, 2024
Merged

Fix DebugParser. #127273

merged 3 commits into from
Jul 14, 2024

Conversation

nnethercote
Copy link
Contributor

I tried using this and it didn't work at all. prev_token is never eof, so the accumulator is always false, which means the then_some always returns None, which means scan always returns None, and tokens always ends up an empty vec. I'm not sure how this code was supposed to work.

(An aside: I find Iterator::scan to be a pretty wretched function, that produces code which is very hard to understand. Probably why this is just one of two uses of it in the entire compiler.)

This commit changes it to a simpler imperative style that produces a valid tokens vec.

r? @workingjubilee

@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 3, 2024
@workingjubilee
Copy link
Member

hmm, I'm quite certain I did test the same code I committed, but it was inside a macro expansion, so it might have been an unusual case... (and lately the specific tokenization of macros may be subtly changing in a way only visible to this, judging by recent commits).

@nnethercote
Copy link
Contributor Author

I'm not aware of any cases where prev_token becomes Eof. Anyway, any problems with merging this, given that the existing code doesn't work in basic cases?

@nnethercote
Copy link
Contributor Author

@workingjubilee I'm using debug_lookahead literally right now in collect_tokens_trailing_token. Here's an example. With the old code, I get this:

Parser { prev_token: Token { kind: Question, span: no-location (#0) }, tokens: [], approx_token_stream_pos: 1, .. }

With the new code, I get this:

Parser { prev_token: Token { kind: Question, span: no-location (#0) }, tokens: [Ident("struct", No), Ident("_S", No), OpenDelim(Parenthesis)], approx_token_stream_pos: 1, .. }

A big difference in usefulness!

@workingjubilee
Copy link
Member

I was using it on code generated from rustc_expand at the time.

@workingjubilee
Copy link
Member

If I recall correctly, what lead to the somewhat awkward formation here is that there were multiple Eof tokens in a row, which I did not understand but also did not try to understand.

Insofar as an "objection", I would prefer to conduct a modest amount of investigation into why the current state of affairs is, er, the current state of affairs, and then r+ this, because "basic" is relative and I wouldn't want to make this useless on what I was doing. But it requires retracing my steps, and from about Thursday to now I was completely exhausted and overheated so I did not get much serious thought done. If you can find out why, based on the scant description, I made the mistake I did (if it was a mistake?), then I am happy to accept something plausible, otherwise I will try to get to it shortly.

@nnethercote
Copy link
Contributor Author

You can get multiple Eofs if you look past the end of the tokenstream. Something weird would have to be happening.

@workingjubilee
Copy link
Member

I see.

I suppose we are filing "macro expansion" under "something weird"?

@nnethercote
Copy link
Contributor Author

I suppose we are filing "macro expansion" under "something weird"?

I don't know how you were using it. I do know that the existing code doesn't work for my simple use case, and the new code does.

If I wrote a test or two would that help?

@workingjubilee
Copy link
Member

Yes, please. I really should have committed some, but I didn't expect this to be this complicated, and it seemed contra the "debug use only" thing... 😅

Using `#[track_caller]` works if the assertion is moved outside of the
closure.
That method is currently badly broken, and the test output reflects
this. The obtained tokens list is always empty, except in the case where
we go two `bump`s past the final token, whereupon it will produce as
many `Eof` tokens as asked for.
It currently doesn't work at all. This commit changes it to a simpler
imperative style that produces a valid `tokens` vec.

(An aside: I find `Iterator::scan` to be a pretty wretched function,
that produces code which is very hard to understand. Probably why this
is just one of two uses of it in the entire compiler.)
@nnethercote
Copy link
Contributor Author

New code is up, with tests showing before and after behaviour.

@workingjubilee
Copy link
Member

Thanks!

I think I got direly confused at some point by hitting some of the "quirks" in the special case of before, and that plus inexperience with the parser (or indeed reasoning about tokenstreams) resulted in the current state of things.

@bors r+

@bors
Copy link
Contributor

bors commented Jul 14, 2024

📌 Commit aa0e8e1 has been approved by workingjubilee

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 Jul 14, 2024
@nnethercote
Copy link
Contributor Author

@bors rollup

bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 14, 2024
…iaskrgr

Rollup of 5 pull requests

Successful merges:

 - rust-lang#127273 (Fix `DebugParser`.)
 - rust-lang#127587 (Report usage of lib features in ast validation)
 - rust-lang#127592 (doc: Suggest `str::repeat` over `iter::repeat().take().collect()`)
 - rust-lang#127630 (Remove lang feature for type ascription (since it's a lib feature now))
 - rust-lang#127711 (Add regression test for a gce + effects ICE)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit febe442 into rust-lang:master Jul 14, 2024
6 checks passed
@rustbot rustbot added this to the 1.81.0 milestone Jul 14, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jul 14, 2024
Rollup merge of rust-lang#127273 - nnethercote:fix-DebugParser, r=workingjubilee

Fix `DebugParser`.

I tried using this and it didn't work at all. `prev_token` is never eof, so the accumulator is always false, which means the `then_some` always returns `None`, which means `scan` always returns `None`, and `tokens` always ends up an empty vec. I'm not sure how this code was supposed to work.

(An aside: I find `Iterator::scan` to be a pretty wretched function, that produces code which is very hard to understand. Probably why this is just one of two uses of it in the entire compiler.)

This commit changes it to a simpler imperative style that produces a valid `tokens` vec.

r? `@workingjubilee`
@nnethercote nnethercote deleted the fix-DebugParser branch July 14, 2024 22:24
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