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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
// SPDX-FileCopyrightText: 2026 Blackcat InformaticsĀ® Inc. <paudley@blackcatinformatics.ca>
// SPDX-License-Identifier: MIT OR Apache-2.0 OR MulanPSL-2.0

//! Code-head lookup from an actual indexed source owner.

mod aliases;
mod library;
mod syntax;
pub use library::{ExternLibrary, ExternRouteKind};

use std::error::Error;
use std::fmt;

use purrdf_lex::allocation::Memory;

use crate::index::{Index, UseKind};
use crate::meter::{ItemAdmission, Meter};
use crate::reach::{Home, fill_homes};
use crate::resolve::Heads;
use crate::{
    ImplId, ItemId, LoadError, LoadOptions, Namespace, Reach, RelPath, Resolution, ResolveError,
    SymbolKind, TargetId, TestRole, Usage, Workspace,
};

/// Failure to establish a reading context or resolve its module-level code path.
#[derive(Debug)]
#[non_exhaustive]
pub enum CodeResolveError {
    /// The load-local owner is absent from this workspace.
    UnknownItem(ItemId),
    /// The load-local impl is absent from this workspace.
    UnknownImpl(ImplId),
    /// The byte is outside the selected item's source span.
    OutsideItem(ItemId, usize),
    /// An indexed owner has no source-owned reading module.
    MissingContext(ItemId),
    /// A definition-scope query names an item that is not a module.
    ExpectedModule(ItemId),
    /// Macro definition tokens require invocation context before name resolution.
    MacroDefinition(ItemId),
    /// The shared resolver refused the path; its original cause is retained.
    Resolve(ResolveError),
    /// The library's public surface refused its original resource or resolution query.
    Exports(Box<crate::ExportError>),
    /// Configured syntax selection preserves its original parser/load refusal.
    ConfiguredSyntax(Box<LoadError>),
    /// The bounded code query preserves its original resource refusal.
    Query(Box<LoadError>),
    /// Generic applicability of this inherent method is not established.
    MethodApplicability(ItemId),
    /// Alias-member applicability is outside the explicitly supported syntax.
    AliasMember {
        /// The actual alias definition needing additional evidence.
        alias: ItemId,
        /// The requested associated member.
        member: String,
        /// The missing applicability evidence.
        reason: &'static str,
    },
    /// A configured syntax query lacks an actual admitted target/file context.
    SyntaxContext {
        /// The requested actual target.
        target: TargetId,
        /// The requested source file.
        file: RelPath,
        /// The missing context that the caller must supply.
        reason: &'static str,
    },
    /// A definition exists but is not exported by the selected library.
    NotExported {
        /// The selected actual library root.
        root: ItemId,
        /// The requested relative API path.
        path: String,
    },
}

impl fmt::Display for CodeResolveError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnknownItem(id) => write!(f, "source owner {id:?} is absent from this workspace"),
            Self::UnknownImpl(id) => write!(f, "source impl {id:?} is absent from this workspace"),
            Self::OutsideItem(id, byte) => write!(f, "byte {byte} is outside source owner {id:?}"),
            Self::MissingContext(id) => write!(f, "source owner {id:?} has no reading module"),
            Self::ExpectedModule(id) => write!(f, "source scope {id:?} is not a module"),
            Self::MacroDefinition(id) => write!(
                f,
                "source owner {id:?} is a macro definition; code lookup requires an actual invocation context"
            ),
            Self::Resolve(error) => error.fmt(f),
            Self::Exports(error) => error.fmt(f),
            Self::ConfiguredSyntax(error) | Self::Query(error) => error.fmt(f),
            Self::MethodApplicability(id) => write!(
                f,
                "inherent method {id:?} needs generic applicability evidence beyond plain nominal types and unconstrained forwarded const parameters"
            ),
            Self::AliasMember {
                alias,
                member,
                reason,
            } => write!(f, "alias {alias:?} member {member:?}: {reason}"),
            Self::SyntaxContext {
                target,
                file,
                reason,
            } => write!(
                f,
                "configured syntax for {file} in target {target:?}: {reason}"
            ),
            Self::NotExported { root, path } => write!(
                f,
                "library root {root:?} does not export the definition of {path:?}"
            ),
        }
    }
}

impl Error for CodeResolveError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Resolve(error) => Some(error),
            Self::Exports(error) => Some(error.as_ref()),
            Self::ConfiguredSyntax(error) | Self::Query(error) => Some(error.as_ref()),
            _ => None,
        }
    }
}

/// Module-level code lookup with source-owned item/module/target association.
///
/// A lookup uses code-head edition and dev/test rules, rather than `use` rules.
/// The caller must first classify lexical locals, parameters, generic bindings
/// and block imports. This query does not infer those scopes, expand macros,
/// resolve `Self` or authenticate an unindexed dependency from its spelling.
/// Item identities belong to the workspace that constructed this query.
pub struct CodeResolver<'a> {
    workspace: &'a Workspace,
    options: &'a LoadOptions,
    index: &'a Index,
    homes: Vec<Home>,
    usage: Usage,
}

/// One import in the module index, with its exact target and reading context.
pub struct IndexedImport<'query, 'workspace> {
    code: &'query CodeResolver<'workspace>,
    position: usize,
}

impl IndexedImport<'_, '_> {
    /// The target and location where this flattened import was written.
    #[must_use]
    pub fn site(&self) -> (TargetId, &RelPath, usize) {
        let declaration = &self.code.index.uses[self.position];
        (
            self.code.index.modules[declaration.module].target,
            &declaration.file,
            declaration.line,
        )
    }

    /// The imported path segments, whether they start with `::`, whether
    /// the declaration imports a glob from that path, and the namespaces
    /// in which the full path is read.
    #[must_use]
    pub fn path(&self) -> (&[String], bool, bool, &'static [Namespace]) {
        let declaration = &self.code.index.uses[self.position];
        let (segments, glob) = match &declaration.kind {
            UseKind::Named { segs, .. } => (segs, false),
            UseKind::Glob { segs } => (segs, true),
        };
        (
            segments,
            declaration.leading_colon,
            glob,
            declaration.kind.terminal_namespaces(),
        )
    }

    /// Resolve the whole import path or a prefix of it in its own use context.
    ///
    /// # Errors
    /// Preserves shared resolver errors, including invalid prefixes.
    pub fn resolve(
        &self,
        path: &str,
        namespace: Namespace,
    ) -> Result<Resolution, CodeResolveError> {
        let declaration = &self.code.index.uses[self.position];
        self.code
            .resolve_code_at(
                declaration.module,
                path,
                namespace,
                declaration.heads(self.position),
            )
            .map(|(resolution, _)| resolution)
    }
}

impl<'a> CodeResolver<'a> {
    pub(crate) fn new(
        workspace: &'a Workspace,
        index: &'a Index,
        options: &'a LoadOptions,
        usage: Usage,
    ) -> Result<Self, LoadError> {
        let meter = Meter::new(options);
        let root = RelPath::root();
        meter.items(usage.item_bytes, &root, None)?;
        meter.items(
            size_of::<&Workspace>() + size_of::<&LoadOptions>(),
            &root,
            None,
        )?;
        let mut homes = Vec::new();
        {
            let mut admission = ItemAdmission::new(&meter, &root);
            Memory::new(&mut admission)
                .reserve(&mut homes, index.items.len())
                .map_err(|error| admission.error(error))?;
        }
        homes.resize(index.items.len(), Home::default());
        fill_homes(index, &mut homes, true);
        let mut usage = usage;
        usage.item_bytes = meter.usage().item_bytes;
        Ok(Self {
            workspace,
            options,
            index,
            homes,
            usage,
        })
    }

    /// The original load plus this query's retained reading-table allowance.
    #[must_use]
    pub const fn usage(&self) -> Usage {
        self.usage
    }

    /// Every flattened module import in its exact admitted reading context.
    pub fn indexed_imports(&self) -> impl Iterator<Item = IndexedImport<'_, 'a>> {
        (0..self.index.uses.len()).map(|position| IndexedImport {
            code: self,
            position,
        })
    }

    /// Resolve a name at the file's own module, as reached by this workspace.
    /// Use this for file-level syntax that has no named item of its own.
    ///
    /// # Errors
    /// Refuses a reach whose target or module is absent from this index.
    /// Preserves every shared resolver error.
    pub fn resolve_from_file_reach(
        &self,
        reach: &Reach,
        path: &str,
        namespace: Namespace,
    ) -> Result<Resolution, CodeResolveError> {
        let (module, heads) = self.file_context(reach, false)?;
        self.resolve_code_at(module, path, namespace, heads)
            .map(|(resolution, _)| resolution)
    }

    /// Resolve an import spelling in a file's own module and target.
    ///
    /// # Errors
    /// Refuses an absent reach and preserves the shared resolver errors.
    pub fn resolve_import_from_file_reach(
        &self,
        reach: &Reach,
        path: &str,
        namespace: Namespace,
    ) -> Result<Resolution, CodeResolveError> {
        let (module, heads) = self.file_context(reach, true)?;
        self.resolve_code_at(module, path, namespace, heads)
            .map(|(resolution, _)| resolution)
    }

    fn file_context(
        &self,
        reach: &Reach,
        import: bool,
    ) -> Result<(usize, Heads), CodeResolveError> {
        let module = self
            .index
            .target_roots
            .get(reach.target.index())
            .and_then(|&root| {
                self.index
                    .module_by_path
                    .get(&(root, reach.module_path.clone()))
            })
            .copied()
            .ok_or_else(|| {
                CodeResolveError::Resolve(ResolveError::UnknownModule(
                    reach.module_path.to_string(),
                ))
            })?;
        Ok((module, reading_heads(reach.test, import)))
    }

    /// Read a module-level path in the lexical module of an indexed impl.
    /// Use this for the impl header, which has no named item of its own.
    ///
    /// # Errors
    /// Refuses an unknown impl and preserves every shared resolver error.
    pub fn resolve_from_impl(
        &self,
        implementation: ImplId,
        path: &str,
        namespace: Namespace,
        import: bool,
    ) -> Result<Resolution, CodeResolveError> {
        let block = self
            .workspace
            .impl_block(implementation)
            .ok_or(CodeResolveError::UnknownImpl(implementation))?;
        let test = block.test != TestRole::None;
        let heads = if import {
            Heads::Use { test, own: None }
        } else {
            Heads::Code { test }
        };
        self.resolve_code_at(block.module_index, path, namespace, heads)
            .map(|(resolution, _)| resolution)
    }

    fn module(&self, owner: ItemId, byte: usize) -> Result<usize, CodeResolveError> {
        let item = self
            .index
            .items
            .get(owner.index())
            .ok_or(CodeResolveError::UnknownItem(owner))?;
        if !(item.span.start_byte..item.span.end_byte).contains(&byte) {
            return Err(CodeResolveError::OutsideItem(owner, byte));
        }
        if item.kind == SymbolKind::Mod
            && item
                .body
                .as_ref()
                .is_some_and(|body| (body.start_byte..body.end_byte).contains(&byte))
        {
            return self
                .index
                .module_of
                .get(&owner)
                .copied()
                .ok_or(CodeResolveError::MissingContext(owner));
        }
        self.homes[owner.index()]
            .module()
            .ok_or(CodeResolveError::MissingContext(owner))
    }

    /// The actual reading module of a byte within an indexed owner.
    /// Inline module bodies read their own module; their declaration headers
    /// read the containing module. Method and trait ownership comes from Source.
    ///
    /// # Errors
    /// Refuses an absent owner, outside byte or missing reading context.
    pub fn reading_module(&self, owner: ItemId, byte: usize) -> Result<&str, CodeResolveError> {
        Ok(self.index.modules[self.module(owner, byte)?].path.as_str())
    }

    /// Whether an owner's reading module and its ancestors have no indexed
    /// configuration-dependent declarations or file reaches.
    ///
    /// This classifies the module route. The caller still checks the syntax
    /// of the occurrence and its enclosing items.
    ///
    /// # Errors
    /// Refuses an absent owner, outside byte, missing context or exceeded query grant.
    pub fn has_unconditional_reading_module(
        &self,
        owner: ItemId,
        byte: usize,
    ) -> Result<bool, CodeResolveError> {
        let module = self.module(owner, byte)?;
        self.owner_query(owner, |index, steps| {
            index.module_is_unconditional(module, steps)
        })
    }

    /// Check a file reach's module and ancestor declarations for configuration dependence.
    ///
    /// # Errors
    /// Refuses an absent reach or an exceeded query grant.
    pub fn has_unconditional_file_reach(&self, reach: &Reach) -> Result<bool, CodeResolveError> {
        self.file_query(reach, false, |index, module, _, steps| {
            index.module_is_unconditional(module, steps)
        })
    }

    /// Whether an indexed item is defined in this actual module or a descendant.
    /// A relocated export and an impl member keep their lexical defining module.
    /// Distinct targets never share scope merely because their paths agree.
    ///
    /// # Errors
    /// Refuses absent identities, a non-module scope or missing reading context.
    pub fn is_defined_in(&self, owner: ItemId, scope: ItemId) -> Result<bool, CodeResolveError> {
        let item = self
            .index
            .items
            .get(owner.index())
            .ok_or(CodeResolveError::UnknownItem(owner))?;
        let module = self
            .index
            .items
            .get(scope.index())
            .ok_or(CodeResolveError::UnknownItem(scope))?;
        if module.kind != SymbolKind::Mod {
            return Err(CodeResolveError::ExpectedModule(scope));
        }
        if item.target != module.target {
            return Ok(false);
        }
        if owner == scope {
            return Ok(true);
        }
        let wanted = self
            .index
            .module_of
            .get(&scope)
            .copied()
            .or_else(|| {
                self.index
                    .root_items
                    .get(&self.index.target_roots[module.target.index()])
                    .filter(|id| **id == scope)
                    .and_then(|_| self.homes[scope.index()].module())
            })
            .ok_or(CodeResolveError::MissingContext(scope))?;
        let mut actual = self.homes[owner.index()]
            .module()
            .ok_or(CodeResolveError::MissingContext(owner))?;
        loop {
            if actual == wanted {
                return Ok(true);
            }
            let Some(parent) = self.index.modules[actual].parent else {
                return Ok(false);
            };
            actual = parent;
        }
    }

    /// Resolve a module-level code path in one namespace from its actual owner.
    /// Lexical shadow classification is a separate prerequisite; successful
    /// module lookup alone is not proof that an occurrence names that item.
    ///
    /// # Errors
    /// Refuses missing reading context and preserves every shared resolver error.
    pub fn resolve_from_item_at(
        &self,
        owner: ItemId,
        byte: usize,
        path: &str,
        namespace: Namespace,
    ) -> Result<Resolution, CodeResolveError> {
        self.resolve_owned(owner, byte, path, namespace, false)
    }

    /// Resolve an import spelling from an actual source owner's reading module.
    /// This preserves edition-specific `use` roots for block import observations;
    /// it does not register a lexical import or infer a missing external namespace.
    ///
    /// # Errors
    /// The owner, macro-body and shared resolver refusals of code lookup.
    pub fn resolve_import_from_item_at(
        &self,
        owner: ItemId,
        byte: usize,
        path: &str,
        namespace: Namespace,
    ) -> Result<Resolution, CodeResolveError> {
        self.resolve_owned(owner, byte, path, namespace, true)
    }

    /// Follow visible imports to candidate members of indexed enums.
    ///
    /// Each candidate carries an enum identity and the terminal spelling in
    /// the import. The caller validates the declaration and constructor form
    /// and requires a unique surviving definition. Classify lexical shadowing before
    /// asking this module-level query. Set `import` when the spelling was
    /// written in an import declaration, to preserve its edition rules.
    ///
    /// An empty candidate list means the inspected routes supply no variants.
    /// An existing definition or an inconclusive or conditional route returns
    /// `None`, so the caller cannot infer the name is available to another route.
    ///
    /// # Errors
    /// Refuses missing owner context, invalid paths and native
    /// resource exhaustion.
    pub fn enum_member_imports_from_item_at(
        &self,
        owner: ItemId,
        byte: usize,
        path: &str,
        namespace: Namespace,
        import: bool,
    ) -> Result<Option<Vec<(ItemId, String)>>, CodeResolveError> {
        let (module, heads) = self.owned_context(owner, byte, import)?;
        self.owner_query(owner, |index, steps| {
            index.enum_member_import(module, path, namespace, heads, steps)
        })
    }

    /// Follow visible enum member imports from a file's own module and target.
    ///
    /// As [`Self::enum_member_imports_from_item_at`], validate candidates against the
    /// enum declaration and set `import` for a spelling read in an import.
    ///
    /// # Errors
    /// Refuses an absent reach, invalid path or exhausted query resources.
    pub fn enum_member_imports_from_file_reach(
        &self,
        reach: &Reach,
        path: &str,
        namespace: Namespace,
        import: bool,
    ) -> Result<Option<Vec<(ItemId, String)>>, CodeResolveError> {
        self.file_query(reach, import, |index, module, heads, steps| {
            index.enum_member_import(module, path, namespace, heads, steps)
        })
    }

    /// Find a single named type whose module and import route is unconditional.
    /// Lexical bindings inside blocks and type alias substitution belong to the caller.
    ///
    /// # Errors
    /// Refuses an invalid reading context or path and native resource exhaustion.
    pub fn unconditional_type_from_item_at(
        &self,
        owner: ItemId,
        byte: usize,
        path: &str,
        import: bool,
    ) -> Result<Option<ItemId>, CodeResolveError> {
        let (module, heads) = self.owned_context(owner, byte, import)?;
        self.owner_query(owner, |index, steps| {
            index.unconditional_type_path(module, path, heads, steps)
        })
    }

    /// Find a single named type by an unconditional route from a file's own module.
    ///
    /// # Errors
    /// Refuses an absent reach, invalid path or exhausted query resources.
    pub fn unconditional_type_from_file_reach(
        &self,
        reach: &Reach,
        path: &str,
        import: bool,
    ) -> Result<Option<ItemId>, CodeResolveError> {
        self.file_query(reach, import, |index, module, heads, steps| {
            index.unconditional_type_path(module, path, heads, steps)
        })
    }

    fn file_query<T>(
        &self,
        reach: &Reach,
        import: bool,
        query: impl FnOnce(&'a Index, usize, Heads, crate::meter::Steps<'_>) -> Result<T, ResolveError>,
    ) -> Result<T, CodeResolveError> {
        let (module, heads) = self.file_context(reach, import)?;
        let (file, line) = self.index.module_site(module);
        self.query_at((&file, line), |index, steps| {
            query(index, module, heads, steps)
        })
    }

    fn owner_query<T>(
        &self,
        owner: ItemId,
        query: impl FnOnce(&'a Index, crate::meter::Steps<'_>) -> Result<T, ResolveError>,
    ) -> Result<T, CodeResolveError> {
        let item = &self.index.items[owner.index()];
        self.query_at((&item.file, Some(item.name_line)), query)
    }

    fn query_at<T>(
        &self,
        (file, line): (&RelPath, Option<usize>),
        query: impl FnOnce(&'a Index, crate::meter::Steps<'_>) -> Result<T, ResolveError>,
    ) -> Result<T, CodeResolveError> {
        let meter = Meter::resume_query(
            self.options,
            Usage {
                item_bytes: self.usage.item_bytes,
                ..Usage::default()
            },
        );
        let steps = crate::meter::Steps::QueryItems {
            meter: &meter,
            file,
            line,
        };
        let answer = query(self.index, steps);
        Index::query_refusal(steps)
            .map_err(|refused| CodeResolveError::Resolve(self.index.refused(refused)))?;
        answer.map_err(CodeResolveError::Resolve)
    }

    fn resolve_owned(
        &self,
        owner: ItemId,
        byte: usize,
        path: &str,
        namespace: Namespace,
        import: bool,
    ) -> Result<Resolution, CodeResolveError> {
        let (module, heads) = self.owned_context(owner, byte, import)?;
        self.resolve_code_at(module, path, namespace, heads)
            .map(|(resolution, _)| resolution)
    }

    fn owned_context(
        &self,
        owner: ItemId,
        byte: usize,
        import: bool,
    ) -> Result<(usize, Heads), CodeResolveError> {
        let module = self.module(owner, byte)?;
        let item = &self.index.items[owner.index()];
        if item.kind == SymbolKind::MacroRules
            && item
                .body
                .as_ref()
                .is_some_and(|body| (body.start_byte..body.end_byte).contains(&byte))
        {
            return Err(CodeResolveError::MacroDefinition(owner));
        }
        let test = self.index.items[owner.index()].test != TestRole::None;
        Ok((module, reading_heads(test, import)))
    }
}

fn reading_heads(test: bool, import: bool) -> Heads {
    if import {
        Heads::Use { test, own: None }
    } else {
        Heads::Code { test }
    }
}