aboutsummaryrefslogtreecommitdiff
path: root/src/parse_assoc_type.rs
blob: 048e58c24c3be854fc52d9e7c35fe0735dfe87d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};
use syn::spanned::Spanned;
use syn::{GenericArgument, Ident, PathArguments, PathSegment, TraitItemType, Type};

use crate::match_assoc_type;
use crate::parse_trait_sig::TypeTransform;
use crate::syn_utils::{iter_type, lifetime_bounds, trait_bounds};

#[derive(Debug)]
pub enum AssocTypeParseError {
    AssocTypeInBound,
    GenericAssociatedType,
    NoIntoBound,
}

#[derive(Debug, Clone)]
pub struct BoxType {
    pub inner: TokenStream,
    pub placeholder_lifetime: bool,
}

impl ToTokens for BoxType {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let inner = &self.inner;
        match self.placeholder_lifetime {
            true => tokens.extend(quote! {Box<#inner + '_>}),
            false => tokens.extend(quote! {Box<#inner>}),
        }
    }
}

pub enum DestType<'a> {
    Into(&'a Type),
    Box(BoxType),
}

impl DestType<'_> {
    pub fn get_dest(&self) -> Type {
        match self {
            DestType::Into(ty) => (*ty).clone(),
            DestType::Box(b) => Type::Verbatim(quote!(#b)),
        }
    }

    pub fn type_transformation(&self) -> TypeTransform {
        match self {
            DestType::Into(_) => TypeTransform::Into,
            DestType::Box(b) => TypeTransform::Box(b.clone()),
        }
    }
}

pub fn parse_assoc_type(
    assoc_type: &TraitItemType,
) -> Result<(&Ident, DestType), (Span, AssocTypeParseError)> {
    if let Some(bound) = trait_bounds(&assoc_type.bounds).next() {
        if let PathSegment {
            ident,
            arguments: PathArguments::AngleBracketed(args),
        } = bound.path.segments.first().unwrap()
        {
            if ident == "Into" && args.args.len() == 1 {
                if let GenericArgument::Type(into_type) = args.args.first().unwrap() {
                    // provide a better error message for type A: Into<Self::B>
                    if iter_type(into_type).any(match_assoc_type) {
                        return Err((into_type.span(), AssocTypeParseError::AssocTypeInBound));
                    }

                    // TODO: support lifetime GATs (see the currently failing tests/gats.rs)
                    if !assoc_type.generics.params.is_empty() {
                        return Err((
                            assoc_type.generics.params.span(),
                            AssocTypeParseError::GenericAssociatedType,
                        ));
                    }

                    return Ok((&assoc_type.ident, DestType::Into(into_type)));
                }
            }
        }
        let path = &bound.path;
        return Ok((
            &assoc_type.ident,
            DestType::Box(BoxType {
                inner: quote! {dyn #path},
                placeholder_lifetime: !lifetime_bounds(&assoc_type.bounds)
                    .any(|l| l.ident == "static"),
            }),
        ));
    }
    Err((assoc_type.span(), AssocTypeParseError::NoIntoBound))
}

#[cfg(test)]
mod tests {
    use quote::quote;
    use syn::{TraitItemType, Type};

    use crate::parse_assoc_type::{parse_assoc_type, AssocTypeParseError, DestType};

    #[test]
    fn ok() {
        let type1: TraitItemType = syn::parse2(quote! {
            type A: Into<String>;
        })
        .unwrap();

        assert!(matches!(
            parse_assoc_type(&type1),
            Ok((id, DestType::Into(Type::Path(path))))
            if id == "A" && path.path.is_ident("String")
        ));
    }

    #[test]
    fn err_no_bound() {
        let type1: TraitItemType = syn::parse2(quote! {
            type A;
        })
        .unwrap();

        assert!(matches!(
            parse_assoc_type(&type1),
            Err((_, AssocTypeParseError::NoIntoBound))
        ));
    }

    #[test]
    fn err_assoc_type_in_bound() {
        let type1: TraitItemType = syn::parse2(quote! {
            type A: Into<Self::B>;
        })
        .unwrap();

        assert!(matches!(
            parse_assoc_type(&type1),
            Err((_, AssocTypeParseError::AssocTypeInBound))
        ));
    }

    #[test]
    fn err_gat_type() {
        let type1: TraitItemType = syn::parse2(quote! {
            type A<X>: Into<Foobar<X>>;
        })
        .unwrap();

        assert!(matches!(
            parse_assoc_type(&type1),
            Err((_, AssocTypeParseError::GenericAssociatedType))
        ));
    }

    #[test]
    fn err_gat_lifetime() {
        let type1: TraitItemType = syn::parse2(quote! {
            type A<'a>: Into<Foobar<'a>>;
        })
        .unwrap();

        assert!(matches!(
            parse_assoc_type(&type1),
            Err((_, AssocTypeParseError::GenericAssociatedType))
        ));
    }
}