Skip to content

Commit aa0e8e1

Browse files
committed
Fix DebugParser.
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.)
1 parent cf2dfb2 commit aa0e8e1

File tree

2 files changed

+104
-14
lines changed

2 files changed

+104
-14
lines changed

compiler/rustc_parse/src/parser/mod.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -1537,14 +1537,16 @@ impl<'a> Parser<'a> {
15371537

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

compiler/rustc_parse/src/parser/tests.rs

+94-6
Original file line numberDiff line numberDiff line change
@@ -1541,11 +1541,36 @@ fn debug_lookahead() {
15411541
ctxt: #0,
15421542
},
15431543
},
1544-
tokens: [],
1544+
tokens: [
1545+
Ident(
1546+
\"fn\",
1547+
No,
1548+
),
1549+
Ident(
1550+
\"f\",
1551+
No,
1552+
),
1553+
OpenDelim(
1554+
Parenthesis,
1555+
),
1556+
Ident(
1557+
\"x\",
1558+
No,
1559+
),
1560+
Colon,
1561+
Ident(
1562+
\"u32\",
1563+
No,
1564+
),
1565+
CloseDelim(
1566+
Parenthesis,
1567+
),
1568+
],
15451569
approx_token_stream_pos: 1,
15461570
..
15471571
}"
15481572
);
1573+
// There are 13 tokens. We request 15, get 14; the last one is `Eof`.
15491574
assert_eq!(
15501575
&format!("{:#?}", p.debug_lookahead(15)),
15511576
"Parser {
@@ -1561,7 +1586,51 @@ fn debug_lookahead() {
15611586
ctxt: #0,
15621587
},
15631588
},
1564-
tokens: [],
1589+
tokens: [
1590+
Ident(
1591+
\"fn\",
1592+
No,
1593+
),
1594+
Ident(
1595+
\"f\",
1596+
No,
1597+
),
1598+
OpenDelim(
1599+
Parenthesis,
1600+
),
1601+
Ident(
1602+
\"x\",
1603+
No,
1604+
),
1605+
Colon,
1606+
Ident(
1607+
\"u32\",
1608+
No,
1609+
),
1610+
CloseDelim(
1611+
Parenthesis,
1612+
),
1613+
OpenDelim(
1614+
Brace,
1615+
),
1616+
Ident(
1617+
\"x\",
1618+
No,
1619+
),
1620+
CloseDelim(
1621+
Brace,
1622+
),
1623+
Ident(
1624+
\"struct\",
1625+
No,
1626+
),
1627+
Ident(
1628+
\"S\",
1629+
No,
1630+
),
1631+
Semi,
1632+
Eof,
1633+
],
15651634
approx_token_stream_pos: 1,
15661635
..
15671636
}"
@@ -1588,7 +1657,12 @@ fn debug_lookahead() {
15881657
ctxt: #0,
15891658
},
15901659
},
1591-
tokens: [],
1660+
tokens: [
1661+
Ident(
1662+
\"x\",
1663+
No,
1664+
),
1665+
],
15921666
approx_token_stream_pos: 9,
15931667
..
15941668
}"
@@ -1610,7 +1684,23 @@ fn debug_lookahead() {
16101684
ctxt: #0,
16111685
},
16121686
},
1613-
tokens: [],
1687+
tokens: [
1688+
Ident(
1689+
\"x\",
1690+
No,
1691+
),
1692+
CloseDelim(
1693+
Brace,
1694+
),
1695+
Ident(
1696+
\"struct\",
1697+
No,
1698+
),
1699+
Ident(
1700+
\"S\",
1701+
No,
1702+
),
1703+
],
16141704
approx_token_stream_pos: 9,
16151705
..
16161706
}"
@@ -1637,8 +1727,6 @@ fn debug_lookahead() {
16371727
},
16381728
tokens: [
16391729
Eof,
1640-
Eof,
1641-
Eof,
16421730
],
16431731
approx_token_stream_pos: 15,
16441732
..

0 commit comments

Comments
 (0)