blob: c7da1b9d8cebb089c8841eb9623598853adaba23 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnayb79ee962016-09-04 09:39:20 -07009use super::*;
David Tolnay2ae520a2017-12-29 11:19:50 -050010use proc_macro2::TokenStream;
David Tolnay94d2b792018-04-29 12:26:10 -070011use punctuated::Punctuated;
David Tolnay2ae520a2017-12-29 11:19:50 -050012#[cfg(feature = "extra-traits")]
13use std::hash::{Hash, Hasher};
14#[cfg(feature = "extra-traits")]
David Tolnayc43b44e2017-12-30 23:55:54 -050015use tt::TokenStreamHelper;
David Tolnayb79ee962016-09-04 09:39:20 -070016
Alex Crichton62a0a592017-05-22 13:58:53 -070017ast_enum_of_structs! {
David Tolnay7949ae22018-01-07 00:14:51 -080018 /// The possible types that a Rust value could have.
David Tolnay614a0142018-01-07 10:25:43 -080019 ///
David Tolnay461d98e2018-01-07 11:07:19 -080020 /// *This type is available if Syn is built with the `"derive"` or `"full"`
21 /// feature.*
22 ///
David Tolnay614a0142018-01-07 10:25:43 -080023 /// # Syntax tree enum
24 ///
25 /// This type is a [syntax tree enum].
26 ///
27 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayfd6bf5c2017-11-12 09:41:14 -080028 pub enum Type {
David Tolnay7949ae22018-01-07 00:14:51 -080029 /// A dynamically sized slice type: `[T]`.
David Tolnay461d98e2018-01-07 11:07:19 -080030 ///
31 /// *This type is available if Syn is built with the `"derive"` or
32 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -080033 pub Slice(TypeSlice {
David Tolnay32954ef2017-12-26 22:43:16 -050034 pub bracket_token: token::Bracket,
David Tolnayeadbda32017-12-29 02:33:47 -050035 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070036 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080037
David Tolnay7949ae22018-01-07 00:14:51 -080038 /// A fixed size array type: `[T; n]`.
David Tolnay461d98e2018-01-07 11:07:19 -080039 ///
40 /// *This type is available if Syn is built with the `"derive"` or
41 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -080042 pub Array(TypeArray {
David Tolnay32954ef2017-12-26 22:43:16 -050043 pub bracket_token: token::Bracket,
David Tolnayeadbda32017-12-29 02:33:47 -050044 pub elem: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080045 pub semi_token: Token![;],
David Tolnayeadbda32017-12-29 02:33:47 -050046 pub len: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -070047 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080048
David Tolnay7949ae22018-01-07 00:14:51 -080049 /// A raw pointer type: `*const T` or `*mut T`.
David Tolnay461d98e2018-01-07 11:07:19 -080050 ///
51 /// *This type is available if Syn is built with the `"derive"` or
52 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -080053 pub Ptr(TypePtr {
David Tolnayf8db7ba2017-11-11 22:52:16 -080054 pub star_token: Token![*],
55 pub const_token: Option<Token![const]>,
David Tolnay136aaa32017-12-29 02:37:36 -050056 pub mutability: Option<Token![mut]>,
57 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070058 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080059
David Tolnay7949ae22018-01-07 00:14:51 -080060 /// A reference type: `&'a T` or `&'a mut T`.
David Tolnay461d98e2018-01-07 11:07:19 -080061 ///
62 /// *This type is available if Syn is built with the `"derive"` or
63 /// `"full"` feature.*
David Tolnay0a89b4d2017-11-13 00:55:45 -080064 pub Reference(TypeReference {
David Tolnayf8db7ba2017-11-11 22:52:16 -080065 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -070066 pub lifetime: Option<Lifetime>,
David Tolnay136aaa32017-12-29 02:37:36 -050067 pub mutability: Option<Token![mut]>,
68 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070069 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080070
David Tolnay7949ae22018-01-07 00:14:51 -080071 /// A bare function type: `fn(usize) -> bool`.
David Tolnay461d98e2018-01-07 11:07:19 -080072 ///
73 /// *This type is available if Syn is built with the `"derive"` or
74 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -080075 pub BareFn(TypeBareFn {
David Tolnaya7d69fc2018-08-26 13:30:24 -040076 pub lifetimes: Option<BoundLifetimes>,
David Tolnaybe7a9592017-12-29 02:39:53 -050077 pub unsafety: Option<Token![unsafe]>,
78 pub abi: Option<Abi>,
79 pub fn_token: Token![fn],
David Tolnaybe7a9592017-12-29 02:39:53 -050080 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -050081 pub inputs: Punctuated<BareFnArg, Token![,]>,
David Tolnaybe7a9592017-12-29 02:39:53 -050082 pub variadic: Option<Token![...]>,
83 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -070084 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080085
David Tolnay7949ae22018-01-07 00:14:51 -080086 /// The never type: `!`.
David Tolnay461d98e2018-01-07 11:07:19 -080087 ///
88 /// *This type is available if Syn is built with the `"derive"` or
89 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -080090 pub Never(TypeNever {
David Tolnayf8db7ba2017-11-11 22:52:16 -080091 pub bang_token: Token![!],
Alex Crichtonccbb45d2017-05-23 10:58:24 -070092 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080093
David Tolnay7949ae22018-01-07 00:14:51 -080094 /// A tuple type: `(A, B, C, String)`.
David Tolnay461d98e2018-01-07 11:07:19 -080095 ///
96 /// *This type is available if Syn is built with the `"derive"` or
97 /// `"full"` feature.*
David Tolnay05362582017-12-26 01:33:57 -050098 pub Tuple(TypeTuple {
David Tolnay32954ef2017-12-26 22:43:16 -050099 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500100 pub elems: Punctuated<Type, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700101 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800102
103 /// A path like `std::slice::Iter`, optionally qualified with a
David Tolnay7949ae22018-01-07 00:14:51 -0800104 /// self-type as in `<Vec<T> as SomeTrait>::Associated`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700105 ///
David Tolnay7949ae22018-01-07 00:14:51 -0800106 /// Type arguments are stored in the Path itself.
David Tolnay461d98e2018-01-07 11:07:19 -0800107 ///
108 /// *This type is available if Syn is built with the `"derive"` or
109 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800110 pub Path(TypePath {
Alex Crichton62a0a592017-05-22 13:58:53 -0700111 pub qself: Option<QSelf>,
112 pub path: Path,
113 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800114
David Tolnay7949ae22018-01-07 00:14:51 -0800115 /// A trait object type `Bound1 + Bound2 + Bound3` where `Bound` is a
116 /// trait or a lifetime.
David Tolnay461d98e2018-01-07 11:07:19 -0800117 ///
118 /// *This type is available if Syn is built with the `"derive"` or
119 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800120 pub TraitObject(TypeTraitObject {
David Tolnaye45b59f2017-12-25 18:44:49 -0500121 pub dyn_token: Option<Token![dyn]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500122 pub bounds: Punctuated<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700123 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800124
David Tolnay7949ae22018-01-07 00:14:51 -0800125 /// An `impl Bound1 + Bound2 + Bound3` type where `Bound` is a trait or
126 /// a lifetime.
David Tolnay461d98e2018-01-07 11:07:19 -0800127 ///
128 /// *This type is available if Syn is built with the `"derive"` or
129 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800130 pub ImplTrait(TypeImplTrait {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800131 pub impl_token: Token![impl],
David Tolnayf2cfd722017-12-31 18:02:51 -0500132 pub bounds: Punctuated<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700133 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800134
135 /// A parenthesized type equivalent to the inner type.
David Tolnay461d98e2018-01-07 11:07:19 -0800136 ///
137 /// *This type is available if Syn is built with the `"derive"` or
138 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800139 pub Paren(TypeParen {
David Tolnay32954ef2017-12-26 22:43:16 -0500140 pub paren_token: token::Paren,
David Tolnayeadbda32017-12-29 02:33:47 -0500141 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700142 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800143
144 /// A type contained within invisible delimiters.
David Tolnay461d98e2018-01-07 11:07:19 -0800145 ///
146 /// *This type is available if Syn is built with the `"derive"` or
147 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800148 pub Group(TypeGroup {
David Tolnay32954ef2017-12-26 22:43:16 -0500149 pub group_token: token::Group,
David Tolnayeadbda32017-12-29 02:33:47 -0500150 pub elem: Box<Type>,
Michael Layzell93c36282017-06-04 20:43:14 -0400151 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800152
David Tolnay7949ae22018-01-07 00:14:51 -0800153 /// Indication that a type should be inferred by the compiler: `_`.
David Tolnay461d98e2018-01-07 11:07:19 -0800154 ///
155 /// *This type is available if Syn is built with the `"derive"` or
156 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800157 pub Infer(TypeInfer {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800158 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700159 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800160
Alex Crichton62a0a592017-05-22 13:58:53 -0700161 /// A macro in the type position.
David Tolnay461d98e2018-01-07 11:07:19 -0800162 ///
163 /// *This type is available if Syn is built with the `"derive"` or
164 /// `"full"` feature.*
David Tolnay323279a2017-12-29 11:26:32 -0500165 pub Macro(TypeMacro {
166 pub mac: Macro,
167 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800168
David Tolnay7949ae22018-01-07 00:14:51 -0800169 /// Tokens in type position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800170 ///
171 /// *This type is available if Syn is built with the `"derive"` or
172 /// `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500173 pub Verbatim(TypeVerbatim #manual_extra_traits {
174 pub tts: TokenStream,
175 }),
176 }
177}
178
179#[cfg(feature = "extra-traits")]
180impl Eq for TypeVerbatim {}
181
182#[cfg(feature = "extra-traits")]
183impl PartialEq for TypeVerbatim {
184 fn eq(&self, other: &Self) -> bool {
185 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
186 }
187}
188
189#[cfg(feature = "extra-traits")]
190impl Hash for TypeVerbatim {
191 fn hash<H>(&self, state: &mut H)
192 where
193 H: Hasher,
194 {
195 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700196 }
197}
198
199ast_struct! {
David Tolnayf01e37b2018-01-06 23:38:26 -0800200 /// The binary interface of a function: `extern "C"`.
David Tolnay461d98e2018-01-07 11:07:19 -0800201 ///
202 /// *This type is available if Syn is built with the `"derive"` or `"full"`
203 /// feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700204 pub struct Abi {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800205 pub extern_token: Token![extern],
David Tolnayf01e37b2018-01-06 23:38:26 -0800206 pub name: Option<LitStr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700207 }
208}
209
210ast_struct! {
David Tolnay7949ae22018-01-07 00:14:51 -0800211 /// An argument in a function type: the `usize` in `fn(usize) -> bool`.
David Tolnay461d98e2018-01-07 11:07:19 -0800212 ///
213 /// *This type is available if Syn is built with the `"derive"` or `"full"`
214 /// feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700215 pub struct BareFnArg {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800216 pub name: Option<(BareFnArgName, Token![:])>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800217 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700218 }
219}
220
Alex Crichton23a15f62017-08-28 12:34:23 -0700221ast_enum! {
David Tolnay7949ae22018-01-07 00:14:51 -0800222 /// Name of an argument in a function type: the `n` in `fn(n: usize)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800223 ///
224 /// *This type is available if Syn is built with the `"derive"` or `"full"`
225 /// feature.*
Alex Crichton23a15f62017-08-28 12:34:23 -0700226 pub enum BareFnArgName {
David Tolnay7949ae22018-01-07 00:14:51 -0800227 /// Argument given a name.
Alex Crichton23a15f62017-08-28 12:34:23 -0700228 Named(Ident),
David Tolnay7949ae22018-01-07 00:14:51 -0800229 /// Argument not given a name, matched with `_`.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800230 Wild(Token![_]),
Alex Crichton23a15f62017-08-28 12:34:23 -0700231 }
232}
Alex Crichton62a0a592017-05-22 13:58:53 -0700233
234ast_enum! {
David Tolnay7949ae22018-01-07 00:14:51 -0800235 /// Return type of a function signature.
David Tolnay461d98e2018-01-07 11:07:19 -0800236 ///
237 /// *This type is available if Syn is built with the `"derive"` or `"full"`
238 /// feature.*
David Tolnayf93b90d2017-11-11 19:21:26 -0800239 pub enum ReturnType {
Alex Crichton62a0a592017-05-22 13:58:53 -0700240 /// Return type is not specified.
241 ///
David Tolnay7949ae22018-01-07 00:14:51 -0800242 /// Functions default to `()` and closures default to type inference.
Alex Crichton62a0a592017-05-22 13:58:53 -0700243 Default,
David Tolnay7949ae22018-01-07 00:14:51 -0800244 /// A particular type is returned.
David Tolnay4a3f59a2017-12-28 21:21:12 -0500245 Type(Token![->], Box<Type>),
Alex Crichton62a0a592017-05-22 13:58:53 -0700246 }
David Tolnayb79ee962016-09-04 09:39:20 -0700247}
248
David Tolnay86eca752016-09-04 11:26:41 -0700249#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700250pub mod parsing {
251 use super::*;
David Tolnay60291082018-08-28 09:54:49 -0700252
David Tolnay10951d52018-08-31 10:27:39 -0700253 use parse::{Parse, ParseStream, Result};
David Tolnay60291082018-08-28 09:54:49 -0700254 use path;
David Tolnayda4049b2016-09-04 10:59:23 -0700255
David Tolnaya7d69fc2018-08-26 13:30:24 -0400256 impl Parse for Type {
257 fn parse(input: ParseStream) -> Result<Self> {
258 ambig_ty(input, true)
Alex Crichton954046c2017-05-30 21:49:42 -0700259 }
260 }
David Tolnay0047c712016-12-21 21:59:25 -0500261
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800262 impl Type {
Michael Layzell9bf2b002017-06-04 18:49:53 -0400263 /// In some positions, types may not contain the `+` character, to
264 /// disambiguate them. For example in the expression `1 as T`, T may not
265 /// contain a `+` character.
266 ///
267 /// This parser does not allow a `+`, while the default parser does.
David Tolnaya7d69fc2018-08-26 13:30:24 -0400268 pub fn without_plus(input: ParseStream) -> Result<Self> {
269 ambig_ty(input, false)
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800270 }
Alex Crichton954046c2017-05-30 21:49:42 -0700271 }
David Tolnayb79ee962016-09-04 09:39:20 -0700272
David Tolnaya7d69fc2018-08-26 13:30:24 -0400273 fn ambig_ty(input: ParseStream, allow_plus: bool) -> Result<Type> {
274 if input.peek(token::Group) {
275 return input.parse().map(Type::Group);
276 }
277
278 let mut lifetimes = None::<BoundLifetimes>;
279 let mut lookahead = input.lookahead1();
280 if lookahead.peek(Token![for]) {
281 lifetimes = input.parse()?;
282 lookahead = input.lookahead1();
283 if !lookahead.peek(Ident)
284 && !lookahead.peek(Token![fn])
285 && !lookahead.peek(Token![unsafe])
286 && !lookahead.peek(Token![extern])
287 && !lookahead.peek(Token![super])
288 && !lookahead.peek(Token![self])
289 && !lookahead.peek(Token![Self])
290 && !lookahead.peek(Token![crate])
291 {
292 return Err(lookahead.error());
293 }
294 }
295
296 if lookahead.peek(token::Paren) {
297 let content;
298 let paren_token = parenthesized!(content in input);
299 if content.is_empty() {
300 return Ok(Type::Tuple(TypeTuple {
301 paren_token: paren_token,
302 elems: Punctuated::new(),
303 }));
304 }
305 if content.peek(Lifetime) {
306 return Ok(Type::Paren(TypeParen {
307 paren_token: paren_token,
308 elem: Box::new(Type::TraitObject(content.parse()?)),
309 }));
310 }
David Tolnay5f7a91c2018-10-27 22:22:58 -0700311 if content.peek(Token![?]) {
312 return Ok(Type::TraitObject(TypeTraitObject {
313 dyn_token: None,
314 bounds: {
315 let mut bounds = Punctuated::new();
316 bounds.push_value(content.parse()?);
317 while let Some(plus) = input.parse()? {
318 bounds.push_punct(plus);
319 bounds.push_value(input.parse()?);
320 }
321 bounds
322 },
323 }));
324 }
David Tolnaya7d69fc2018-08-26 13:30:24 -0400325 let first: Type = content.parse()?;
326 if content.peek(Token![,]) {
David Tolnay4ace1c72018-10-27 21:49:05 -0700327 return Ok(Type::Tuple(TypeTuple {
David Tolnaya7d69fc2018-08-26 13:30:24 -0400328 paren_token: paren_token,
329 elems: {
330 let mut elems = Punctuated::new();
331 elems.push_value(first);
332 elems.push_punct(content.parse()?);
333 let rest: Punctuated<Type, Token![,]> =
334 content.parse_terminated(Parse::parse)?;
335 elems.extend(rest);
336 elems
337 },
David Tolnay4ace1c72018-10-27 21:49:05 -0700338 }));
David Tolnaya7d69fc2018-08-26 13:30:24 -0400339 }
David Tolnay4ace1c72018-10-27 21:49:05 -0700340 if allow_plus && input.peek(Token![+]) {
David Tolnay4df58512018-10-27 22:34:36 -0700341 loop {
342 let first = match first {
343 Type::Path(TypePath { qself: None, path }) => {
344 TypeParamBound::Trait(TraitBound {
David Tolnay4ace1c72018-10-27 21:49:05 -0700345 paren_token: Some(paren_token),
346 modifier: TraitBoundModifier::None,
347 lifetimes: None,
348 path: path,
David Tolnay4df58512018-10-27 22:34:36 -0700349 })
350 }
351 Type::TraitObject(TypeTraitObject { dyn_token: None, ref bounds }) => {
352 if bounds.len() > 1 || bounds.trailing_punct() {
353 break;
354 }
355 match first {
356 Type::TraitObject(TypeTraitObject { bounds, .. }) => {
357 bounds.into_iter().next().unwrap()
358 }
359 _ => unreachable!(),
360 }
361 }
362 _ => break,
363 };
364 return Ok(Type::TraitObject(TypeTraitObject {
365 dyn_token: None,
366 bounds: {
367 let mut bounds = Punctuated::new();
368 bounds.push_value(first);
David Tolnay4ace1c72018-10-27 21:49:05 -0700369 while let Some(plus) = input.parse()? {
370 bounds.push_punct(plus);
371 bounds.push_value(input.parse()?);
372 }
373 bounds
374 },
375 }));
376 }
377 }
378 Ok(Type::Paren(TypeParen {
379 paren_token: paren_token,
380 elem: Box::new(first),
381 }))
David Tolnaya7d69fc2018-08-26 13:30:24 -0400382 } else if lookahead.peek(Token![fn])
383 || lookahead.peek(Token![unsafe])
384 || lookahead.peek(Token![extern]) && !input.peek2(Token![::])
385 {
386 let mut bare_fn: TypeBareFn = input.parse()?;
387 bare_fn.lifetimes = lifetimes;
388 Ok(Type::BareFn(bare_fn))
389 } else if lookahead.peek(Ident)
390 || input.peek(Token![super])
391 || input.peek(Token![self])
392 || input.peek(Token![Self])
393 || input.peek(Token![crate])
394 || input.peek(Token![extern])
395 || lookahead.peek(Token![::])
396 || lookahead.peek(Token![<])
397 {
398 if input.peek(Token![dyn]) {
399 let mut trait_object: TypeTraitObject = input.parse()?;
400 if lifetimes.is_some() {
401 match *trait_object.bounds.iter_mut().next().unwrap() {
402 TypeParamBound::Trait(ref mut trait_bound) => {
403 trait_bound.lifetimes = lifetimes;
404 }
405 TypeParamBound::Lifetime(_) => unreachable!(),
406 }
407 }
408 return Ok(Type::TraitObject(trait_object));
409 }
410
411 let ty: TypePath = input.parse()?;
412 if ty.qself.is_some() {
413 return Ok(Type::Path(ty));
414 }
415
416 if input.peek(Token![!]) && !input.peek(Token![!=]) {
417 let mut contains_arguments = false;
418 for segment in &ty.path.segments {
419 match segment.arguments {
420 PathArguments::None => {}
421 PathArguments::AngleBracketed(_) | PathArguments::Parenthesized(_) => {
422 contains_arguments = true;
423 }
424 }
425 }
426
427 if !contains_arguments {
428 let bang_token: Token![!] = input.parse()?;
429 let (delimiter, tts) = mac::parse_delimiter(input)?;
430 return Ok(Type::Macro(TypeMacro {
431 mac: Macro {
432 path: ty.path,
433 bang_token: bang_token,
434 delimiter: delimiter,
435 tts: tts,
436 },
437 }));
438 }
439 }
440
441 if lifetimes.is_some() || allow_plus && input.peek(Token![+]) {
442 let mut bounds = Punctuated::new();
443 bounds.push_value(TypeParamBound::Trait(TraitBound {
444 paren_token: None,
445 modifier: TraitBoundModifier::None,
446 lifetimes: lifetimes,
447 path: ty.path,
448 }));
David Tolnayf5ebc192018-08-30 18:23:46 -0700449 if allow_plus {
450 while input.peek(Token![+]) {
451 bounds.push_punct(input.parse()?);
David Tolnay1b210d12018-10-27 22:07:00 -0700452 if input.peek(Token![>]) {
453 break;
454 }
David Tolnayf5ebc192018-08-30 18:23:46 -0700455 bounds.push_value(input.parse()?);
456 }
David Tolnaya7d69fc2018-08-26 13:30:24 -0400457 }
458 return Ok(Type::TraitObject(TypeTraitObject {
459 dyn_token: None,
460 bounds: bounds,
461 }));
462 }
463
464 Ok(Type::Path(ty))
465 } else if lookahead.peek(token::Bracket) {
466 let content;
467 let bracket_token = bracketed!(content in input);
468 let elem: Type = content.parse()?;
469 if content.peek(Token![;]) {
470 Ok(Type::Array(TypeArray {
471 bracket_token: bracket_token,
David Tolnayeadbda32017-12-29 02:33:47 -0500472 elem: Box::new(elem),
David Tolnaya7d69fc2018-08-26 13:30:24 -0400473 semi_token: content.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -0700474 len: content.parse()?,
David Tolnaya7d69fc2018-08-26 13:30:24 -0400475 }))
476 } else {
477 Ok(Type::Slice(TypeSlice {
478 bracket_token: bracket_token,
479 elem: Box::new(elem),
480 }))
Alex Crichton954046c2017-05-30 21:49:42 -0700481 }
David Tolnaya7d69fc2018-08-26 13:30:24 -0400482 } else if lookahead.peek(Token![*]) {
483 input.parse().map(Type::Ptr)
484 } else if lookahead.peek(Token![&]) {
485 input.parse().map(Type::Reference)
486 } else if lookahead.peek(Token![!]) && !input.peek(Token![=]) {
487 input.parse().map(Type::Never)
488 } else if lookahead.peek(Token![impl ]) {
489 input.parse().map(Type::ImplTrait)
490 } else if lookahead.peek(Token![_]) {
491 input.parse().map(Type::Infer)
492 } else if lookahead.peek(Lifetime) {
493 input.parse().map(Type::TraitObject)
494 } else {
495 Err(lookahead.error())
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800496 }
Alex Crichton954046c2017-05-30 21:49:42 -0700497 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700498
David Tolnaya7d69fc2018-08-26 13:30:24 -0400499 impl Parse for TypeSlice {
500 fn parse(input: ParseStream) -> Result<Self> {
501 let content;
502 Ok(TypeSlice {
503 bracket_token: bracketed!(content in input),
504 elem: content.parse()?,
Michael Layzell92639a52017-06-01 00:07:44 -0400505 })
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800506 }
Alex Crichton954046c2017-05-30 21:49:42 -0700507 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700508
David Tolnaya7d69fc2018-08-26 13:30:24 -0400509 impl Parse for TypeArray {
510 fn parse(input: ParseStream) -> Result<Self> {
511 let content;
512 Ok(TypeArray {
513 bracket_token: bracketed!(content in input),
514 elem: content.parse()?,
515 semi_token: content.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -0700516 len: content.parse()?,
David Tolnaya7d69fc2018-08-26 13:30:24 -0400517 })
518 }
519 }
520
521 impl Parse for TypePtr {
522 fn parse(input: ParseStream) -> Result<Self> {
523 let star_token: Token![*] = input.parse()?;
524
525 let lookahead = input.lookahead1();
526 let (const_token, mutability) = if lookahead.peek(Token![const]) {
527 (Some(input.parse()?), None)
528 } else if lookahead.peek(Token![mut]) {
529 (None, Some(input.parse()?))
530 } else {
531 return Err(lookahead.error());
532 };
533
534 Ok(TypePtr {
535 star_token: star_token,
536 const_token: const_token,
David Tolnay136aaa32017-12-29 02:37:36 -0500537 mutability: mutability,
David Tolnaya7d69fc2018-08-26 13:30:24 -0400538 elem: Box::new(input.call(Type::without_plus)?),
Michael Layzell92639a52017-06-01 00:07:44 -0400539 })
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800540 }
Alex Crichton954046c2017-05-30 21:49:42 -0700541 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700542
David Tolnaya7d69fc2018-08-26 13:30:24 -0400543 impl Parse for TypeReference {
544 fn parse(input: ParseStream) -> Result<Self> {
545 Ok(TypeReference {
546 and_token: input.parse()?,
547 lifetime: input.parse()?,
548 mutability: input.parse()?,
549 // & binds tighter than +, so we don't allow + here.
550 elem: Box::new(input.call(Type::without_plus)?),
Michael Layzell92639a52017-06-01 00:07:44 -0400551 })
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800552 }
Alex Crichton954046c2017-05-30 21:49:42 -0700553 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700554
David Tolnaya7d69fc2018-08-26 13:30:24 -0400555 impl Parse for TypeBareFn {
556 fn parse(input: ParseStream) -> Result<Self> {
557 let args;
558 let allow_variadic;
559 Ok(TypeBareFn {
560 lifetimes: input.parse()?,
561 unsafety: input.parse()?,
562 abi: input.parse()?,
563 fn_token: input.parse()?,
564 paren_token: parenthesized!(args in input),
565 inputs: {
David Tolnayf5ebc192018-08-30 18:23:46 -0700566 let mut inputs = Punctuated::new();
567 while !args.is_empty() && !args.peek(Token![...]) {
568 inputs.push_value(args.parse()?);
569 if args.is_empty() {
570 break;
571 }
572 inputs.push_punct(args.parse()?);
573 }
David Tolnaya7d69fc2018-08-26 13:30:24 -0400574 allow_variadic = inputs.empty_or_trailing();
575 inputs
576 },
577 variadic: {
578 if allow_variadic && args.peek(Token![...]) {
579 Some(args.parse()?)
580 } else {
581 None
582 }
583 },
584 output: input.call(ReturnType::without_plus)?,
Michael Layzell92639a52017-06-01 00:07:44 -0400585 })
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800586 }
Alex Crichton954046c2017-05-30 21:49:42 -0700587 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700588
David Tolnaya7d69fc2018-08-26 13:30:24 -0400589 impl Parse for TypeNever {
590 fn parse(input: ParseStream) -> Result<Self> {
591 Ok(TypeNever {
592 bang_token: input.parse()?,
David Tolnay7d38c7e2017-12-25 22:31:50 -0500593 })
David Tolnaya7d69fc2018-08-26 13:30:24 -0400594 }
595 }
596
597 impl Parse for TypeInfer {
598 fn parse(input: ParseStream) -> Result<Self> {
599 Ok(TypeInfer {
600 underscore_token: input.parse()?,
601 })
602 }
603 }
604
605 impl Parse for TypeTuple {
606 fn parse(input: ParseStream) -> Result<Self> {
607 let content;
608 Ok(TypeTuple {
609 paren_token: parenthesized!(content in input),
David Tolnay60484fe2018-08-30 18:43:04 -0700610 elems: content.parse_terminated(Type::parse)?,
David Tolnaya7d69fc2018-08-26 13:30:24 -0400611 })
612 }
613 }
614
615 impl Parse for TypeMacro {
616 fn parse(input: ParseStream) -> Result<Self> {
617 Ok(TypeMacro {
618 mac: input.parse()?,
619 })
620 }
621 }
622
623 impl Parse for TypePath {
624 fn parse(input: ParseStream) -> Result<Self> {
David Tolnay60291082018-08-28 09:54:49 -0700625 let (qself, mut path) = path::parsing::qpath(input, false)?;
David Tolnaya7d69fc2018-08-26 13:30:24 -0400626
David Tolnay60291082018-08-28 09:54:49 -0700627 if path.segments.last().unwrap().value().arguments.is_empty()
David Tolnaya7d69fc2018-08-26 13:30:24 -0400628 && input.peek(token::Paren)
629 {
630 let args: ParenthesizedGenericArguments = input.parse()?;
David Tolnay60291082018-08-28 09:54:49 -0700631 let parenthesized = PathArguments::Parenthesized(args);
David Tolnaya7d69fc2018-08-26 13:30:24 -0400632 path.segments.last_mut().unwrap().value_mut().arguments = parenthesized;
633 }
634
635 Ok(TypePath {
636 qself: qself,
637 path: path,
638 })
639 }
David Tolnay7d38c7e2017-12-25 22:31:50 -0500640 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700641
Geoffry Songac02b182018-05-19 22:11:31 -0700642 impl ReturnType {
David Tolnaya7d69fc2018-08-26 13:30:24 -0400643 pub fn without_plus(input: ParseStream) -> Result<Self> {
644 Self::parse(input, false)
645 }
Geoffry Songac02b182018-05-19 22:11:31 -0700646
David Tolnaya7d69fc2018-08-26 13:30:24 -0400647 pub fn parse(input: ParseStream, allow_plus: bool) -> Result<Self> {
648 if input.peek(Token![->]) {
649 let arrow = input.parse()?;
650 let ty = ambig_ty(input, allow_plus)?;
651 Ok(ReturnType::Type(arrow, Box::new(ty)))
652 } else {
653 Ok(ReturnType::Default)
654 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800655 }
Alex Crichton954046c2017-05-30 21:49:42 -0700656 }
657
David Tolnaya7d69fc2018-08-26 13:30:24 -0400658 impl Parse for ReturnType {
659 fn parse(input: ParseStream) -> Result<Self> {
660 Self::parse(input, true)
661 }
662 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800663
David Tolnaya7d69fc2018-08-26 13:30:24 -0400664 impl Parse for TypeTraitObject {
665 fn parse(input: ParseStream) -> Result<Self> {
666 Self::parse(input, true)
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800667 }
David Tolnay0a169d42017-12-29 17:57:29 -0500668 }
669
David Tolnaye39b0702018-01-09 17:57:31 -0800670 fn at_least_one_type(bounds: &Punctuated<TypeParamBound, Token![+]>) -> bool {
671 for bound in bounds {
672 if let TypeParamBound::Trait(_) = *bound {
673 return true;
674 }
675 }
676 false
677 }
678
David Tolnay7d38c7e2017-12-25 22:31:50 -0500679 impl TypeTraitObject {
David Tolnaya7d69fc2018-08-26 13:30:24 -0400680 pub fn without_plus(input: ParseStream) -> Result<Self> {
681 Self::parse(input, false)
682 }
David Tolnay0a169d42017-12-29 17:57:29 -0500683
David Tolnay7d38c7e2017-12-25 22:31:50 -0500684 // Only allow multiple trait references if allow_plus is true.
David Tolnaya7d69fc2018-08-26 13:30:24 -0400685 pub fn parse(input: ParseStream, allow_plus: bool) -> Result<Self> {
686 Ok(TypeTraitObject {
687 dyn_token: input.parse()?,
688 bounds: {
David Tolnayf5ebc192018-08-30 18:23:46 -0700689 let mut bounds = Punctuated::new();
690 if allow_plus {
691 loop {
692 bounds.push_value(input.parse()?);
693 if !input.peek(Token![+]) {
694 break;
695 }
696 bounds.push_punct(input.parse()?);
David Tolnay1b210d12018-10-27 22:07:00 -0700697 if input.peek(Token![>]) {
698 break;
699 }
David Tolnayf5ebc192018-08-30 18:23:46 -0700700 }
David Tolnaya7d69fc2018-08-26 13:30:24 -0400701 } else {
David Tolnaya7d69fc2018-08-26 13:30:24 -0400702 bounds.push_value(input.parse()?);
David Tolnayf5ebc192018-08-30 18:23:46 -0700703 }
David Tolnaya7d69fc2018-08-26 13:30:24 -0400704 // Just lifetimes like `'a + 'b` is not a TraitObject.
705 if !at_least_one_type(&bounds) {
706 return Err(input.error("expected at least one type"));
707 }
David Tolnayf2cfd722017-12-31 18:02:51 -0500708 bounds
David Tolnaya7d69fc2018-08-26 13:30:24 -0400709 },
David Tolnay7d38c7e2017-12-25 22:31:50 -0500710 })
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800711 }
Alex Crichton954046c2017-05-30 21:49:42 -0700712 }
David Tolnayb79ee962016-09-04 09:39:20 -0700713
David Tolnaya7d69fc2018-08-26 13:30:24 -0400714 impl Parse for TypeImplTrait {
715 fn parse(input: ParseStream) -> Result<Self> {
716 Ok(TypeImplTrait {
717 impl_token: input.parse()?,
718 // NOTE: rust-lang/rust#34511 includes discussion about whether
719 // or not + should be allowed in ImplTrait directly without ().
David Tolnayf5ebc192018-08-30 18:23:46 -0700720 bounds: {
721 let mut bounds = Punctuated::new();
722 loop {
723 bounds.push_value(input.parse()?);
724 if !input.peek(Token![+]) {
725 break;
726 }
727 bounds.push_punct(input.parse()?);
728 }
729 bounds
730 },
Michael Layzell93c36282017-06-04 20:43:14 -0400731 })
David Tolnay79777332018-01-07 10:04:42 -0800732 }
Michael Layzell93c36282017-06-04 20:43:14 -0400733 }
734
David Tolnaya7d69fc2018-08-26 13:30:24 -0400735 impl Parse for TypeGroup {
736 fn parse(input: ParseStream) -> Result<Self> {
David Tolnay10951d52018-08-31 10:27:39 -0700737 let group = private::parse_group(input)?;
David Tolnaya7d69fc2018-08-26 13:30:24 -0400738 Ok(TypeGroup {
David Tolnayf57f76f2018-08-31 10:23:17 -0700739 group_token: group.token,
740 elem: group.content.parse()?,
David Tolnaya7d69fc2018-08-26 13:30:24 -0400741 })
742 }
743 }
David Tolnay79777332018-01-07 10:04:42 -0800744
David Tolnaya7d69fc2018-08-26 13:30:24 -0400745 impl Parse for TypeParen {
746 fn parse(input: ParseStream) -> Result<Self> {
747 Self::parse(input, false)
David Tolnay79777332018-01-07 10:04:42 -0800748 }
David Tolnay0a169d42017-12-29 17:57:29 -0500749 }
750
751 impl TypeParen {
David Tolnaya7d69fc2018-08-26 13:30:24 -0400752 fn parse(input: ParseStream, allow_plus: bool) -> Result<Self> {
753 let content;
754 Ok(TypeParen {
755 paren_token: parenthesized!(content in input),
756 elem: Box::new(ambig_ty(&content, allow_plus)?),
Michael Layzell92639a52017-06-01 00:07:44 -0400757 })
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800758 }
Alex Crichton954046c2017-05-30 21:49:42 -0700759 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700760
David Tolnaya7d69fc2018-08-26 13:30:24 -0400761 impl Parse for BareFnArg {
762 fn parse(input: ParseStream) -> Result<Self> {
763 Ok(BareFnArg {
764 name: {
765 if (input.peek(Ident) || input.peek(Token![_]))
766 && !input.peek2(Token![::])
767 && input.peek2(Token![:])
768 {
769 let name: BareFnArgName = input.parse()?;
770 let colon: Token![:] = input.parse()?;
771 Some((name, colon))
772 } else {
773 None
774 }
775 },
776 ty: input.parse()?,
777 })
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800778 }
Alex Crichton23a15f62017-08-28 12:34:23 -0700779 }
780
David Tolnaya7d69fc2018-08-26 13:30:24 -0400781 impl Parse for BareFnArgName {
782 fn parse(input: ParseStream) -> Result<Self> {
783 let lookahead = input.lookahead1();
784 if lookahead.peek(Ident) {
785 input.parse().map(BareFnArgName::Named)
786 } else if lookahead.peek(Token![_]) {
787 input.parse().map(BareFnArgName::Wild)
788 } else {
789 Err(lookahead.error())
790 }
791 }
792 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800793
David Tolnaya7d69fc2018-08-26 13:30:24 -0400794 impl Parse for Abi {
795 fn parse(input: ParseStream) -> Result<Self> {
796 Ok(Abi {
797 extern_token: input.parse()?,
798 name: input.parse()?,
799 })
800 }
801 }
802
803 impl Parse for Option<Abi> {
804 fn parse(input: ParseStream) -> Result<Self> {
805 if input.peek(Token![extern]) {
806 input.parse().map(Some)
807 } else {
808 Ok(None)
809 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800810 }
Alex Crichton954046c2017-05-30 21:49:42 -0700811 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700812}
David Tolnay87d0b442016-09-04 11:52:12 -0700813
814#[cfg(feature = "printing")]
815mod printing {
816 use super::*;
David Tolnay64023912018-08-31 09:51:12 -0700817
Alex Crichtona74a1c82018-05-16 10:20:44 -0700818 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -0700819 use quote::ToTokens;
David Tolnay87d0b442016-09-04 11:52:12 -0700820
David Tolnay64023912018-08-31 09:51:12 -0700821 use print::TokensOrDefault;
822
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800823 impl ToTokens for TypeSlice {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700824 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700825 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500826 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700827 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700828 }
829 }
830
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800831 impl ToTokens for TypeArray {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700832 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700833 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500834 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700835 self.semi_token.to_tokens(tokens);
David Tolnayeadbda32017-12-29 02:33:47 -0500836 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700837 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700838 }
839 }
840
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800841 impl ToTokens for TypePtr {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700842 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700843 self.star_token.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500844 match self.mutability {
David Tolnay24237fb2017-12-29 02:15:26 -0500845 Some(ref tok) => tok.to_tokens(tokens),
846 None => {
Alex Crichton259ee532017-07-14 06:51:02 -0700847 TokensOrDefault(&self.const_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400848 }
849 }
David Tolnay136aaa32017-12-29 02:37:36 -0500850 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700851 }
852 }
853
David Tolnay0a89b4d2017-11-13 00:55:45 -0800854 impl ToTokens for TypeReference {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700855 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700856 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700857 self.lifetime.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500858 self.mutability.to_tokens(tokens);
859 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700860 }
861 }
862
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800863 impl ToTokens for TypeBareFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700864 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaybe7a9592017-12-29 02:39:53 -0500865 self.lifetimes.to_tokens(tokens);
866 self.unsafety.to_tokens(tokens);
867 self.abi.to_tokens(tokens);
868 self.fn_token.to_tokens(tokens);
869 self.paren_token.surround(tokens, |tokens| {
870 self.inputs.to_tokens(tokens);
871 if let Some(ref variadic) = self.variadic {
872 if !self.inputs.empty_or_trailing() {
David Tolnay7ac699c2018-08-24 14:00:58 -0400873 let span = variadic.spans[0];
874 Token![,](span).to_tokens(tokens);
David Tolnaybe7a9592017-12-29 02:39:53 -0500875 }
876 variadic.to_tokens(tokens);
877 }
878 });
879 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700880 }
881 }
882
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800883 impl ToTokens for TypeNever {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700884 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700885 self.bang_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700886 }
887 }
888
David Tolnay05362582017-12-26 01:33:57 -0500889 impl ToTokens for TypeTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700890 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700891 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500892 self.elems.to_tokens(tokens);
David Tolnaydb402062018-03-31 22:23:30 +0200893 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700894 }
895 }
896
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800897 impl ToTokens for TypePath {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700898 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay12f3b6f2018-09-01 16:10:53 -0700899 private::print_path(tokens, &self.qself, &self.path);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700900 }
901 }
902
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800903 impl ToTokens for TypeTraitObject {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700904 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye45b59f2017-12-25 18:44:49 -0500905 self.dyn_token.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700906 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700907 }
908 }
909
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800910 impl ToTokens for TypeImplTrait {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700911 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700912 self.impl_token.to_tokens(tokens);
913 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700914 }
915 }
916
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800917 impl ToTokens for TypeGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700918 fn to_tokens(&self, tokens: &mut TokenStream) {
Michael Layzell93c36282017-06-04 20:43:14 -0400919 self.group_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500920 self.elem.to_tokens(tokens);
Michael Layzell93c36282017-06-04 20:43:14 -0400921 });
922 }
923 }
924
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800925 impl ToTokens for TypeParen {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700926 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700927 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500928 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700929 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700930 }
931 }
932
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800933 impl ToTokens for TypeInfer {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700934 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700935 self.underscore_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700936 }
937 }
938
David Tolnay323279a2017-12-29 11:26:32 -0500939 impl ToTokens for TypeMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700940 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay323279a2017-12-29 11:26:32 -0500941 self.mac.to_tokens(tokens);
942 }
943 }
944
David Tolnay2ae520a2017-12-29 11:19:50 -0500945 impl ToTokens for TypeVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700946 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -0500947 self.tts.to_tokens(tokens);
948 }
949 }
950
David Tolnayf93b90d2017-11-11 19:21:26 -0800951 impl ToTokens for ReturnType {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700952 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700953 match *self {
David Tolnayf93b90d2017-11-11 19:21:26 -0800954 ReturnType::Default => {}
David Tolnay4a3f59a2017-12-28 21:21:12 -0500955 ReturnType::Type(ref arrow, ref ty) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700956 arrow.to_tokens(tokens);
957 ty.to_tokens(tokens);
958 }
David Tolnay87d0b442016-09-04 11:52:12 -0700959 }
960 }
961 }
962
David Tolnay62f374c2016-10-02 13:37:00 -0700963 impl ToTokens for BareFnArg {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700964 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700965 if let Some((ref name, ref colon)) = self.name {
David Tolnay62f374c2016-10-02 13:37:00 -0700966 name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700967 colon.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700968 }
969 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700970 }
971 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700972
Alex Crichton23a15f62017-08-28 12:34:23 -0700973 impl ToTokens for BareFnArgName {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700974 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton23a15f62017-08-28 12:34:23 -0700975 match *self {
976 BareFnArgName::Named(ref t) => t.to_tokens(tokens),
977 BareFnArgName::Wild(ref t) => t.to_tokens(tokens),
978 }
979 }
980 }
981
David Tolnayb8d8ef52016-10-29 14:30:08 -0700982 impl ToTokens for Abi {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700983 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700984 self.extern_token.to_tokens(tokens);
David Tolnayd5125762017-12-29 02:42:17 -0500985 self.name.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -0700986 }
987 }
David Tolnay87d0b442016-09-04 11:52:12 -0700988}