]> git.lizzy.rs Git - rust.git/blob - src/librustc_ast_pretty/pprust.rs
Update deprecation version to 1.42 for Error::description
[rust.git] / src / librustc_ast_pretty / pprust.rs
1 use crate::pp::Breaks::{Consistent, Inconsistent};
2 use crate::pp::{self, Breaks};
3
4 use rustc_ast::ast::{self, BlockCheckMode, PatKind, RangeEnd, RangeSyntax};
5 use rustc_ast::ast::{Attribute, GenericArg, MacArgs};
6 use rustc_ast::ast::{GenericBound, SelfKind, TraitBoundModifier};
7 use rustc_ast::attr;
8 use rustc_ast::ptr::P;
9 use rustc_ast::token::{self, BinOpToken, DelimToken, Nonterminal, Token, TokenKind};
10 use rustc_ast::tokenstream::{self, TokenStream, TokenTree};
11 use rustc_ast::util::parser::{self, AssocOp, Fixity};
12 use rustc_ast::util::{classify, comments};
13 use rustc_span::edition::Edition;
14 use rustc_span::source_map::{SourceMap, Spanned};
15 use rustc_span::symbol::{kw, sym, IdentPrinter};
16 use rustc_span::{BytePos, FileName, Span};
17
18 use std::borrow::Cow;
19
20 #[cfg(test)]
21 mod tests;
22
23 pub enum MacHeader<'a> {
24     Path(&'a ast::Path),
25     Keyword(&'static str),
26 }
27
28 pub enum AnnNode<'a> {
29     Ident(&'a ast::Ident),
30     Name(&'a ast::Name),
31     Block(&'a ast::Block),
32     Item(&'a ast::Item),
33     SubItem(ast::NodeId),
34     Expr(&'a ast::Expr),
35     Pat(&'a ast::Pat),
36     Crate(&'a ast::Crate),
37 }
38
39 pub trait PpAnn {
40     fn pre(&self, _state: &mut State<'_>, _node: AnnNode<'_>) {}
41     fn post(&self, _state: &mut State<'_>, _node: AnnNode<'_>) {}
42 }
43
44 #[derive(Copy, Clone)]
45 pub struct NoAnn;
46
47 impl PpAnn for NoAnn {}
48
49 pub struct Comments<'a> {
50     sm: &'a SourceMap,
51     comments: Vec<comments::Comment>,
52     current: usize,
53 }
54
55 impl<'a> Comments<'a> {
56     pub fn new(sm: &'a SourceMap, filename: FileName, input: String) -> Comments<'a> {
57         let comments = comments::gather_comments(sm, filename, input);
58         Comments { sm, comments, current: 0 }
59     }
60
61     pub fn next(&self) -> Option<comments::Comment> {
62         self.comments.get(self.current).cloned()
63     }
64
65     pub fn trailing_comment(
66         &mut self,
67         span: rustc_span::Span,
68         next_pos: Option<BytePos>,
69     ) -> Option<comments::Comment> {
70         if let Some(cmnt) = self.next() {
71             if cmnt.style != comments::Trailing {
72                 return None;
73             }
74             let span_line = self.sm.lookup_char_pos(span.hi());
75             let comment_line = self.sm.lookup_char_pos(cmnt.pos);
76             let next = next_pos.unwrap_or_else(|| cmnt.pos + BytePos(1));
77             if span.hi() < cmnt.pos && cmnt.pos < next && span_line.line == comment_line.line {
78                 return Some(cmnt);
79             }
80         }
81
82         None
83     }
84 }
85
86 pub struct State<'a> {
87     pub s: pp::Printer,
88     comments: Option<Comments<'a>>,
89     ann: &'a (dyn PpAnn + 'a),
90     is_expanded: bool,
91 }
92
93 crate const INDENT_UNIT: usize = 4;
94
95 /// Requires you to pass an input filename and reader so that
96 /// it can scan the input text for comments to copy forward.
97 pub fn print_crate<'a>(
98     sm: &'a SourceMap,
99     krate: &ast::Crate,
100     filename: FileName,
101     input: String,
102     ann: &'a dyn PpAnn,
103     is_expanded: bool,
104     edition: Edition,
105     has_injected_crate: bool,
106 ) -> String {
107     let mut s = State {
108         s: pp::mk_printer(),
109         comments: Some(Comments::new(sm, filename, input)),
110         ann,
111         is_expanded,
112     };
113
114     if is_expanded && has_injected_crate {
115         // We need to print `#![no_std]` (and its feature gate) so that
116         // compiling pretty-printed source won't inject libstd again.
117         // However, we don't want these attributes in the AST because
118         // of the feature gate, so we fake them up here.
119
120         // `#![feature(prelude_import)]`
121         let pi_nested = attr::mk_nested_word_item(ast::Ident::with_dummy_span(sym::prelude_import));
122         let list = attr::mk_list_item(ast::Ident::with_dummy_span(sym::feature), vec![pi_nested]);
123         let fake_attr = attr::mk_attr_inner(list);
124         s.print_attribute(&fake_attr);
125
126         // Currently, in Rust 2018 we don't have `extern crate std;` at the crate
127         // root, so this is not needed, and actually breaks things.
128         if edition == Edition::Edition2015 {
129             // `#![no_std]`
130             let no_std_meta = attr::mk_word_item(ast::Ident::with_dummy_span(sym::no_std));
131             let fake_attr = attr::mk_attr_inner(no_std_meta);
132             s.print_attribute(&fake_attr);
133         }
134     }
135
136     s.print_mod(&krate.module, &krate.attrs);
137     s.print_remaining_comments();
138     s.ann.post(&mut s, AnnNode::Crate(krate));
139     s.s.eof()
140 }
141
142 pub fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
143     let mut printer =
144         State { s: pp::mk_printer(), comments: None, ann: &NoAnn, is_expanded: false };
145     f(&mut printer);
146     printer.s.eof()
147 }
148
149 // This makes comma-separated lists look slightly nicer,
150 // and also addresses a specific regression described in issue #63896.
151 fn tt_prepend_space(tt: &TokenTree) -> bool {
152     match tt {
153         TokenTree::Token(token) => match token.kind {
154             token::Comma => false,
155             _ => true,
156         },
157         _ => true,
158     }
159 }
160
161 fn binop_to_string(op: BinOpToken) -> &'static str {
162     match op {
163         token::Plus => "+",
164         token::Minus => "-",
165         token::Star => "*",
166         token::Slash => "/",
167         token::Percent => "%",
168         token::Caret => "^",
169         token::And => "&",
170         token::Or => "|",
171         token::Shl => "<<",
172         token::Shr => ">>",
173     }
174 }
175
176 pub fn literal_to_string(lit: token::Lit) -> String {
177     let token::Lit { kind, symbol, suffix } = lit;
178     let mut out = match kind {
179         token::Byte => format!("b'{}'", symbol),
180         token::Char => format!("'{}'", symbol),
181         token::Str => format!("\"{}\"", symbol),
182         token::StrRaw(n) => {
183             format!("r{delim}\"{string}\"{delim}", delim = "#".repeat(n as usize), string = symbol)
184         }
185         token::ByteStr => format!("b\"{}\"", symbol),
186         token::ByteStrRaw(n) => {
187             format!("br{delim}\"{string}\"{delim}", delim = "#".repeat(n as usize), string = symbol)
188         }
189         token::Integer | token::Float | token::Bool | token::Err => symbol.to_string(),
190     };
191
192     if let Some(suffix) = suffix {
193         out.push_str(&suffix.as_str())
194     }
195
196     out
197 }
198
199 /// Print the token kind precisely, without converting `$crate` into its respective crate name.
200 pub fn token_kind_to_string(tok: &TokenKind) -> String {
201     token_kind_to_string_ext(tok, None)
202 }
203
204 fn token_kind_to_string_ext(tok: &TokenKind, convert_dollar_crate: Option<Span>) -> String {
205     match *tok {
206         token::Eq => "=".to_string(),
207         token::Lt => "<".to_string(),
208         token::Le => "<=".to_string(),
209         token::EqEq => "==".to_string(),
210         token::Ne => "!=".to_string(),
211         token::Ge => ">=".to_string(),
212         token::Gt => ">".to_string(),
213         token::Not => "!".to_string(),
214         token::Tilde => "~".to_string(),
215         token::OrOr => "||".to_string(),
216         token::AndAnd => "&&".to_string(),
217         token::BinOp(op) => binop_to_string(op).to_string(),
218         token::BinOpEq(op) => format!("{}=", binop_to_string(op)),
219
220         /* Structural symbols */
221         token::At => "@".to_string(),
222         token::Dot => ".".to_string(),
223         token::DotDot => "..".to_string(),
224         token::DotDotDot => "...".to_string(),
225         token::DotDotEq => "..=".to_string(),
226         token::Comma => ",".to_string(),
227         token::Semi => ";".to_string(),
228         token::Colon => ":".to_string(),
229         token::ModSep => "::".to_string(),
230         token::RArrow => "->".to_string(),
231         token::LArrow => "<-".to_string(),
232         token::FatArrow => "=>".to_string(),
233         token::OpenDelim(token::Paren) => "(".to_string(),
234         token::CloseDelim(token::Paren) => ")".to_string(),
235         token::OpenDelim(token::Bracket) => "[".to_string(),
236         token::CloseDelim(token::Bracket) => "]".to_string(),
237         token::OpenDelim(token::Brace) => "{".to_string(),
238         token::CloseDelim(token::Brace) => "}".to_string(),
239         token::OpenDelim(token::NoDelim) | token::CloseDelim(token::NoDelim) => " ".to_string(),
240         token::Pound => "#".to_string(),
241         token::Dollar => "$".to_string(),
242         token::Question => "?".to_string(),
243         token::SingleQuote => "'".to_string(),
244
245         /* Literals */
246         token::Literal(lit) => literal_to_string(lit),
247
248         /* Name components */
249         token::Ident(s, is_raw) => IdentPrinter::new(s, is_raw, convert_dollar_crate).to_string(),
250         token::Lifetime(s) => s.to_string(),
251
252         /* Other */
253         token::DocComment(s) => s.to_string(),
254         token::Eof => "<eof>".to_string(),
255         token::Whitespace => " ".to_string(),
256         token::Comment => "/* */".to_string(),
257         token::Shebang(s) => format!("/* shebang: {}*/", s),
258         token::Unknown(s) => s.to_string(),
259
260         token::Interpolated(ref nt) => nonterminal_to_string(nt),
261     }
262 }
263
264 /// Print the token precisely, without converting `$crate` into its respective crate name.
265 pub fn token_to_string(token: &Token) -> String {
266     token_to_string_ext(token, false)
267 }
268
269 fn token_to_string_ext(token: &Token, convert_dollar_crate: bool) -> String {
270     let convert_dollar_crate = convert_dollar_crate.then_some(token.span);
271     token_kind_to_string_ext(&token.kind, convert_dollar_crate)
272 }
273
274 pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
275     match *nt {
276         token::NtExpr(ref e) => expr_to_string(e),
277         token::NtMeta(ref e) => attr_item_to_string(e),
278         token::NtTy(ref e) => ty_to_string(e),
279         token::NtPath(ref e) => path_to_string(e),
280         token::NtItem(ref e) => item_to_string(e),
281         token::NtBlock(ref e) => block_to_string(e),
282         token::NtStmt(ref e) => stmt_to_string(e),
283         token::NtPat(ref e) => pat_to_string(e),
284         token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(e, is_raw).to_string(),
285         token::NtLifetime(e) => e.to_string(),
286         token::NtLiteral(ref e) => expr_to_string(e),
287         token::NtTT(ref tree) => tt_to_string(tree.clone()),
288         token::NtVis(ref e) => vis_to_string(e),
289     }
290 }
291
292 pub fn ty_to_string(ty: &ast::Ty) -> String {
293     to_string(|s| s.print_type(ty))
294 }
295
296 pub fn bounds_to_string(bounds: &[ast::GenericBound]) -> String {
297     to_string(|s| s.print_type_bounds("", bounds))
298 }
299
300 pub fn pat_to_string(pat: &ast::Pat) -> String {
301     to_string(|s| s.print_pat(pat))
302 }
303
304 pub fn expr_to_string(e: &ast::Expr) -> String {
305     to_string(|s| s.print_expr(e))
306 }
307
308 pub fn tt_to_string(tt: tokenstream::TokenTree) -> String {
309     to_string(|s| s.print_tt(tt, false))
310 }
311
312 pub fn tts_to_string(tokens: TokenStream) -> String {
313     to_string(|s| s.print_tts(tokens, false))
314 }
315
316 pub fn stmt_to_string(stmt: &ast::Stmt) -> String {
317     to_string(|s| s.print_stmt(stmt))
318 }
319
320 pub fn item_to_string(i: &ast::Item) -> String {
321     to_string(|s| s.print_item(i))
322 }
323
324 pub fn generic_params_to_string(generic_params: &[ast::GenericParam]) -> String {
325     to_string(|s| s.print_generic_params(generic_params))
326 }
327
328 pub fn path_to_string(p: &ast::Path) -> String {
329     to_string(|s| s.print_path(p, false, 0))
330 }
331
332 pub fn path_segment_to_string(p: &ast::PathSegment) -> String {
333     to_string(|s| s.print_path_segment(p, false))
334 }
335
336 pub fn vis_to_string(v: &ast::Visibility) -> String {
337     to_string(|s| s.print_visibility(v))
338 }
339
340 fn block_to_string(blk: &ast::Block) -> String {
341     to_string(|s| {
342         // Containing cbox, will be closed by `print_block` at `}`.
343         s.cbox(INDENT_UNIT);
344         // Head-ibox, will be closed by `print_block` after `{`.
345         s.ibox(0);
346         s.print_block(blk)
347     })
348 }
349
350 pub fn meta_list_item_to_string(li: &ast::NestedMetaItem) -> String {
351     to_string(|s| s.print_meta_list_item(li))
352 }
353
354 fn attr_item_to_string(ai: &ast::AttrItem) -> String {
355     to_string(|s| s.print_attr_item(ai, ai.path.span))
356 }
357
358 pub fn attribute_to_string(attr: &ast::Attribute) -> String {
359     to_string(|s| s.print_attribute(attr))
360 }
361
362 pub fn param_to_string(arg: &ast::Param) -> String {
363     to_string(|s| s.print_param(arg, false))
364 }
365
366 fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String {
367     format!("{}{}", to_string(|s| s.print_visibility(vis)), s)
368 }
369
370 impl std::ops::Deref for State<'_> {
371     type Target = pp::Printer;
372     fn deref(&self) -> &Self::Target {
373         &self.s
374     }
375 }
376
377 impl std::ops::DerefMut for State<'_> {
378     fn deref_mut(&mut self) -> &mut Self::Target {
379         &mut self.s
380     }
381 }
382
383 pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::DerefMut {
384     fn comments(&mut self) -> &mut Option<Comments<'a>>;
385     fn print_ident(&mut self, ident: ast::Ident);
386     fn print_generic_args(&mut self, args: &ast::GenericArgs, colons_before_params: bool);
387
388     fn strsep<T, F>(
389         &mut self,
390         sep: &'static str,
391         space_before: bool,
392         b: Breaks,
393         elts: &[T],
394         mut op: F,
395     ) where
396         F: FnMut(&mut Self, &T),
397     {
398         self.rbox(0, b);
399         if let Some((first, rest)) = elts.split_first() {
400             op(self, first);
401             for elt in rest {
402                 if space_before {
403                     self.space();
404                 }
405                 self.word_space(sep);
406                 op(self, elt);
407             }
408         }
409         self.end();
410     }
411
412     fn commasep<T, F>(&mut self, b: Breaks, elts: &[T], op: F)
413     where
414         F: FnMut(&mut Self, &T),
415     {
416         self.strsep(",", false, b, elts, op)
417     }
418
419     fn maybe_print_comment(&mut self, pos: BytePos) {
420         while let Some(ref cmnt) = self.next_comment() {
421             if cmnt.pos < pos {
422                 self.print_comment(cmnt);
423             } else {
424                 break;
425             }
426         }
427     }
428
429     fn print_comment(&mut self, cmnt: &comments::Comment) {
430         match cmnt.style {
431             comments::Mixed => {
432                 assert_eq!(cmnt.lines.len(), 1);
433                 self.zerobreak();
434                 self.word(cmnt.lines[0].clone());
435                 self.zerobreak()
436             }
437             comments::Isolated => {
438                 self.hardbreak_if_not_bol();
439                 for line in &cmnt.lines {
440                     // Don't print empty lines because they will end up as trailing
441                     // whitespace.
442                     if !line.is_empty() {
443                         self.word(line.clone());
444                     }
445                     self.hardbreak();
446                 }
447             }
448             comments::Trailing => {
449                 if !self.is_beginning_of_line() {
450                     self.word(" ");
451                 }
452                 if cmnt.lines.len() == 1 {
453                     self.word(cmnt.lines[0].clone());
454                     self.hardbreak()
455                 } else {
456                     self.ibox(0);
457                     for line in &cmnt.lines {
458                         if !line.is_empty() {
459                             self.word(line.clone());
460                         }
461                         self.hardbreak();
462                     }
463                     self.end();
464                 }
465             }
466             comments::BlankLine => {
467                 // We need to do at least one, possibly two hardbreaks.
468                 let twice = match self.last_token() {
469                     pp::Token::String(s) => ";" == s,
470                     pp::Token::Begin(_) => true,
471                     pp::Token::End => true,
472                     _ => false,
473                 };
474                 if twice {
475                     self.hardbreak();
476                 }
477                 self.hardbreak();
478             }
479         }
480         if let Some(cmnts) = self.comments() {
481             cmnts.current += 1;
482         }
483     }
484
485     fn next_comment(&mut self) -> Option<comments::Comment> {
486         self.comments().as_mut().and_then(|c| c.next())
487     }
488
489     fn print_literal(&mut self, lit: &ast::Lit) {
490         self.maybe_print_comment(lit.span.lo());
491         self.word(lit.token.to_string())
492     }
493
494     fn print_string(&mut self, st: &str, style: ast::StrStyle) {
495         let st = match style {
496             ast::StrStyle::Cooked => (format!("\"{}\"", st.escape_debug())),
497             ast::StrStyle::Raw(n) => {
498                 format!("r{delim}\"{string}\"{delim}", delim = "#".repeat(n as usize), string = st)
499             }
500         };
501         self.word(st)
502     }
503
504     fn print_inner_attributes(&mut self, attrs: &[ast::Attribute]) {
505         self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, true)
506     }
507
508     fn print_inner_attributes_no_trailing_hardbreak(&mut self, attrs: &[ast::Attribute]) {
509         self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, false)
510     }
511
512     fn print_outer_attributes(&mut self, attrs: &[ast::Attribute]) {
513         self.print_either_attributes(attrs, ast::AttrStyle::Outer, false, true)
514     }
515
516     fn print_inner_attributes_inline(&mut self, attrs: &[ast::Attribute]) {
517         self.print_either_attributes(attrs, ast::AttrStyle::Inner, true, true)
518     }
519
520     fn print_outer_attributes_inline(&mut self, attrs: &[ast::Attribute]) {
521         self.print_either_attributes(attrs, ast::AttrStyle::Outer, true, true)
522     }
523
524     fn print_either_attributes(
525         &mut self,
526         attrs: &[ast::Attribute],
527         kind: ast::AttrStyle,
528         is_inline: bool,
529         trailing_hardbreak: bool,
530     ) {
531         let mut count = 0;
532         for attr in attrs {
533             if attr.style == kind {
534                 self.print_attribute_inline(attr, is_inline);
535                 if is_inline {
536                     self.nbsp();
537                 }
538                 count += 1;
539             }
540         }
541         if count > 0 && trailing_hardbreak && !is_inline {
542             self.hardbreak_if_not_bol();
543         }
544     }
545
546     fn print_attribute(&mut self, attr: &ast::Attribute) {
547         self.print_attribute_inline(attr, false)
548     }
549
550     fn print_attribute_inline(&mut self, attr: &ast::Attribute, is_inline: bool) {
551         if !is_inline {
552             self.hardbreak_if_not_bol();
553         }
554         self.maybe_print_comment(attr.span.lo());
555         match attr.kind {
556             ast::AttrKind::Normal(ref item) => {
557                 match attr.style {
558                     ast::AttrStyle::Inner => self.word("#!["),
559                     ast::AttrStyle::Outer => self.word("#["),
560                 }
561                 self.print_attr_item(&item, attr.span);
562                 self.word("]");
563             }
564             ast::AttrKind::DocComment(comment) => {
565                 self.word(comment.to_string());
566                 self.hardbreak()
567             }
568         }
569     }
570
571     fn print_attr_item(&mut self, item: &ast::AttrItem, span: Span) {
572         self.ibox(0);
573         match &item.args {
574             MacArgs::Delimited(_, delim, tokens) => self.print_mac_common(
575                 Some(MacHeader::Path(&item.path)),
576                 false,
577                 None,
578                 delim.to_token(),
579                 tokens.clone(),
580                 true,
581                 span,
582             ),
583             MacArgs::Empty | MacArgs::Eq(..) => {
584                 self.print_path(&item.path, false, 0);
585                 if let MacArgs::Eq(_, tokens) = &item.args {
586                     self.space();
587                     self.word_space("=");
588                     self.print_tts(tokens.clone(), true);
589                 }
590             }
591         }
592         self.end();
593     }
594
595     fn print_meta_list_item(&mut self, item: &ast::NestedMetaItem) {
596         match item {
597             ast::NestedMetaItem::MetaItem(ref mi) => self.print_meta_item(mi),
598             ast::NestedMetaItem::Literal(ref lit) => self.print_literal(lit),
599         }
600     }
601
602     fn print_meta_item(&mut self, item: &ast::MetaItem) {
603         self.ibox(INDENT_UNIT);
604         match item.kind {
605             ast::MetaItemKind::Word => self.print_path(&item.path, false, 0),
606             ast::MetaItemKind::NameValue(ref value) => {
607                 self.print_path(&item.path, false, 0);
608                 self.space();
609                 self.word_space("=");
610                 self.print_literal(value);
611             }
612             ast::MetaItemKind::List(ref items) => {
613                 self.print_path(&item.path, false, 0);
614                 self.popen();
615                 self.commasep(Consistent, &items[..], |s, i| s.print_meta_list_item(i));
616                 self.pclose();
617             }
618         }
619         self.end();
620     }
621
622     /// This doesn't deserve to be called "pretty" printing, but it should be
623     /// meaning-preserving. A quick hack that might help would be to look at the
624     /// spans embedded in the TTs to decide where to put spaces and newlines.
625     /// But it'd be better to parse these according to the grammar of the
626     /// appropriate macro, transcribe back into the grammar we just parsed from,
627     /// and then pretty-print the resulting AST nodes (so, e.g., we print
628     /// expression arguments as expressions). It can be done! I think.
629     fn print_tt(&mut self, tt: tokenstream::TokenTree, convert_dollar_crate: bool) {
630         match tt {
631             TokenTree::Token(ref token) => {
632                 self.word(token_to_string_ext(&token, convert_dollar_crate));
633                 match token.kind {
634                     token::DocComment(..) => self.hardbreak(),
635                     _ => {}
636                 }
637             }
638             TokenTree::Delimited(dspan, delim, tts) => {
639                 self.print_mac_common(
640                     None,
641                     false,
642                     None,
643                     delim,
644                     tts,
645                     convert_dollar_crate,
646                     dspan.entire(),
647                 );
648             }
649         }
650     }
651
652     fn print_tts(&mut self, tts: tokenstream::TokenStream, convert_dollar_crate: bool) {
653         for (i, tt) in tts.into_trees().enumerate() {
654             if i != 0 && tt_prepend_space(&tt) {
655                 self.space();
656             }
657             self.print_tt(tt, convert_dollar_crate);
658         }
659     }
660
661     fn print_mac_common(
662         &mut self,
663         header: Option<MacHeader<'_>>,
664         has_bang: bool,
665         ident: Option<ast::Ident>,
666         delim: DelimToken,
667         tts: TokenStream,
668         convert_dollar_crate: bool,
669         span: Span,
670     ) {
671         if delim == DelimToken::Brace {
672             self.cbox(INDENT_UNIT);
673         }
674         match header {
675             Some(MacHeader::Path(path)) => self.print_path(path, false, 0),
676             Some(MacHeader::Keyword(kw)) => self.word(kw),
677             None => {}
678         }
679         if has_bang {
680             self.word("!");
681         }
682         if let Some(ident) = ident {
683             self.nbsp();
684             self.print_ident(ident);
685         }
686         match delim {
687             DelimToken::Brace => {
688                 if header.is_some() || has_bang || ident.is_some() {
689                     self.nbsp();
690                 }
691                 self.word("{");
692                 if !tts.is_empty() {
693                     self.space();
694                 }
695             }
696             _ => self.word(token_kind_to_string(&token::OpenDelim(delim))),
697         }
698         self.ibox(0);
699         self.print_tts(tts, convert_dollar_crate);
700         self.end();
701         match delim {
702             DelimToken::Brace => self.bclose(span),
703             _ => self.word(token_kind_to_string(&token::CloseDelim(delim))),
704         }
705     }
706
707     fn print_path(&mut self, path: &ast::Path, colons_before_params: bool, depth: usize) {
708         self.maybe_print_comment(path.span.lo());
709
710         for (i, segment) in path.segments[..path.segments.len() - depth].iter().enumerate() {
711             if i > 0 {
712                 self.word("::")
713             }
714             self.print_path_segment(segment, colons_before_params);
715         }
716     }
717
718     fn print_path_segment(&mut self, segment: &ast::PathSegment, colons_before_params: bool) {
719         if segment.ident.name != kw::PathRoot {
720             self.print_ident(segment.ident);
721             if let Some(ref args) = segment.args {
722                 self.print_generic_args(args, colons_before_params);
723             }
724         }
725     }
726
727     fn head<S: Into<Cow<'static, str>>>(&mut self, w: S) {
728         let w = w.into();
729         // Outer-box is consistent.
730         self.cbox(INDENT_UNIT);
731         // Head-box is inconsistent.
732         self.ibox(w.len() + 1);
733         // Keyword that starts the head.
734         if !w.is_empty() {
735             self.word_nbsp(w);
736         }
737     }
738
739     fn bopen(&mut self) {
740         self.word("{");
741         self.end(); // Close the head-box.
742     }
743
744     fn bclose_maybe_open(&mut self, span: rustc_span::Span, close_box: bool) {
745         self.maybe_print_comment(span.hi());
746         self.break_offset_if_not_bol(1, -(INDENT_UNIT as isize));
747         self.word("}");
748         if close_box {
749             self.end(); // Close the outer-box.
750         }
751     }
752
753     fn bclose(&mut self, span: rustc_span::Span) {
754         self.bclose_maybe_open(span, true)
755     }
756
757     fn break_offset_if_not_bol(&mut self, n: usize, off: isize) {
758         if !self.is_beginning_of_line() {
759             self.break_offset(n, off)
760         } else {
761             if off != 0 && self.last_token().is_hardbreak_tok() {
762                 // We do something pretty sketchy here: tuck the nonzero
763                 // offset-adjustment we were going to deposit along with the
764                 // break into the previous hardbreak.
765                 self.replace_last_token(pp::Printer::hardbreak_tok_offset(off));
766             }
767         }
768     }
769 }
770
771 impl<'a> PrintState<'a> for State<'a> {
772     fn comments(&mut self) -> &mut Option<Comments<'a>> {
773         &mut self.comments
774     }
775
776     fn print_ident(&mut self, ident: ast::Ident) {
777         self.s.word(IdentPrinter::for_ast_ident(ident, ident.is_raw_guess()).to_string());
778         self.ann.post(self, AnnNode::Ident(&ident))
779     }
780
781     fn print_generic_args(&mut self, args: &ast::GenericArgs, colons_before_params: bool) {
782         if colons_before_params {
783             self.s.word("::")
784         }
785
786         match *args {
787             ast::GenericArgs::AngleBracketed(ref data) => {
788                 self.s.word("<");
789
790                 self.commasep(Inconsistent, &data.args, |s, generic_arg| {
791                     s.print_generic_arg(generic_arg)
792                 });
793
794                 let mut comma = !data.args.is_empty();
795
796                 for constraint in data.constraints.iter() {
797                     if comma {
798                         self.word_space(",")
799                     }
800                     self.print_ident(constraint.ident);
801                     self.s.space();
802                     match constraint.kind {
803                         ast::AssocTyConstraintKind::Equality { ref ty } => {
804                             self.word_space("=");
805                             self.print_type(ty);
806                         }
807                         ast::AssocTyConstraintKind::Bound { ref bounds } => {
808                             self.print_type_bounds(":", &*bounds);
809                         }
810                     }
811                     comma = true;
812                 }
813
814                 self.s.word(">")
815             }
816
817             ast::GenericArgs::Parenthesized(ref data) => {
818                 self.s.word("(");
819                 self.commasep(Inconsistent, &data.inputs, |s, ty| s.print_type(ty));
820                 self.s.word(")");
821                 self.print_fn_ret_ty(&data.output);
822             }
823         }
824     }
825 }
826
827 impl<'a> State<'a> {
828     // Synthesizes a comment that was not textually present in the original source
829     // file.
830     pub fn synth_comment(&mut self, text: String) {
831         self.s.word("/*");
832         self.s.space();
833         self.s.word(text);
834         self.s.space();
835         self.s.word("*/")
836     }
837
838     crate fn commasep_cmnt<T, F, G>(&mut self, b: Breaks, elts: &[T], mut op: F, mut get_span: G)
839     where
840         F: FnMut(&mut State<'_>, &T),
841         G: FnMut(&T) -> rustc_span::Span,
842     {
843         self.rbox(0, b);
844         let len = elts.len();
845         let mut i = 0;
846         for elt in elts {
847             self.maybe_print_comment(get_span(elt).hi());
848             op(self, elt);
849             i += 1;
850             if i < len {
851                 self.s.word(",");
852                 self.maybe_print_trailing_comment(get_span(elt), Some(get_span(&elts[i]).hi()));
853                 self.space_if_not_bol();
854             }
855         }
856         self.end();
857     }
858
859     crate fn commasep_exprs(&mut self, b: Breaks, exprs: &[P<ast::Expr>]) {
860         self.commasep_cmnt(b, exprs, |s, e| s.print_expr(e), |e| e.span)
861     }
862
863     pub fn print_mod(&mut self, _mod: &ast::Mod, attrs: &[ast::Attribute]) {
864         self.print_inner_attributes(attrs);
865         for item in &_mod.items {
866             self.print_item(item);
867         }
868     }
869
870     crate fn print_foreign_mod(&mut self, nmod: &ast::ForeignMod, attrs: &[Attribute]) {
871         self.print_inner_attributes(attrs);
872         for item in &nmod.items {
873             self.print_foreign_item(item);
874         }
875     }
876
877     pub fn print_opt_lifetime(&mut self, lifetime: &Option<ast::Lifetime>) {
878         if let Some(lt) = *lifetime {
879             self.print_lifetime(lt);
880             self.nbsp();
881         }
882     }
883
884     crate fn print_generic_arg(&mut self, generic_arg: &GenericArg) {
885         match generic_arg {
886             GenericArg::Lifetime(lt) => self.print_lifetime(*lt),
887             GenericArg::Type(ty) => self.print_type(ty),
888             GenericArg::Const(ct) => self.print_expr(&ct.value),
889         }
890     }
891
892     pub fn print_type(&mut self, ty: &ast::Ty) {
893         self.maybe_print_comment(ty.span.lo());
894         self.ibox(0);
895         match ty.kind {
896             ast::TyKind::Slice(ref ty) => {
897                 self.s.word("[");
898                 self.print_type(ty);
899                 self.s.word("]");
900             }
901             ast::TyKind::Ptr(ref mt) => {
902                 self.s.word("*");
903                 self.print_mt(mt, true);
904             }
905             ast::TyKind::Rptr(ref lifetime, ref mt) => {
906                 self.s.word("&");
907                 self.print_opt_lifetime(lifetime);
908                 self.print_mt(mt, false);
909             }
910             ast::TyKind::Never => {
911                 self.s.word("!");
912             }
913             ast::TyKind::Tup(ref elts) => {
914                 self.popen();
915                 self.commasep(Inconsistent, &elts[..], |s, ty| s.print_type(ty));
916                 if elts.len() == 1 {
917                     self.s.word(",");
918                 }
919                 self.pclose();
920             }
921             ast::TyKind::Paren(ref typ) => {
922                 self.popen();
923                 self.print_type(typ);
924                 self.pclose();
925             }
926             ast::TyKind::BareFn(ref f) => {
927                 self.print_ty_fn(f.ext, f.unsafety, &f.decl, None, &f.generic_params);
928             }
929             ast::TyKind::Path(None, ref path) => {
930                 self.print_path(path, false, 0);
931             }
932             ast::TyKind::Path(Some(ref qself), ref path) => self.print_qpath(path, qself, false),
933             ast::TyKind::TraitObject(ref bounds, syntax) => {
934                 let prefix = if syntax == ast::TraitObjectSyntax::Dyn { "dyn" } else { "" };
935                 self.print_type_bounds(prefix, &bounds[..]);
936             }
937             ast::TyKind::ImplTrait(_, ref bounds) => {
938                 self.print_type_bounds("impl", &bounds[..]);
939             }
940             ast::TyKind::Array(ref ty, ref length) => {
941                 self.s.word("[");
942                 self.print_type(ty);
943                 self.s.word("; ");
944                 self.print_expr(&length.value);
945                 self.s.word("]");
946             }
947             ast::TyKind::Typeof(ref e) => {
948                 self.s.word("typeof(");
949                 self.print_expr(&e.value);
950                 self.s.word(")");
951             }
952             ast::TyKind::Infer => {
953                 self.s.word("_");
954             }
955             ast::TyKind::Err => {
956                 self.popen();
957                 self.s.word("/*ERROR*/");
958                 self.pclose();
959             }
960             ast::TyKind::ImplicitSelf => {
961                 self.s.word("Self");
962             }
963             ast::TyKind::Mac(ref m) => {
964                 self.print_mac(m);
965             }
966             ast::TyKind::CVarArgs => {
967                 self.s.word("...");
968             }
969         }
970         self.end();
971     }
972
973     crate fn print_foreign_item(&mut self, item: &ast::ForeignItem) {
974         let ast::Item { id, span, ident, attrs, kind, vis, tokens: _ } = item;
975         self.print_nested_item_kind(*id, *span, *ident, attrs, kind, vis);
976     }
977
978     fn print_nested_item_kind(
979         &mut self,
980         id: ast::NodeId,
981         span: Span,
982         ident: ast::Ident,
983         attrs: &[Attribute],
984         kind: &ast::AssocItemKind,
985         vis: &ast::Visibility,
986     ) {
987         self.ann.pre(self, AnnNode::SubItem(id));
988         self.hardbreak_if_not_bol();
989         self.maybe_print_comment(span.lo());
990         self.print_outer_attributes(attrs);
991         match kind {
992             ast::ForeignItemKind::Fn(def, sig, gen, body) => {
993                 self.print_fn_full(sig, ident, gen, vis, *def, body.as_deref(), attrs);
994             }
995             ast::ForeignItemKind::Const(def, ty, body) => {
996                 self.print_item_const(ident, None, ty, body.as_deref(), vis, *def);
997             }
998             ast::ForeignItemKind::Static(ty, mutbl, body) => {
999                 let def = ast::Defaultness::Final;
1000                 self.print_item_const(ident, Some(*mutbl), ty, body.as_deref(), vis, def);
1001             }
1002             ast::ForeignItemKind::TyAlias(def, generics, bounds, ty) => {
1003                 self.print_associated_type(ident, generics, bounds, ty.as_deref(), vis, *def);
1004             }
1005             ast::ForeignItemKind::Macro(m) => {
1006                 self.print_mac(m);
1007                 if m.args.need_semicolon() {
1008                     self.s.word(";");
1009                 }
1010             }
1011         }
1012         self.ann.post(self, AnnNode::SubItem(id))
1013     }
1014
1015     fn print_item_const(
1016         &mut self,
1017         ident: ast::Ident,
1018         mutbl: Option<ast::Mutability>,
1019         ty: &ast::Ty,
1020         body: Option<&ast::Expr>,
1021         vis: &ast::Visibility,
1022         defaultness: ast::Defaultness,
1023     ) {
1024         self.head("");
1025         self.print_visibility(vis);
1026         self.print_defaultness(defaultness);
1027         let leading = match mutbl {
1028             None => "const",
1029             Some(ast::Mutability::Not) => "static",
1030             Some(ast::Mutability::Mut) => "static mut",
1031         };
1032         self.word_space(leading);
1033         self.print_ident(ident);
1034         self.word_space(":");
1035         self.print_type(ty);
1036         self.s.space();
1037         self.end(); // end the head-ibox
1038         if let Some(body) = body {
1039             self.word_space("=");
1040             self.print_expr(body);
1041         }
1042         self.s.word(";");
1043         self.end(); // end the outer cbox
1044     }
1045
1046     fn print_associated_type(
1047         &mut self,
1048         ident: ast::Ident,
1049         generics: &ast::Generics,
1050         bounds: &ast::GenericBounds,
1051         ty: Option<&ast::Ty>,
1052         vis: &ast::Visibility,
1053         defaultness: ast::Defaultness,
1054     ) {
1055         self.head("");
1056         self.print_visibility(vis);
1057         self.print_defaultness(defaultness);
1058         self.word_space("type");
1059         self.print_ident(ident);
1060         self.print_generic_params(&generics.params);
1061         self.print_type_bounds(":", bounds);
1062         self.print_where_clause(&generics.where_clause);
1063         if let Some(ty) = ty {
1064             self.s.space();
1065             self.word_space("=");
1066             self.print_type(ty);
1067         }
1068         self.s.word(";");
1069         self.end(); // end inner head-block
1070         self.end(); // end outer head-block
1071     }
1072
1073     /// Pretty-prints an item.
1074     crate fn print_item(&mut self, item: &ast::Item) {
1075         self.hardbreak_if_not_bol();
1076         self.maybe_print_comment(item.span.lo());
1077         self.print_outer_attributes(&item.attrs);
1078         self.ann.pre(self, AnnNode::Item(item));
1079         match item.kind {
1080             ast::ItemKind::ExternCrate(orig_name) => {
1081                 self.head(visibility_qualified(&item.vis, "extern crate"));
1082                 if let Some(orig_name) = orig_name {
1083                     self.print_name(orig_name);
1084                     self.s.space();
1085                     self.s.word("as");
1086                     self.s.space();
1087                 }
1088                 self.print_ident(item.ident);
1089                 self.s.word(";");
1090                 self.end(); // end inner head-block
1091                 self.end(); // end outer head-block
1092             }
1093             ast::ItemKind::Use(ref tree) => {
1094                 self.head(visibility_qualified(&item.vis, "use"));
1095                 self.print_use_tree(tree);
1096                 self.s.word(";");
1097                 self.end(); // end inner head-block
1098                 self.end(); // end outer head-block
1099             }
1100             ast::ItemKind::Static(ref ty, mutbl, ref body) => {
1101                 let def = ast::Defaultness::Final;
1102                 self.print_item_const(item.ident, Some(mutbl), ty, body.as_deref(), &item.vis, def);
1103             }
1104             ast::ItemKind::Const(def, ref ty, ref body) => {
1105                 self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis, def);
1106             }
1107             ast::ItemKind::Fn(def, ref sig, ref gen, ref body) => {
1108                 let body = body.as_deref();
1109                 self.print_fn_full(sig, item.ident, gen, &item.vis, def, body, &item.attrs);
1110             }
1111             ast::ItemKind::Mod(ref _mod) => {
1112                 self.head(visibility_qualified(&item.vis, "mod"));
1113                 self.print_ident(item.ident);
1114
1115                 if _mod.inline || self.is_expanded {
1116                     self.nbsp();
1117                     self.bopen();
1118                     self.print_mod(_mod, &item.attrs);
1119                     self.bclose(item.span);
1120                 } else {
1121                     self.s.word(";");
1122                     self.end(); // end inner head-block
1123                     self.end(); // end outer head-block
1124                 }
1125             }
1126             ast::ItemKind::ForeignMod(ref nmod) => {
1127                 self.head("extern");
1128                 if let Some(abi) = nmod.abi {
1129                     self.print_literal(&abi.as_lit());
1130                     self.nbsp();
1131                 }
1132                 self.bopen();
1133                 self.print_foreign_mod(nmod, &item.attrs);
1134                 self.bclose(item.span);
1135             }
1136             ast::ItemKind::GlobalAsm(ref ga) => {
1137                 self.head(visibility_qualified(&item.vis, "global_asm!"));
1138                 self.s.word(ga.asm.to_string());
1139                 self.end();
1140             }
1141             ast::ItemKind::TyAlias(def, ref generics, ref bounds, ref ty) => {
1142                 let ty = ty.as_deref();
1143                 self.print_associated_type(item.ident, generics, bounds, ty, &item.vis, def);
1144             }
1145             ast::ItemKind::Enum(ref enum_definition, ref params) => {
1146                 self.print_enum_def(enum_definition, params, item.ident, item.span, &item.vis);
1147             }
1148             ast::ItemKind::Struct(ref struct_def, ref generics) => {
1149                 self.head(visibility_qualified(&item.vis, "struct"));
1150                 self.print_struct(struct_def, generics, item.ident, item.span, true);
1151             }
1152             ast::ItemKind::Union(ref struct_def, ref generics) => {
1153                 self.head(visibility_qualified(&item.vis, "union"));
1154                 self.print_struct(struct_def, generics, item.ident, item.span, true);
1155             }
1156             ast::ItemKind::Impl {
1157                 unsafety,
1158                 polarity,
1159                 defaultness,
1160                 constness,
1161                 ref generics,
1162                 ref of_trait,
1163                 ref self_ty,
1164                 ref items,
1165             } => {
1166                 self.head("");
1167                 self.print_visibility(&item.vis);
1168                 self.print_defaultness(defaultness);
1169                 self.print_unsafety(unsafety);
1170                 self.word_nbsp("impl");
1171                 self.print_constness(constness);
1172
1173                 if !generics.params.is_empty() {
1174                     self.print_generic_params(&generics.params);
1175                     self.s.space();
1176                 }
1177
1178                 if polarity == ast::ImplPolarity::Negative {
1179                     self.s.word("!");
1180                 }
1181
1182                 if let Some(ref t) = *of_trait {
1183                     self.print_trait_ref(t);
1184                     self.s.space();
1185                     self.word_space("for");
1186                 }
1187
1188                 self.print_type(self_ty);
1189                 self.print_where_clause(&generics.where_clause);
1190
1191                 self.s.space();
1192                 self.bopen();
1193                 self.print_inner_attributes(&item.attrs);
1194                 for impl_item in items {
1195                     self.print_assoc_item(impl_item);
1196                 }
1197                 self.bclose(item.span);
1198             }
1199             ast::ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref trait_items) => {
1200                 self.head("");
1201                 self.print_visibility(&item.vis);
1202                 self.print_unsafety(unsafety);
1203                 self.print_is_auto(is_auto);
1204                 self.word_nbsp("trait");
1205                 self.print_ident(item.ident);
1206                 self.print_generic_params(&generics.params);
1207                 let mut real_bounds = Vec::with_capacity(bounds.len());
1208                 for b in bounds.iter() {
1209                     if let GenericBound::Trait(ref ptr, ast::TraitBoundModifier::Maybe) = *b {
1210                         self.s.space();
1211                         self.word_space("for ?");
1212                         self.print_trait_ref(&ptr.trait_ref);
1213                     } else {
1214                         real_bounds.push(b.clone());
1215                     }
1216                 }
1217                 self.print_type_bounds(":", &real_bounds[..]);
1218                 self.print_where_clause(&generics.where_clause);
1219                 self.s.word(" ");
1220                 self.bopen();
1221                 self.print_inner_attributes(&item.attrs);
1222                 for trait_item in trait_items {
1223                     self.print_assoc_item(trait_item);
1224                 }
1225                 self.bclose(item.span);
1226             }
1227             ast::ItemKind::TraitAlias(ref generics, ref bounds) => {
1228                 self.head("");
1229                 self.print_visibility(&item.vis);
1230                 self.word_nbsp("trait");
1231                 self.print_ident(item.ident);
1232                 self.print_generic_params(&generics.params);
1233                 let mut real_bounds = Vec::with_capacity(bounds.len());
1234                 // FIXME(durka) this seems to be some quite outdated syntax
1235                 for b in bounds.iter() {
1236                     if let GenericBound::Trait(ref ptr, ast::TraitBoundModifier::Maybe) = *b {
1237                         self.s.space();
1238                         self.word_space("for ?");
1239                         self.print_trait_ref(&ptr.trait_ref);
1240                     } else {
1241                         real_bounds.push(b.clone());
1242                     }
1243                 }
1244                 self.nbsp();
1245                 self.print_type_bounds("=", &real_bounds[..]);
1246                 self.print_where_clause(&generics.where_clause);
1247                 self.s.word(";");
1248             }
1249             ast::ItemKind::Mac(ref mac) => {
1250                 self.print_mac(mac);
1251                 if mac.args.need_semicolon() {
1252                     self.s.word(";");
1253                 }
1254             }
1255             ast::ItemKind::MacroDef(ref macro_def) => {
1256                 let (kw, has_bang) = if macro_def.legacy {
1257                     ("macro_rules", true)
1258                 } else {
1259                     self.print_visibility(&item.vis);
1260                     ("macro", false)
1261                 };
1262                 self.print_mac_common(
1263                     Some(MacHeader::Keyword(kw)),
1264                     has_bang,
1265                     Some(item.ident),
1266                     macro_def.body.delim(),
1267                     macro_def.body.inner_tokens(),
1268                     true,
1269                     item.span,
1270                 );
1271             }
1272         }
1273         self.ann.post(self, AnnNode::Item(item))
1274     }
1275
1276     fn print_trait_ref(&mut self, t: &ast::TraitRef) {
1277         self.print_path(&t.path, false, 0)
1278     }
1279
1280     fn print_formal_generic_params(&mut self, generic_params: &[ast::GenericParam]) {
1281         if !generic_params.is_empty() {
1282             self.s.word("for");
1283             self.print_generic_params(generic_params);
1284             self.nbsp();
1285         }
1286     }
1287
1288     fn print_poly_trait_ref(&mut self, t: &ast::PolyTraitRef) {
1289         self.print_formal_generic_params(&t.bound_generic_params);
1290         self.print_trait_ref(&t.trait_ref)
1291     }
1292
1293     crate fn print_enum_def(
1294         &mut self,
1295         enum_definition: &ast::EnumDef,
1296         generics: &ast::Generics,
1297         ident: ast::Ident,
1298         span: rustc_span::Span,
1299         visibility: &ast::Visibility,
1300     ) {
1301         self.head(visibility_qualified(visibility, "enum"));
1302         self.print_ident(ident);
1303         self.print_generic_params(&generics.params);
1304         self.print_where_clause(&generics.where_clause);
1305         self.s.space();
1306         self.print_variants(&enum_definition.variants, span)
1307     }
1308
1309     crate fn print_variants(&mut self, variants: &[ast::Variant], span: rustc_span::Span) {
1310         self.bopen();
1311         for v in variants {
1312             self.space_if_not_bol();
1313             self.maybe_print_comment(v.span.lo());
1314             self.print_outer_attributes(&v.attrs);
1315             self.ibox(INDENT_UNIT);
1316             self.print_variant(v);
1317             self.s.word(",");
1318             self.end();
1319             self.maybe_print_trailing_comment(v.span, None);
1320         }
1321         self.bclose(span)
1322     }
1323
1324     crate fn print_visibility(&mut self, vis: &ast::Visibility) {
1325         match vis.node {
1326             ast::VisibilityKind::Public => self.word_nbsp("pub"),
1327             ast::VisibilityKind::Crate(sugar) => match sugar {
1328                 ast::CrateSugar::PubCrate => self.word_nbsp("pub(crate)"),
1329                 ast::CrateSugar::JustCrate => self.word_nbsp("crate"),
1330             },
1331             ast::VisibilityKind::Restricted { ref path, .. } => {
1332                 let path = to_string(|s| s.print_path(path, false, 0));
1333                 if path == "self" || path == "super" {
1334                     self.word_nbsp(format!("pub({})", path))
1335                 } else {
1336                     self.word_nbsp(format!("pub(in {})", path))
1337                 }
1338             }
1339             ast::VisibilityKind::Inherited => {}
1340         }
1341     }
1342
1343     crate fn print_defaultness(&mut self, defaultness: ast::Defaultness) {
1344         if let ast::Defaultness::Default(_) = defaultness {
1345             self.word_nbsp("default");
1346         }
1347     }
1348
1349     crate fn print_struct(
1350         &mut self,
1351         struct_def: &ast::VariantData,
1352         generics: &ast::Generics,
1353         ident: ast::Ident,
1354         span: rustc_span::Span,
1355         print_finalizer: bool,
1356     ) {
1357         self.print_ident(ident);
1358         self.print_generic_params(&generics.params);
1359         match struct_def {
1360             ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
1361                 if let ast::VariantData::Tuple(..) = struct_def {
1362                     self.popen();
1363                     self.commasep(Inconsistent, struct_def.fields(), |s, field| {
1364                         s.maybe_print_comment(field.span.lo());
1365                         s.print_outer_attributes(&field.attrs);
1366                         s.print_visibility(&field.vis);
1367                         s.print_type(&field.ty)
1368                     });
1369                     self.pclose();
1370                 }
1371                 self.print_where_clause(&generics.where_clause);
1372                 if print_finalizer {
1373                     self.s.word(";");
1374                 }
1375                 self.end();
1376                 self.end(); // Close the outer-box.
1377             }
1378             ast::VariantData::Struct(..) => {
1379                 self.print_where_clause(&generics.where_clause);
1380                 self.nbsp();
1381                 self.bopen();
1382                 self.hardbreak_if_not_bol();
1383
1384                 for field in struct_def.fields() {
1385                     self.hardbreak_if_not_bol();
1386                     self.maybe_print_comment(field.span.lo());
1387                     self.print_outer_attributes(&field.attrs);
1388                     self.print_visibility(&field.vis);
1389                     self.print_ident(field.ident.unwrap());
1390                     self.word_nbsp(":");
1391                     self.print_type(&field.ty);
1392                     self.s.word(",");
1393                 }
1394
1395                 self.bclose(span)
1396             }
1397         }
1398     }
1399
1400     crate fn print_variant(&mut self, v: &ast::Variant) {
1401         self.head("");
1402         self.print_visibility(&v.vis);
1403         let generics = ast::Generics::default();
1404         self.print_struct(&v.data, &generics, v.ident, v.span, false);
1405         match v.disr_expr {
1406             Some(ref d) => {
1407                 self.s.space();
1408                 self.word_space("=");
1409                 self.print_expr(&d.value)
1410             }
1411             _ => {}
1412         }
1413     }
1414
1415     crate fn print_assoc_item(&mut self, item: &ast::AssocItem) {
1416         let ast::Item { id, span, ident, attrs, kind, vis, tokens: _ } = item;
1417         self.print_nested_item_kind(*id, *span, *ident, attrs, kind, vis);
1418     }
1419
1420     crate fn print_stmt(&mut self, st: &ast::Stmt) {
1421         self.maybe_print_comment(st.span.lo());
1422         match st.kind {
1423             ast::StmtKind::Local(ref loc) => {
1424                 self.print_outer_attributes(&loc.attrs);
1425                 self.space_if_not_bol();
1426                 self.ibox(INDENT_UNIT);
1427                 self.word_nbsp("let");
1428
1429                 self.ibox(INDENT_UNIT);
1430                 self.print_local_decl(loc);
1431                 self.end();
1432                 if let Some(ref init) = loc.init {
1433                     self.nbsp();
1434                     self.word_space("=");
1435                     self.print_expr(init);
1436                 }
1437                 self.s.word(";");
1438                 self.end();
1439             }
1440             ast::StmtKind::Item(ref item) => self.print_item(item),
1441             ast::StmtKind::Expr(ref expr) => {
1442                 self.space_if_not_bol();
1443                 self.print_expr_outer_attr_style(expr, false);
1444                 if classify::expr_requires_semi_to_be_stmt(expr) {
1445                     self.s.word(";");
1446                 }
1447             }
1448             ast::StmtKind::Semi(ref expr) => {
1449                 self.space_if_not_bol();
1450                 self.print_expr_outer_attr_style(expr, false);
1451                 self.s.word(";");
1452             }
1453             ast::StmtKind::Empty => {
1454                 self.space_if_not_bol();
1455                 self.s.word(";");
1456             }
1457             ast::StmtKind::Mac(ref mac) => {
1458                 let (ref mac, style, ref attrs) = **mac;
1459                 self.space_if_not_bol();
1460                 self.print_outer_attributes(attrs);
1461                 self.print_mac(mac);
1462                 if style == ast::MacStmtStyle::Semicolon {
1463                     self.s.word(";");
1464                 }
1465             }
1466         }
1467         self.maybe_print_trailing_comment(st.span, None)
1468     }
1469
1470     crate fn print_block(&mut self, blk: &ast::Block) {
1471         self.print_block_with_attrs(blk, &[])
1472     }
1473
1474     crate fn print_block_unclosed_indent(&mut self, blk: &ast::Block) {
1475         self.print_block_maybe_unclosed(blk, &[], false)
1476     }
1477
1478     crate fn print_block_with_attrs(&mut self, blk: &ast::Block, attrs: &[ast::Attribute]) {
1479         self.print_block_maybe_unclosed(blk, attrs, true)
1480     }
1481
1482     crate fn print_block_maybe_unclosed(
1483         &mut self,
1484         blk: &ast::Block,
1485         attrs: &[ast::Attribute],
1486         close_box: bool,
1487     ) {
1488         match blk.rules {
1489             BlockCheckMode::Unsafe(..) => self.word_space("unsafe"),
1490             BlockCheckMode::Default => (),
1491         }
1492         self.maybe_print_comment(blk.span.lo());
1493         self.ann.pre(self, AnnNode::Block(blk));
1494         self.bopen();
1495
1496         self.print_inner_attributes(attrs);
1497
1498         for (i, st) in blk.stmts.iter().enumerate() {
1499             match st.kind {
1500                 ast::StmtKind::Expr(ref expr) if i == blk.stmts.len() - 1 => {
1501                     self.maybe_print_comment(st.span.lo());
1502                     self.space_if_not_bol();
1503                     self.print_expr_outer_attr_style(expr, false);
1504                     self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi()));
1505                 }
1506                 _ => self.print_stmt(st),
1507             }
1508         }
1509
1510         self.bclose_maybe_open(blk.span, close_box);
1511         self.ann.post(self, AnnNode::Block(blk))
1512     }
1513
1514     /// Print a `let pat = scrutinee` expression.
1515     crate fn print_let(&mut self, pat: &ast::Pat, scrutinee: &ast::Expr) {
1516         self.s.word("let ");
1517
1518         self.print_pat(pat);
1519         self.s.space();
1520
1521         self.word_space("=");
1522         self.print_expr_cond_paren(
1523             scrutinee,
1524             Self::cond_needs_par(scrutinee)
1525                 || parser::needs_par_as_let_scrutinee(scrutinee.precedence().order()),
1526         )
1527     }
1528
1529     fn print_else(&mut self, els: Option<&ast::Expr>) {
1530         if let Some(_else) = els {
1531             match _else.kind {
1532                 // Another `else if` block.
1533                 ast::ExprKind::If(ref i, ref then, ref e) => {
1534                     self.cbox(INDENT_UNIT - 1);
1535                     self.ibox(0);
1536                     self.s.word(" else if ");
1537                     self.print_expr_as_cond(i);
1538                     self.s.space();
1539                     self.print_block(then);
1540                     self.print_else(e.as_deref())
1541                 }
1542                 // Final `else` block.
1543                 ast::ExprKind::Block(ref b, _) => {
1544                     self.cbox(INDENT_UNIT - 1);
1545                     self.ibox(0);
1546                     self.s.word(" else ");
1547                     self.print_block(b)
1548                 }
1549                 // Constraints would be great here!
1550                 _ => {
1551                     panic!("print_if saw if with weird alternative");
1552                 }
1553             }
1554         }
1555     }
1556
1557     crate fn print_if(&mut self, test: &ast::Expr, blk: &ast::Block, elseopt: Option<&ast::Expr>) {
1558         self.head("if");
1559
1560         self.print_expr_as_cond(test);
1561         self.s.space();
1562
1563         self.print_block(blk);
1564         self.print_else(elseopt)
1565     }
1566
1567     crate fn print_mac(&mut self, m: &ast::Mac) {
1568         self.print_mac_common(
1569             Some(MacHeader::Path(&m.path)),
1570             true,
1571             None,
1572             m.args.delim(),
1573             m.args.inner_tokens(),
1574             true,
1575             m.span(),
1576         );
1577     }
1578
1579     fn print_call_post(&mut self, args: &[P<ast::Expr>]) {
1580         self.popen();
1581         self.commasep_exprs(Inconsistent, args);
1582         self.pclose()
1583     }
1584
1585     crate fn print_expr_maybe_paren(&mut self, expr: &ast::Expr, prec: i8) {
1586         self.print_expr_cond_paren(expr, expr.precedence().order() < prec)
1587     }
1588
1589     /// Prints an expr using syntax that's acceptable in a condition position, such as the `cond` in
1590     /// `if cond { ... }`.
1591     crate fn print_expr_as_cond(&mut self, expr: &ast::Expr) {
1592         self.print_expr_cond_paren(expr, Self::cond_needs_par(expr))
1593     }
1594
1595     /// Does `expr` need parenthesis when printed in a condition position?
1596     fn cond_needs_par(expr: &ast::Expr) -> bool {
1597         match expr.kind {
1598             // These cases need parens due to the parse error observed in #26461: `if return {}`
1599             // parses as the erroneous construct `if (return {})`, not `if (return) {}`.
1600             ast::ExprKind::Closure(..) | ast::ExprKind::Ret(..) | ast::ExprKind::Break(..) => true,
1601
1602             _ => parser::contains_exterior_struct_lit(expr),
1603         }
1604     }
1605
1606     /// Prints `expr` or `(expr)` when `needs_par` holds.
1607     fn print_expr_cond_paren(&mut self, expr: &ast::Expr, needs_par: bool) {
1608         if needs_par {
1609             self.popen();
1610         }
1611         self.print_expr(expr);
1612         if needs_par {
1613             self.pclose();
1614         }
1615     }
1616
1617     fn print_expr_vec(&mut self, exprs: &[P<ast::Expr>], attrs: &[Attribute]) {
1618         self.ibox(INDENT_UNIT);
1619         self.s.word("[");
1620         self.print_inner_attributes_inline(attrs);
1621         self.commasep_exprs(Inconsistent, &exprs[..]);
1622         self.s.word("]");
1623         self.end();
1624     }
1625
1626     fn print_expr_repeat(
1627         &mut self,
1628         element: &ast::Expr,
1629         count: &ast::AnonConst,
1630         attrs: &[Attribute],
1631     ) {
1632         self.ibox(INDENT_UNIT);
1633         self.s.word("[");
1634         self.print_inner_attributes_inline(attrs);
1635         self.print_expr(element);
1636         self.word_space(";");
1637         self.print_expr(&count.value);
1638         self.s.word("]");
1639         self.end();
1640     }
1641
1642     fn print_expr_struct(
1643         &mut self,
1644         path: &ast::Path,
1645         fields: &[ast::Field],
1646         wth: &Option<P<ast::Expr>>,
1647         attrs: &[Attribute],
1648     ) {
1649         self.print_path(path, true, 0);
1650         self.s.word("{");
1651         self.print_inner_attributes_inline(attrs);
1652         self.commasep_cmnt(
1653             Consistent,
1654             &fields[..],
1655             |s, field| {
1656                 s.print_outer_attributes(&field.attrs);
1657                 s.ibox(INDENT_UNIT);
1658                 if !field.is_shorthand {
1659                     s.print_ident(field.ident);
1660                     s.word_space(":");
1661                 }
1662                 s.print_expr(&field.expr);
1663                 s.end();
1664             },
1665             |f| f.span,
1666         );
1667         match *wth {
1668             Some(ref expr) => {
1669                 self.ibox(INDENT_UNIT);
1670                 if !fields.is_empty() {
1671                     self.s.word(",");
1672                     self.s.space();
1673                 }
1674                 self.s.word("..");
1675                 self.print_expr(expr);
1676                 self.end();
1677             }
1678             _ => {
1679                 if !fields.is_empty() {
1680                     self.s.word(",")
1681                 }
1682             }
1683         }
1684         self.s.word("}");
1685     }
1686
1687     fn print_expr_tup(&mut self, exprs: &[P<ast::Expr>], attrs: &[Attribute]) {
1688         self.popen();
1689         self.print_inner_attributes_inline(attrs);
1690         self.commasep_exprs(Inconsistent, &exprs[..]);
1691         if exprs.len() == 1 {
1692             self.s.word(",");
1693         }
1694         self.pclose()
1695     }
1696
1697     fn print_expr_call(&mut self, func: &ast::Expr, args: &[P<ast::Expr>]) {
1698         let prec = match func.kind {
1699             ast::ExprKind::Field(..) => parser::PREC_FORCE_PAREN,
1700             _ => parser::PREC_POSTFIX,
1701         };
1702
1703         self.print_expr_maybe_paren(func, prec);
1704         self.print_call_post(args)
1705     }
1706
1707     fn print_expr_method_call(&mut self, segment: &ast::PathSegment, args: &[P<ast::Expr>]) {
1708         let base_args = &args[1..];
1709         self.print_expr_maybe_paren(&args[0], parser::PREC_POSTFIX);
1710         self.s.word(".");
1711         self.print_ident(segment.ident);
1712         if let Some(ref args) = segment.args {
1713             self.print_generic_args(args, true);
1714         }
1715         self.print_call_post(base_args)
1716     }
1717
1718     fn print_expr_binary(&mut self, op: ast::BinOp, lhs: &ast::Expr, rhs: &ast::Expr) {
1719         let assoc_op = AssocOp::from_ast_binop(op.node);
1720         let prec = assoc_op.precedence() as i8;
1721         let fixity = assoc_op.fixity();
1722
1723         let (left_prec, right_prec) = match fixity {
1724             Fixity::Left => (prec, prec + 1),
1725             Fixity::Right => (prec + 1, prec),
1726             Fixity::None => (prec + 1, prec + 1),
1727         };
1728
1729         let left_prec = match (&lhs.kind, op.node) {
1730             // These cases need parens: `x as i32 < y` has the parser thinking that `i32 < y` is
1731             // the beginning of a path type. It starts trying to parse `x as (i32 < y ...` instead
1732             // of `(x as i32) < ...`. We need to convince it _not_ to do that.
1733             (&ast::ExprKind::Cast { .. }, ast::BinOpKind::Lt)
1734             | (&ast::ExprKind::Cast { .. }, ast::BinOpKind::Shl) => parser::PREC_FORCE_PAREN,
1735             // We are given `(let _ = a) OP b`.
1736             //
1737             // - When `OP <= LAnd` we should print `let _ = a OP b` to avoid redundant parens
1738             //   as the parser will interpret this as `(let _ = a) OP b`.
1739             //
1740             // - Otherwise, e.g. when we have `(let a = b) < c` in AST,
1741             //   parens are required since the parser would interpret `let a = b < c` as
1742             //   `let a = (b < c)`. To achieve this, we force parens.
1743             (&ast::ExprKind::Let { .. }, _) if !parser::needs_par_as_let_scrutinee(prec) => {
1744                 parser::PREC_FORCE_PAREN
1745             }
1746             _ => left_prec,
1747         };
1748
1749         self.print_expr_maybe_paren(lhs, left_prec);
1750         self.s.space();
1751         self.word_space(op.node.to_string());
1752         self.print_expr_maybe_paren(rhs, right_prec)
1753     }
1754
1755     fn print_expr_unary(&mut self, op: ast::UnOp, expr: &ast::Expr) {
1756         self.s.word(ast::UnOp::to_string(op));
1757         self.print_expr_maybe_paren(expr, parser::PREC_PREFIX)
1758     }
1759
1760     fn print_expr_addr_of(
1761         &mut self,
1762         kind: ast::BorrowKind,
1763         mutability: ast::Mutability,
1764         expr: &ast::Expr,
1765     ) {
1766         self.s.word("&");
1767         match kind {
1768             ast::BorrowKind::Ref => self.print_mutability(mutability, false),
1769             ast::BorrowKind::Raw => {
1770                 self.word_nbsp("raw");
1771                 self.print_mutability(mutability, true);
1772             }
1773         }
1774         self.print_expr_maybe_paren(expr, parser::PREC_PREFIX)
1775     }
1776
1777     pub fn print_expr(&mut self, expr: &ast::Expr) {
1778         self.print_expr_outer_attr_style(expr, true)
1779     }
1780
1781     fn print_expr_outer_attr_style(&mut self, expr: &ast::Expr, is_inline: bool) {
1782         self.maybe_print_comment(expr.span.lo());
1783
1784         let attrs = &expr.attrs;
1785         if is_inline {
1786             self.print_outer_attributes_inline(attrs);
1787         } else {
1788             self.print_outer_attributes(attrs);
1789         }
1790
1791         self.ibox(INDENT_UNIT);
1792         self.ann.pre(self, AnnNode::Expr(expr));
1793         match expr.kind {
1794             ast::ExprKind::Box(ref expr) => {
1795                 self.word_space("box");
1796                 self.print_expr_maybe_paren(expr, parser::PREC_PREFIX);
1797             }
1798             ast::ExprKind::Array(ref exprs) => {
1799                 self.print_expr_vec(&exprs[..], attrs);
1800             }
1801             ast::ExprKind::Repeat(ref element, ref count) => {
1802                 self.print_expr_repeat(element, count, attrs);
1803             }
1804             ast::ExprKind::Struct(ref path, ref fields, ref wth) => {
1805                 self.print_expr_struct(path, &fields[..], wth, attrs);
1806             }
1807             ast::ExprKind::Tup(ref exprs) => {
1808                 self.print_expr_tup(&exprs[..], attrs);
1809             }
1810             ast::ExprKind::Call(ref func, ref args) => {
1811                 self.print_expr_call(func, &args[..]);
1812             }
1813             ast::ExprKind::MethodCall(ref segment, ref args) => {
1814                 self.print_expr_method_call(segment, &args[..]);
1815             }
1816             ast::ExprKind::Binary(op, ref lhs, ref rhs) => {
1817                 self.print_expr_binary(op, lhs, rhs);
1818             }
1819             ast::ExprKind::Unary(op, ref expr) => {
1820                 self.print_expr_unary(op, expr);
1821             }
1822             ast::ExprKind::AddrOf(k, m, ref expr) => {
1823                 self.print_expr_addr_of(k, m, expr);
1824             }
1825             ast::ExprKind::Lit(ref lit) => {
1826                 self.print_literal(lit);
1827             }
1828             ast::ExprKind::Cast(ref expr, ref ty) => {
1829                 let prec = AssocOp::As.precedence() as i8;
1830                 self.print_expr_maybe_paren(expr, prec);
1831                 self.s.space();
1832                 self.word_space("as");
1833                 self.print_type(ty);
1834             }
1835             ast::ExprKind::Type(ref expr, ref ty) => {
1836                 let prec = AssocOp::Colon.precedence() as i8;
1837                 self.print_expr_maybe_paren(expr, prec);
1838                 self.word_space(":");
1839                 self.print_type(ty);
1840             }
1841             ast::ExprKind::Let(ref pat, ref scrutinee) => {
1842                 self.print_let(pat, scrutinee);
1843             }
1844             ast::ExprKind::If(ref test, ref blk, ref elseopt) => {
1845                 self.print_if(test, blk, elseopt.as_deref())
1846             }
1847             ast::ExprKind::While(ref test, ref blk, opt_label) => {
1848                 if let Some(label) = opt_label {
1849                     self.print_ident(label.ident);
1850                     self.word_space(":");
1851                 }
1852                 self.head("while");
1853                 self.print_expr_as_cond(test);
1854                 self.s.space();
1855                 self.print_block_with_attrs(blk, attrs);
1856             }
1857             ast::ExprKind::ForLoop(ref pat, ref iter, ref blk, opt_label) => {
1858                 if let Some(label) = opt_label {
1859                     self.print_ident(label.ident);
1860                     self.word_space(":");
1861                 }
1862                 self.head("for");
1863                 self.print_pat(pat);
1864                 self.s.space();
1865                 self.word_space("in");
1866                 self.print_expr_as_cond(iter);
1867                 self.s.space();
1868                 self.print_block_with_attrs(blk, attrs);
1869             }
1870             ast::ExprKind::Loop(ref blk, opt_label) => {
1871                 if let Some(label) = opt_label {
1872                     self.print_ident(label.ident);
1873                     self.word_space(":");
1874                 }
1875                 self.head("loop");
1876                 self.s.space();
1877                 self.print_block_with_attrs(blk, attrs);
1878             }
1879             ast::ExprKind::Match(ref expr, ref arms) => {
1880                 self.cbox(INDENT_UNIT);
1881                 self.ibox(INDENT_UNIT);
1882                 self.word_nbsp("match");
1883                 self.print_expr_as_cond(expr);
1884                 self.s.space();
1885                 self.bopen();
1886                 self.print_inner_attributes_no_trailing_hardbreak(attrs);
1887                 for arm in arms {
1888                     self.print_arm(arm);
1889                 }
1890                 self.bclose(expr.span);
1891             }
1892             ast::ExprKind::Closure(
1893                 capture_clause,
1894                 asyncness,
1895                 movability,
1896                 ref decl,
1897                 ref body,
1898                 _,
1899             ) => {
1900                 self.print_movability(movability);
1901                 self.print_asyncness(asyncness);
1902                 self.print_capture_clause(capture_clause);
1903
1904                 self.print_fn_params_and_ret(decl, true);
1905                 self.s.space();
1906                 self.print_expr(body);
1907                 self.end(); // need to close a box
1908
1909                 // a box will be closed by print_expr, but we didn't want an overall
1910                 // wrapper so we closed the corresponding opening. so create an
1911                 // empty box to satisfy the close.
1912                 self.ibox(0);
1913             }
1914             ast::ExprKind::Block(ref blk, opt_label) => {
1915                 if let Some(label) = opt_label {
1916                     self.print_ident(label.ident);
1917                     self.word_space(":");
1918                 }
1919                 // containing cbox, will be closed by print-block at }
1920                 self.cbox(INDENT_UNIT);
1921                 // head-box, will be closed by print-block after {
1922                 self.ibox(0);
1923                 self.print_block_with_attrs(blk, attrs);
1924             }
1925             ast::ExprKind::Async(capture_clause, _, ref blk) => {
1926                 self.word_nbsp("async");
1927                 self.print_capture_clause(capture_clause);
1928                 self.s.space();
1929                 // cbox/ibox in analogy to the `ExprKind::Block` arm above
1930                 self.cbox(INDENT_UNIT);
1931                 self.ibox(0);
1932                 self.print_block_with_attrs(blk, attrs);
1933             }
1934             ast::ExprKind::Await(ref expr) => {
1935                 self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX);
1936                 self.s.word(".await");
1937             }
1938             ast::ExprKind::Assign(ref lhs, ref rhs, _) => {
1939                 let prec = AssocOp::Assign.precedence() as i8;
1940                 self.print_expr_maybe_paren(lhs, prec + 1);
1941                 self.s.space();
1942                 self.word_space("=");
1943                 self.print_expr_maybe_paren(rhs, prec);
1944             }
1945             ast::ExprKind::AssignOp(op, ref lhs, ref rhs) => {
1946                 let prec = AssocOp::Assign.precedence() as i8;
1947                 self.print_expr_maybe_paren(lhs, prec + 1);
1948                 self.s.space();
1949                 self.s.word(op.node.to_string());
1950                 self.word_space("=");
1951                 self.print_expr_maybe_paren(rhs, prec);
1952             }
1953             ast::ExprKind::Field(ref expr, ident) => {
1954                 self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX);
1955                 self.s.word(".");
1956                 self.print_ident(ident);
1957             }
1958             ast::ExprKind::Index(ref expr, ref index) => {
1959                 self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX);
1960                 self.s.word("[");
1961                 self.print_expr(index);
1962                 self.s.word("]");
1963             }
1964             ast::ExprKind::Range(ref start, ref end, limits) => {
1965                 // Special case for `Range`.  `AssocOp` claims that `Range` has higher precedence
1966                 // than `Assign`, but `x .. x = x` gives a parse error instead of `x .. (x = x)`.
1967                 // Here we use a fake precedence value so that any child with lower precedence than
1968                 // a "normal" binop gets parenthesized.  (`LOr` is the lowest-precedence binop.)
1969                 let fake_prec = AssocOp::LOr.precedence() as i8;
1970                 if let Some(ref e) = *start {
1971                     self.print_expr_maybe_paren(e, fake_prec);
1972                 }
1973                 if limits == ast::RangeLimits::HalfOpen {
1974                     self.s.word("..");
1975                 } else {
1976                     self.s.word("..=");
1977                 }
1978                 if let Some(ref e) = *end {
1979                     self.print_expr_maybe_paren(e, fake_prec);
1980                 }
1981             }
1982             ast::ExprKind::Path(None, ref path) => self.print_path(path, true, 0),
1983             ast::ExprKind::Path(Some(ref qself), ref path) => self.print_qpath(path, qself, true),
1984             ast::ExprKind::Break(opt_label, ref opt_expr) => {
1985                 self.s.word("break");
1986                 self.s.space();
1987                 if let Some(label) = opt_label {
1988                     self.print_ident(label.ident);
1989                     self.s.space();
1990                 }
1991                 if let Some(ref expr) = *opt_expr {
1992                     self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
1993                     self.s.space();
1994                 }
1995             }
1996             ast::ExprKind::Continue(opt_label) => {
1997                 self.s.word("continue");
1998                 self.s.space();
1999                 if let Some(label) = opt_label {
2000                     self.print_ident(label.ident);
2001                     self.s.space()
2002                 }
2003             }
2004             ast::ExprKind::Ret(ref result) => {
2005                 self.s.word("return");
2006                 if let Some(ref expr) = *result {
2007                     self.s.word(" ");
2008                     self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
2009                 }
2010             }
2011             ast::ExprKind::InlineAsm(ref a) => {
2012                 self.s.word("asm!");
2013                 self.popen();
2014                 self.print_string(&a.asm.as_str(), a.asm_str_style);
2015                 self.word_space(":");
2016
2017                 self.commasep(Inconsistent, &a.outputs, |s, out| {
2018                     let constraint = out.constraint.as_str();
2019                     let mut ch = constraint.chars();
2020                     match ch.next() {
2021                         Some('=') if out.is_rw => {
2022                             s.print_string(&format!("+{}", ch.as_str()), ast::StrStyle::Cooked)
2023                         }
2024                         _ => s.print_string(&constraint, ast::StrStyle::Cooked),
2025                     }
2026                     s.popen();
2027                     s.print_expr(&out.expr);
2028                     s.pclose();
2029                 });
2030                 self.s.space();
2031                 self.word_space(":");
2032
2033                 self.commasep(Inconsistent, &a.inputs, |s, &(co, ref o)| {
2034                     s.print_string(&co.as_str(), ast::StrStyle::Cooked);
2035                     s.popen();
2036                     s.print_expr(o);
2037                     s.pclose();
2038                 });
2039                 self.s.space();
2040                 self.word_space(":");
2041
2042                 self.commasep(Inconsistent, &a.clobbers, |s, co| {
2043                     s.print_string(&co.as_str(), ast::StrStyle::Cooked);
2044                 });
2045
2046                 let mut options = vec![];
2047                 if a.volatile {
2048                     options.push("volatile");
2049                 }
2050                 if a.alignstack {
2051                     options.push("alignstack");
2052                 }
2053                 if a.dialect == ast::AsmDialect::Intel {
2054                     options.push("intel");
2055                 }
2056
2057                 if !options.is_empty() {
2058                     self.s.space();
2059                     self.word_space(":");
2060                     self.commasep(Inconsistent, &options, |s, &co| {
2061                         s.print_string(co, ast::StrStyle::Cooked);
2062                     });
2063                 }
2064
2065                 self.pclose();
2066             }
2067             ast::ExprKind::Mac(ref m) => self.print_mac(m),
2068             ast::ExprKind::Paren(ref e) => {
2069                 self.popen();
2070                 self.print_inner_attributes_inline(attrs);
2071                 self.print_expr(e);
2072                 self.pclose();
2073             }
2074             ast::ExprKind::Yield(ref e) => {
2075                 self.s.word("yield");
2076                 match *e {
2077                     Some(ref expr) => {
2078                         self.s.space();
2079                         self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
2080                     }
2081                     _ => (),
2082                 }
2083             }
2084             ast::ExprKind::Try(ref e) => {
2085                 self.print_expr_maybe_paren(e, parser::PREC_POSTFIX);
2086                 self.s.word("?")
2087             }
2088             ast::ExprKind::TryBlock(ref blk) => {
2089                 self.head("try");
2090                 self.s.space();
2091                 self.print_block_with_attrs(blk, attrs)
2092             }
2093             ast::ExprKind::Err => {
2094                 self.popen();
2095                 self.s.word("/*ERROR*/");
2096                 self.pclose()
2097             }
2098         }
2099         self.ann.post(self, AnnNode::Expr(expr));
2100         self.end();
2101     }
2102
2103     crate fn print_local_decl(&mut self, loc: &ast::Local) {
2104         self.print_pat(&loc.pat);
2105         if let Some(ref ty) = loc.ty {
2106             self.word_space(":");
2107             self.print_type(ty);
2108         }
2109     }
2110
2111     pub fn print_usize(&mut self, i: usize) {
2112         self.s.word(i.to_string())
2113     }
2114
2115     crate fn print_name(&mut self, name: ast::Name) {
2116         self.s.word(name.to_string());
2117         self.ann.post(self, AnnNode::Name(&name))
2118     }
2119
2120     fn print_qpath(&mut self, path: &ast::Path, qself: &ast::QSelf, colons_before_params: bool) {
2121         self.s.word("<");
2122         self.print_type(&qself.ty);
2123         if qself.position > 0 {
2124             self.s.space();
2125             self.word_space("as");
2126             let depth = path.segments.len() - qself.position;
2127             self.print_path(path, false, depth);
2128         }
2129         self.s.word(">");
2130         self.s.word("::");
2131         let item_segment = path.segments.last().unwrap();
2132         self.print_ident(item_segment.ident);
2133         match item_segment.args {
2134             Some(ref args) => self.print_generic_args(args, colons_before_params),
2135             None => {}
2136         }
2137     }
2138
2139     crate fn print_pat(&mut self, pat: &ast::Pat) {
2140         self.maybe_print_comment(pat.span.lo());
2141         self.ann.pre(self, AnnNode::Pat(pat));
2142         /* Pat isn't normalized, but the beauty of it
2143         is that it doesn't matter */
2144         match pat.kind {
2145             PatKind::Wild => self.s.word("_"),
2146             PatKind::Ident(binding_mode, ident, ref sub) => {
2147                 match binding_mode {
2148                     ast::BindingMode::ByRef(mutbl) => {
2149                         self.word_nbsp("ref");
2150                         self.print_mutability(mutbl, false);
2151                     }
2152                     ast::BindingMode::ByValue(ast::Mutability::Not) => {}
2153                     ast::BindingMode::ByValue(ast::Mutability::Mut) => {
2154                         self.word_nbsp("mut");
2155                     }
2156                 }
2157                 self.print_ident(ident);
2158                 if let Some(ref p) = *sub {
2159                     self.s.space();
2160                     self.s.word_space("@");
2161                     self.print_pat(p);
2162                 }
2163             }
2164             PatKind::TupleStruct(ref path, ref elts) => {
2165                 self.print_path(path, true, 0);
2166                 self.popen();
2167                 self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(p));
2168                 self.pclose();
2169             }
2170             PatKind::Or(ref pats) => {
2171                 self.strsep("|", true, Inconsistent, &pats[..], |s, p| s.print_pat(p));
2172             }
2173             PatKind::Path(None, ref path) => {
2174                 self.print_path(path, true, 0);
2175             }
2176             PatKind::Path(Some(ref qself), ref path) => {
2177                 self.print_qpath(path, qself, false);
2178             }
2179             PatKind::Struct(ref path, ref fields, etc) => {
2180                 self.print_path(path, true, 0);
2181                 self.nbsp();
2182                 self.word_space("{");
2183                 self.commasep_cmnt(
2184                     Consistent,
2185                     &fields[..],
2186                     |s, f| {
2187                         s.cbox(INDENT_UNIT);
2188                         if !f.is_shorthand {
2189                             s.print_ident(f.ident);
2190                             s.word_nbsp(":");
2191                         }
2192                         s.print_pat(&f.pat);
2193                         s.end();
2194                     },
2195                     |f| f.pat.span,
2196                 );
2197                 if etc {
2198                     if !fields.is_empty() {
2199                         self.word_space(",");
2200                     }
2201                     self.s.word("..");
2202                 }
2203                 self.s.space();
2204                 self.s.word("}");
2205             }
2206             PatKind::Tuple(ref elts) => {
2207                 self.popen();
2208                 self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(p));
2209                 if elts.len() == 1 {
2210                     self.s.word(",");
2211                 }
2212                 self.pclose();
2213             }
2214             PatKind::Box(ref inner) => {
2215                 self.s.word("box ");
2216                 self.print_pat(inner);
2217             }
2218             PatKind::Ref(ref inner, mutbl) => {
2219                 self.s.word("&");
2220                 if mutbl == ast::Mutability::Mut {
2221                     self.s.word("mut ");
2222                 }
2223                 self.print_pat(inner);
2224             }
2225             PatKind::Lit(ref e) => self.print_expr(&**e),
2226             PatKind::Range(ref begin, ref end, Spanned { node: ref end_kind, .. }) => {
2227                 if let Some(e) = begin {
2228                     self.print_expr(e);
2229                     self.s.space();
2230                 }
2231                 match *end_kind {
2232                     RangeEnd::Included(RangeSyntax::DotDotDot) => self.s.word("..."),
2233                     RangeEnd::Included(RangeSyntax::DotDotEq) => self.s.word("..="),
2234                     RangeEnd::Excluded => self.s.word(".."),
2235                 }
2236                 if let Some(e) = end {
2237                     self.print_expr(e);
2238                 }
2239             }
2240             PatKind::Slice(ref elts) => {
2241                 self.s.word("[");
2242                 self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(p));
2243                 self.s.word("]");
2244             }
2245             PatKind::Rest => self.s.word(".."),
2246             PatKind::Paren(ref inner) => {
2247                 self.popen();
2248                 self.print_pat(inner);
2249                 self.pclose();
2250             }
2251             PatKind::Mac(ref m) => self.print_mac(m),
2252         }
2253         self.ann.post(self, AnnNode::Pat(pat))
2254     }
2255
2256     fn print_arm(&mut self, arm: &ast::Arm) {
2257         // Note, I have no idea why this check is necessary, but here it is.
2258         if arm.attrs.is_empty() {
2259             self.s.space();
2260         }
2261         self.cbox(INDENT_UNIT);
2262         self.ibox(0);
2263         self.maybe_print_comment(arm.pat.span.lo());
2264         self.print_outer_attributes(&arm.attrs);
2265         self.print_pat(&arm.pat);
2266         self.s.space();
2267         if let Some(ref e) = arm.guard {
2268             self.word_space("if");
2269             self.print_expr(e);
2270             self.s.space();
2271         }
2272         self.word_space("=>");
2273
2274         match arm.body.kind {
2275             ast::ExprKind::Block(ref blk, opt_label) => {
2276                 if let Some(label) = opt_label {
2277                     self.print_ident(label.ident);
2278                     self.word_space(":");
2279                 }
2280
2281                 // The block will close the pattern's ibox.
2282                 self.print_block_unclosed_indent(blk);
2283
2284                 // If it is a user-provided unsafe block, print a comma after it.
2285                 if let BlockCheckMode::Unsafe(ast::UserProvided) = blk.rules {
2286                     self.s.word(",");
2287                 }
2288             }
2289             _ => {
2290                 self.end(); // Close the ibox for the pattern.
2291                 self.print_expr(&arm.body);
2292                 self.s.word(",");
2293             }
2294         }
2295         self.end(); // Close enclosing cbox.
2296     }
2297
2298     fn print_explicit_self(&mut self, explicit_self: &ast::ExplicitSelf) {
2299         match explicit_self.node {
2300             SelfKind::Value(m) => {
2301                 self.print_mutability(m, false);
2302                 self.s.word("self")
2303             }
2304             SelfKind::Region(ref lt, m) => {
2305                 self.s.word("&");
2306                 self.print_opt_lifetime(lt);
2307                 self.print_mutability(m, false);
2308                 self.s.word("self")
2309             }
2310             SelfKind::Explicit(ref typ, m) => {
2311                 self.print_mutability(m, false);
2312                 self.s.word("self");
2313                 self.word_space(":");
2314                 self.print_type(typ)
2315             }
2316         }
2317     }
2318
2319     fn print_fn_full(
2320         &mut self,
2321         sig: &ast::FnSig,
2322         name: ast::Ident,
2323         generics: &ast::Generics,
2324         vis: &ast::Visibility,
2325         defaultness: ast::Defaultness,
2326         body: Option<&ast::Block>,
2327         attrs: &[ast::Attribute],
2328     ) {
2329         if body.is_some() {
2330             self.head("");
2331         }
2332         self.print_visibility(vis);
2333         self.print_defaultness(defaultness);
2334         self.print_fn(&sig.decl, sig.header, Some(name), generics);
2335         if let Some(body) = body {
2336             self.nbsp();
2337             self.print_block_with_attrs(body, attrs);
2338         } else {
2339             self.s.word(";");
2340         }
2341     }
2342
2343     crate fn print_fn(
2344         &mut self,
2345         decl: &ast::FnDecl,
2346         header: ast::FnHeader,
2347         name: Option<ast::Ident>,
2348         generics: &ast::Generics,
2349     ) {
2350         self.print_fn_header_info(header);
2351         if let Some(name) = name {
2352             self.nbsp();
2353             self.print_ident(name);
2354         }
2355         self.print_generic_params(&generics.params);
2356         self.print_fn_params_and_ret(decl, false);
2357         self.print_where_clause(&generics.where_clause)
2358     }
2359
2360     crate fn print_fn_params_and_ret(&mut self, decl: &ast::FnDecl, is_closure: bool) {
2361         let (open, close) = if is_closure { ("|", "|") } else { ("(", ")") };
2362         self.word(open);
2363         self.commasep(Inconsistent, &decl.inputs, |s, param| s.print_param(param, is_closure));
2364         self.word(close);
2365         self.print_fn_ret_ty(&decl.output)
2366     }
2367
2368     crate fn print_movability(&mut self, movability: ast::Movability) {
2369         match movability {
2370             ast::Movability::Static => self.word_space("static"),
2371             ast::Movability::Movable => {}
2372         }
2373     }
2374
2375     crate fn print_asyncness(&mut self, asyncness: ast::Async) {
2376         if asyncness.is_async() {
2377             self.word_nbsp("async");
2378         }
2379     }
2380
2381     crate fn print_capture_clause(&mut self, capture_clause: ast::CaptureBy) {
2382         match capture_clause {
2383             ast::CaptureBy::Value => self.word_space("move"),
2384             ast::CaptureBy::Ref => {}
2385         }
2386     }
2387
2388     pub fn print_type_bounds(&mut self, prefix: &'static str, bounds: &[ast::GenericBound]) {
2389         if !bounds.is_empty() {
2390             self.s.word(prefix);
2391             let mut first = true;
2392             for bound in bounds {
2393                 if !(first && prefix.is_empty()) {
2394                     self.nbsp();
2395                 }
2396                 if first {
2397                     first = false;
2398                 } else {
2399                     self.word_space("+");
2400                 }
2401
2402                 match bound {
2403                     GenericBound::Trait(tref, modifier) => {
2404                         if modifier == &TraitBoundModifier::Maybe {
2405                             self.s.word("?");
2406                         }
2407                         self.print_poly_trait_ref(tref);
2408                     }
2409                     GenericBound::Outlives(lt) => self.print_lifetime(*lt),
2410                 }
2411             }
2412         }
2413     }
2414
2415     crate fn print_lifetime(&mut self, lifetime: ast::Lifetime) {
2416         self.print_name(lifetime.ident.name)
2417     }
2418
2419     crate fn print_lifetime_bounds(
2420         &mut self,
2421         lifetime: ast::Lifetime,
2422         bounds: &ast::GenericBounds,
2423     ) {
2424         self.print_lifetime(lifetime);
2425         if !bounds.is_empty() {
2426             self.s.word(": ");
2427             for (i, bound) in bounds.iter().enumerate() {
2428                 if i != 0 {
2429                     self.s.word(" + ");
2430                 }
2431                 match bound {
2432                     ast::GenericBound::Outlives(lt) => self.print_lifetime(*lt),
2433                     _ => panic!(),
2434                 }
2435             }
2436         }
2437     }
2438
2439     crate fn print_generic_params(&mut self, generic_params: &[ast::GenericParam]) {
2440         if generic_params.is_empty() {
2441             return;
2442         }
2443
2444         self.s.word("<");
2445
2446         self.commasep(Inconsistent, &generic_params, |s, param| {
2447             s.print_outer_attributes_inline(&param.attrs);
2448
2449             match param.kind {
2450                 ast::GenericParamKind::Lifetime => {
2451                     let lt = ast::Lifetime { id: param.id, ident: param.ident };
2452                     s.print_lifetime_bounds(lt, &param.bounds)
2453                 }
2454                 ast::GenericParamKind::Type { ref default } => {
2455                     s.print_ident(param.ident);
2456                     s.print_type_bounds(":", &param.bounds);
2457                     if let Some(ref default) = default {
2458                         s.s.space();
2459                         s.word_space("=");
2460                         s.print_type(default)
2461                     }
2462                 }
2463                 ast::GenericParamKind::Const { ref ty } => {
2464                     s.word_space("const");
2465                     s.print_ident(param.ident);
2466                     s.s.space();
2467                     s.word_space(":");
2468                     s.print_type(ty);
2469                     s.print_type_bounds(":", &param.bounds)
2470                 }
2471             }
2472         });
2473
2474         self.s.word(">");
2475     }
2476
2477     crate fn print_where_clause(&mut self, where_clause: &ast::WhereClause) {
2478         if where_clause.predicates.is_empty() {
2479             return;
2480         }
2481
2482         self.s.space();
2483         self.word_space("where");
2484
2485         for (i, predicate) in where_clause.predicates.iter().enumerate() {
2486             if i != 0 {
2487                 self.word_space(",");
2488             }
2489
2490             match *predicate {
2491                 ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
2492                     ref bound_generic_params,
2493                     ref bounded_ty,
2494                     ref bounds,
2495                     ..
2496                 }) => {
2497                     self.print_formal_generic_params(bound_generic_params);
2498                     self.print_type(bounded_ty);
2499                     self.print_type_bounds(":", bounds);
2500                 }
2501                 ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
2502                     ref lifetime,
2503                     ref bounds,
2504                     ..
2505                 }) => {
2506                     self.print_lifetime_bounds(*lifetime, bounds);
2507                 }
2508                 ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
2509                     ref lhs_ty,
2510                     ref rhs_ty,
2511                     ..
2512                 }) => {
2513                     self.print_type(lhs_ty);
2514                     self.s.space();
2515                     self.word_space("=");
2516                     self.print_type(rhs_ty);
2517                 }
2518             }
2519         }
2520     }
2521
2522     crate fn print_use_tree(&mut self, tree: &ast::UseTree) {
2523         match tree.kind {
2524             ast::UseTreeKind::Simple(rename, ..) => {
2525                 self.print_path(&tree.prefix, false, 0);
2526                 if let Some(rename) = rename {
2527                     self.s.space();
2528                     self.word_space("as");
2529                     self.print_ident(rename);
2530                 }
2531             }
2532             ast::UseTreeKind::Glob => {
2533                 if !tree.prefix.segments.is_empty() {
2534                     self.print_path(&tree.prefix, false, 0);
2535                     self.s.word("::");
2536                 }
2537                 self.s.word("*");
2538             }
2539             ast::UseTreeKind::Nested(ref items) => {
2540                 if tree.prefix.segments.is_empty() {
2541                     self.s.word("{");
2542                 } else {
2543                     self.print_path(&tree.prefix, false, 0);
2544                     self.s.word("::{");
2545                 }
2546                 self.commasep(Inconsistent, &items[..], |this, &(ref tree, _)| {
2547                     this.print_use_tree(tree)
2548                 });
2549                 self.s.word("}");
2550             }
2551         }
2552     }
2553
2554     pub fn print_mutability(&mut self, mutbl: ast::Mutability, print_const: bool) {
2555         match mutbl {
2556             ast::Mutability::Mut => self.word_nbsp("mut"),
2557             ast::Mutability::Not => {
2558                 if print_const {
2559                     self.word_nbsp("const");
2560                 }
2561             }
2562         }
2563     }
2564
2565     crate fn print_mt(&mut self, mt: &ast::MutTy, print_const: bool) {
2566         self.print_mutability(mt.mutbl, print_const);
2567         self.print_type(&mt.ty)
2568     }
2569
2570     crate fn print_param(&mut self, input: &ast::Param, is_closure: bool) {
2571         self.ibox(INDENT_UNIT);
2572
2573         self.print_outer_attributes_inline(&input.attrs);
2574
2575         match input.ty.kind {
2576             ast::TyKind::Infer if is_closure => self.print_pat(&input.pat),
2577             _ => {
2578                 if let Some(eself) = input.to_self() {
2579                     self.print_explicit_self(&eself);
2580                 } else {
2581                     let invalid = if let PatKind::Ident(_, ident, _) = input.pat.kind {
2582                         ident.name == kw::Invalid
2583                     } else {
2584                         false
2585                     };
2586                     if !invalid {
2587                         self.print_pat(&input.pat);
2588                         self.s.word(":");
2589                         self.s.space();
2590                     }
2591                     self.print_type(&input.ty);
2592                 }
2593             }
2594         }
2595         self.end();
2596     }
2597
2598     crate fn print_fn_ret_ty(&mut self, fn_ret_ty: &ast::FnRetTy) {
2599         if let ast::FnRetTy::Ty(ty) = fn_ret_ty {
2600             self.space_if_not_bol();
2601             self.ibox(INDENT_UNIT);
2602             self.word_space("->");
2603             self.print_type(ty);
2604             self.end();
2605             self.maybe_print_comment(ty.span.lo());
2606         }
2607     }
2608
2609     crate fn print_ty_fn(
2610         &mut self,
2611         ext: ast::Extern,
2612         unsafety: ast::Unsafe,
2613         decl: &ast::FnDecl,
2614         name: Option<ast::Ident>,
2615         generic_params: &[ast::GenericParam],
2616     ) {
2617         self.ibox(INDENT_UNIT);
2618         if !generic_params.is_empty() {
2619             self.s.word("for");
2620             self.print_generic_params(generic_params);
2621         }
2622         let generics = ast::Generics {
2623             params: Vec::new(),
2624             where_clause: ast::WhereClause { predicates: Vec::new(), span: rustc_span::DUMMY_SP },
2625             span: rustc_span::DUMMY_SP,
2626         };
2627         let header = ast::FnHeader { unsafety, ext, ..ast::FnHeader::default() };
2628         self.print_fn(decl, header, name, &generics);
2629         self.end();
2630     }
2631
2632     crate fn maybe_print_trailing_comment(
2633         &mut self,
2634         span: rustc_span::Span,
2635         next_pos: Option<BytePos>,
2636     ) {
2637         if let Some(cmnts) = self.comments() {
2638             if let Some(cmnt) = cmnts.trailing_comment(span, next_pos) {
2639                 self.print_comment(&cmnt);
2640             }
2641         }
2642     }
2643
2644     crate fn print_remaining_comments(&mut self) {
2645         // If there aren't any remaining comments, then we need to manually
2646         // make sure there is a line break at the end.
2647         if self.next_comment().is_none() {
2648             self.s.hardbreak();
2649         }
2650         while let Some(ref cmnt) = self.next_comment() {
2651             self.print_comment(cmnt);
2652         }
2653     }
2654
2655     crate fn print_fn_header_info(&mut self, header: ast::FnHeader) {
2656         self.print_constness(header.constness);
2657         self.print_asyncness(header.asyncness);
2658         self.print_unsafety(header.unsafety);
2659
2660         match header.ext {
2661             ast::Extern::None => {}
2662             ast::Extern::Implicit => {
2663                 self.word_nbsp("extern");
2664             }
2665             ast::Extern::Explicit(abi) => {
2666                 self.word_nbsp("extern");
2667                 self.print_literal(&abi.as_lit());
2668                 self.nbsp();
2669             }
2670         }
2671
2672         self.s.word("fn")
2673     }
2674
2675     crate fn print_unsafety(&mut self, s: ast::Unsafe) {
2676         match s {
2677             ast::Unsafe::No => {}
2678             ast::Unsafe::Yes(_) => self.word_nbsp("unsafe"),
2679         }
2680     }
2681
2682     crate fn print_constness(&mut self, s: ast::Const) {
2683         match s {
2684             ast::Const::No => {}
2685             ast::Const::Yes(_) => self.word_nbsp("const"),
2686         }
2687     }
2688
2689     crate fn print_is_auto(&mut self, s: ast::IsAuto) {
2690         match s {
2691             ast::IsAuto::Yes => self.word_nbsp("auto"),
2692             ast::IsAuto::No => {}
2693         }
2694     }
2695 }