raf-source 0.0.0

Workspace source index: a Tree file seam with disk and memory backends under canonical path containment.
Documentation
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// SPDX-FileCopyrightText: 2026 Blackcat InformaticsĀ® Inc. <paudley@blackcatinformatics.ca>
// SPDX-License-Identifier: MIT OR Apache-2.0 OR MulanPSL-2.0

//! Canonical source loading through bounded native parallel workers.

#![cfg(target_os = "linux")]

use std::fmt;
use std::io;
use std::num::NonZeroUsize;
use std::panic::{self, AssertUnwindSafe};
use std::sync::Mutex;

use raf_source::{
    Budget, DeclaredCfg, DeclaredConfiguration, Entry, EntryKind, FileId, LoadErrorKind,
    LoadOptions, MemoryTree, Opened, ParallelOptions, ParallelTree, RelPath, TargetKind, Tree,
    TreeError, Workspace,
};

const MANIFEST: &str = "[package]\nname = \"demo\"\nversion = \"0.1.0\"\nedition = \"2021\"\n";
const GRANT: usize = 16 * 1024 * 1024;

fn options(workers: usize) -> ParallelOptions {
    ParallelOptions::new(NonZeroUsize::new(workers).expect("nonzero workers"), GRANT)
        .expect("scheduler fits")
}

#[test]
fn nested_inline_alternative_alias_and_target_loads_keep_the_serial_index_and_usage() {
    let tree = MemoryTree::from([
        ("Cargo.toml", MANIFEST),
        (
            "src/lib.rs",
            "mod a; mod b; mod inline { mod nested; } #[path=\"a.rs\"] mod alias; #[cfg_attr(test,path=\"other.rs\")] mod selected;",
        ),
        ("src/a.rs", "mod child; pub fn a() {}"),
        ("src/a/child.rs", "pub fn child() {}"),
        ("src/child.rs", "pub fn child() {}"),
        ("src/b.rs", "pub fn b() {}"),
        ("src/inline/nested.rs", "pub fn nested() {}"),
        ("src/selected.rs", "pub fn selected() {}"),
        ("src/other.rs", "pub fn other() {}"),
        ("src/main.rs", "fn main() {}"),
        ("tests/case.rs", "#[test] fn case() {}"),
    ]);
    let load = LoadOptions::default();
    let serial = Workspace::load_with(&tree, &["."], &load).expect("serial loads");
    for workers in [1, 2, 4] {
        let parallel =
            Workspace::par_load(&tree, &["."], &load, &options(workers)).expect("parallel loads");
        assert_eq!(serial, parallel);
        assert_eq!(serial.usage(), parallel.usage());
        let usage = parallel
            .parallel_usage()
            .expect("parallel counts finalized");
        assert!(usage.peak_live_jobs <= workers);
        assert!(usage.peak_scheduler_bytes <= GRANT);
        assert!(usage.peak_worker_stack_bytes <= workers * raf_source::PARSE_STACK_BYTES);
    }
    // MemoryTree accepts long components without creating any host path. A
    // plain module file retains this concrete stem as its child-directory context.
    let stem = "m".repeat(64 * 1024);
    let tree: MemoryTree = [
        ("Cargo.toml".to_owned(), MANIFEST.to_owned()),
        ("src/lib.rs".to_owned(), format!("mod {stem};")),
        (format!("src/{stem}.rs"), "mod child;".to_owned()),
        (
            format!("src/{stem}/child.rs"),
            "pub fn child() {}".to_owned(),
        ),
    ]
    .into_iter()
    .collect();
    let serial = Workspace::load_with(&tree, &["."], &load).expect("valid memory paths");
    let workers = NonZeroUsize::new(1).expect("nonzero");
    let below = ParallelOptions::new(workers, 32 * 1024)
        .expect("fixed scheduler fits below known stem size");
    let refused =
        Workspace::par_load(&tree, &["."], &load, &below).expect_err("retained stem exceeds grant");
    assert!(matches!(
        refused.kind,
        LoadErrorKind::Storage(purrdf_lex::allocation::StorageError::AdmissionFailed)
    ));
    let above = ParallelOptions::new(workers, 256 * 1024).expect("larger scheduler grant");
    let valid = Workspace::par_load(&tree, &["."], &load, &above).expect("known context fits");
    assert_eq!(serial, valid);
    assert_eq!(serial.usage(), valid.usage());
}

#[derive(Debug)]
struct OriginalCause;

impl fmt::Display for OriginalCause {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("original backend cause")
    }
}

impl std::error::Error for OriginalCause {}

struct FailingTree {
    tree: MemoryTree,
    panic: bool,
    alias: bool,
}

impl Tree for FailingTree {
    fn read(&self, path: &RelPath, maximum: u64) -> Result<String, TreeError> {
        self.tree.read(path, maximum)
    }

    fn read_unseen(
        &self,
        path: &RelPath,
        maximum: u64,
        seen: &dyn Fn(&FileId) -> bool,
    ) -> Result<Opened, TreeError> {
        if self.alias && matches!(path.as_str(), "src/a.rs" | "src/b.rs") {
            let id = FileId::from_key("shared");
            if seen(&id) {
                return Ok(Opened::Seen(id));
            }
            if path.as_str() == "src/a.rs" {
                return self
                    .tree
                    .read(path, maximum)
                    .map(|text| Opened::Read { id, text });
            }
        }
        if path.as_str() == "src/b.rs" {
            if self.panic {
                panic::panic_any(OriginalCause);
            }
            return Err(TreeError::Io {
                path: path.to_string(),
                source: io::Error::other(OriginalCause),
            });
        }
        self.tree.read_unseen(path, maximum, seen)
    }

    fn list(&self, path: &RelPath) -> Result<Vec<Entry>, TreeError> {
        self.tree.list(path)
    }

    fn entry(&self, path: &RelPath) -> Result<Option<EntryKind>, TreeError> {
        self.tree.entry(path)
    }
}

impl ParallelTree for FailingTree {}

fn failing_tree(early: &str, panic: bool) -> FailingTree {
    FailingTree {
        tree: MemoryTree::from([
            ("Cargo.toml", MANIFEST),
            ("src/lib.rs", "mod a; mod b;"),
            ("src/a.rs", early),
            ("src/a/child.rs", "fn broken("),
            ("src/b.rs", "pub fn b() {}"),
        ]),
        panic,
        alias: false,
    }
}

#[test]
fn an_earlier_canonical_descendant_error_wins_over_a_speculative_backend_panic() {
    let tree = failing_tree("mod child;", true);
    let load = LoadOptions::default();
    let serial = Workspace::load_with(&tree, &["."], &load).expect_err("invalid descendant");
    let result = panic::catch_unwind(AssertUnwindSafe(|| {
        Workspace::par_load(&tree, &["."], &load, &options(2))
    }));
    let parallel = result
        .expect("unreached panic discarded")
        .expect_err("invalid descendant");
    assert_eq!(serial.to_string(), parallel.to_string());
    assert_eq!(parallel.file.as_str(), "src/a/child.rs");
}

#[test]
fn reached_backend_failures_keep_original_typed_causes_and_panic_payloads() {
    let load = LoadOptions::default();
    let tree = failing_tree("pub fn a() {}", false);
    let error =
        Workspace::par_load(&tree, &["."], &load, &options(2)).expect_err("backend refuses");
    let LoadErrorKind::Tree(source) = error.kind else {
        panic!("original Tree cause required")
    };
    let TreeError::Io { source, .. } = *source else {
        panic!("original I/O cause required")
    };
    assert!(
        source
            .get_ref()
            .expect("custom source")
            .is::<OriginalCause>()
    );

    let tree = failing_tree("pub fn a() {}", true);
    let payload = panic::catch_unwind(AssertUnwindSafe(|| {
        Workspace::par_load(&tree, &["."], &load, &options(2))
    }))
    .expect_err("reached backend panic resumes");
    assert!(payload.is::<OriginalCause>());
}

#[test]
fn canonical_fact_allowances_refuse_the_exact_serial_position_beside_the_valid_limit() {
    let text = format!("/// {}\npub fn payload() {{}}\n", "d".repeat(4096));
    let tree = MemoryTree::from([
        ("Cargo.toml", MANIFEST),
        ("src/lib.rs", "mod a;\nmod b;\n"),
        ("src/a.rs", text.as_str()),
        ("src/b.rs", text.as_str()),
    ]);
    let serial = Workspace::load(&tree, &["."]).expect("unlimited facts");
    let mut load = LoadOptions::default();
    load.max_fact_bytes = serial.usage().fact_bytes;
    let valid = Workspace::par_load(&tree, &["."], &load, &options(2)).expect("exact limit");
    assert_eq!(serial, valid);
    assert_eq!(serial.usage(), valid.usage());
    load.max_fact_bytes -= 1;
    let serial = Workspace::load_with(&tree, &["."], &load).expect_err("one byte below");
    let parallel =
        Workspace::par_load(&tree, &["."], &load, &options(2)).expect_err("one byte below");
    assert_eq!(serial.to_string(), parallel.to_string());
    assert_eq!(parallel.file.as_str(), "src/b.rs");
    assert!(parallel.line.is_some());
    assert!(matches!(
        parallel.kind,
        LoadErrorKind::Budget {
            budget: Budget::FactBytes,
            ..
        }
    ));
}

struct ConfiguredTree {
    tree: MemoryTree,
    opened: Mutex<Vec<RelPath>>,
}

impl ConfiguredTree {
    fn new(shared: &str) -> Self {
        Self {
            tree: MemoryTree::from([
                ("Cargo.toml", MANIFEST),
                ("src/lib.rs", "#[cfg(test)] mod inactive; mod a; mod b;"),
                ("src/a.rs", "mod child;"),
                ("src/a/child.rs", shared),
                ("src/b.rs", shared),
                ("src/inactive.rs", "fn this_should_not_parse("),
            ]),
            opened: Mutex::default(),
        }
    }

    fn take_opened(&self) -> Vec<RelPath> {
        std::mem::take(&mut *self.opened.lock().expect("observation lock"))
    }
}

impl Tree for ConfiguredTree {
    fn read(&self, path: &RelPath, maximum: u64) -> Result<String, TreeError> {
        self.opened
            .lock()
            .expect("observation lock")
            .push(path.clone());
        self.tree.read(path, maximum)
    }

    fn read_unseen(
        &self,
        path: &RelPath,
        maximum: u64,
        seen: &dyn Fn(&FileId) -> bool,
    ) -> Result<Opened, TreeError> {
        if matches!(path.as_str(), "src/a/child.rs" | "src/b.rs") {
            let id = FileId::from_key("configured-shared");
            if seen(&id) {
                return Ok(Opened::Seen(id));
            }
            return self
                .read(path, maximum)
                .map(|text| Opened::Read { id, text });
        }
        self.opened
            .lock()
            .expect("observation lock")
            .push(path.clone());
        self.tree.read_unseen(path, maximum, seen)
    }

    fn list(&self, path: &RelPath) -> Result<Vec<Entry>, TreeError> {
        self.tree.list(path)
    }

    fn entry(&self, path: &RelPath) -> Result<Option<EntryKind>, TreeError> {
        self.tree.entry(path)
    }
}

impl ParallelTree for ConfiguredTree {}

#[test]
fn configured_parallel_ignores_inactive_paths_and_locates_a_reused_alias_at_its_canonical_reach() {
    // A speculative spelling shares its syntax with the canonical descendant.
    // Configuration and fact refusals use the descendant's path and grant.
    let manifest = RelPath::new("Cargo.toml").expect("manifest path");
    let declared = DeclaredConfiguration::new(
        DeclaredCfg::complete("configured-parser-fixture", &[]),
        &[],
        TargetKind::Lib,
        "demo",
    );
    let parallel_options = options(2);
    let serial = |tree: &ConfiguredTree, load: &LoadOptions| {
        Workspace::load_configured_target(tree, &manifest, declared, load)
    };
    let parallel = |tree: &ConfiguredTree, load: &LoadOptions| {
        Workspace::par_load_configured_target(tree, &manifest, declared, load, &parallel_options)
    };
    let good = ConfiguredTree::new("#[cfg(not(test))] pub fn shared_alias() {}");
    let mut load = LoadOptions::default();
    let expected = serial(&good, &load).expect("serial configured fixture");
    drop(good.take_opened());
    load.max_fact_bytes = expected.usage().fact_bytes;
    let actual = parallel(&good, &load).expect("exact canonical fact grant");
    assert_eq!(expected, actual);
    assert_eq!(expected.usage(), actual.usage());
    let opened = good.take_opened();
    assert!(!opened.iter().any(|path| path.as_str() == "src/inactive.rs"));
    let position = |name| {
        opened
            .iter()
            .position(|path| path.as_str() == name)
            .expect("active read")
    };
    assert!(
        position("src/b.rs") < position("src/a/child.rs"),
        "alias parsed speculatively"
    );

    load.max_fact_bytes -= 1;
    let serial_error = serial(&good, &load).expect_err("one byte below canonical fact grant");
    let parallel_error = parallel(&good, &load).expect_err("same canonical fact refusal");
    assert_eq!(serial_error.to_string(), parallel_error.to_string());
    assert_eq!(parallel_error.file.as_str(), "src/a/child.rs");
    assert!(matches!(
        parallel_error.kind,
        LoadErrorKind::Budget {
            budget: Budget::FactBytes,
            ..
        }
    ));

    load.max_fact_bytes += 1;
    let bad = ConfiguredTree::new("#[cfg(not(test, test))] pub fn shared_alias() {}");
    let serial_error = serial(&bad, &load).expect_err("invalid configured predicate");
    let parallel_error = parallel(&bad, &load).expect_err("same configured predicate refusal");
    assert_eq!(serial_error.to_string(), parallel_error.to_string());
    assert_eq!(parallel_error.file.as_str(), "src/a/child.rs");
    assert!(matches!(
        parallel_error.kind,
        LoadErrorKind::Locked {
            field: "configuration",
            ..
        }
    ));
}

#[test]
fn a_committed_physical_alias_overrides_provisional_unseen_read_errors_and_panics() {
    for panic in [false, true] {
        let tree = FailingTree {
            tree: MemoryTree::from([
                ("Cargo.toml", MANIFEST),
                ("src/lib.rs", "mod a; mod b;"),
                ("src/a.rs", "pub fn shared() {}"),
                ("src/b.rs", "pub fn shared() {}"),
            ]),
            panic,
            alias: true,
        };
        let serial = Workspace::load(&tree, &["."]).expect("alias was already read");
        let parallel = panic::catch_unwind(AssertUnwindSafe(|| {
            Workspace::par_load(&tree, &["."], &LoadOptions::default(), &options(2))
        }))
        .expect("provisional alias panic is not canonical")
        .expect("committed alias needs no unreadable second observation");
        assert_eq!(serial, parallel);
        assert_eq!(serial.usage(), parallel.usage());
    }
}

fn broken_tree(entries: &[(&str, &str)]) -> MemoryTree {
    let mut all = vec![("Cargo.toml", MANIFEST)];
    all.extend_from_slice(entries);
    MemoryTree::from_iter(all)
}

fn assert_same_error(tree: &MemoryTree, expected_file: &str) {
    let load = LoadOptions::default();
    let serial = Workspace::load_with(tree, &["."], &load).expect_err("serial refuses");
    assert_eq!(serial.file.as_str(), expected_file);
    for workers in [1, 2, 4] {
        let parallel = Workspace::par_load(tree, &["."], &load, &options(workers))
            .expect_err("parallel refuses");
        assert_eq!(
            serial.to_string(),
            parallel.to_string(),
            "workers {workers}"
        );
        assert_eq!(serial.file, parallel.file, "workers {workers}");
        assert_eq!(serial.line, parallel.line, "workers {workers}");
        assert!(matches!(parallel.kind, LoadErrorKind::Syntax { .. }));
    }
}

#[test]
fn a_later_speculative_syntax_error_does_not_mask_an_earlier_canonical_error() {
    let both = broken_tree(&[
        ("src/lib.rs", "mod a;\nmod b;\n"),
        ("src/a.rs", "mod c;\n"),
        ("src/a/c.rs", "fn broken( {\n"),
        ("src/b.rs", "fn also_broken( {\n"),
    ]);
    assert_same_error(&both, "src/a/c.rs");
    let neighbour = broken_tree(&[
        ("src/lib.rs", "mod a;\nmod b;\n"),
        ("src/a.rs", "mod c;\n"),
        ("src/a/c.rs", "pub fn fine() {}\n"),
        ("src/b.rs", "fn also_broken( {\n"),
    ]);
    assert_same_error(&neighbour, "src/b.rs");
}

#[test]
fn a_reached_speculative_syntax_error_equals_the_serial_error() {
    let broken = broken_tree(&[
        ("src/lib.rs", "mod a;\nmod b;\nmod c;\n"),
        ("src/a.rs", "pub fn a() {}\n"),
        ("src/b.rs", "pub fn b() {}\n\nstruct {\n"),
        ("src/c.rs", "pub fn c() {}\n"),
    ]);
    assert_same_error(&broken, "src/b.rs");
    let valid = broken_tree(&[
        ("src/lib.rs", "mod a;\nmod b;\nmod c;\n"),
        ("src/a.rs", "pub fn a() {}\n"),
        ("src/b.rs", "pub fn b() {}\n"),
        ("src/c.rs", "pub fn c() {}\n"),
    ]);
    let serial = Workspace::load(&valid, &["."]).expect("serial loads");
    let parallel = Workspace::par_load(&valid, &["."], &LoadOptions::default(), &options(2))
        .expect("parallel loads");
    assert_eq!(serial, parallel);
}

#[test]
fn a_canonical_reach_names_the_file_in_a_diagnostic_found_through_another_spelling() {
    let broken = broken_tree(&[
        (
            "src/lib.rs",
            "mod a;\n#[path = \"a.rs\"]\nmod alias;\nmod z;\n",
        ),
        ("src/a.rs", "fn broken( {\n"),
        ("src/z.rs", "pub fn z() {}\n"),
    ]);
    assert_same_error(&broken, "src/a.rs");
    let valid = broken_tree(&[
        (
            "src/lib.rs",
            "mod a;\n#[path = \"a.rs\"]\nmod alias;\nmod z;\n",
        ),
        ("src/a.rs", "pub fn a() {}\n"),
        ("src/z.rs", "pub fn z() {}\n"),
    ]);
    let serial = Workspace::load(&valid, &["."]).expect("serial loads");
    let parallel = Workspace::par_load(&valid, &["."], &LoadOptions::default(), &options(2))
        .expect("parallel loads");
    assert_eq!(serial, parallel);
}

#[test]
fn scheduler_refusals_are_typed_beside_a_valid_grant() {
    let one = NonZeroUsize::new(1).expect("nonzero");
    assert!(ParallelOptions::new(one, GRANT).is_ok());
    let overflow = ParallelOptions::new(NonZeroUsize::new(usize::MAX).expect("nonzero"), GRANT);
    assert_eq!(
        overflow,
        Err(raf_source::ParallelOptionsError::SizeOverflow)
    );
    let short = ParallelOptions::new(one, 0);
    assert!(matches!(
        short,
        Err(raf_source::ParallelOptionsError::SchedulerStorage { maximum: 0, required }) if required > 0
    ));
}