Debian Codegraph @

.cargo/vendor/syn/tests/test_ty.rs

raw
1 #![allow(
2 clippy::elidable_lifetime_names,
3 clippy::needless_lifetimes,
4 clippy::uninlined_format_args
5 )]
6
7 #[macro_use]
8 mod snapshot;
9
10 mod debug;
11
12 use proc_macro2::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree};
13 use quote::{quote, ToTokens as _};
14 use syn::punctuated::Punctuated;
15 use syn::{parse_quote, token, Token, Type, TypeTuple};
16
17 #[test]
18 fn test_mut_self() {
19 syn::parse_str::<Type>("fn(mut self)").unwrap();
20 syn::parse_str::<Type>("fn(mut self,)").unwrap();
21 syn::parse_str::<Type>("fn(mut self: ())").unwrap();
22 syn::parse_str::<Type>("fn(mut self: ...)").unwrap_err();
23 syn::parse_str::<Type>("fn(mut self: mut self)").unwrap_err();
24 syn::parse_str::<Type>("fn(mut self::T)").unwrap_err();
25 }
26
27 #[test]
28 fn test_macro_variable_type() {
29 // mimics the token stream corresponding to `$ty<T>`
30 let tokens = TokenStream::from_iter([
31 TokenTree::Group(Group::new(Delimiter::None, quote! { ty })),
32 TokenTree::Punct(Punct::new('<', Spacing::Alone)),
33 TokenTree::Ident(Ident::new("T", Span::call_site())),
34 TokenTree::Punct(Punct::new('>', Spacing::Alone)),
35 ]);
36
37 snapshot!(tokens as Type, @r#"
38 Type::Path {
39 path: Path {
40 segments: [
41 PathSegment {
42 ident: "ty",
43 arguments: PathArguments::AngleBracketed {
44 args: [
45 GenericArgument::Type(Type::Path {
46 path: Path {
47 segments: [
48 PathSegment {
49 ident: "T",
50 },
51 ],
52 },
53 }),
54 ],
55 },
56 },
57 ],
58 },
59 }
60 "#);
61
62 // mimics the token stream corresponding to `$ty::<T>`
63 let tokens = TokenStream::from_iter([
64 TokenTree::Group(Group::new(Delimiter::None, quote! { ty })),
65 TokenTree::Punct(Punct::new(':', Spacing::Joint)),
66 TokenTree::Punct(Punct::new(':', Spacing::Alone)),
67 TokenTree::Punct(Punct::new('<', Spacing::Alone)),
68 TokenTree::Ident(Ident::new("T", Span::call_site())),
69 TokenTree::Punct(Punct::new('>', Spacing::Alone)),
70 ]);
71
72 snapshot!(tokens as Type, @r#"
73 Type::Path {
74 path: Path {
75 segments: [
76 PathSegment {
77 ident: "ty",
78 arguments: PathArguments::AngleBracketed {
79 colon2_token: Some,
80 args: [
81 GenericArgument::Type(Type::Path {
82 path: Path {
83 segments: [
84 PathSegment {
85 ident: "T",
86 },
87 ],
88 },
89 }),
90 ],
91 },
92 },
93 ],
94 },
95 }
96 "#);
97 }
98
99 #[test]
100 fn test_group_angle_brackets() {
101 // mimics the token stream corresponding to `Option<$ty>`
102 let tokens = TokenStream::from_iter([
103 TokenTree::Ident(Ident::new("Option", Span::call_site())),
104 TokenTree::Punct(Punct::new('<', Spacing::Alone)),
105 TokenTree::Group(Group::new(Delimiter::None, quote! { Vec<u8> })),
106 TokenTree::Punct(Punct::new('>', Spacing::Alone)),
107 ]);
108
109 snapshot!(tokens as Type, @r#"
110 Type::Path {
111 path: Path {
112 segments: [
113 PathSegment {
114 ident: "Option",
115 arguments: PathArguments::AngleBracketed {
116 args: [
117 GenericArgument::Type(Type::Group {
118 elem: Type::Path {
119 path: Path {
120 segments: [
121 PathSegment {
122 ident: "Vec",
123 arguments: PathArguments::AngleBracketed {
124 args: [
125 GenericArgument::Type(Type::Path {
126 path: Path {
127 segments: [
128 PathSegment {
129 ident: "u8",
130 },
131 ],
132 },
133 }),
134 ],
135 },
136 },
137 ],
138 },
139 },
140 }),
141 ],
142 },
143 },
144 ],
145 },
146 }
147 "#);
148 }
149
150 #[test]
151 fn test_group_colons() {
152 // mimics the token stream corresponding to `$ty::Item`
153 let tokens = TokenStream::from_iter([
154 TokenTree::Group(Group::new(Delimiter::None, quote! { Vec<u8> })),
155 TokenTree::Punct(Punct::new(':', Spacing::Joint)),
156 TokenTree::Punct(Punct::new(':', Spacing::Alone)),
157 TokenTree::Ident(Ident::new("Item", Span::call_site())),
158 ]);
159
160 snapshot!(tokens as Type, @r#"
161 Type::Path {
162 path: Path {
163 segments: [
164 PathSegment {
165 ident: "Vec",
166 arguments: PathArguments::AngleBracketed {
167 args: [
168 GenericArgument::Type(Type::Path {
169 path: Path {
170 segments: [
171 PathSegment {
172 ident: "u8",
173 },
174 ],
175 },
176 }),
177 ],
178 },
179 },
180 Token![::],
181 PathSegment {
182 ident: "Item",
183 },
184 ],
185 },
186 }
187 "#);
188
189 let tokens = TokenStream::from_iter([
190 TokenTree::Group(Group::new(Delimiter::None, quote! { [T] })),
191 TokenTree::Punct(Punct::new(':', Spacing::Joint)),
192 TokenTree::Punct(Punct::new(':', Spacing::Alone)),
193 TokenTree::Ident(Ident::new("Element", Span::call_site())),
194 ]);
195
196 snapshot!(tokens as Type, @r#"
197 Type::Path {
198 qself: Some(QSelf {
199 ty: Type::Slice {
200 elem: Type::Path {
201 path: Path {
202 segments: [
203 PathSegment {
204 ident: "T",
205 },
206 ],
207 },
208 },
209 },
210 position: 0,
211 }),
212 path: Path {
213 leading_colon: Some,
214 segments: [
215 PathSegment {
216 ident: "Element",
217 },
218 ],
219 },
220 }
221 "#);
222 }
223
224 #[test]
225 fn test_trait_object() {
226 let tokens = quote!(dyn for<'a> Trait<'a> + 'static);
227 snapshot!(tokens as Type, @r#"
228 Type::TraitObject {
229 dyn_token: Some,
230 bounds: [
231 TypeParamBound::Trait(TraitBound {
232 lifetimes: Some(BoundLifetimes {
233 lifetimes: [
234 GenericParam::Lifetime(LifetimeParam {
235 lifetime: Lifetime {
236 ident: "a",
237 },
238 }),
239 ],
240 }),
241 path: Path {
242 segments: [
243 PathSegment {
244 ident: "Trait",
245 arguments: PathArguments::AngleBracketed {
246 args: [
247 GenericArgument::Lifetime(Lifetime {
248 ident: "a",
249 }),
250 ],
251 },
252 },
253 ],
254 },
255 }),
256 Token![+],
257 TypeParamBound::Lifetime {
258 ident: "static",
259 },
260 ],
261 }
262 "#);
263
264 let tokens = quote!(dyn 'a + Trait);
265 snapshot!(tokens as Type, @r#"
266 Type::TraitObject {
267 dyn_token: Some,
268 bounds: [
269 TypeParamBound::Lifetime {
270 ident: "a",
271 },
272 Token![+],
273 TypeParamBound::Trait(TraitBound {
274 path: Path {
275 segments: [
276 PathSegment {
277 ident: "Trait",
278 },
279 ],
280 },
281 }),
282 ],
283 }
284 "#);
285
286 // None of the following are valid Rust types.
287 syn::parse_str::<Type>("for<'a> dyn Trait<'a>").unwrap_err();
288 syn::parse_str::<Type>("dyn for<'a> 'a + Trait").unwrap_err();
289 }
290
291 #[test]
292 fn test_trailing_plus() {
293 #[rustfmt::skip]
294 let tokens = quote!(impl Trait +);
295 snapshot!(tokens as Type, @r#"
296 Type::ImplTrait {
297 bounds: [
298 TypeParamBound::Trait(TraitBound {
299 path: Path {
300 segments: [
301 PathSegment {
302 ident: "Trait",
303 },
304 ],
305 },
306 }),
307 Token![+],
308 ],
309 }
310 "#);
311
312 #[rustfmt::skip]
313 let tokens = quote!(dyn Trait +);
314 snapshot!(tokens as Type, @r#"
315 Type::TraitObject {
316 dyn_token: Some,
317 bounds: [
318 TypeParamBound::Trait(TraitBound {
319 path: Path {
320 segments: [
321 PathSegment {
322 ident: "Trait",
323 },
324 ],
325 },
326 }),
327 Token![+],
328 ],
329 }
330 "#);
331
332 #[rustfmt::skip]
333 let tokens = quote!(Trait +);
334 snapshot!(tokens as Type, @r#"
335 Type::TraitObject {
336 bounds: [
337 TypeParamBound::Trait(TraitBound {
338 path: Path {
339 segments: [
340 PathSegment {
341 ident: "Trait",
342 },
343 ],
344 },
345 }),
346 Token![+],
347 ],
348 }
349 "#);
350 }
351
352 #[test]
353 fn test_tuple_comma() {
354 let mut expr = TypeTuple {
355 paren_token: token::Paren::default(),
356 elems: Punctuated::new(),
357 };
358 snapshot!(expr.to_token_stream() as Type, @"Type::Tuple");
359
360 expr.elems.push_value(parse_quote!(_));
361 // Must not parse to Type::Paren
362 snapshot!(expr.to_token_stream() as Type, @r#"
363 Type::Tuple {
364 elems: [
365 Type::Infer,
366 Token![,],
367 ],
368 }
369 "#);
370
371 expr.elems.push_punct(<Token![,]>::default());
372 snapshot!(expr.to_token_stream() as Type, @r#"
373 Type::Tuple {
374 elems: [
375 Type::Infer,
376 Token![,],
377 ],
378 }
379 "#);
380
381 expr.elems.push_value(parse_quote!(_));
382 snapshot!(expr.to_token_stream() as Type, @r#"
383 Type::Tuple {
384 elems: [
385 Type::Infer,
386 Token![,],
387 Type::Infer,
388 ],
389 }
390 "#);
391
392 expr.elems.push_punct(<Token![,]>::default());
393 snapshot!(expr.to_token_stream() as Type, @r#"
394 Type::Tuple {
395 elems: [
396 Type::Infer,
397 Token![,],
398 Type::Infer,
399 Token![,],
400 ],
401 }
402 "#);
403 }
404
405 #[test]
406 fn test_impl_trait_use() {
407 let tokens = quote! {
408 impl Sized + use<'_, 'a, A, Test>
409 };
410
411 snapshot!(tokens as Type, @r#"
412 Type::ImplTrait {
413 bounds: [
414 TypeParamBound::Trait(TraitBound {
415 path: Path {
416 segments: [
417 PathSegment {
418 ident: "Sized",
419 },
420 ],
421 },
422 }),
423 Token![+],
424 TypeParamBound::PreciseCapture(PreciseCapture {
425 params: [
426 CapturedParam::Lifetime(Lifetime {
427 ident: "_",
428 }),
429 Token![,],
430 CapturedParam::Lifetime(Lifetime {
431 ident: "a",
432 }),
433 Token![,],
434 CapturedParam::Ident("A"),
435 Token![,],
436 CapturedParam::Ident("Test"),
437 ],
438 }),
439 ],
440 }
441 "#);
442
443 let trailing = quote! {
444 impl Sized + use<'_,>
445 };
446
447 snapshot!(trailing as Type, @r#"
448 Type::ImplTrait {
449 bounds: [
450 TypeParamBound::Trait(TraitBound {
451 path: Path {
452 segments: [
453 PathSegment {
454 ident: "Sized",
455 },
456 ],
457 },
458 }),
459 Token![+],
460 TypeParamBound::PreciseCapture(PreciseCapture {
461 params: [
462 CapturedParam::Lifetime(Lifetime {
463 ident: "_",
464 }),
465 Token![,],
466 ],
467 }),
468 ],
469 }
470 "#);
471 }