//! The `@fip` check (ADR 0040) as diagnostics, shared by `louise check`, //! the commands that compile, and the language server (issue #223): it //! used to live in the driver, so the editor never showed it. //! //! The check needs Perceus' reuse decisions, so it lowers to Core and runs //! Perceus on a copy, as the native backend would — only when a function //! is marked `@fip`, since that is a whole-program pass. use louise_core::ir::Program; use louise_diag::Diagnostic; use crate::Analysis; /// The code of an `@fip` function that allocates. pub const FIP_ERROR: &str = "E0901"; /// The `@fip` functions of `analysis` that allocate. Nothing to do, and /// cheap, unless a function is marked; meant for an analysis without /// errors (lowering assumes a well-typed program). pub fn fip_diagnostics(analysis: &Analysis) -> Vec { if !analysis.hir.defs.iter().any(|(_, d)| d.attrs.fip) { return Vec::new(); } let program = louise_core::lower::lower(&analysis.hir, &analysis.typeck); fip_diagnostics_of(&program) } /// [`fip_diagnostics`] of a program already lowered. pub fn fip_diagnostics_of(program: &Program) -> Vec { if !program.fns.iter().any(|f| f.fip) { return Vec::new(); } let mut program = program.clone(); louise_core::evidence::run(&mut program); // No `opt` here: the check's diagnostics point at the program as // written, and the optimiser can only remove allocations, so the // compiled artifact allocates no more than what passed. TRMC runs so // the check sees the same shapes the native backend does. louise_core::trmc::run(&mut program); louise_perceus::insert_rc(&mut program); louise_perceus::fip::check(&program) .into_iter() // A violation has a span unless its function has none, which only // generated code lacks — and generated code is never `@fip`. .filter_map(|v| { // `module.name`: the module is in the diagnostic's path. let name = &program.fns[v.function as usize].name; let name = name.rsplit_once('.').map_or(name.as_str(), |(_, n)| n); Some( Diagnostic::error( FIP_ERROR, format!("`{name}` is `@fip` but allocates"), v.span?, ) .with_primary_label(&v.what) .with_note( "an `@fip` function builds its cells in those of the values \ it matched on, and calls only functions that allocate nothing", ), ) }) .collect() }