1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! **Every byte of the file appears exactly once in the tree** — invariant I1, which everything above the parser
//! relies on: a formatter rewrites text from the tree, a language server maps a client's position through it, and a
//! rename edits the ranges in it. A tree that drops bytes is not a wrong answer, it is a *shifted* one: every offset
//! after the dropped run points at the wrong place.
//!
//! The tests here are the invariant made checkable: a file that parses cleanly must give its text back byte for byte, a
//! file with a comment in it must keep the comment, and the container that reads declarations until a `}` must keep the
//! comments between them. The last one **was** a failure — `docs/parser-review.md` §4 has the measurement (22 315 bytes in,
//! 19 987 out, zero syntax errors) and the cause: the linkage block's loop did not emit the trivia a declaration's `;`
//! stops before, so the comment was at its cursor when the next iteration began.
use ;
/// Parse and give the text back, asserting the round trip.
/// **A comment between two declarations is kept — outside a linkage block, which is the shape that always worked.**
///
/// The control for the test below: the same text, without `extern "C" { … }`, round trips. That is what makes the failure
/// the linkage block's rather than the comment machinery's.
/// **A comment inside `extern "C" { … }` is kept, and so is every byte after it.**
///
/// This was the parser's one correctness bug: the comment's bytes were consumed and emitted nowhere, the tree kept a
/// **zero-width `ErrorNode`** in its place, and every offset after it shifted. The mechanism was the container's, not the
/// comment machinery's:
///
/// ```text
/// parse_declaration reads `int f(int a, int b);`; its `;` deliberately stops **before** the comment
/// the linkage loop begins its next iteration with the cursor on that comment
/// parse_declaration fails on a comment
/// the recovery branch marks an `ErrorNode` and calls `bump`, which emits nothing for a trivia token
/// ```
///
/// The class body's loop already emitted the trivia at the top of every iteration (`parse_class_body_members`); the
/// linkage block's is the second container and did not. It does now, which is what this test pins: `cxxabi.h` gave 22 315
/// bytes in and 19 987 out before the fix, and the whole file after it.