Skip to main content

raf_source/code/
syntax.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//! Reuse the admitted target's declaration selection on the bounded parse worker.
5
6use super::{CodeResolveError, CodeResolver};
7use crate::{RelPath, SourceFile, TargetId, meter::Meter};
8
9impl CodeResolver<'_> {
10    /// Inspect an actual loaded file after its target's declared header selection.
11    ///
12    /// Uses the same parser and configuration filter as indexing. Spans retain
13    /// their original physical bytes. Selection covers modeled declaration headers,
14    /// not conditional body expressions or macro expansion. Borrowed syntax cannot
15    /// escape the parse worker. Temporary selection uses the original load allowance after
16    /// this query's retained reading table; it never receives a fresh allowance.
17    ///
18    /// # Errors
19    /// Refuses absent/unconfigured targets, files not reached by that target, and
20    /// original bounded parser/configuration failures with their typed causes.
21    pub fn with_configured_syntax<R: Send>(
22        &self,
23        target: TargetId,
24        path: &RelPath,
25        inspect: impl FnOnce(&SourceFile, &syn::File) -> R + Send,
26    ) -> Result<R, CodeResolveError> {
27        let missing = |reason| CodeResolveError::SyntaxContext {
28            target,
29            file: path.clone(),
30            reason,
31        };
32        let target_id = target;
33        let target = self
34            .workspace
35            .target(target_id)
36            .ok_or_else(|| missing("target is absent"))?;
37        let meter = Meter::resume(self.options, self.usage);
38        let refused = |error| CodeResolveError::ConfiguredSyntax(Box::new(error));
39        let mut file = None;
40        for candidate in self.workspace.files() {
41            meter
42                .work(
43                    candidate
44                        .path
45                        .as_str()
46                        .len()
47                        .saturating_add(path.as_str().len())
48                        .saturating_add(1),
49                    path,
50                    None,
51                )
52                .map_err(refused)?;
53            if candidate.path == *path {
54                file = Some(candidate);
55                break;
56            }
57        }
58        let file = file.ok_or_else(|| missing("source file is absent"))?;
59        meter
60            .work(file.reached.len(), path, None)
61            .map_err(refused)?;
62        if !file.reached.iter().any(|reach| reach.target == target_id) {
63            return Err(missing("target does not reach this source file"));
64        }
65        let configuration = if let Some(configuration) = self.workspace.selected_configuration() {
66            configuration
67        } else {
68            self.workspace
69                .locked_binding(target.package_id)
70                .ok_or_else(|| missing("supply its exact declared configuration"))?
71                .configuration_for_under(target.kind, &target.name, &meter, path)
72                .map_err(refused)?
73                .ok_or_else(|| missing("supply its exact declared configuration"))?
74        };
75        let options = self.options;
76        let usage = meter.usage();
77        crate::parse::parse_counted_mut(path, &file.text, options.max_file_tokens, move |syntax| {
78            let meter = Meter::resume(options, usage);
79            crate::configuration::filter(syntax, configuration, &meter, path)?;
80            Ok::<_, crate::LoadError>(inspect(file, syntax))
81        })
82        .map_err(|error| CodeResolveError::ConfiguredSyntax(Box::new(error)))?
83        .0
84        .map_err(|error| CodeResolveError::ConfiguredSyntax(Box::new(error)))
85    }
86}