rils_frontend 0.4.0

Static analysis and semantic frontend for the Rils scripting language
Documentation
//! A source template must supply the same contract as a real trait implementation.
use std::collections::HashSet;

use super::*;

pub(super) fn validate(implementation: &BuiltinImpl) -> Result<(), String> {
    let Type::Named { name, .. } = &implementation.trait_type else {
        return Err("implementation requires a named trait".into());
    };
    let declaration = crate::standard_library::builtin_trait_name(name)
        .and_then(rils_builtins::builtin)
        .ok_or_else(|| format!("unknown exported trait `{name}`"))?;
    let source = declaration
        .source
        .ok_or_else(|| format!("trait `{name}` has no declaration source"))?;
    let tokens = crate::lex(source).map_err(|error| error.message)?;
    let program =
        rils_syntax::parser::parse_builtin_declarations(tokens).map_err(|error| error.message)?;
    let Some(Stmt::Trait {
        methods,
        associated_types,
        ..
    }) = program
        .statements
        .iter()
        .find(|statement| matches!(statement, Stmt::Trait { .. }))
    else {
        return Err(format!("trait `{name}` has no trait declaration"));
    };
    let mut supplied_methods = HashSet::new();
    for method in &implementation.methods {
        if super::receiver_mode(method) != Some(rils_builtins::ReceiverMode::Owned) {
            return Err(format!(
                "source implementation method `{}` requires an owned receiver",
                method.name
            ));
        }
        if !supplied_methods.insert(method.name.as_str()) {
            return Err(format!("duplicate method `{}`", method.name));
        }
        if !methods.iter().any(|declared| declared.name == method.name) {
            return Err(format!("unknown method `{}`", method.name));
        }
    }
    for method in methods {
        if method.body.is_none() && !supplied_methods.contains(method.name.as_str()) {
            return Err(format!("missing required method `{}`", method.name));
        }
    }
    let mut supplied_types = HashSet::new();
    for associated in &implementation.associated_types {
        if !supplied_types.insert(associated.name.as_str()) {
            return Err(format!("duplicate associated type `{}`", associated.name));
        }
        if !associated_types
            .iter()
            .any(|declared| declared.name == associated.name)
        {
            return Err(format!("unknown associated type `{}`", associated.name));
        }
        if associated.value.is_none() {
            return Err(format!(
                "missing associated type value `{}`",
                associated.name
            ));
        }
    }
    for associated in associated_types {
        if associated.value.is_none() && !supplied_types.contains(associated.name.as_str()) {
            return Err(format!(
                "missing required associated type `{}`",
                associated.name
            ));
        }
    }
    Ok(())
}

#[cfg(test)]
#[path = "../../tests/unit/builtin_impl_validation.rs"]
mod tests;