knave 0.14.16

A Lisp programming language for humans and agents that infers types and ownership and compiles to readable Rust
Documentation
use rustc_hash::FxHashMap;

use super::ty::{Type, TypeVar};
use super::typed::{Borrow, TypedExpr, TypedExprKind};
use crate::source::Span;

pub struct PendingBorrow {
    pub var: TypeVar,
    pub argument: Type,
    pub span: Span,
}

pub struct DecidedBorrows {
    decided: FxHashMap<usize, Borrow>,
}

impl DecidedBorrows {
    pub fn new(decided: FxHashMap<usize, Borrow>) -> Self {
        Self { decided }
    }

    pub fn patch(&self, expr: &mut TypedExpr) {
        let arguments = match &mut expr.kind {
            TypedExprKind::Call(_, arguments) | TypedExprKind::Recur(arguments) => Some(arguments),
            _ => None,
        };
        if let Some(arguments) = arguments {
            for argument in arguments.iter_mut() {
                if let Borrow::Pending(id) = argument.borrow {
                    argument.borrow = self.decided.get(&id).copied().unwrap_or(Borrow::None);
                }
            }
        }
        for child in expr.children_mut() {
            self.patch(child);
        }
    }
}