parser/grammar/
generic_args.rs

1use super::*;
2
3// test_err generic_arg_list_recover_expr
4// const _: () = T::<0, ,T>;
5// const _: () = T::<0, ,T>();
6pub(super) fn opt_generic_arg_list_expr(p: &mut Parser<'_>) {
7    let m;
8    if p.at(T![::]) && p.nth(2) == T![<] {
9        m = p.start();
10        p.bump(T![::]);
11    } else {
12        return;
13    }
14
15    delimited(
16        p,
17        T![<],
18        T![>],
19        T![,],
20        || "expected generic argument".into(),
21        GENERIC_ARG_FIRST,
22        generic_arg,
23    );
24    m.complete(p, GENERIC_ARG_LIST);
25}
26
27pub(crate) const GENERIC_ARG_FIRST: TokenSet = TokenSet::new(&[
28    LIFETIME_IDENT,
29    IDENT,
30    T!['{'],
31    T![true],
32    T![false],
33    T![-],
34    INT_NUMBER,
35    FLOAT_NUMBER,
36    CHAR,
37    BYTE,
38    STRING,
39    BYTE_STRING,
40    C_STRING,
41])
42.union(types::TYPE_FIRST);
43
44// Despite its name, it can also be used for generic param list.
45const GENERIC_ARG_RECOVERY_SET: TokenSet = TokenSet::new(&[T![>], T![,]]);
46
47// test generic_arg
48// type T = S<i32, dyn T, fn()>;
49pub(crate) fn generic_arg(p: &mut Parser<'_>) -> bool {
50    match p.current() {
51        LIFETIME_IDENT if !p.nth_at(1, T![+]) => lifetime_arg(p),
52        T!['{'] | T![true] | T![false] | T![-] => const_arg(p),
53        k if k.is_literal() => const_arg(p),
54        // test generic_arg_bounds
55        // type Plain = Foo<Item, Item::Item, Item: Bound, Item = Item>;
56        // type GenericArgs = Foo<Item<T>, Item::<T>, Item<T>: Bound, Item::<T>: Bound, Item<T> = Item, Item::<T> = Item>;
57        // type ParenthesizedArgs = Foo<Item(T), Item::(T), Item(T): Bound, Item::(T): Bound, Item(T) = Item, Item::(T) = Item>;
58        // type RTN = Foo<Item(..), Item(..), Item(..): Bound, Item(..): Bound, Item(..) = Item, Item(..) = Item>;
59
60        // test edition_2015_dyn_prefix_inside_generic_arg 2015
61        // type A = Foo<dyn T>;
62        T![ident] if !p.current_edition().at_least_2018() && types::is_dyn_weak(p) => type_arg(p),
63        // test macro_inside_generic_arg
64        // type A = Foo<syn::Token![_]>;
65        k if PATH_NAME_REF_KINDS.contains(k) => {
66            let m = p.start();
67            name_ref_mod_path(p);
68            paths::opt_path_type_args(p);
69            match p.current() {
70                T![=] => {
71                    p.bump_any();
72                    if types::TYPE_FIRST.contains(p.current()) {
73                        // test assoc_type_eq
74                        // type T = StreamingIterator<Item<'a> = &'a T>;
75                        types::type_(p);
76                    } else if p.at_ts(GENERIC_ARG_RECOVERY_SET) {
77                        // Although `const_arg()` recovers as expected, we want to
78                        // handle those here to give the following message because
79                        // we don't know whether this associated item is a type or
80                        // const at this point.
81
82                        // test_err recover_from_missing_assoc_item_binding
83                        // fn f() -> impl Iterator<Item = , Item = > {}
84                        p.error("missing associated item binding");
85                    } else {
86                        // test assoc_const_eq
87                        // fn foo<F: Foo<N=3>>() {}
88                        // const TEST: usize = 3;
89                        // fn bar<F: Foo<N={TEST}>>() {}
90                        const_arg(p);
91                    }
92                    m.complete(p, ASSOC_TYPE_ARG);
93                }
94                // test assoc_type_bound
95                // type T = StreamingIterator<Item<'a>: Clone>;
96                // type T = StreamingIterator<Item(T): Clone>;
97                T![:] if !p.at(T![::]) => {
98                    generic_params::bounds(p);
99                    m.complete(p, ASSOC_TYPE_ARG);
100                }
101                // Turned out to be just a normal path type (mirror `path_or_macro_type`)
102                _ => {
103                    let m = m.complete(p, PATH_SEGMENT).precede(p).complete(p, PATH);
104                    let m = paths::type_path_for_qualifier(p, m);
105                    let m = if p.at(T![!]) && !p.at(T![!=]) {
106                        let m = m.precede(p);
107                        items::macro_call_after_excl(p);
108                        m.complete(p, MACRO_CALL).precede(p).complete(p, MACRO_TYPE)
109                    } else {
110                        m.precede(p).complete(p, PATH_TYPE)
111                    };
112                    types::opt_type_bounds_as_dyn_trait_type(p, m).precede(p).complete(p, TYPE_ARG);
113                }
114            }
115        }
116        _ if p.at_ts(types::TYPE_FIRST) => type_arg(p),
117        _ => return false,
118    }
119    true
120}
121
122// test lifetime_arg
123// type T = S<'static>;
124fn lifetime_arg(p: &mut Parser<'_>) {
125    let m = p.start();
126    lifetime(p);
127    m.complete(p, LIFETIME_ARG);
128}
129
130pub(super) fn const_arg_expr(p: &mut Parser<'_>) {
131    // The tests in here are really for `const_arg`, which wraps the content
132    // CONST_ARG.
133    match p.current() {
134        // test const_arg_block
135        // type T = S<{90 + 2}>;
136        T!['{'] => {
137            expressions::block_expr(p);
138        }
139        // test const_arg_literal
140        // type T = S<"hello", 0xdeadbeef>;
141        k if k.is_literal() => {
142            expressions::literal(p);
143        }
144        // test const_arg_bool_literal
145        // type T = S<true>;
146        T![true] | T![false] => {
147            expressions::literal(p);
148        }
149        // test const_arg_negative_number
150        // type T = S<-92>;
151        T![-] => {
152            let lm = p.start();
153            p.bump(T![-]);
154            expressions::literal(p);
155            lm.complete(p, PREFIX_EXPR);
156        }
157        _ if paths::is_path_start(p) => {
158            // This shouldn't be hit by `const_arg`
159            let lm = p.start();
160            paths::expr_path(p);
161            lm.complete(p, PATH_EXPR);
162        }
163        _ => {
164            // test_err recover_from_missing_const_default
165            // struct A<const N: i32 = , const M: i32 =>;
166            p.err_recover("expected a generic const argument", GENERIC_ARG_RECOVERY_SET);
167        }
168    }
169}
170
171// test const_arg
172// type T = S<92>;
173pub(super) fn const_arg(p: &mut Parser<'_>) {
174    let m = p.start();
175    const_arg_expr(p);
176    m.complete(p, CONST_ARG);
177}
178
179pub(crate) fn type_arg(p: &mut Parser<'_>) {
180    let m = p.start();
181    types::type_(p);
182    m.complete(p, TYPE_ARG);
183}