Skip to content

Commit 2d39794

Browse files
committed
session: a program read no longer ends the drain when its root has no summary
CI failed `a_header_read_in_two_programs_answers_each_its_own_way` with `both translation units are read: ["/p/a.cpp"]` — one program read, the other silently not — and it passed locally on the same commit. It is not a platform difference: it is a race between the index and the unit schedule, and a slower machine loses it. The two adjacent lines: `the_next_unit_to_read` deliberately accepts an open file whose summary is absent (its module question is `is_none_or`), so the drain may ask for the program of a file the index has not caught up with — and `gather_a_unit_read` began with `summary(root)?`, so the read returned `None`, `read_a_looked_at_unit` returned `None`, and the caller's `while let Some(reading)` loop ended with a translation unit unread and no diagnostic anywhere. The caller that asks for a program now does the one parse the program needs: `read_a_looked_at_unit` checks `summary(&root)` and, when it is missing, runs the pump's own `index_one` for that root before `read_the_unit`. Nothing new is introduced, and the cost is one parse the index owed anyway. Pinned by `session::tests::a_program_is_read_before_its_root_has_a_summary`, which builds the state deliberately — two open files and no `index_everything`, so neither has a summary: without the fix: assertion failed: both open files are read, summary or not: [] with the fix: both are read, and the CI test passes The assertion counts programs read rather than waiting on a timeout, so it asks the same question on every platform. The failing test is not disabled and no retry was added: the schedule is what changed. `docs/buglist.md` section 14 records it, with the mechanism and both measurements.
1 parent ddc682c commit 2d39794

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

‎crates/cpp_code_analysis/src/session/mod.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7071,6 +7071,16 @@ impl<F: FileProvider + Clone> Session<F> {
70717071
pub fn read_a_looked_at_unit(&mut self) -> Option<UnitReading> {
70727072
let root = self.the_next_unit_to_read()?;
70737073

7074+
// **A program's root has to have a summary, so build it here when the index has not caught up.** The schedule
7075+
// accepts an open file whose summary is absent — and the read then used to fail, which ended the drain with a
7076+
// translation unit unread. Measured on CI: `both translation units are read: ["/p/a.cpp"]`, one root summarised
7077+
// by `index_everything` and the other not. The one parse is the pump's own `index_one`, so nothing new is
7078+
// introduced: the caller that asks for a program is allowed to do the work the program needs.
7079+
if self.store.index().summary(&root).is_none() {
7080+
let ready = self.prefetched.remove(&queue_key(&root));
7081+
self.index_one(root.clone(), Priority::Open, 0, ready);
7082+
}
7083+
70747084
let reading = self.read_the_unit(&root);
70757085
// **A reading that could not be built is retried, not marked done.** This used to mark the root read whatever
70767086
// happened, and for a module unit that was fatal: the unit's summary is queued rather than read when the pump

‎crates/cpp_code_analysis/src/session/tests.rs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,6 +2137,40 @@ fn a_files_rendering_is_its_reading_and_not_one_answer_beside_another() {
21372137
);
21382138
}
21392139

2140+
/// **A program is read even when its root's summary has not been built yet** — the CI failure, made deterministic.
2141+
///
2142+
/// `the_next_unit_to_read` accepts an open file whose summary is absent (its module question is `is_none_or`), so the
2143+
/// drain can ask for a program the index has not caught up with — and the read used to refuse it with a `?`, after
2144+
/// which the `while let Some(reading)` loop of the drain ended and a whole translation unit went unread. Measured on
2145+
/// CI: `both translation units are read: ["/p/a.cpp"]`, where `index_everything`'s rounds had built one root's summary
2146+
/// and not the other's; a faster machine built both, which is why the same test passed locally with the same code.
2147+
///
2148+
/// This test builds that state deliberately: two open files and **no** indexing, so neither has a summary.
2149+
#[test]
2150+
fn a_program_is_read_before_its_root_has_a_summary() {
2151+
let cfg = "#ifdef A\nstruct X { int from_a; };\n#else\nstruct X { long from_b; };\n#endif\n";
2152+
let a = "#define A 1\n#include \"cfg.h\"\n";
2153+
let b = "#include \"cfg.h\"\n";
2154+
let files = MemoryFiles::new()
2155+
.with_file("/p/cfg.h", cfg)
2156+
.with_file("/p/a.cpp", a)
2157+
.with_file("/p/b.cpp", b);
2158+
let fixture = Memory::new("a-program-without-its-summary", &files);
2159+
let mut session = fixture.session();
2160+
session.did_open("/p/a.cpp", a);
2161+
session.did_open("/p/b.cpp", b);
2162+
2163+
let mut read = Vec::new();
2164+
while let Some(reading) = session.read_a_looked_at_unit() {
2165+
read.push(reading.root.clone());
2166+
}
2167+
assert_eq!(
2168+
read.len(),
2169+
2,
2170+
"both open files are read, summary or not: {read:?}"
2171+
);
2172+
}
2173+
21402174
/// **The same header, read in two translation units, answers each of them its own way** — §5 step two's acceptance.
21412175
///
21422176
/// `cfg.h` declares `X` one way when `A` is defined and another when it is not, so `a.cpp` (which defines it before

‎docs/buglist.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,35 @@ the machine. Each entry says how it was measured, so the next reader can reprodu
287287

288288

289289

290+
### 14. A program read ended the drain when its root's summary had not been built yet
291+
292+
* **The report**: CI failed `session::tests::a_header_read_in_two_programs_answers_each_its_own_way` with
293+
`both translation units are read: ["/p/a.cpp"]` — one program read, the other silently not. It passed locally on the
294+
same commit, so the first question was whether it is a platform difference. **It is not.** It is a race between the
295+
index and the unit schedule, and a slower machine loses it.
296+
* **The mechanism, in two adjacent lines.** `the_next_unit_to_read` deliberately accepts an open file whose summary is
297+
absent — its module question is `is_none_or` — so the drain may ask for the program of a file the index has not
298+
caught up with. `gather_a_unit_read` then began with `self.store.index().summary(root)?`, and
299+
`translation_unit_of` refuses a root with no summary even for an open file (it can fall back to a *directive* summary,
300+
but only when the whole directive closure is describable). The `?` returned `None`, `read_a_looked_at_unit` returned
301+
`None`, and the caller's `while let Some(reading) = …` loop **ended** — one file read, one lost, and no diagnostic
302+
anywhere. `index_everything` builds both summaries on a fast machine and one of them on a slower one.
303+
* **The fix**: the caller that asks for a program does the one parse the program needs. `read_a_looked_at_unit` now
304+
checks `summary(&root)` and, when it is missing, runs the pump's own `index_one` for that root before
305+
`read_the_unit`. Nothing new is introduced — it is the step `advance` already performs — and the cost is one parse the
306+
index owed anyway.
307+
* **Measured, both directions.** `session::tests::a_program_is_read_before_its_root_has_a_summary` builds the state
308+
deliberately — two open files and **no** `index_everything`, so neither has a summary:
309+
310+
```text
311+
without the fix: assertion failed: both open files are read, summary or not: []
312+
with the fix: both are read, and the CI test passes
313+
```
314+
315+
The assertion is on the **number of programs read**, not on a timeout, so it asks the same question the CI failure
316+
asked and it is deterministic on every platform.
317+
* **Not disabled**, and no retry loop: the test stays, and the schedule is what changed.
318+
290319
### 13. A sweep of GCC's, Clang's and MSVC's own headers — three gaps fixed, one open
291320

292321
* **Why this exists.** Section 12's `__real__` was found by chasing one name. The question after it was the right one:

0 commit comments

Comments
 (0)