yulang-runtime-refine 0.1.0

Runtime type refinement, validation, invariant checks, and hygiene printing for Yulang.
Documentation
use super::*;

impl RefineRewriter {
    pub(super) fn cast_if_needed(&self, expr: Expr, expected: Option<&RuntimeType>) -> Expr {
        let Some(expected) = expected else {
            return expr;
        };
        let expected = substitute_hir_type(expected, &self.substitutions);
        let (expected_core, actual_core) = match (&expected, &expr.ty) {
            (RuntimeType::Value(expected_core), RuntimeType::Value(actual_core)) => {
                (expected_core.clone(), actual_core.clone())
            }
            _ => return expr,
        };
        if expected_core == actual_core || type_compatible(&expected_core, &actual_core) {
            return expr;
        }
        if !needs_runtime_coercion(&expected_core, &actual_core) {
            return expr;
        }
        Expr {
            ty: expected,
            kind: ExprKind::Coerce {
                from: actual_core,
                to: expected_core,
                expr: Box::new(expr),
            },
        }
    }
}