Skip to main content

qtbridge_gen/
meta_call_check.rs

1use syn::spanned::Spanned;
2
3use crate::utils::signature_utils::{get_return_type, get_typed_args, is_arg_self_ref};
4use crate::utils::type_to_string::type_to_string_fallback;
5use crate::utils::type_utils::{is_mut_ref, remove_ref};
6
7/// Checks whether the given signature can participate in meta-calls
8/// (as slot callbacks or property getters/setters).
9///
10/// Validates structural constraints only. Whether a concrete `QMetaTypeCompatible`
11/// impl exists for each type is deferred to the Rust compiler.
12pub fn check_meta_call_signature(src: &syn::Signature) -> syn::Result<()> {
13    if !src.inputs.first().is_some_and(|arg| is_arg_self_ref(arg, None)) {
14        return Err(syn::Error::new(src.ident.span(), "First argument must be &self"));
15    }
16
17    get_typed_args(src)
18        .try_for_each(|arg| {
19            check_meta_call_type(arg.ty.as_ref())
20                .map_err(|err| syn::Error::new(err.span(), format!("The function argument is not compatible with meta call.\n{err}")))
21        })?;
22    get_return_type(&src.output)
23        .map(|ty| {
24            check_meta_call_type(ty)
25                .map_err(|err| syn::Error::new(err.span(), format!("The function return type is not compatible with meta call.\n{err}")))
26        })
27        .unwrap_or(Ok(()))
28}
29
30/// Checks whether the given type can appear as an argument or return type in a meta-call.
31pub fn check_meta_call_type(ty: &syn::Type) -> syn::Result<()> {
32    let ty_wo_ref = remove_ref(ty);
33    match ty_wo_ref {
34        syn::Type::Path(_) => Ok(()),
35        syn::Type::Array(array) =>
36            Err(syn::Error::new(array.span(), "Arrays are currently not supported")),
37        syn::Type::Ptr(ptr) =>
38            Err(syn::Error::new(ptr.span(), "Pointers are not supported")),
39        syn::Type::Reference(type_ref) =>
40            Err(syn::Error::new(type_ref.span(), "References to reference are not supported")),
41        syn::Type::Slice(slice) =>
42            Err(syn::Error::new(slice.span(), "Slices are currently not supported")),
43        syn::Type::Tuple(tuple) =>
44            Err(syn::Error::new(tuple.span(), "Tuples are not supported")),
45        _ => Err(syn::Error::new(ty_wo_ref.span(), format!("Type category ('{:?}') of type '{}' is not supported", std::mem::discriminant(ty_wo_ref), type_to_string_fallback(ty_wo_ref))))
46    }?;
47
48    if is_mut_ref(ty) {
49        return Err(syn::Error::new(ty.span(), format!("Mutable references are not supported. Found: '{}'", type_to_string_fallback(ty))))
50    }
51
52    Ok(())
53}