//! `@comptime` compilation shared by the CLI, testkit and language server. //! //! Like `@fip`, the source-only analyzers lower a temporary Program only when //! a marker is present. Compilation itself supplies its already-lowered Core //! to [`evaluate_comptime`] after the existing `@fip` diagnostics. use std::collections::BTreeSet; use louise_core::ir::Program; use louise_core::lower::Settings; use louise_diag::Diagnostic; use louise_hir::DefId; use crate::{Analysis, Db}; /// A structured source-analysis cancellation outcome. Ordinary source errors /// remain `Ok(Vec)`; `Cancelled` deliberately carries no partial /// diagnostic packet for a caller to publish. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ComptimeDiagnosticsError { Cancelled, } /// Apply the one transactional CTFE transform used by every Core consumer. pub fn evaluate_comptime( analysis: &Analysis, program: Program, ) -> Result> { if !has_comptime(analysis) { return Ok(program); } louise_comptime::evaluate_declarations(&analysis.hir, &analysis.typeck, program) } /// `@fip` first, then CTFE, for a check/LSP analysis with no earlier errors. /// Keeping this order prevents a CTFE rewrite from hiding an existing `@fip` /// violation and matches the command pipeline. `db` supplies the root package /// settings retained while its sources were loaded. This compatibility entry /// never cancels, so current check and LSP scheduling remain unchanged. pub fn semantic_diagnostics(db: &Db, analysis: &Analysis) -> Vec { uncancelled(semantic_diagnostics_with(db, analysis, &|| false)) } /// [`semantic_diagnostics`] with cooperative CTFE machine cancellation. /// Existing `@fip` errors still short-circuit before CTFE, irrespective of the /// predicate. A later cancelled packet discards diagnostics from earlier /// packets by returning `Err(Cancelled)`: a future scheduler must drop that /// whole candidate rather than publish a partial analysis. Source parsing, /// lowering, FIP analysis and CTFE preparation are not interruptible here. pub fn semantic_diagnostics_with( db: &Db, analysis: &Analysis, cancelled: &dyn Fn() -> bool, ) -> Result, ComptimeDiagnosticsError> { let fip = crate::fip_diagnostics(analysis); if !fip.is_empty() { return Ok(fip); } comptime_diagnostics_with(db, analysis, cancelled) } /// CTFE diagnostics without retaining a transformed program. This /// compatibility entry never cancels. Fresh evaluator state is dropped inside /// the driver after every success or failure. Each root-package resolution is /// checked with its own immutable settings, just as compiling that root would /// be; source outside packages keeps the default settings. No setting is read /// from the host environment here. pub fn comptime_diagnostics(db: &Db, analysis: &Analysis) -> Vec { uncancelled(comptime_diagnostics_with(db, analysis, &|| false)) } /// [`comptime_diagnostics`] with cooperative direct-machine cancellation. /// Returning `Err(Cancelled)` publishes neither partial diagnostics nor any /// transformed Program. The borrowed `Db` and its package/source membership /// are not mutated. No new diagnostic represents cancellation. pub fn comptime_diagnostics_with( db: &Db, analysis: &Analysis, cancelled: &dyn Fn() -> bool, ) -> Result, ComptimeDiagnosticsError> { if !has_comptime(analysis) { return Ok(Vec::new()); } let marked = marked_roots(analysis); let mut assigned = BTreeSet::new(); let mut diagnostics = Vec::new(); // Dependencies are evaluated with the root's policy too, matching a real // compilation whose single lowered Program has that root's intrinsic body. // A shared dependency can therefore be checked once for each root that // owns it, rather than accidentally inheriting whichever root loaded last. for package in &db.packages { let selected: BTreeSet = marked .iter() .copied() .filter(|def| { let marker = analysis.hir.defs[*def].attrs.comptime.expect("marked root"); package.sources.contains(&marker.file) }) .collect(); if selected.is_empty() { continue; } let found = evaluate_selected_with(analysis, &selected, &package.lower, cancelled)?; assigned.extend(selected.iter().copied()); diagnostics.extend(found); } let standalone: BTreeSet = marked.difference(&assigned).copied().collect(); if !standalone.is_empty() { diagnostics.extend(evaluate_selected_with( analysis, &standalone, &Settings::default(), cancelled, )?); } diagnostics.sort_by_key(|diagnostic| { ( diagnostic.primary.span.file, diagnostic.primary.span.range.start(), ) }); Ok(diagnostics) } fn uncancelled(result: Result, ComptimeDiagnosticsError>) -> Vec { match result { Ok(diagnostics) => diagnostics, Err(ComptimeDiagnosticsError::Cancelled) => { // Both compatibility callers supply `&|| false`; the direct // machine cannot produce Cancelled unless that predicate is true. unreachable!("never-cancelled semantic diagnostics were cancelled") } } } fn evaluate_selected_with( analysis: &Analysis, selected: &BTreeSet, settings: &Settings, cancelled: &dyn Fn() -> bool, ) -> Result, ComptimeDiagnosticsError> { let program = louise_core::lower::lower_with(&analysis.hir, &analysis.typeck, settings); match louise_comptime::evaluate_declarations_for_with( &analysis.hir, &analysis.typeck, program, selected, cancelled, ) { Ok(_) => Ok(Vec::new()), Err(louise_comptime::CtfeDriverError::Diagnostics(diagnostics)) => Ok(diagnostics), Err(louise_comptime::CtfeDriverError::Cancelled) => { Err(ComptimeDiagnosticsError::Cancelled) } } } fn marked_roots(analysis: &Analysis) -> BTreeSet { analysis .hir .defs .iter() .filter_map(|(def, definition)| definition.attrs.comptime.map(|_| def)) .collect() } fn has_comptime(analysis: &Analysis) -> bool { analysis .hir .defs .iter() .any(|(_, definition)| definition.attrs.comptime.is_some()) }