tobogganSe connecter

louise

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

63 lignes · 2658 octetsBrut · Historique
1//! The `@fip` check (ADR 0040) as diagnostics, shared by `louise check`,
2//! the commands that compile, and the language server (issue #223): it
3//! used to live in the driver, so the editor never showed it.
4//!
5//! The check needs Perceus' reuse decisions, so it lowers to Core and runs
6//! Perceus on a copy, as the native backend would — only when a function
7//! is marked `@fip`, since that is a whole-program pass.
8
9use louise_core::ir::Program;
10use louise_diag::Diagnostic;
11
12use crate::Analysis;
13
14/// The code of an `@fip` function that allocates.
15pub const FIP_ERROR: &str = "E0901";
16
17/// The `@fip` functions of `analysis` that allocate. Nothing to do, and
18/// cheap, unless a function is marked; meant for an analysis without
19/// errors (lowering assumes a well-typed program).
20pub fn fip_diagnostics(analysis: &Analysis) -> Vec<Diagnostic> {
21 if !analysis.hir.defs.iter().any(|(_, d)| d.attrs.fip) {
22 return Vec::new();
23 }
24 let program = louise_core::lower::lower(&analysis.hir, &analysis.typeck);
25 fip_diagnostics_of(&program)
26}
27
28/// [`fip_diagnostics`] of a program already lowered.
29pub fn fip_diagnostics_of(program: &Program) -> Vec<Diagnostic> {
30 if !program.fns.iter().any(|f| f.fip) {
31 return Vec::new();
32 }
33 let mut program = program.clone();
34 louise_core::evidence::run(&mut program);
35 // No `opt` here: the check's diagnostics point at the program as
36 // written, and the optimiser can only remove allocations, so the
37 // compiled artifact allocates no more than what passed. TRMC runs so
38 // the check sees the same shapes the native backend does.
39 louise_core::trmc::run(&mut program);
40 louise_perceus::insert_rc(&mut program);
41 louise_perceus::fip::check(&program)
42 .into_iter()
43 // A violation has a span unless its function has none, which only
44 // generated code lacks — and generated code is never `@fip`.
45 .filter_map(|v| {
46 // `module.name`: the module is in the diagnostic's path.
47 let name = &program.fns[v.function as usize].name;
48 let name = name.rsplit_once('.').map_or(name.as_str(), |(_, n)| n);
49 Some(
50 Diagnostic::error(
51 FIP_ERROR,
52 format!("`{name}` is `@fip` but allocates"),
53 v.span?,
54 )
55 .with_primary_label(&v.what)
56 .with_note(
57 "an `@fip` function builds its cells in those of the values \
58 it matched on, and calls only functions that allocate nothing",
59 ),
60 )
61 })
62 .collect()
63}