tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

175 lignes · 6612 octetsBrut · Historique
1//! `@comptime` compilation shared by the CLI, testkit and language server.
2//!
3//! Like `@fip`, the source-only analyzers lower a temporary Program only when
4//! a marker is present. Compilation itself supplies its already-lowered Core
5//! to [`evaluate_comptime`] after the existing `@fip` diagnostics.
6
7use std::collections::BTreeSet;
8
9use louise_core::ir::Program;
10use louise_core::lower::Settings;
11use louise_diag::Diagnostic;
12use louise_hir::DefId;
13
14use crate::{Analysis, Db};
15
16/// A structured source-analysis cancellation outcome. Ordinary source errors
17/// remain `Ok(Vec<Diagnostic>)`; `Cancelled` deliberately carries no partial
18/// diagnostic packet for a caller to publish.
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
20pub enum ComptimeDiagnosticsError {
21 Cancelled,
22}
23
24/// Apply the one transactional CTFE transform used by every Core consumer.
25pub fn evaluate_comptime(
26 analysis: &Analysis,
27 program: Program,
28) -> Result<Program, Vec<Diagnostic>> {
29 if !has_comptime(analysis) {
30 return Ok(program);
31 }
32 louise_comptime::evaluate_declarations(&analysis.hir, &analysis.typeck, program)
33}
34
35/// `@fip` first, then CTFE, for a check/LSP analysis with no earlier errors.
36/// Keeping this order prevents a CTFE rewrite from hiding an existing `@fip`
37/// violation and matches the command pipeline. `db` supplies the root package
38/// settings retained while its sources were loaded. This compatibility entry
39/// never cancels, so current check and LSP scheduling remain unchanged.
40pub fn semantic_diagnostics(db: &Db, analysis: &Analysis) -> Vec<Diagnostic> {
41 uncancelled(semantic_diagnostics_with(db, analysis, &|| false))
42}
43
44/// [`semantic_diagnostics`] with cooperative CTFE machine cancellation.
45/// Existing `@fip` errors still short-circuit before CTFE, irrespective of the
46/// predicate. A later cancelled packet discards diagnostics from earlier
47/// packets by returning `Err(Cancelled)`: a future scheduler must drop that
48/// whole candidate rather than publish a partial analysis. Source parsing,
49/// lowering, FIP analysis and CTFE preparation are not interruptible here.
50pub fn semantic_diagnostics_with(
51 db: &Db,
52 analysis: &Analysis,
53 cancelled: &dyn Fn() -> bool,
54) -> Result<Vec<Diagnostic>, ComptimeDiagnosticsError> {
55 let fip = crate::fip_diagnostics(analysis);
56 if !fip.is_empty() {
57 return Ok(fip);
58 }
59 comptime_diagnostics_with(db, analysis, cancelled)
60}
61
62/// CTFE diagnostics without retaining a transformed program. This
63/// compatibility entry never cancels. Fresh evaluator state is dropped inside
64/// the driver after every success or failure. Each root-package resolution is
65/// checked with its own immutable settings, just as compiling that root would
66/// be; source outside packages keeps the default settings. No setting is read
67/// from the host environment here.
68pub fn comptime_diagnostics(db: &Db, analysis: &Analysis) -> Vec<Diagnostic> {
69 uncancelled(comptime_diagnostics_with(db, analysis, &|| false))
70}
71
72/// [`comptime_diagnostics`] with cooperative direct-machine cancellation.
73/// Returning `Err(Cancelled)` publishes neither partial diagnostics nor any
74/// transformed Program. The borrowed `Db` and its package/source membership
75/// are not mutated. No new diagnostic represents cancellation.
76pub fn comptime_diagnostics_with(
77 db: &Db,
78 analysis: &Analysis,
79 cancelled: &dyn Fn() -> bool,
80) -> Result<Vec<Diagnostic>, ComptimeDiagnosticsError> {
81 if !has_comptime(analysis) {
82 return Ok(Vec::new());
83 }
84 let marked = marked_roots(analysis);
85 let mut assigned = BTreeSet::new();
86 let mut diagnostics = Vec::new();
87
88 // Dependencies are evaluated with the root's policy too, matching a real
89 // compilation whose single lowered Program has that root's intrinsic body.
90 // A shared dependency can therefore be checked once for each root that
91 // owns it, rather than accidentally inheriting whichever root loaded last.
92 for package in &db.packages {
93 let selected: BTreeSet<DefId> = marked
94 .iter()
95 .copied()
96 .filter(|def| {
97 let marker = analysis.hir.defs[*def].attrs.comptime.expect("marked root");
98 package.sources.contains(&marker.file)
99 })
100 .collect();
101 if selected.is_empty() {
102 continue;
103 }
104 let found = evaluate_selected_with(analysis, &selected, &package.lower, cancelled)?;
105 assigned.extend(selected.iter().copied());
106 diagnostics.extend(found);
107 }
108
109 let standalone: BTreeSet<DefId> = marked.difference(&assigned).copied().collect();
110 if !standalone.is_empty() {
111 diagnostics.extend(evaluate_selected_with(
112 analysis,
113 &standalone,
114 &Settings::default(),
115 cancelled,
116 )?);
117 }
118 diagnostics.sort_by_key(|diagnostic| {
119 (
120 diagnostic.primary.span.file,
121 diagnostic.primary.span.range.start(),
122 )
123 });
124 Ok(diagnostics)
125}
126
127fn uncancelled(result: Result<Vec<Diagnostic>, ComptimeDiagnosticsError>) -> Vec<Diagnostic> {
128 match result {
129 Ok(diagnostics) => diagnostics,
130 Err(ComptimeDiagnosticsError::Cancelled) => {
131 // Both compatibility callers supply `&|| false`; the direct
132 // machine cannot produce Cancelled unless that predicate is true.
133 unreachable!("never-cancelled semantic diagnostics were cancelled")
134 }
135 }
136}
137
138fn evaluate_selected_with(
139 analysis: &Analysis,
140 selected: &BTreeSet<DefId>,
141 settings: &Settings,
142 cancelled: &dyn Fn() -> bool,
143) -> Result<Vec<Diagnostic>, ComptimeDiagnosticsError> {
144 let program = louise_core::lower::lower_with(&analysis.hir, &analysis.typeck, settings);
145 match louise_comptime::evaluate_declarations_for_with(
146 &analysis.hir,
147 &analysis.typeck,
148 program,
149 selected,
150 cancelled,
151 ) {
152 Ok(_) => Ok(Vec::new()),
153 Err(louise_comptime::CtfeDriverError::Diagnostics(diagnostics)) => Ok(diagnostics),
154 Err(louise_comptime::CtfeDriverError::Cancelled) => {
155 Err(ComptimeDiagnosticsError::Cancelled)
156 }
157 }
158}
159
160fn marked_roots(analysis: &Analysis) -> BTreeSet<DefId> {
161 analysis
162 .hir
163 .defs
164 .iter()
165 .filter_map(|(def, definition)| definition.attrs.comptime.map(|_| def))
166 .collect()
167}
168
169fn has_comptime(analysis: &Analysis) -> bool {
170 analysis
171 .hir
172 .defs
173 .iter()
174 .any(|(_, definition)| definition.attrs.comptime.is_some())
175}