Skip to content

Commit 402c7c2

Browse files
committed
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.
1 parent 7d97c59 commit 402c7c2

File tree

1 file changed

+11
-8
lines changed
  • compiler/rustc_parse/src/parser

1 file changed

+11
-8
lines changed

compiler/rustc_parse/src/parser/mod.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -1541,14 +1541,17 @@ impl<'a> Parser<'a> {
15411541

15421542
// we don't need N spans, but we want at least one, so print all of prev_token
15431543
dbg_fmt.field("prev_token", &parser.prev_token);
1544-
// make it easier to peek farther ahead by taking TokenKinds only until EOF
1545-
let tokens = (0..*lookahead)
1546-
.map(|i| parser.look_ahead(i, |tok| tok.kind.clone()))
1547-
.scan(parser.prev_token == TokenKind::Eof, |eof, tok| {
1548-
let current = eof.then_some(tok.clone()); // include a trailing EOF token
1549-
*eof |= &tok == &TokenKind::Eof;
1550-
current
1551-
});
1544+
1545+
// Don't look ahead past EOF.
1546+
let mut tokens = vec![];
1547+
for i in 0..*lookahead {
1548+
let tok = parser.look_ahead(i, |tok| tok.kind.clone());
1549+
let is_eof = tok == TokenKind::Eof;
1550+
tokens.push(tok);
1551+
if is_eof {
1552+
break;
1553+
}
1554+
}
15521555
dbg_fmt.field_with("tokens", |field| field.debug_list().entries(tokens).finish());
15531556
dbg_fmt.field("approx_token_stream_pos", &parser.num_bump_calls);
15541557

0 commit comments

Comments
 (0)