Skip to main content

raf_source/resolve/
observation.rs

1// SPDX-FileCopyrightText: 2026 Blackcat InformaticsĀ® Inc. <paudley@blackcatinformatics.ca>
2// SPDX-License-Identifier: MIT OR Apache-2.0 OR MulanPSL-2.0
3
4//! Optional observations from the ordinary failed native lookup, without a replay.
5
6use super::{Index, Namespace, Resolution, ResolveError, Source, Steps, TargetId};
7use crate::error::LoadError;
8use crate::meter::{Meter, QueryObservation};
9
10/// One canonical lookup and the actual indexed targets its missing routes read.
11/// Target identities are local to this workspace load, not provider authority.
12#[derive(Debug, Clone, PartialEq, Eq)]
13#[non_exhaustive]
14pub enum ResolutionAttempt {
15    /// The ordinary resolved definition identity.
16    Resolved(Resolution),
17    /// No definition resolved. Targets are distinct, in first observation order;
18    /// they include private import routes followed by the native resolver.
19    NotFound {
20        /// Targets whose module scopes the failed lookup actually read.
21        attempted_targets: Vec<TargetId>,
22    },
23}
24
25impl Index {
26    pub(crate) fn resolve_observed(
27        &self,
28        path: &str,
29        namespace: Namespace,
30        meter: &Meter,
31    ) -> Result<ResolutionAttempt, ResolveError> {
32        let observation = QueryObservation::new(meter);
33        let steps = Steps::Observed(&observation);
34        let answer = self.resolve_counted(path, Some(namespace), None, steps);
35        match self.query_checked(steps, answer) {
36            Ok(resolution) => Ok(ResolutionAttempt::Resolved(resolution)),
37            Err(ResolveError::NotFound(_)) => Ok(ResolutionAttempt::NotFound {
38                attempted_targets: observation.finish(),
39            }),
40            Err(error) => Err(error),
41        }
42    }
43
44    pub(super) fn record_failed_routes<'a>(
45        &self,
46        sources: impl Iterator<Item = &'a Source>,
47        steps: Steps<'_>,
48    ) -> Result<(), LoadError> {
49        let Steps::Observed(observation) = steps else {
50            return Ok(());
51        };
52        for source in sources {
53            let (file, line) = self.module_site(source.module);
54            observation.record(self.modules[source.module].target, &file, line)?;
55        }
56        Ok(())
57    }
58}