rils_frontend 0.4.0

Static analysis and semantic frontend for the Rils scripting language
Documentation
use crate::Type;

/// Whether this declaration exposes a trait implementation or trait-owned members.
/// Conditional bounds are checked separately by `builtin_implements_with`.
pub(crate) fn builtin_declares_trait(
    declaration: &rils_builtins::BuiltinDeclaration,
    trait_name: &str,
) -> bool {
    declaration
        .trait_impls
        .iter()
        .any(|implementation| implementation.trait_name == trait_name)
        || declaration
            .members
            .iter()
            .any(|member| member.trait_name == Some(trait_name))
}

/// Checks an exact native type's declared trait and resolves its conditional
/// requirements to actual type arguments. Structural receiver mappings use
/// their own declarations; references never inherit an owned receiver's traits.
pub fn builtin_implements_with(
    object: &Type,
    trait_name: &str,
    mut satisfies: impl FnMut(&Type, &str) -> bool,
) -> bool {
    let Some((declaration, arguments)) = super::builtin_type_arguments(object) else {
        return false;
    };
    if declaration.type_parameters.len() != arguments.len() {
        return false;
    }
    if !super::builtin_instance_supported(declaration, object) {
        return false;
    }
    rils_builtins::native_implements_with(declaration.path, trait_name, |parameter, bound| {
        declaration
            .type_parameters
            .iter()
            .position(|name| *name == parameter)
            .is_some_and(|index| satisfies(arguments[index], bound))
    })
}