harn-cli 0.10.116

CLI for the Harn programming language — run, test, REPL, format, and lint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! `harn fix` repair: give every declared parameter a type.
//!
//! `HARN-TYP-028` says a parameter has no type. This pass says what the type
//! is. It reads the whole module graph once, infers each parameter from the
//! body that uses it and the call sites that feed it, and then re-checks the
//! file it is about to rewrite. An inferred type that makes the file stop
//! checking is thrown away and the parameter falls back to `unknown`, so the
//! migration can never trade a missing annotation for a broken build.
//!
//! `unknown` is a real outcome, not a failure mode to hide. The pass counts every
//! parameter it gave up on and why, and reports the count with the plan.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use harn_lexer::{FixEdit, Span};
use harn_parser::analysis::{AnalysisDatabase, SourceId, SourceVersion};
use harn_parser::param_annotations::{self, UnannotatedParam};
use harn_parser::{DiagnosticCode as Code, Repair, RepairId, RepairSafety, SNode};

use super::{RepairCandidate, RepairImpactWire};
use crate::commands;
use crate::package;

#[path = "parameter_annotations/inference.rs"]
mod inference;

use inference::{Cause, Inference, ModuleFacts, SettledTypes};

/// How the migration went, in numbers a human can act on.
#[derive(Debug, Default, Clone)]
pub(super) struct AnnotationResidue {
    /// Parameters that got a type the program proved.
    pub(super) inferred: usize,
    /// Parameters left as validated `unknown` boundaries for a human to review.
    pub(super) unresolved: usize,
    /// Count per reason, across both outcomes.
    pub(super) causes: BTreeMap<&'static str, usize>,
}

impl AnnotationResidue {
    fn record(&mut self, inference: &Inference) {
        if inference.rendered != "unknown" && inference.rendered != "any" {
            self.inferred += 1;
        } else {
            self.unresolved += 1;
        }
        *self.causes.entry(inference.cause.as_str()).or_default() += 1;
    }

    pub(super) fn total(&self) -> usize {
        self.inferred + self.unresolved
    }

    /// Share of parameters the pass could not prove anything about.
    pub(super) fn unresolved_share(&self) -> f64 {
        let total = self.total();
        if total == 0 {
            return 0.0;
        }
        #[expect(
            clippy::cast_precision_loss,
            reason = "counts are parameter tallies, far below f64's exact integer range"
        )]
        {
            self.unresolved as f64 / total as f64
        }
    }
}

struct Module {
    path: PathBuf,
    source: String,
    program: Vec<SNode>,
    sites: Vec<UnannotatedParam>,
}

/// Plan an annotation for every parameter `HARN-TYP-028` reports.
pub(super) fn plan(
    files: &[PathBuf],
    module_graph: &harn_modules::ModuleGraph,
    analysis: &mut AnalysisDatabase,
) -> (Vec<RepairCandidate>, AnnotationResidue) {
    let mut modules: Vec<Module> = Vec::new();
    for file in files {
        let config = check_config(file);
        let output = commands::check::analyze_file(analysis, file, &config, module_graph);
        analysis.remove_source(&SourceId::path(file));
        let Ok(output) = output else {
            continue;
        };
        modules.push(Module {
            path: file.clone(),
            sites: param_annotations::unannotated_params(&output.program),
            source: output.source,
            program: output.program,
        });
    }

    let inferences = converge(&modules, module_graph);

    let mut candidates = Vec::new();
    let mut residue = AnnotationResidue::default();
    for (module, mut chosen) in modules.iter().zip(inferences) {
        if module.sites.is_empty() {
            continue;
        }
        settle(module, module_graph, analysis, &mut chosen);
        for (site, inference) in module.sites.iter().zip(&chosen) {
            residue.record(inference);
            let Some(edit) = annotation_edit(&module.source, site, &inference.rendered) else {
                continue;
            };
            candidates.push(candidate(
                &module.path,
                site,
                &inference.rendered,
                inference.cause,
                edit,
            ));
        }
    }
    (candidates, residue)
}

/// Infer every parameter, then infer again with the answers in hand.
///
/// One pass can only use the types already written in the source, so a helper
/// whose caller is itself unannotated stays unknown. Feeding each round's
/// inferences back as declared types lets the answer propagate along the call
/// graph until it stops changing.
fn converge(modules: &[Module], module_graph: &harn_modules::ModuleGraph) -> Vec<Vec<Inference>> {
    const MAX_ROUNDS: usize = 6;
    let resolutions = module_resolutions(modules, module_graph);
    let mut settled = SettledTypes::new();
    let mut chosen: Vec<Vec<Inference>> = Vec::new();
    for _ in 0..MAX_ROUNDS {
        let mut facts = ModuleFacts::default();
        for (index, module) in modules.iter().enumerate() {
            facts.merge(inference::collect(
                index,
                &module.source,
                &module.program,
                &settled,
                &resolutions[index],
            ));
        }
        chosen = modules
            .iter()
            .enumerate()
            .map(|(index, module)| {
                module
                    .sites
                    .iter()
                    .map(|site| inference::infer(index, site, &facts))
                    .collect()
            })
            .collect();
        let next = settled_types(modules, &chosen);
        if next == settled {
            break;
        }
        settled = next;
    }
    chosen
}

fn settled_types(modules: &[Module], chosen: &[Vec<Inference>]) -> SettledTypes {
    let mut settled = SettledTypes::new();
    for (module_index, (module, inferences)) in modules.iter().zip(chosen).enumerate() {
        for (site, inference) in module.sites.iter().zip(inferences) {
            if inference.cause.is_inferred() && inference.rendered != "unknown" {
                settled.insert(
                    (module_index, site.owner.clone(), site.index),
                    inference.rendered.clone(),
                );
            }
        }
    }
    settled
}

fn module_resolutions(
    modules: &[Module],
    module_graph: &harn_modules::ModuleGraph,
) -> Vec<inference::ModuleResolution> {
    let mut indexes = std::collections::HashMap::new();
    for (index, module) in modules.iter().enumerate() {
        for key in module_path_keys(&module.path) {
            indexes.insert(key, index);
        }
    }

    modules
        .iter()
        .enumerate()
        .map(|(index, module)| {
            let mut resolution = inference::ModuleResolution::default();
            let mut local = std::collections::HashSet::new();
            harn_parser::visit::walk_program(&module.program, &mut |node| {
                let name = match &node.node {
                    harn_parser::Node::FnDecl { name, .. }
                    | harn_parser::Node::Pipeline { name, .. }
                    | harn_parser::Node::ToolDecl { name, .. } => Some(name),
                    _ => None,
                };
                if let Some(name) = name {
                    local.insert(name.clone());
                    resolution.callables.insert(name.clone(), index);
                }
            });

            let mut ambiguous = std::collections::HashSet::new();
            for import in module_graph.imports_for_module(&module.path) {
                let Some(path) = import.resolved_path.as_ref() else {
                    continue;
                };
                let Some(target) = module_path_keys(path)
                    .into_iter()
                    .find_map(|key| indexes.get(&key).copied())
                else {
                    continue;
                };
                if let Some(alias) = import.namespace_alias {
                    resolution.namespaces.insert(alias, target);
                    continue;
                }
                let names = import
                    .selective_names
                    .unwrap_or_else(|| module_graph.exports_for_module(path));
                for name in names {
                    if local.contains(&name) {
                        continue;
                    }
                    match resolution.callables.insert(name.clone(), target) {
                        Some(previous) if previous != target => {
                            ambiguous.insert(name);
                        }
                        _ => {}
                    }
                }
            }
            for name in ambiguous {
                resolution.callables.remove(&name);
            }
            resolution
        })
        .collect()
}

fn module_path_keys(path: &Path) -> Vec<PathBuf> {
    let mut keys = vec![std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf())];
    let marker = "crates/harn-stdlib/src/stdlib/";
    if let Some((_, relative)) = path.to_string_lossy().split_once(marker) {
        keys.push(Path::new("<std>").join(relative).with_extension(""));
    }
    keys
}

fn check_config(file: &Path) -> package::CheckConfig {
    let mut config = package::load_check_config(Some(file));
    commands::check::apply_harn_lint_config(file, &mut config);
    config
}

/// Keep the largest deterministic subset of inferred annotations that checks.
///
/// Every site outside the subset is written as explicit `unknown`, preserving
/// a dynamic input boundary while requiring safe narrowing before use. A clean
/// chunk can be accepted at once. A failing chunk is split until the one
/// annotation that introduces a new error is isolated. Accepted chunks stay
/// enabled while the next chunk is tested, so the final set is proven together
/// rather than only one annotation at a time.
fn settle(
    module: &Module,
    module_graph: &harn_modules::ModuleGraph,
    analysis: &mut AnalysisDatabase,
    chosen: &mut [Inference],
) {
    let sites = module.sites.as_slice();
    let config = check_config(&module.path);
    let baseline = check_diagnostics(module, &module.source, module_graph, analysis, &config, 0);
    let inferred = chosen
        .iter()
        .enumerate()
        .filter_map(|(index, inference)| inference.cause.is_inferred().then_some(index))
        .collect::<Vec<_>>();
    if inferred.is_empty() {
        return;
    }
    let mut accepted = std::collections::BTreeSet::new();
    let mut pending = vec![inferred];
    let mut round = 0usize;
    while let Some(chunk) = pending.pop() {
        round += 1;
        let enabled = accepted
            .iter()
            .copied()
            .chain(chunk.iter().copied())
            .collect();
        let Some(candidate_source) = rewrite_selected(&module.source, sites, chosen, &enabled)
        else {
            break;
        };
        let after = check_diagnostics(
            module,
            &candidate_source,
            module_graph,
            analysis,
            &config,
            round,
        );
        if !has_new_diagnostics(&baseline, &after) {
            accepted.extend(chunk);
        } else if chunk.len() > 1 {
            let middle = chunk.len() / 2;
            pending.push(chunk[middle..].to_vec());
            pending.push(chunk[..middle].to_vec());
        }
    }
    for (index, inference) in chosen.iter_mut().enumerate() {
        if inference.cause.is_inferred() && !accepted.contains(&index) {
            *inference = Inference::rejected();
        }
    }
}

/// Diagnostic counts keyed by code and message, so inserted bytes do not make
/// an existing diagnostic look new.
type DiagnosticIndex = BTreeMap<(Code, String), usize>;

fn check_diagnostics(
    module: &Module,
    source: &str,
    module_graph: &harn_modules::ModuleGraph,
    analysis: &mut AnalysisDatabase,
    config: &package::CheckConfig,
    round: usize,
) -> DiagnosticIndex {
    let id = SourceId::new(format!(
        "{}#annotate-{round}",
        module.path.to_string_lossy()
    ));
    analysis.set_source(id.clone(), source.to_string(), SourceVersion(1));
    let typecheck_config = commands::check::typecheck_config(&module.path, config, module_graph);
    let output = analysis.typecheck(&id, typecheck_config);
    analysis.remove_source(&id);
    let Ok(output) = output else {
        return DiagnosticIndex::new();
    };
    let mut index = DiagnosticIndex::new();
    for diagnostic in output.diagnostics {
        // Only errors can turn a working file into a broken one. A warning the
        // annotation newly reveals (an untyped dict read, say) is the checker
        // seeing more than it used to, which is the point of the migration.
        if diagnostic.code == Code::ImplicitAnyParameter
            || diagnostic.severity != harn_parser::DiagnosticSeverity::Error
        {
            continue;
        }
        *index
            .entry((diagnostic.code, diagnostic.message))
            .or_default() += 1;
    }
    index
}

fn has_new_diagnostics(baseline: &DiagnosticIndex, after: &DiagnosticIndex) -> bool {
    after
        .iter()
        .any(|(key, count)| *count > baseline.get(key).copied().unwrap_or_default())
}

/// Splice every annotation into `source`, using `unknown` outside `enabled`.
fn rewrite_selected(
    source: &str,
    sites: &[UnannotatedParam],
    chosen: &[Inference],
    enabled: &std::collections::BTreeSet<usize>,
) -> Option<String> {
    let mut insertions: Vec<(usize, String)> = Vec::with_capacity(sites.len());
    for (index, (site, inference)) in sites.iter().zip(chosen).enumerate() {
        let offset = param_annotations::annotation_insert_offset(source, site)?;
        let rendered = if enabled.contains(&index) {
            inference.rendered.as_str()
        } else {
            "unknown"
        };
        insertions.push((offset, format!(": {rendered}")));
    }
    insertions.sort_by_key(|(offset, _)| *offset);
    let mut out = String::with_capacity(source.len() + insertions.len() * 8);
    let mut cursor = 0usize;
    for (offset, text) in &insertions {
        out.push_str(source.get(cursor..*offset)?);
        out.push_str(text);
        cursor = *offset;
    }
    out.push_str(source.get(cursor..)?);
    Some(out)
}

fn annotation_edit(source: &str, site: &UnannotatedParam, annotation: &str) -> Option<FixEdit> {
    let offset = param_annotations::annotation_insert_offset(source, site)?;
    Some(FixEdit {
        span: Span::with_offsets(offset, offset, site.span.line, site.span.column),
        replacement: format!(": {annotation}"),
    })
}

fn candidate(
    file: &Path,
    site: &UnannotatedParam,
    annotation: &str,
    cause: Cause,
    edit: FixEdit,
) -> RepairCandidate {
    RepairCandidate {
        file: file.to_string_lossy().into_owned(),
        source: "typecheck",
        severity: "error",
        code: Code::ImplicitAnyParameter,
        message: format!(
            "{} `{}` parameter `{}` has no type annotation",
            site.kind.as_str(),
            site.owner,
            site.name
        ),
        unresolved_name: None,
        expected_type: None,
        span: Some(site.span),
        repair: Repair {
            id: RepairId::from_static("types/annotate-parameter"),
            summary: format!("Annotate `{}` as `{annotation}`", site.name),
            safety: RepairSafety::SurfaceChanging,
        },
        impact: RepairImpactWire {
            classification: "parameter-annotation".to_string(),
            strategy: Some("infer-from-body-and-call-sites".to_string()),
            signature_changes: Vec::new(),
            requires_cross_module_caller_updates: false,
            notes: vec![format!("inference: {}", cause.as_str())],
        },
        edits: vec![edit],
    }
}

impl From<&AnnotationResidue> for super::ParameterAnnotationsWire {
    fn from(residue: &AnnotationResidue) -> Self {
        super::ParameterAnnotationsWire {
            total: residue.total(),
            inferred: residue.inferred,
            unresolved: residue.unresolved,
            unresolved_share: (residue.unresolved_share() * 10_000.0).round() / 10_000.0,
            causes: residue
                .causes
                .iter()
                .map(|(cause, count)| ((*cause).to_string(), *count))
                .collect(),
        }
    }
}