Debian Codegraph @

debian/missing-sources/zerofrom-derive/src/visitor.rs

raw symbols SCIP
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 //! Visitor for determining whether a type has type and non-static lifetime parameters
6 //! (duplicated in yoke/derive/src/visitor.rs)
7
8 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#;
9 use synrust-analyzer cargo syn 2.0.114 crate/::visitrust-analyzer cargo syn 2.0.114 gen/visit/::{visit_lifetimerust-analyzer cargo syn 2.0.114 gen/visit/visit_lifetime()., visit_typerust-analyzer cargo syn 2.0.114 gen/visit/visit_type()., visit_type_pathrust-analyzer cargo syn 2.0.114 gen/visit/visit_type_path()., Visitrust-analyzer cargo syn 2.0.114 gen/visit/Visit#};
10 use synrust-analyzer cargo syn 2.0.114 crate/::{Identrust-analyzer cargo proc-macro2 1.0.106 Ident#, Lifetimerust-analyzer cargo syn 2.0.114 lifetime/Lifetime#, Typerust-analyzer cargo syn 2.0.114 ty/Type#, TypePathrust-analyzer cargo syn 2.0.114 ty/TypePath#};
11
12 struct TypeVisitorrust-analyzer cargo zerofrom-derive 0.1.6 visitor/TypeVisitor#<'a> {
13 /// The type parameters in scope
14 typaramsrust-analyzer cargo zerofrom-derive 0.1.6 visitor/TypeVisitor#typarams.: &'a HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<Identrust-analyzer cargo proc-macro2 1.0.106 Ident#, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Identrust-analyzer cargo proc-macro2 1.0.106 Ident#>>,
15 /// Whether we found a type parameter
16 found_typaramsrust-analyzer cargo zerofrom-derive 0.1.6 visitor/TypeVisitor#found_typarams.: bool,
17 /// Whether we found a non-'static lifetime parameter
18 found_lifetimesrust-analyzer cargo zerofrom-derive 0.1.6 visitor/TypeVisitor#found_lifetimes.: bool,
19 }
20
21 impl<'ast> Visitrust-analyzer cargo syn 2.0.114 gen/visit/Visit#<'ast> for TypeVisitorrust-analyzer cargo zerofrom-derive 0.1.6 visitor/TypeVisitor#<'_> {
22 fn visit_lifetimerust-analyzer cargo zerofrom-derive 0.1.6 visitor/impl#[`TypeVisitor<'_>`][`Visit<'ast>`]visit_lifetime().(&mut self, lt: &'ast Lifetimerust-analyzer cargo syn 2.0.114 lifetime/Lifetime#) {
23 if lt.identrust-analyzer cargo syn 2.0.114 lifetime/Lifetime#ident. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne()."static" {
24 self.found_lifetimesrust-analyzer cargo zerofrom-derive 0.1.6 visitor/TypeVisitor#found_lifetimes. = true;
25 }
26 visit_lifetimerust-analyzer cargo syn 2.0.114 gen/visit/visit_lifetime().(self, lt)
27 }
28 fn visit_type_pathrust-analyzer cargo zerofrom-derive 0.1.6 visitor/impl#[`TypeVisitor<'_>`][`Visit<'ast>`]visit_type_path().(&mut self, ty: &'ast TypePathrust-analyzer cargo syn 2.0.114 ty/TypePath#) {
29 // We only need to check ty.path.get_ident() and not ty.qself or any
30 // generics in ty.path because the visitor will eventually visit those
31 // types on its own
32 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ident) = ty.pathrust-analyzer cargo syn 2.0.114 ty/TypePath#path..get_identrust-analyzer cargo syn 2.0.114 path/impl#[Path]get_ident().() {
33 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(maybe_borrowed) = self.typaramsrust-analyzer cargo zerofrom-derive 0.1.6 visitor/TypeVisitor#typarams..getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(ident) {
34 self.found_typaramsrust-analyzer cargo zerofrom-derive 0.1.6 visitor/TypeVisitor#found_typarams. = true;
35 if maybe_borrowed.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() {
36 self.found_lifetimesrust-analyzer cargo zerofrom-derive 0.1.6 visitor/TypeVisitor#found_lifetimes. = true;
37 }
38 }
39 }
40
41 visit_type_pathrust-analyzer cargo syn 2.0.114 gen/visit/visit_type_path().(self, ty)
42 }
43 }
44
45 /// Checks if a type has type or lifetime parameters, given the local context of
46 /// named type parameters. Returns (has_type_params, has_lifetime_params)
47 pub fn check_type_for_parametersrust-analyzer cargo zerofrom-derive 0.1.6 visitor/check_type_for_parameters().(
48 ty: &Typerust-analyzer cargo syn 2.0.114 ty/Type#,
49 typarams: &HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<Identrust-analyzer cargo proc-macro2 1.0.106 Ident#, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Identrust-analyzer cargo proc-macro2 1.0.106 Ident#>>,
50 ) -> (bool, bool) {
51 let mut visit = TypeVisitorrust-analyzer cargo zerofrom-derive 0.1.6 visitor/TypeVisitor# {
52 typaramsrust-analyzer cargo zerofrom-derive 0.1.6 visitor/TypeVisitor#typarams.,
53 found_typaramsrust-analyzer cargo zerofrom-derive 0.1.6 visitor/TypeVisitor#found_typarams.: false,
54 found_lifetimesrust-analyzer cargo zerofrom-derive 0.1.6 visitor/TypeVisitor#found_lifetimes.: false,
55 };
56 visit_typerust-analyzer cargo syn 2.0.114 gen/visit/visit_type().(&mut visit, ty);
57
58 (visit.found_typaramsrust-analyzer cargo zerofrom-derive 0.1.6 visitor/TypeVisitor#found_typarams., visit.found_lifetimesrust-analyzer cargo zerofrom-derive 0.1.6 visitor/TypeVisitor#found_lifetimes.)
59 }
60
61 #[cfg(test)]
62 mod tests {
63 use proc_macro2rust-analyzer cargo proc-macro2 1.0.106 crate/::Spanrust-analyzer cargo proc-macro2 1.0.106 Span#;
64 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#;
65 use synrust-analyzer cargo syn 2.0.114 crate/::{parse_quoterust-analyzer cargo syn 2.0.114 parse_quote/, Identrust-analyzer cargo proc-macro2 1.0.106 Ident#};
66
67 use superrust-analyzer cargo zerofrom-derive 0.1.6 crate/::check_type_for_parameters;
68 fn make_typarams(params: &[&str]) -> HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<Identrust-analyzer cargo proc-macro2 1.0.106 Ident#, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Identrust-analyzer cargo proc-macro2 1.0.106 Ident#>> {
69 params
70 .iter()
71 .map(|x| (Identrust-analyzer cargo proc-macro2 1.0.106 Ident#::new(x, Span::call_site()), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#))
72 .collect()
73 }
74
75 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
76 fn test_simple_type() {
77 let environment = make_typarams(&["T", "U", "V"]);
78
79 let ty = parse_quote!(Foo<'a, T>);
80 let check = check_type_for_parametersrust-analyzer cargo zerofrom-derive 0.1.6 visitor/check_type_for_parameters().(&ty, &environment);
81 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(check, (true, true));
82
83 let ty = parse_quote!(Foo<T>);
84 let check = check_type_for_parametersrust-analyzer cargo zerofrom-derive 0.1.6 visitor/check_type_for_parameters().(&ty, &environment);
85 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(check, (true, false));
86
87 let ty = parse_quote!(Foo<'static, T>);
88 let check = check_type_for_parametersrust-analyzer cargo zerofrom-derive 0.1.6 visitor/check_type_for_parameters().(&ty, &environment);
89 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(check, (true, false));
90
91 let ty = parse_quote!(Foo<'a>);
92 let check = check_type_for_parametersrust-analyzer cargo zerofrom-derive 0.1.6 visitor/check_type_for_parameters().(&ty, &environment);
93 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(check, (false, true));
94
95 let ty = parse_quote!(Foo<'a, Bar<U>, Baz<(V, u8)>>);
96 let check = check_type_for_parametersrust-analyzer cargo zerofrom-derive 0.1.6 visitor/check_type_for_parameters().(&ty, &environment);
97 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(check, (true, true));
98
99 let ty = parse_quote!(Foo<'a, W>);
100 let check = check_type_for_parametersrust-analyzer cargo zerofrom-derive 0.1.6 visitor/check_type_for_parameters().(&ty, &environment);
101 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(check, (false, true));
102 }
103
104 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
105 fn test_assoc_types() {
106 let environment = make_typarams(&["T"]);
107
108 let ty = parse_quote!(<Foo as SomeTrait<'a, T>>::Output);
109 let check = check_type_for_parametersrust-analyzer cargo zerofrom-derive 0.1.6 visitor/check_type_for_parameters().(&ty, &environment);
110 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(check, (true, true));
111
112 let ty = parse_quote!(<Foo as SomeTrait<'static, T>>::Output);
113 let check = check_type_for_parametersrust-analyzer cargo zerofrom-derive 0.1.6 visitor/check_type_for_parameters().(&ty, &environment);
114 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(check, (true, false));
115
116 let ty = parse_quote!(<T as SomeTrait<'static, Foo>>::Output);
117 let check = check_type_for_parametersrust-analyzer cargo zerofrom-derive 0.1.6 visitor/check_type_for_parameters().(&ty, &environment);
118 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(check, (true, false));
119 }
120 }