blob: 693d71c7d4929161550c0daf2ddec94d18f52f41 [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 Tolnay056de302018-01-05 14:29:05 -08009use super::*;
David Tolnay94d2b792018-04-29 12:26:10 -070010use punctuated::Punctuated;
David Tolnay056de302018-01-05 14:29:05 -080011
12ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -080013 /// A path at which a named item is exported: `std::collections::HashMap`.
David Tolnay461d98e2018-01-07 11:07:19 -080014 ///
15 /// *This type is available if Syn is built with the `"derive"` or `"full"`
16 /// feature.*
David Tolnay056de302018-01-05 14:29:05 -080017 pub struct Path {
David Tolnay056de302018-01-05 14:29:05 -080018 pub leading_colon: Option<Token![::]>,
David Tolnay056de302018-01-05 14:29:05 -080019 pub segments: Punctuated<PathSegment, Token![::]>,
20 }
21}
22
David Tolnay056de302018-01-05 14:29:05 -080023impl<T> From<T> for Path
24where
25 T: Into<PathSegment>,
26{
27 fn from(segment: T) -> Self {
28 let mut path = Path {
29 leading_colon: None,
30 segments: Punctuated::new(),
31 };
David Tolnay56080682018-01-06 14:01:52 -080032 path.segments.push_value(segment.into());
David Tolnay056de302018-01-05 14:29:05 -080033 path
34 }
35}
36
37ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -080038 /// A segment of a path together with any path arguments on that segment.
David Tolnay461d98e2018-01-07 11:07:19 -080039 ///
40 /// *This type is available if Syn is built with the `"derive"` or `"full"`
41 /// feature.*
David Tolnay056de302018-01-05 14:29:05 -080042 pub struct PathSegment {
David Tolnay056de302018-01-05 14:29:05 -080043 pub ident: Ident,
David Tolnay056de302018-01-05 14:29:05 -080044 pub arguments: PathArguments,
45 }
46}
47
48impl<T> From<T> for PathSegment
49where
50 T: Into<Ident>,
51{
52 fn from(ident: T) -> Self {
53 PathSegment {
54 ident: ident.into(),
55 arguments: PathArguments::None,
56 }
57 }
58}
59
60ast_enum! {
David Tolnayc0435192018-01-07 11:46:08 -080061 /// Angle bracketed or parenthesized arguments of a path segment.
David Tolnay461d98e2018-01-07 11:07:19 -080062 ///
63 /// *This type is available if Syn is built with the `"derive"` or `"full"`
64 /// feature.*
David Tolnayc0435192018-01-07 11:46:08 -080065 ///
66 /// ## Angle bracketed
67 ///
68 /// The `<'a, T>` in `std::slice::iter<'a, T>`.
69 ///
70 /// ## Parenthesized
71 ///
72 /// The `(A, B) -> C` in `Fn(A, B) -> C`.
David Tolnay056de302018-01-05 14:29:05 -080073 pub enum PathArguments {
74 None,
David Tolnaye826d812018-01-06 23:59:39 -080075 /// The `<'a, T>` in `std::slice::iter<'a, T>`.
David Tolnay056de302018-01-05 14:29:05 -080076 AngleBracketed(AngleBracketedGenericArguments),
David Tolnayc0435192018-01-07 11:46:08 -080077 /// The `(A, B) -> C` in `Fn(A, B) -> C`.
David Tolnay056de302018-01-05 14:29:05 -080078 Parenthesized(ParenthesizedGenericArguments),
79 }
80}
81
82impl Default for PathArguments {
83 fn default() -> Self {
84 PathArguments::None
85 }
86}
87
88impl PathArguments {
89 pub fn is_empty(&self) -> bool {
90 match *self {
91 PathArguments::None => true,
92 PathArguments::AngleBracketed(ref bracketed) => bracketed.args.is_empty(),
93 PathArguments::Parenthesized(_) => false,
94 }
95 }
96}
97
98ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -080099 /// An individual generic argument, like `'a`, `T`, or `Item = T`.
David Tolnay461d98e2018-01-07 11:07:19 -0800100 ///
101 /// *This type is available if Syn is built with the `"derive"` or `"full"`
102 /// feature.*
David Tolnay056de302018-01-05 14:29:05 -0800103 pub enum GenericArgument {
David Tolnaye826d812018-01-06 23:59:39 -0800104 /// A lifetime argument.
David Tolnay056de302018-01-05 14:29:05 -0800105 Lifetime(Lifetime),
David Tolnaye826d812018-01-06 23:59:39 -0800106 /// A type argument.
David Tolnay056de302018-01-05 14:29:05 -0800107 Type(Type),
David Tolnayc0435192018-01-07 11:46:08 -0800108 /// A binding (equality constraint) on an associated type: the `Item =
109 /// u8` in `Iterator<Item = u8>`.
David Tolnay056de302018-01-05 14:29:05 -0800110 Binding(Binding),
David Tolnay9d0882a2018-09-01 19:49:14 -0700111 /// An associated type bound: `Iterator<Item: Display>`.
112 Constraint(Constraint),
David Tolnaye826d812018-01-06 23:59:39 -0800113 /// A const expression. Must be inside of a block.
David Tolnay056de302018-01-05 14:29:05 -0800114 ///
115 /// NOTE: Identity expressions are represented as Type arguments, as
116 /// they are indistinguishable syntactically.
117 Const(Expr),
118 }
119}
120
121ast_struct! {
David Tolnaye826d812018-01-06 23:59:39 -0800122 /// Angle bracketed arguments of a path segment: the `<K, V>` in `HashMap<K,
123 /// V>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800124 ///
125 /// *This type is available if Syn is built with the `"derive"` or `"full"`
126 /// feature.*
David Tolnay056de302018-01-05 14:29:05 -0800127 pub struct AngleBracketedGenericArguments {
128 pub colon2_token: Option<Token![::]>,
129 pub lt_token: Token![<],
130 pub args: Punctuated<GenericArgument, Token![,]>,
131 pub gt_token: Token![>],
132 }
133}
134
135ast_struct! {
David Tolnaye826d812018-01-06 23:59:39 -0800136 /// A binding (equality constraint) on an associated type: `Item = u8`.
David Tolnay461d98e2018-01-07 11:07:19 -0800137 ///
138 /// *This type is available if Syn is built with the `"derive"` or `"full"`
139 /// feature.*
David Tolnay056de302018-01-05 14:29:05 -0800140 pub struct Binding {
141 pub ident: Ident,
142 pub eq_token: Token![=],
143 pub ty: Type,
144 }
145}
146
147ast_struct! {
David Tolnay9d0882a2018-09-01 19:49:14 -0700148 /// An associated type bound: `Iterator<Item: Display>`.
149 ///
150 /// *This type is available if Syn is built with the `"derive"` or `"full"`
151 /// feature.*
152 pub struct Constraint {
153 pub ident: Ident,
154 pub colon_token: Token![:],
155 pub bounds: Punctuated<TypeParamBound, Token![+]>,
156 }
157}
158
159ast_struct! {
David Tolnayc0435192018-01-07 11:46:08 -0800160 /// Arguments of a function path segment: the `(A, B) -> C` in `Fn(A,B) ->
161 /// C`.
David Tolnay461d98e2018-01-07 11:07:19 -0800162 ///
163 /// *This type is available if Syn is built with the `"derive"` or `"full"`
164 /// feature.*
David Tolnay056de302018-01-05 14:29:05 -0800165 pub struct ParenthesizedGenericArguments {
166 pub paren_token: token::Paren,
167 /// `(A, B)`
168 pub inputs: Punctuated<Type, Token![,]>,
169 /// `C`
170 pub output: ReturnType,
171 }
172}
173
174ast_struct! {
David Tolnaye826d812018-01-06 23:59:39 -0800175 /// The explicit Self type in a qualified path: the `T` in `<T as
David Tolnay05658502018-01-07 09:56:37 -0800176 /// Display>::fmt`.
David Tolnaye826d812018-01-06 23:59:39 -0800177 ///
178 /// The actual path, including the trait and the associated item, is stored
179 /// separately. The `position` field represents the index of the associated
David Tolnay056de302018-01-05 14:29:05 -0800180 /// item qualified with this Self type.
181 ///
182 /// ```text
183 /// <Vec<T> as a::b::Trait>::AssociatedItem
184 /// ^~~~~~ ~~~~~~~~~~~~~~^
185 /// ty position = 3
186 ///
187 /// <Vec<T>>::AssociatedItem
188 /// ^~~~~~ ^
189 /// ty position = 0
190 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800191 ///
192 /// *This type is available if Syn is built with the `"derive"` or `"full"`
193 /// feature.*
David Tolnay056de302018-01-05 14:29:05 -0800194 pub struct QSelf {
195 pub lt_token: Token![<],
196 pub ty: Box<Type>,
197 pub position: usize,
198 pub as_token: Option<Token![as]>,
199 pub gt_token: Token![>],
200 }
201}
202
203#[cfg(feature = "parsing")]
204pub mod parsing {
205 use super::*;
David Tolnay94d304f2018-08-30 23:43:53 -0700206
David Tolnay310b3262018-08-30 15:33:00 -0700207 #[cfg(feature = "full")]
208 use expr;
David Tolnay94d304f2018-08-30 23:43:53 -0700209 use ext::IdentExt;
David Tolnay73b7ca12018-08-30 21:05:13 -0700210 use parse::{Parse, ParseStream, Result};
David Tolnay056de302018-01-05 14:29:05 -0800211
David Tolnay4fb71232018-08-25 23:14:50 -0400212 impl Parse for Path {
213 fn parse(input: ParseStream) -> Result<Self> {
David Tolnay60291082018-08-28 09:54:49 -0700214 Self::parse_helper(input, false)
David Tolnay056de302018-01-05 14:29:05 -0800215 }
216 }
217
David Tolnay4fb71232018-08-25 23:14:50 -0400218 impl Parse for GenericArgument {
219 fn parse(input: ParseStream) -> Result<Self> {
David Tolnay66cb0c42018-08-31 09:01:30 -0700220 if input.peek(Lifetime) && !input.peek2(Token![+]) {
David Tolnay4fb71232018-08-25 23:14:50 -0400221 return Ok(GenericArgument::Lifetime(input.parse()?));
222 }
David Tolnay056de302018-01-05 14:29:05 -0800223
David Tolnay4fb71232018-08-25 23:14:50 -0400224 if input.peek(Ident) && input.peek2(Token![=]) {
225 return Ok(GenericArgument::Binding(input.parse()?));
226 }
David Tolnay056de302018-01-05 14:29:05 -0800227
David Tolnay4fb71232018-08-25 23:14:50 -0400228 #[cfg(feature = "full")]
229 {
David Tolnay9d0882a2018-09-01 19:49:14 -0700230 if input.peek(Ident) && input.peek2(Token![:]) && !input.peek2(Token![::]) {
231 return Ok(GenericArgument::Constraint(input.parse()?));
232 }
233
David Tolnay4fb71232018-08-25 23:14:50 -0400234 if input.peek(Lit) {
David Tolnay310b3262018-08-30 15:33:00 -0700235 let lit = input.call(expr::parsing::expr_lit)?;
David Tolnay4fb71232018-08-25 23:14:50 -0400236 return Ok(GenericArgument::Const(Expr::Lit(lit)));
237 }
238
239 if input.peek(token::Brace) {
David Tolnay310b3262018-08-30 15:33:00 -0700240 let block = input.call(expr::parsing::expr_block)?;
David Tolnay4fb71232018-08-25 23:14:50 -0400241 return Ok(GenericArgument::Const(Expr::Block(block)));
242 }
243 }
244
David Tolnay8db2d662018-08-30 17:40:59 -0700245 input.parse().map(GenericArgument::Type)
David Tolnay056de302018-01-05 14:29:05 -0800246 }
247 }
248
David Tolnay4fb71232018-08-25 23:14:50 -0400249 impl Parse for AngleBracketedGenericArguments {
250 fn parse(input: ParseStream) -> Result<Self> {
251 Ok(AngleBracketedGenericArguments {
252 colon2_token: input.parse()?,
253 lt_token: input.parse()?,
254 args: {
255 let mut args = Punctuated::new();
256 loop {
257 if input.peek(Token![>]) {
258 break;
259 }
260 let value = input.parse()?;
261 args.push_value(value);
262 if input.peek(Token![>]) {
263 break;
264 }
265 let punct = input.parse()?;
266 args.push_punct(punct);
267 }
268 args
269 },
270 gt_token: input.parse()?,
David Tolnay056de302018-01-05 14:29:05 -0800271 })
David Tolnay056de302018-01-05 14:29:05 -0800272 }
273 }
274
David Tolnay4fb71232018-08-25 23:14:50 -0400275 impl Parse for ParenthesizedGenericArguments {
276 fn parse(input: ParseStream) -> Result<Self> {
277 let content;
278 Ok(ParenthesizedGenericArguments {
279 paren_token: parenthesized!(content in input),
David Tolnayf5ebc192018-08-30 18:23:46 -0700280 inputs: content.parse_terminated(Type::parse)?,
David Tolnaya7d69fc2018-08-26 13:30:24 -0400281 output: input.call(ReturnType::without_plus)?,
David Tolnay056de302018-01-05 14:29:05 -0800282 })
David Tolnay056de302018-01-05 14:29:05 -0800283 }
284 }
285
David Tolnay4fb71232018-08-25 23:14:50 -0400286 impl Parse for PathSegment {
287 fn parse(input: ParseStream) -> Result<Self> {
David Tolnay60291082018-08-28 09:54:49 -0700288 Self::parse_helper(input, false)
289 }
290 }
291
292 impl PathSegment {
293 fn parse_helper(input: ParseStream, expr_style: bool) -> Result<Self> {
David Tolnay4fb71232018-08-25 23:14:50 -0400294 if input.peek(Token![super])
295 || input.peek(Token![self])
296 || input.peek(Token![Self])
297 || input.peek(Token![crate])
298 || input.peek(Token![extern])
299 {
David Tolnay0dea1b92018-08-30 17:47:29 -0700300 let ident = input.call(Ident::parse_any)?;
David Tolnay4fb71232018-08-25 23:14:50 -0400301 return Ok(PathSegment::from(ident));
302 }
303
304 let ident = input.parse()?;
David Tolnay60291082018-08-28 09:54:49 -0700305 if !expr_style && input.peek(Token![<]) && !input.peek(Token![<=])
David Tolnay4fb71232018-08-25 23:14:50 -0400306 || input.peek(Token![::]) && input.peek3(Token![<])
307 {
308 Ok(PathSegment {
David Tolnay056de302018-01-05 14:29:05 -0800309 ident: ident,
David Tolnay4fb71232018-08-25 23:14:50 -0400310 arguments: PathArguments::AngleBracketed(input.parse()?),
David Tolnay056de302018-01-05 14:29:05 -0800311 })
David Tolnay4fb71232018-08-25 23:14:50 -0400312 } else {
313 Ok(PathSegment::from(ident))
314 }
David Tolnay056de302018-01-05 14:29:05 -0800315 }
316 }
317
David Tolnay4fb71232018-08-25 23:14:50 -0400318 impl Parse for Binding {
319 fn parse(input: ParseStream) -> Result<Self> {
320 Ok(Binding {
321 ident: input.parse()?,
322 eq_token: input.parse()?,
David Tolnaya7d69fc2018-08-26 13:30:24 -0400323 ty: input.parse()?,
David Tolnay056de302018-01-05 14:29:05 -0800324 })
David Tolnay056de302018-01-05 14:29:05 -0800325 }
326 }
327
David Tolnay9d0882a2018-09-01 19:49:14 -0700328 #[cfg(feature = "full")]
329 impl Parse for Constraint {
330 fn parse(input: ParseStream) -> Result<Self> {
331 Ok(Constraint {
332 ident: input.parse()?,
333 colon_token: input.parse()?,
334 bounds: {
335 let mut bounds = Punctuated::new();
336 loop {
337 if input.peek(Token![,]) || input.peek(Token![>]) {
338 break;
339 }
340 let value = input.parse()?;
341 bounds.push_value(value);
342 if !input.peek(Token![+]) {
343 break;
344 }
345 let punct = input.parse()?;
346 bounds.push_punct(punct);
347 }
348 bounds
349 },
350 })
351 }
352 }
353
David Tolnay056de302018-01-05 14:29:05 -0800354 impl Path {
David Tolnaybbbd5302018-09-01 16:00:42 -0700355 /// Parse a `Path` containing no path arguments on any of its segments.
356 ///
David Tolnay206edfb2018-09-01 16:02:20 -0700357 /// *This function is available if Syn is built with the `"parsing"`
358 /// feature.*
359 ///
David Tolnaybbbd5302018-09-01 16:00:42 -0700360 /// # Example
361 ///
362 /// ```
363 /// # extern crate syn;
364 /// #
365 /// use syn::{Path, Token};
366 /// use syn::parse::{Parse, ParseStream, Result};
367 ///
368 /// // A simplified single `use` statement like:
369 /// //
370 /// // use std::collections::HashMap;
371 /// //
372 /// // Note that generic parameters are not allowed in a `use` statement
373 /// // so the following must not be accepted.
374 /// //
375 /// // use a::<b>::c;
376 /// struct SingleUse {
377 /// use_token: Token![use],
378 /// path: Path,
379 /// }
380 ///
381 /// impl Parse for SingleUse {
382 /// fn parse(input: ParseStream) -> Result<Self> {
383 /// Ok(SingleUse {
384 /// use_token: input.parse()?,
385 /// path: input.call(Path::parse_mod_style)?,
386 /// })
387 /// }
388 /// }
389 /// #
390 /// # fn main() {}
391 /// ```
David Tolnaya7d69fc2018-08-26 13:30:24 -0400392 pub fn parse_mod_style(input: ParseStream) -> Result<Self> {
393 Ok(Path {
394 leading_colon: input.parse()?,
395 segments: {
396 let mut segments = Punctuated::new();
397 loop {
398 if !input.peek(Ident)
399 && !input.peek(Token![super])
400 && !input.peek(Token![self])
401 && !input.peek(Token![Self])
402 && !input.peek(Token![crate])
403 && !input.peek(Token![extern])
404 {
405 break;
406 }
David Tolnay0dea1b92018-08-30 17:47:29 -0700407 let ident = Ident::parse_any(input)?;
David Tolnaya7d69fc2018-08-26 13:30:24 -0400408 segments.push_value(PathSegment::from(ident));
409 if !input.peek(Token![::]) {
410 break;
411 }
412 let punct = input.parse()?;
413 segments.push_punct(punct);
414 }
415 if segments.is_empty() {
416 return Err(input.error("expected path"));
417 } else if segments.trailing_punct() {
418 return Err(input.error("expected path segment"));
419 }
420 segments
421 },
422 })
423 }
424
David Tolnay60291082018-08-28 09:54:49 -0700425 fn parse_helper(input: ParseStream, expr_style: bool) -> Result<Self> {
426 if input.peek(Token![dyn]) {
427 return Err(input.error("expected path"));
428 }
429
430 Ok(Path {
431 leading_colon: input.parse()?,
432 segments: {
433 let mut segments = Punctuated::new();
434 let value = PathSegment::parse_helper(input, expr_style)?;
435 segments.push_value(value);
436 while input.peek(Token![::]) {
437 let punct: Token![::] = input.parse()?;
438 segments.push_punct(punct);
439 let value = PathSegment::parse_helper(input, expr_style)?;
440 segments.push_value(value);
441 }
442 segments
443 },
444 })
445 }
David Tolnay056de302018-01-05 14:29:05 -0800446 }
447
David Tolnay60291082018-08-28 09:54:49 -0700448 pub fn qpath(input: ParseStream, expr_style: bool) -> Result<(Option<QSelf>, Path)> {
449 if input.peek(Token![<]) {
450 let lt_token: Token![<] = input.parse()?;
451 let this: Type = input.parse()?;
452 let path = if input.peek(Token![as]) {
453 let as_token: Token![as] = input.parse()?;
454 let path: Path = input.parse()?;
455 Some((as_token, path))
456 } else {
457 None
458 };
459 let gt_token: Token![>] = input.parse()?;
460 let colon2_token: Token![::] = input.parse()?;
461 let mut rest = Punctuated::new();
462 loop {
463 let path = PathSegment::parse_helper(input, expr_style)?;
464 rest.push_value(path);
465 if !input.peek(Token![::]) {
466 break;
467 }
468 let punct: Token![::] = input.parse()?;
469 rest.push_punct(punct);
470 }
471 let (position, as_token, path) = match path {
472 Some((as_token, mut path)) => {
473 let pos = path.segments.len();
474 path.segments.push_punct(colon2_token);
475 path.segments.extend(rest.into_pairs());
476 (pos, Some(as_token), path)
477 }
478 None => {
479 let path = Path {
480 leading_colon: Some(colon2_token),
481 segments: rest,
482 };
483 (0, None, path)
484 }
485 };
486 let qself = QSelf {
487 lt_token: lt_token,
488 ty: Box::new(this),
489 position: position,
490 as_token: as_token,
491 gt_token: gt_token,
492 };
493 Ok((Some(qself), path))
494 } else {
495 let path = Path::parse_helper(input, expr_style)?;
496 Ok((None, path))
497 }
498 }
David Tolnay056de302018-01-05 14:29:05 -0800499}
500
501#[cfg(feature = "printing")]
502mod printing {
503 use super::*;
David Tolnay64023912018-08-31 09:51:12 -0700504
Alex Crichtona74a1c82018-05-16 10:20:44 -0700505 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -0700506 use quote::ToTokens;
David Tolnay056de302018-01-05 14:29:05 -0800507
David Tolnay64023912018-08-31 09:51:12 -0700508 use print::TokensOrDefault;
509
David Tolnay056de302018-01-05 14:29:05 -0800510 impl ToTokens for Path {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700511 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay056de302018-01-05 14:29:05 -0800512 self.leading_colon.to_tokens(tokens);
513 self.segments.to_tokens(tokens);
514 }
515 }
516
517 impl ToTokens for PathSegment {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700518 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay056de302018-01-05 14:29:05 -0800519 self.ident.to_tokens(tokens);
520 self.arguments.to_tokens(tokens);
521 }
522 }
523
524 impl ToTokens for PathArguments {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700525 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay056de302018-01-05 14:29:05 -0800526 match *self {
527 PathArguments::None => {}
528 PathArguments::AngleBracketed(ref arguments) => {
529 arguments.to_tokens(tokens);
530 }
531 PathArguments::Parenthesized(ref arguments) => {
532 arguments.to_tokens(tokens);
533 }
534 }
535 }
536 }
537
538 impl ToTokens for GenericArgument {
539 #[cfg_attr(feature = "cargo-clippy", allow(match_same_arms))]
Alex Crichtona74a1c82018-05-16 10:20:44 -0700540 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay056de302018-01-05 14:29:05 -0800541 match *self {
542 GenericArgument::Lifetime(ref lt) => lt.to_tokens(tokens),
543 GenericArgument::Type(ref ty) => ty.to_tokens(tokens),
544 GenericArgument::Binding(ref tb) => tb.to_tokens(tokens),
David Tolnay9d0882a2018-09-01 19:49:14 -0700545 GenericArgument::Constraint(ref tc) => tc.to_tokens(tokens),
David Tolnay056de302018-01-05 14:29:05 -0800546 GenericArgument::Const(ref e) => match *e {
547 Expr::Lit(_) => e.to_tokens(tokens),
548
549 // NOTE: We should probably support parsing blocks with only
550 // expressions in them without the full feature for const
551 // generics.
552 #[cfg(feature = "full")]
553 Expr::Block(_) => e.to_tokens(tokens),
554
555 // ERROR CORRECTION: Add braces to make sure that the
556 // generated code is valid.
557 _ => token::Brace::default().surround(tokens, |tokens| {
558 e.to_tokens(tokens);
559 }),
560 },
561 }
562 }
563 }
564
565 impl ToTokens for AngleBracketedGenericArguments {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700566 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay056de302018-01-05 14:29:05 -0800567 self.colon2_token.to_tokens(tokens);
568 self.lt_token.to_tokens(tokens);
David Tolnay298570b2018-01-11 16:38:36 -0800569
570 // Print lifetimes before types and consts, all before bindings,
571 // regardless of their order in self.args.
572 //
573 // TODO: ordering rules for const arguments vs type arguments have
574 // not been settled yet. https://github.com/rust-lang/rust/issues/44580
575 let mut trailing_or_empty = true;
576 for param in self.args.pairs() {
David Tolnay9d0882a2018-09-01 19:49:14 -0700577 match **param.value() {
578 GenericArgument::Lifetime(_) => {
579 param.to_tokens(tokens);
580 trailing_or_empty = param.punct().is_some();
581 }
582 GenericArgument::Type(_)
583 | GenericArgument::Binding(_)
584 | GenericArgument::Constraint(_)
585 | GenericArgument::Const(_) => {}
David Tolnay298570b2018-01-11 16:38:36 -0800586 }
587 }
588 for param in self.args.pairs() {
589 match **param.value() {
590 GenericArgument::Type(_) | GenericArgument::Const(_) => {
591 if !trailing_or_empty {
592 <Token![,]>::default().to_tokens(tokens);
593 }
594 param.to_tokens(tokens);
595 trailing_or_empty = param.punct().is_some();
596 }
David Tolnay9d0882a2018-09-01 19:49:14 -0700597 GenericArgument::Lifetime(_)
598 | GenericArgument::Binding(_)
599 | GenericArgument::Constraint(_) => {}
David Tolnay298570b2018-01-11 16:38:36 -0800600 }
601 }
602 for param in self.args.pairs() {
David Tolnay9d0882a2018-09-01 19:49:14 -0700603 match **param.value() {
604 GenericArgument::Binding(_) | GenericArgument::Constraint(_) => {
605 if !trailing_or_empty {
606 <Token![,]>::default().to_tokens(tokens);
607 trailing_or_empty = true;
608 }
609 param.to_tokens(tokens);
David Tolnay298570b2018-01-11 16:38:36 -0800610 }
David Tolnay9d0882a2018-09-01 19:49:14 -0700611 GenericArgument::Lifetime(_)
612 | GenericArgument::Type(_)
613 | GenericArgument::Const(_) => {}
David Tolnay298570b2018-01-11 16:38:36 -0800614 }
615 }
616
David Tolnay056de302018-01-05 14:29:05 -0800617 self.gt_token.to_tokens(tokens);
618 }
619 }
620
621 impl ToTokens for Binding {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700622 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay056de302018-01-05 14:29:05 -0800623 self.ident.to_tokens(tokens);
624 self.eq_token.to_tokens(tokens);
625 self.ty.to_tokens(tokens);
626 }
627 }
628
David Tolnay9d0882a2018-09-01 19:49:14 -0700629 impl ToTokens for Constraint {
630 fn to_tokens(&self, tokens: &mut TokenStream) {
631 self.ident.to_tokens(tokens);
632 self.colon_token.to_tokens(tokens);
633 self.bounds.to_tokens(tokens);
634 }
635 }
636
David Tolnay056de302018-01-05 14:29:05 -0800637 impl ToTokens for ParenthesizedGenericArguments {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700638 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay056de302018-01-05 14:29:05 -0800639 self.paren_token.surround(tokens, |tokens| {
640 self.inputs.to_tokens(tokens);
641 });
642 self.output.to_tokens(tokens);
643 }
644 }
David Tolnay05658502018-01-07 09:56:37 -0800645
David Tolnay12f3b6f2018-09-01 16:10:53 -0700646 impl private {
647 pub fn print_path(tokens: &mut TokenStream, qself: &Option<QSelf>, path: &Path) {
648 let qself = match *qself {
David Tolnay05658502018-01-07 09:56:37 -0800649 Some(ref qself) => qself,
David Tolnay12f3b6f2018-09-01 16:10:53 -0700650 None => {
651 path.to_tokens(tokens);
652 return;
653 }
David Tolnay05658502018-01-07 09:56:37 -0800654 };
655 qself.lt_token.to_tokens(tokens);
656 qself.ty.to_tokens(tokens);
657
David Tolnay12f3b6f2018-09-01 16:10:53 -0700658 let pos = if qself.position > 0 && qself.position >= path.segments.len() {
659 path.segments.len() - 1
David Tolnay05658502018-01-07 09:56:37 -0800660 } else {
661 qself.position
662 };
David Tolnay12f3b6f2018-09-01 16:10:53 -0700663 let mut segments = path.segments.pairs();
David Tolnay05658502018-01-07 09:56:37 -0800664 if pos > 0 {
665 TokensOrDefault(&qself.as_token).to_tokens(tokens);
David Tolnay12f3b6f2018-09-01 16:10:53 -0700666 path.leading_colon.to_tokens(tokens);
David Tolnay05658502018-01-07 09:56:37 -0800667 for (i, segment) in segments.by_ref().take(pos).enumerate() {
668 if i + 1 == pos {
669 segment.value().to_tokens(tokens);
670 qself.gt_token.to_tokens(tokens);
671 segment.punct().to_tokens(tokens);
672 } else {
673 segment.to_tokens(tokens);
674 }
675 }
676 } else {
677 qself.gt_token.to_tokens(tokens);
David Tolnay12f3b6f2018-09-01 16:10:53 -0700678 path.leading_colon.to_tokens(tokens);
David Tolnay05658502018-01-07 09:56:37 -0800679 }
680 for segment in segments {
681 segment.to_tokens(tokens);
682 }
683 }
684 }
David Tolnay056de302018-01-05 14:29:05 -0800685}