rils_frontend 0.4.0

Static analysis and semantic frontend for the Rils scripting language
Documentation
//! Ownership of constants and per-instance statics, including their aliases and projections.
use super::*;

impl Checker<'_> {
    pub(super) fn data_place(
        &self,
        expr: &Expr,
    ) -> Option<(&str, &crate::constant_data::DataItem)> {
        let path = match expr {
            Expr::Path { segments, .. } => segments.join("::"),
            Expr::Variable { name, .. }
                if self.lookup(name).is_none_or(|binding| binding.module_data) =>
            {
                name.clone()
            }
            Expr::Member { object, .. } | Expr::Index { object, .. } => {
                return self.data_place(object);
            }
            _ => return None,
        };
        self.declaration_types.data_path(&path, &self.module_path)
    }

    pub(super) fn check_data_move(&mut self, expr: &Expr) -> bool {
        let Some((path, item)) = self.data_place(expr) else {
            return false;
        };
        if matches!(item.kind, crate::ast::BindingKind::Static(_))
            && !self.is_copy(self.expression_types.get(expr).unwrap_or(&item.ty))
        {
            self.diagnostic(
                format!("cannot move a non-Copy value out of static `{path}`"),
                expr.span(),
            );
        }
        true
    }

    pub(super) fn borrow_data(
        &mut self,
        target: &Expr,
        mutable: bool,
        span: Span,
    ) -> Option<ExpressionValue> {
        if let Some((path, item)) = self.data_place(target) {
            let path = path.to_owned();
            if mutable && !item.mutable {
                self.diagnostic(
                    format!("cannot mutably reference immutable data `{path}`"),
                    span,
                );
            }
            let interior = matches!(target, Expr::Index { .. } | Expr::Member { .. });
            let borrow = Borrow {
                root: path,
                interior,
                region: self.scopes.last().unwrap().region,
            };
            self.add_borrow(&borrow);
            return Some(ExpressionValue {
                reference_region: Some(borrow.region),
                borrows: vec![borrow],
            });
        }
        None
    }
    pub(super) fn assign_data(
        &mut self,
        target: &Expr,
        reference_region: Option<RegionId>,
        span: Span,
    ) -> bool {
        if let Some((path, item)) = self.data_place(target) {
            let path = path.to_owned();
            if !item.mutable {
                self.diagnostic(format!("cannot assign to immutable data `{path}`"), span);
            }
            if reference_region.is_some() {
                self.diagnostic("references cannot be stored in static data", span);
            }
            if self
                .active_borrows
                .get(&path)
                .is_some_and(|(_, interior)| *interior > 0)
                && matches!(target, Expr::Path { .. } | Expr::Variable { .. })
            {
                self.diagnostic(
                    format!("cannot replace `{path}` while one of its fields is referenced"),
                    span,
                );
            }
            self.comparison_indices(target);
            return true;
        }
        false
    }
}