]> git.lizzy.rs Git - rust.git/blob - src/librustc_ast_pretty/pprust.rs
Tweak wording
[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_span::edition::Edition;
5 use rustc_span::source_map::{SourceMap, Spanned};
6 use rustc_span::symbol::{kw, sym, IdentPrinter};
7 use rustc_span::{BytePos, FileName, Span};
8 use syntax::ast::{self, BlockCheckMode, PatKind, RangeEnd, RangeSyntax};
9 use syntax::ast::{Attribute, GenericArg, MacArgs};
10 use syntax::ast::{GenericBound, SelfKind, TraitBoundModifier};
11 use syntax::attr;
12 use syntax::ptr::P;
13 use syntax::token::{self, BinOpToken, DelimToken, Nonterminal, Token, TokenKind};
14 use syntax::tokenstream::{self, TokenStream, TokenTree};
15 use syntax::util::parser::{self, AssocOp, Fixity};
16 use syntax::util::{classify, comments};
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.len() != 0;
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                 match expr.kind {
1450                     // Filter out empty `Tup` exprs created for the `redundant_semicolon`
1451                     // lint, as they shouldn't be visible and interact poorly
1452                     // with proc macros.
1453                     ast::ExprKind::Tup(ref exprs) if exprs.is_empty() && expr.attrs.is_empty() => {
1454                         ()
1455                     }
1456                     _ => {
1457                         self.space_if_not_bol();
1458                         self.print_expr_outer_attr_style(expr, false);
1459                         self.s.word(";");
1460                     }
1461                 }
1462             }
1463             ast::StmtKind::Mac(ref mac) => {
1464                 let (ref mac, style, ref attrs) = **mac;
1465                 self.space_if_not_bol();
1466                 self.print_outer_attributes(attrs);
1467                 self.print_mac(mac);
1468                 if style == ast::MacStmtStyle::Semicolon {
1469                     self.s.word(";");
1470                 }
1471             }
1472         }
1473         self.maybe_print_trailing_comment(st.span, None)
1474     }
1475
1476     crate fn print_block(&mut self, blk: &ast::Block) {
1477         self.print_block_with_attrs(blk, &[])
1478     }
1479
1480     crate fn print_block_unclosed_indent(&mut self, blk: &ast::Block) {
1481         self.print_block_maybe_unclosed(blk, &[], false)
1482     }
1483
1484     crate fn print_block_with_attrs(&mut self, blk: &ast::Block, attrs: &[ast::Attribute]) {
1485         self.print_block_maybe_unclosed(blk, attrs, true)
1486     }
1487
1488     crate fn print_block_maybe_unclosed(
1489         &mut self,
1490         blk: &ast::Block,
1491         attrs: &[ast::Attribute],
1492         close_box: bool,
1493     ) {
1494         match blk.rules {
1495             BlockCheckMode::Unsafe(..) => self.word_space("unsafe"),
1496             BlockCheckMode::Default => (),
1497         }
1498         self.maybe_print_comment(blk.span.lo());
1499         self.ann.pre(self, AnnNode::Block(blk));
1500         self.bopen();
1501
1502         self.print_inner_attributes(attrs);
1503
1504         for (i, st) in blk.stmts.iter().enumerate() {
1505             match st.kind {
1506                 ast::StmtKind::Expr(ref expr) if i == blk.stmts.len() - 1 => {
1507                     self.maybe_print_comment(st.span.lo());
1508                     self.space_if_not_bol();
1509                     self.print_expr_outer_attr_style(expr, false);
1510                     self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi()));
1511                 }
1512                 _ => self.print_stmt(st),
1513             }
1514         }
1515
1516         self.bclose_maybe_open(blk.span, close_box);
1517         self.ann.post(self, AnnNode::Block(blk))
1518     }
1519
1520     /// Print a `let pat = scrutinee` expression.
1521     crate fn print_let(&mut self, pat: &ast::Pat, scrutinee: &ast::Expr) {
1522         self.s.word("let ");
1523
1524         self.print_pat(pat);
1525         self.s.space();
1526
1527         self.word_space("=");
1528         self.print_expr_cond_paren(
1529             scrutinee,
1530             Self::cond_needs_par(scrutinee)
1531                 || parser::needs_par_as_let_scrutinee(scrutinee.precedence().order()),
1532         )
1533     }
1534
1535     fn print_else(&mut self, els: Option<&ast::Expr>) {
1536         if let Some(_else) = els {
1537             match _else.kind {
1538                 // Another `else if` block.
1539                 ast::ExprKind::If(ref i, ref then, ref e) => {
1540                     self.cbox(INDENT_UNIT - 1);
1541                     self.ibox(0);
1542                     self.s.word(" else if ");
1543                     self.print_expr_as_cond(i);
1544                     self.s.space();
1545                     self.print_block(then);
1546                     self.print_else(e.as_deref())
1547                 }
1548                 // Final `else` block.
1549                 ast::ExprKind::Block(ref b, _) => {
1550                     self.cbox(INDENT_UNIT - 1);
1551                     self.ibox(0);
1552                     self.s.word(" else ");
1553                     self.print_block(b)
1554                 }
1555                 // Constraints would be great here!
1556                 _ => {
1557                     panic!("print_if saw if with weird alternative");
1558                 }
1559             }
1560         }
1561     }
1562
1563     crate fn print_if(&mut self, test: &ast::Expr, blk: &ast::Block, elseopt: Option<&ast::Expr>) {
1564         self.head("if");
1565
1566         self.print_expr_as_cond(test);
1567         self.s.space();
1568
1569         self.print_block(blk);
1570         self.print_else(elseopt)
1571     }
1572
1573     crate fn print_mac(&mut self, m: &ast::Mac) {
1574         self.print_mac_common(
1575             Some(MacHeader::Path(&m.path)),
1576             true,
1577             None,
1578             m.args.delim(),
1579             m.args.inner_tokens(),
1580             true,
1581             m.span(),
1582         );
1583     }
1584
1585     fn print_call_post(&mut self, args: &[P<ast::Expr>]) {
1586         self.popen();
1587         self.commasep_exprs(Inconsistent, args);
1588         self.pclose()
1589     }
1590
1591     crate fn print_expr_maybe_paren(&mut self, expr: &ast::Expr, prec: i8) {
1592         self.print_expr_cond_paren(expr, expr.precedence().order() < prec)
1593     }
1594
1595     /// Prints an expr using syntax that's acceptable in a condition position, such as the `cond` in
1596     /// `if cond { ... }`.
1597     crate fn print_expr_as_cond(&mut self, expr: &ast::Expr) {
1598         self.print_expr_cond_paren(expr, Self::cond_needs_par(expr))
1599     }
1600
1601     /// Does `expr` need parenthesis when printed in a condition position?
1602     fn cond_needs_par(expr: &ast::Expr) -> bool {
1603         match expr.kind {
1604             // These cases need parens due to the parse error observed in #26461: `if return {}`
1605             // parses as the erroneous construct `if (return {})`, not `if (return) {}`.
1606             ast::ExprKind::Closure(..) | ast::ExprKind::Ret(..) | ast::ExprKind::Break(..) => true,
1607
1608             _ => parser::contains_exterior_struct_lit(expr),
1609         }
1610     }
1611
1612     /// Prints `expr` or `(expr)` when `needs_par` holds.
1613     fn print_expr_cond_paren(&mut self, expr: &ast::Expr, needs_par: bool) {
1614         if needs_par {
1615             self.popen();
1616         }
1617         self.print_expr(expr);
1618         if needs_par {
1619             self.pclose();
1620         }
1621     }
1622
1623     fn print_expr_vec(&mut self, exprs: &[P<ast::Expr>], attrs: &[Attribute]) {
1624         self.ibox(INDENT_UNIT);
1625         self.s.word("[");
1626         self.print_inner_attributes_inline(attrs);
1627         self.commasep_exprs(Inconsistent, &exprs[..]);
1628         self.s.word("]");
1629         self.end();
1630     }
1631
1632     fn print_expr_repeat(
1633         &mut self,
1634         element: &ast::Expr,
1635         count: &ast::AnonConst,
1636         attrs: &[Attribute],
1637     ) {
1638         self.ibox(INDENT_UNIT);
1639         self.s.word("[");
1640         self.print_inner_attributes_inline(attrs);
1641         self.print_expr(element);
1642         self.word_space(";");
1643         self.print_expr(&count.value);
1644         self.s.word("]");
1645         self.end();
1646     }
1647
1648     fn print_expr_struct(
1649         &mut self,
1650         path: &ast::Path,
1651         fields: &[ast::Field],
1652         wth: &Option<P<ast::Expr>>,
1653         attrs: &[Attribute],
1654     ) {
1655         self.print_path(path, true, 0);
1656         self.s.word("{");
1657         self.print_inner_attributes_inline(attrs);
1658         self.commasep_cmnt(
1659             Consistent,
1660             &fields[..],
1661             |s, field| {
1662                 s.print_outer_attributes(&field.attrs);
1663                 s.ibox(INDENT_UNIT);
1664                 if !field.is_shorthand {
1665                     s.print_ident(field.ident);
1666                     s.word_space(":");
1667                 }
1668                 s.print_expr(&field.expr);
1669                 s.end();
1670             },
1671             |f| f.span,
1672         );
1673         match *wth {
1674             Some(ref expr) => {
1675                 self.ibox(INDENT_UNIT);
1676                 if !fields.is_empty() {
1677                     self.s.word(",");
1678                     self.s.space();
1679                 }
1680                 self.s.word("..");
1681                 self.print_expr(expr);
1682                 self.end();
1683             }
1684             _ => {
1685                 if !fields.is_empty() {
1686                     self.s.word(",")
1687                 }
1688             }
1689         }
1690         self.s.word("}");
1691     }
1692
1693     fn print_expr_tup(&mut self, exprs: &[P<ast::Expr>], attrs: &[Attribute]) {
1694         self.popen();
1695         self.print_inner_attributes_inline(attrs);
1696         self.commasep_exprs(Inconsistent, &exprs[..]);
1697         if exprs.len() == 1 {
1698             self.s.word(",");
1699         }
1700         self.pclose()
1701     }
1702
1703     fn print_expr_call(&mut self, func: &ast::Expr, args: &[P<ast::Expr>]) {
1704         let prec = match func.kind {
1705             ast::ExprKind::Field(..) => parser::PREC_FORCE_PAREN,
1706             _ => parser::PREC_POSTFIX,
1707         };
1708
1709         self.print_expr_maybe_paren(func, prec);
1710         self.print_call_post(args)
1711     }
1712
1713     fn print_expr_method_call(&mut self, segment: &ast::PathSegment, args: &[P<ast::Expr>]) {
1714         let base_args = &args[1..];
1715         self.print_expr_maybe_paren(&args[0], parser::PREC_POSTFIX);
1716         self.s.word(".");
1717         self.print_ident(segment.ident);
1718         if let Some(ref args) = segment.args {
1719             self.print_generic_args(args, true);
1720         }
1721         self.print_call_post(base_args)
1722     }
1723
1724     fn print_expr_binary(&mut self, op: ast::BinOp, lhs: &ast::Expr, rhs: &ast::Expr) {
1725         let assoc_op = AssocOp::from_ast_binop(op.node);
1726         let prec = assoc_op.precedence() as i8;
1727         let fixity = assoc_op.fixity();
1728
1729         let (left_prec, right_prec) = match fixity {
1730             Fixity::Left => (prec, prec + 1),
1731             Fixity::Right => (prec + 1, prec),
1732             Fixity::None => (prec + 1, prec + 1),
1733         };
1734
1735         let left_prec = match (&lhs.kind, op.node) {
1736             // These cases need parens: `x as i32 < y` has the parser thinking that `i32 < y` is
1737             // the beginning of a path type. It starts trying to parse `x as (i32 < y ...` instead
1738             // of `(x as i32) < ...`. We need to convince it _not_ to do that.
1739             (&ast::ExprKind::Cast { .. }, ast::BinOpKind::Lt)
1740             | (&ast::ExprKind::Cast { .. }, ast::BinOpKind::Shl) => parser::PREC_FORCE_PAREN,
1741             // We are given `(let _ = a) OP b`.
1742             //
1743             // - When `OP <= LAnd` we should print `let _ = a OP b` to avoid redundant parens
1744             //   as the parser will interpret this as `(let _ = a) OP b`.
1745             //
1746             // - Otherwise, e.g. when we have `(let a = b) < c` in AST,
1747             //   parens are required since the parser would interpret `let a = b < c` as
1748             //   `let a = (b < c)`. To achieve this, we force parens.
1749             (&ast::ExprKind::Let { .. }, _) if !parser::needs_par_as_let_scrutinee(prec) => {
1750                 parser::PREC_FORCE_PAREN
1751             }
1752             _ => left_prec,
1753         };
1754
1755         self.print_expr_maybe_paren(lhs, left_prec);
1756         self.s.space();
1757         self.word_space(op.node.to_string());
1758         self.print_expr_maybe_paren(rhs, right_prec)
1759     }
1760
1761     fn print_expr_unary(&mut self, op: ast::UnOp, expr: &ast::Expr) {
1762         self.s.word(ast::UnOp::to_string(op));
1763         self.print_expr_maybe_paren(expr, parser::PREC_PREFIX)
1764     }
1765
1766     fn print_expr_addr_of(
1767         &mut self,
1768         kind: ast::BorrowKind,
1769         mutability: ast::Mutability,
1770         expr: &ast::Expr,
1771     ) {
1772         self.s.word("&");
1773         match kind {
1774             ast::BorrowKind::Ref => self.print_mutability(mutability, false),
1775             ast::BorrowKind::Raw => {
1776                 self.word_nbsp("raw");
1777                 self.print_mutability(mutability, true);
1778             }
1779         }
1780         self.print_expr_maybe_paren(expr, parser::PREC_PREFIX)
1781     }
1782
1783     pub fn print_expr(&mut self, expr: &ast::Expr) {
1784         self.print_expr_outer_attr_style(expr, true)
1785     }
1786
1787     fn print_expr_outer_attr_style(&mut self, expr: &ast::Expr, is_inline: bool) {
1788         self.maybe_print_comment(expr.span.lo());
1789
1790         let attrs = &expr.attrs;
1791         if is_inline {
1792             self.print_outer_attributes_inline(attrs);
1793         } else {
1794             self.print_outer_attributes(attrs);
1795         }
1796
1797         self.ibox(INDENT_UNIT);
1798         self.ann.pre(self, AnnNode::Expr(expr));
1799         match expr.kind {
1800             ast::ExprKind::Box(ref expr) => {
1801                 self.word_space("box");
1802                 self.print_expr_maybe_paren(expr, parser::PREC_PREFIX);
1803             }
1804             ast::ExprKind::Array(ref exprs) => {
1805                 self.print_expr_vec(&exprs[..], attrs);
1806             }
1807             ast::ExprKind::Repeat(ref element, ref count) => {
1808                 self.print_expr_repeat(element, count, attrs);
1809             }
1810             ast::ExprKind::Struct(ref path, ref fields, ref wth) => {
1811                 self.print_expr_struct(path, &fields[..], wth, attrs);
1812             }
1813             ast::ExprKind::Tup(ref exprs) => {
1814                 self.print_expr_tup(&exprs[..], attrs);
1815             }
1816             ast::ExprKind::Call(ref func, ref args) => {
1817                 self.print_expr_call(func, &args[..]);
1818             }
1819             ast::ExprKind::MethodCall(ref segment, ref args) => {
1820                 self.print_expr_method_call(segment, &args[..]);
1821             }
1822             ast::ExprKind::Binary(op, ref lhs, ref rhs) => {
1823                 self.print_expr_binary(op, lhs, rhs);
1824             }
1825             ast::ExprKind::Unary(op, ref expr) => {
1826                 self.print_expr_unary(op, expr);
1827             }
1828             ast::ExprKind::AddrOf(k, m, ref expr) => {
1829                 self.print_expr_addr_of(k, m, expr);
1830             }
1831             ast::ExprKind::Lit(ref lit) => {
1832                 self.print_literal(lit);
1833             }
1834             ast::ExprKind::Cast(ref expr, ref ty) => {
1835                 let prec = AssocOp::As.precedence() as i8;
1836                 self.print_expr_maybe_paren(expr, prec);
1837                 self.s.space();
1838                 self.word_space("as");
1839                 self.print_type(ty);
1840             }
1841             ast::ExprKind::Type(ref expr, ref ty) => {
1842                 let prec = AssocOp::Colon.precedence() as i8;
1843                 self.print_expr_maybe_paren(expr, prec);
1844                 self.word_space(":");
1845                 self.print_type(ty);
1846             }
1847             ast::ExprKind::Let(ref pat, ref scrutinee) => {
1848                 self.print_let(pat, scrutinee);
1849             }
1850             ast::ExprKind::If(ref test, ref blk, ref elseopt) => {
1851                 self.print_if(test, blk, elseopt.as_deref())
1852             }
1853             ast::ExprKind::While(ref test, ref blk, opt_label) => {
1854                 if let Some(label) = opt_label {
1855                     self.print_ident(label.ident);
1856                     self.word_space(":");
1857                 }
1858                 self.head("while");
1859                 self.print_expr_as_cond(test);
1860                 self.s.space();
1861                 self.print_block_with_attrs(blk, attrs);
1862             }
1863             ast::ExprKind::ForLoop(ref pat, ref iter, ref blk, opt_label) => {
1864                 if let Some(label) = opt_label {
1865                     self.print_ident(label.ident);
1866                     self.word_space(":");
1867                 }
1868                 self.head("for");
1869                 self.print_pat(pat);
1870                 self.s.space();
1871                 self.word_space("in");
1872                 self.print_expr_as_cond(iter);
1873                 self.s.space();
1874                 self.print_block_with_attrs(blk, attrs);
1875             }
1876             ast::ExprKind::Loop(ref blk, opt_label) => {
1877                 if let Some(label) = opt_label {
1878                     self.print_ident(label.ident);
1879                     self.word_space(":");
1880                 }
1881                 self.head("loop");
1882                 self.s.space();
1883                 self.print_block_with_attrs(blk, attrs);
1884             }
1885             ast::ExprKind::Match(ref expr, ref arms) => {
1886                 self.cbox(INDENT_UNIT);
1887                 self.ibox(INDENT_UNIT);
1888                 self.word_nbsp("match");
1889                 self.print_expr_as_cond(expr);
1890                 self.s.space();
1891                 self.bopen();
1892                 self.print_inner_attributes_no_trailing_hardbreak(attrs);
1893                 for arm in arms {
1894                     self.print_arm(arm);
1895                 }
1896                 self.bclose(expr.span);
1897             }
1898             ast::ExprKind::Closure(
1899                 capture_clause,
1900                 asyncness,
1901                 movability,
1902                 ref decl,
1903                 ref body,
1904                 _,
1905             ) => {
1906                 self.print_movability(movability);
1907                 self.print_asyncness(asyncness);
1908                 self.print_capture_clause(capture_clause);
1909
1910                 self.print_fn_params_and_ret(decl, true);
1911                 self.s.space();
1912                 self.print_expr(body);
1913                 self.end(); // need to close a box
1914
1915                 // a box will be closed by print_expr, but we didn't want an overall
1916                 // wrapper so we closed the corresponding opening. so create an
1917                 // empty box to satisfy the close.
1918                 self.ibox(0);
1919             }
1920             ast::ExprKind::Block(ref blk, opt_label) => {
1921                 if let Some(label) = opt_label {
1922                     self.print_ident(label.ident);
1923                     self.word_space(":");
1924                 }
1925                 // containing cbox, will be closed by print-block at }
1926                 self.cbox(INDENT_UNIT);
1927                 // head-box, will be closed by print-block after {
1928                 self.ibox(0);
1929                 self.print_block_with_attrs(blk, attrs);
1930             }
1931             ast::ExprKind::Async(capture_clause, _, ref blk) => {
1932                 self.word_nbsp("async");
1933                 self.print_capture_clause(capture_clause);
1934                 self.s.space();
1935                 // cbox/ibox in analogy to the `ExprKind::Block` arm above
1936                 self.cbox(INDENT_UNIT);
1937                 self.ibox(0);
1938                 self.print_block_with_attrs(blk, attrs);
1939             }
1940             ast::ExprKind::Await(ref expr) => {
1941                 self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX);
1942                 self.s.word(".await");
1943             }
1944             ast::ExprKind::Assign(ref lhs, ref rhs, _) => {
1945                 let prec = AssocOp::Assign.precedence() as i8;
1946                 self.print_expr_maybe_paren(lhs, prec + 1);
1947                 self.s.space();
1948                 self.word_space("=");
1949                 self.print_expr_maybe_paren(rhs, prec);
1950             }
1951             ast::ExprKind::AssignOp(op, ref lhs, ref rhs) => {
1952                 let prec = AssocOp::Assign.precedence() as i8;
1953                 self.print_expr_maybe_paren(lhs, prec + 1);
1954                 self.s.space();
1955                 self.s.word(op.node.to_string());
1956                 self.word_space("=");
1957                 self.print_expr_maybe_paren(rhs, prec);
1958             }
1959             ast::ExprKind::Field(ref expr, ident) => {
1960                 self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX);
1961                 self.s.word(".");
1962                 self.print_ident(ident);
1963             }
1964             ast::ExprKind::Index(ref expr, ref index) => {
1965                 self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX);
1966                 self.s.word("[");
1967                 self.print_expr(index);
1968                 self.s.word("]");
1969             }
1970             ast::ExprKind::Range(ref start, ref end, limits) => {
1971                 // Special case for `Range`.  `AssocOp` claims that `Range` has higher precedence
1972                 // than `Assign`, but `x .. x = x` gives a parse error instead of `x .. (x = x)`.
1973                 // Here we use a fake precedence value so that any child with lower precedence than
1974                 // a "normal" binop gets parenthesized.  (`LOr` is the lowest-precedence binop.)
1975                 let fake_prec = AssocOp::LOr.precedence() as i8;
1976                 if let Some(ref e) = *start {
1977                     self.print_expr_maybe_paren(e, fake_prec);
1978                 }
1979                 if limits == ast::RangeLimits::HalfOpen {
1980                     self.s.word("..");
1981                 } else {
1982                     self.s.word("..=");
1983                 }
1984                 if let Some(ref e) = *end {
1985                     self.print_expr_maybe_paren(e, fake_prec);
1986                 }
1987             }
1988             ast::ExprKind::Path(None, ref path) => self.print_path(path, true, 0),
1989             ast::ExprKind::Path(Some(ref qself), ref path) => self.print_qpath(path, qself, true),
1990             ast::ExprKind::Break(opt_label, ref opt_expr) => {
1991                 self.s.word("break");
1992                 self.s.space();
1993                 if let Some(label) = opt_label {
1994                     self.print_ident(label.ident);
1995                     self.s.space();
1996                 }
1997                 if let Some(ref expr) = *opt_expr {
1998                     self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
1999                     self.s.space();
2000                 }
2001             }
2002             ast::ExprKind::Continue(opt_label) => {
2003                 self.s.word("continue");
2004                 self.s.space();
2005                 if let Some(label) = opt_label {
2006                     self.print_ident(label.ident);
2007                     self.s.space()
2008                 }
2009             }
2010             ast::ExprKind::Ret(ref result) => {
2011                 self.s.word("return");
2012                 if let Some(ref expr) = *result {
2013                     self.s.word(" ");
2014                     self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
2015                 }
2016             }
2017             ast::ExprKind::InlineAsm(ref a) => {
2018                 self.s.word("asm!");
2019                 self.popen();
2020                 self.print_string(&a.asm.as_str(), a.asm_str_style);
2021                 self.word_space(":");
2022
2023                 self.commasep(Inconsistent, &a.outputs, |s, out| {
2024                     let constraint = out.constraint.as_str();
2025                     let mut ch = constraint.chars();
2026                     match ch.next() {
2027                         Some('=') if out.is_rw => {
2028                             s.print_string(&format!("+{}", ch.as_str()), ast::StrStyle::Cooked)
2029                         }
2030                         _ => s.print_string(&constraint, ast::StrStyle::Cooked),
2031                     }
2032                     s.popen();
2033                     s.print_expr(&out.expr);
2034                     s.pclose();
2035                 });
2036                 self.s.space();
2037                 self.word_space(":");
2038
2039                 self.commasep(Inconsistent, &a.inputs, |s, &(co, ref o)| {
2040                     s.print_string(&co.as_str(), ast::StrStyle::Cooked);
2041                     s.popen();
2042                     s.print_expr(o);
2043                     s.pclose();
2044                 });
2045                 self.s.space();
2046                 self.word_space(":");
2047
2048                 self.commasep(Inconsistent, &a.clobbers, |s, co| {
2049                     s.print_string(&co.as_str(), ast::StrStyle::Cooked);
2050                 });
2051
2052                 let mut options = vec![];
2053                 if a.volatile {
2054                     options.push("volatile");
2055                 }
2056                 if a.alignstack {
2057                     options.push("alignstack");
2058                 }
2059                 if a.dialect == ast::AsmDialect::Intel {
2060                     options.push("intel");
2061                 }
2062
2063                 if !options.is_empty() {
2064                     self.s.space();
2065                     self.word_space(":");
2066                     self.commasep(Inconsistent, &options, |s, &co| {
2067                         s.print_string(co, ast::StrStyle::Cooked);
2068                     });
2069                 }
2070
2071                 self.pclose();
2072             }
2073             ast::ExprKind::Mac(ref m) => self.print_mac(m),
2074             ast::ExprKind::Paren(ref e) => {
2075                 self.popen();
2076                 self.print_inner_attributes_inline(attrs);
2077                 self.print_expr(e);
2078                 self.pclose();
2079             }
2080             ast::ExprKind::Yield(ref e) => {
2081                 self.s.word("yield");
2082                 match *e {
2083                     Some(ref expr) => {
2084                         self.s.space();
2085                         self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
2086                     }
2087                     _ => (),
2088                 }
2089             }
2090             ast::ExprKind::Try(ref e) => {
2091                 self.print_expr_maybe_paren(e, parser::PREC_POSTFIX);
2092                 self.s.word("?")
2093             }
2094             ast::ExprKind::TryBlock(ref blk) => {
2095                 self.head("try");
2096                 self.s.space();
2097                 self.print_block_with_attrs(blk, attrs)
2098             }
2099             ast::ExprKind::Err => {
2100                 self.popen();
2101                 self.s.word("/*ERROR*/");
2102                 self.pclose()
2103             }
2104         }
2105         self.ann.post(self, AnnNode::Expr(expr));
2106         self.end();
2107     }
2108
2109     crate fn print_local_decl(&mut self, loc: &ast::Local) {
2110         self.print_pat(&loc.pat);
2111         if let Some(ref ty) = loc.ty {
2112             self.word_space(":");
2113             self.print_type(ty);
2114         }
2115     }
2116
2117     pub fn print_usize(&mut self, i: usize) {
2118         self.s.word(i.to_string())
2119     }
2120
2121     crate fn print_name(&mut self, name: ast::Name) {
2122         self.s.word(name.to_string());
2123         self.ann.post(self, AnnNode::Name(&name))
2124     }
2125
2126     fn print_qpath(&mut self, path: &ast::Path, qself: &ast::QSelf, colons_before_params: bool) {
2127         self.s.word("<");
2128         self.print_type(&qself.ty);
2129         if qself.position > 0 {
2130             self.s.space();
2131             self.word_space("as");
2132             let depth = path.segments.len() - qself.position;
2133             self.print_path(path, false, depth);
2134         }
2135         self.s.word(">");
2136         self.s.word("::");
2137         let item_segment = path.segments.last().unwrap();
2138         self.print_ident(item_segment.ident);
2139         match item_segment.args {
2140             Some(ref args) => self.print_generic_args(args, colons_before_params),
2141             None => {}
2142         }
2143     }
2144
2145     crate fn print_pat(&mut self, pat: &ast::Pat) {
2146         self.maybe_print_comment(pat.span.lo());
2147         self.ann.pre(self, AnnNode::Pat(pat));
2148         /* Pat isn't normalized, but the beauty of it
2149         is that it doesn't matter */
2150         match pat.kind {
2151             PatKind::Wild => self.s.word("_"),
2152             PatKind::Ident(binding_mode, ident, ref sub) => {
2153                 match binding_mode {
2154                     ast::BindingMode::ByRef(mutbl) => {
2155                         self.word_nbsp("ref");
2156                         self.print_mutability(mutbl, false);
2157                     }
2158                     ast::BindingMode::ByValue(ast::Mutability::Not) => {}
2159                     ast::BindingMode::ByValue(ast::Mutability::Mut) => {
2160                         self.word_nbsp("mut");
2161                     }
2162                 }
2163                 self.print_ident(ident);
2164                 if let Some(ref p) = *sub {
2165                     self.s.space();
2166                     self.s.word_space("@");
2167                     self.print_pat(p);
2168                 }
2169             }
2170             PatKind::TupleStruct(ref path, ref elts) => {
2171                 self.print_path(path, true, 0);
2172                 self.popen();
2173                 self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(p));
2174                 self.pclose();
2175             }
2176             PatKind::Or(ref pats) => {
2177                 self.strsep("|", true, Inconsistent, &pats[..], |s, p| s.print_pat(p));
2178             }
2179             PatKind::Path(None, ref path) => {
2180                 self.print_path(path, true, 0);
2181             }
2182             PatKind::Path(Some(ref qself), ref path) => {
2183                 self.print_qpath(path, qself, false);
2184             }
2185             PatKind::Struct(ref path, ref fields, etc) => {
2186                 self.print_path(path, true, 0);
2187                 self.nbsp();
2188                 self.word_space("{");
2189                 self.commasep_cmnt(
2190                     Consistent,
2191                     &fields[..],
2192                     |s, f| {
2193                         s.cbox(INDENT_UNIT);
2194                         if !f.is_shorthand {
2195                             s.print_ident(f.ident);
2196                             s.word_nbsp(":");
2197                         }
2198                         s.print_pat(&f.pat);
2199                         s.end();
2200                     },
2201                     |f| f.pat.span,
2202                 );
2203                 if etc {
2204                     if !fields.is_empty() {
2205                         self.word_space(",");
2206                     }
2207                     self.s.word("..");
2208                 }
2209                 self.s.space();
2210                 self.s.word("}");
2211             }
2212             PatKind::Tuple(ref elts) => {
2213                 self.popen();
2214                 self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(p));
2215                 if elts.len() == 1 {
2216                     self.s.word(",");
2217                 }
2218                 self.pclose();
2219             }
2220             PatKind::Box(ref inner) => {
2221                 self.s.word("box ");
2222                 self.print_pat(inner);
2223             }
2224             PatKind::Ref(ref inner, mutbl) => {
2225                 self.s.word("&");
2226                 if mutbl == ast::Mutability::Mut {
2227                     self.s.word("mut ");
2228                 }
2229                 self.print_pat(inner);
2230             }
2231             PatKind::Lit(ref e) => self.print_expr(&**e),
2232             PatKind::Range(ref begin, ref end, Spanned { node: ref end_kind, .. }) => {
2233                 if let Some(e) = begin {
2234                     self.print_expr(e);
2235                     self.s.space();
2236                 }
2237                 match *end_kind {
2238                     RangeEnd::Included(RangeSyntax::DotDotDot) => self.s.word("..."),
2239                     RangeEnd::Included(RangeSyntax::DotDotEq) => self.s.word("..="),
2240                     RangeEnd::Excluded => self.s.word(".."),
2241                 }
2242                 if let Some(e) = end {
2243                     self.print_expr(e);
2244                 }
2245             }
2246             PatKind::Slice(ref elts) => {
2247                 self.s.word("[");
2248                 self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(p));
2249                 self.s.word("]");
2250             }
2251             PatKind::Rest => self.s.word(".."),
2252             PatKind::Paren(ref inner) => {
2253                 self.popen();
2254                 self.print_pat(inner);
2255                 self.pclose();
2256             }
2257             PatKind::Mac(ref m) => self.print_mac(m),
2258         }
2259         self.ann.post(self, AnnNode::Pat(pat))
2260     }
2261
2262     fn print_arm(&mut self, arm: &ast::Arm) {
2263         // Note, I have no idea why this check is necessary, but here it is.
2264         if arm.attrs.is_empty() {
2265             self.s.space();
2266         }
2267         self.cbox(INDENT_UNIT);
2268         self.ibox(0);
2269         self.maybe_print_comment(arm.pat.span.lo());
2270         self.print_outer_attributes(&arm.attrs);
2271         self.print_pat(&arm.pat);
2272         self.s.space();
2273         if let Some(ref e) = arm.guard {
2274             self.word_space("if");
2275             self.print_expr(e);
2276             self.s.space();
2277         }
2278         self.word_space("=>");
2279
2280         match arm.body.kind {
2281             ast::ExprKind::Block(ref blk, opt_label) => {
2282                 if let Some(label) = opt_label {
2283                     self.print_ident(label.ident);
2284                     self.word_space(":");
2285                 }
2286
2287                 // The block will close the pattern's ibox.
2288                 self.print_block_unclosed_indent(blk);
2289
2290                 // If it is a user-provided unsafe block, print a comma after it.
2291                 if let BlockCheckMode::Unsafe(ast::UserProvided) = blk.rules {
2292                     self.s.word(",");
2293                 }
2294             }
2295             _ => {
2296                 self.end(); // Close the ibox for the pattern.
2297                 self.print_expr(&arm.body);
2298                 self.s.word(",");
2299             }
2300         }
2301         self.end(); // Close enclosing cbox.
2302     }
2303
2304     fn print_explicit_self(&mut self, explicit_self: &ast::ExplicitSelf) {
2305         match explicit_self.node {
2306             SelfKind::Value(m) => {
2307                 self.print_mutability(m, false);
2308                 self.s.word("self")
2309             }
2310             SelfKind::Region(ref lt, m) => {
2311                 self.s.word("&");
2312                 self.print_opt_lifetime(lt);
2313                 self.print_mutability(m, false);
2314                 self.s.word("self")
2315             }
2316             SelfKind::Explicit(ref typ, m) => {
2317                 self.print_mutability(m, false);
2318                 self.s.word("self");
2319                 self.word_space(":");
2320                 self.print_type(typ)
2321             }
2322         }
2323     }
2324
2325     fn print_fn_full(
2326         &mut self,
2327         sig: &ast::FnSig,
2328         name: ast::Ident,
2329         generics: &ast::Generics,
2330         vis: &ast::Visibility,
2331         defaultness: ast::Defaultness,
2332         body: Option<&ast::Block>,
2333         attrs: &[ast::Attribute],
2334     ) {
2335         if body.is_some() {
2336             self.head("");
2337         }
2338         self.print_visibility(vis);
2339         self.print_defaultness(defaultness);
2340         self.print_fn(&sig.decl, sig.header, Some(name), generics);
2341         if let Some(body) = body {
2342             self.nbsp();
2343             self.print_block_with_attrs(body, attrs);
2344         } else {
2345             self.s.word(";");
2346         }
2347     }
2348
2349     crate fn print_fn(
2350         &mut self,
2351         decl: &ast::FnDecl,
2352         header: ast::FnHeader,
2353         name: Option<ast::Ident>,
2354         generics: &ast::Generics,
2355     ) {
2356         self.print_fn_header_info(header);
2357         if let Some(name) = name {
2358             self.nbsp();
2359             self.print_ident(name);
2360         }
2361         self.print_generic_params(&generics.params);
2362         self.print_fn_params_and_ret(decl, false);
2363         self.print_where_clause(&generics.where_clause)
2364     }
2365
2366     crate fn print_fn_params_and_ret(&mut self, decl: &ast::FnDecl, is_closure: bool) {
2367         let (open, close) = if is_closure { ("|", "|") } else { ("(", ")") };
2368         self.word(open);
2369         self.commasep(Inconsistent, &decl.inputs, |s, param| s.print_param(param, is_closure));
2370         self.word(close);
2371         self.print_fn_ret_ty(&decl.output)
2372     }
2373
2374     crate fn print_movability(&mut self, movability: ast::Movability) {
2375         match movability {
2376             ast::Movability::Static => self.word_space("static"),
2377             ast::Movability::Movable => {}
2378         }
2379     }
2380
2381     crate fn print_asyncness(&mut self, asyncness: ast::Async) {
2382         if asyncness.is_async() {
2383             self.word_nbsp("async");
2384         }
2385     }
2386
2387     crate fn print_capture_clause(&mut self, capture_clause: ast::CaptureBy) {
2388         match capture_clause {
2389             ast::CaptureBy::Value => self.word_space("move"),
2390             ast::CaptureBy::Ref => {}
2391         }
2392     }
2393
2394     pub fn print_type_bounds(&mut self, prefix: &'static str, bounds: &[ast::GenericBound]) {
2395         if !bounds.is_empty() {
2396             self.s.word(prefix);
2397             let mut first = true;
2398             for bound in bounds {
2399                 if !(first && prefix.is_empty()) {
2400                     self.nbsp();
2401                 }
2402                 if first {
2403                     first = false;
2404                 } else {
2405                     self.word_space("+");
2406                 }
2407
2408                 match bound {
2409                     GenericBound::Trait(tref, modifier) => {
2410                         if modifier == &TraitBoundModifier::Maybe {
2411                             self.s.word("?");
2412                         }
2413                         self.print_poly_trait_ref(tref);
2414                     }
2415                     GenericBound::Outlives(lt) => self.print_lifetime(*lt),
2416                 }
2417             }
2418         }
2419     }
2420
2421     crate fn print_lifetime(&mut self, lifetime: ast::Lifetime) {
2422         self.print_name(lifetime.ident.name)
2423     }
2424
2425     crate fn print_lifetime_bounds(
2426         &mut self,
2427         lifetime: ast::Lifetime,
2428         bounds: &ast::GenericBounds,
2429     ) {
2430         self.print_lifetime(lifetime);
2431         if !bounds.is_empty() {
2432             self.s.word(": ");
2433             for (i, bound) in bounds.iter().enumerate() {
2434                 if i != 0 {
2435                     self.s.word(" + ");
2436                 }
2437                 match bound {
2438                     ast::GenericBound::Outlives(lt) => self.print_lifetime(*lt),
2439                     _ => panic!(),
2440                 }
2441             }
2442         }
2443     }
2444
2445     crate fn print_generic_params(&mut self, generic_params: &[ast::GenericParam]) {
2446         if generic_params.is_empty() {
2447             return;
2448         }
2449
2450         self.s.word("<");
2451
2452         self.commasep(Inconsistent, &generic_params, |s, param| {
2453             s.print_outer_attributes_inline(&param.attrs);
2454
2455             match param.kind {
2456                 ast::GenericParamKind::Lifetime => {
2457                     let lt = ast::Lifetime { id: param.id, ident: param.ident };
2458                     s.print_lifetime_bounds(lt, &param.bounds)
2459                 }
2460                 ast::GenericParamKind::Type { ref default } => {
2461                     s.print_ident(param.ident);
2462                     s.print_type_bounds(":", &param.bounds);
2463                     if let Some(ref default) = default {
2464                         s.s.space();
2465                         s.word_space("=");
2466                         s.print_type(default)
2467                     }
2468                 }
2469                 ast::GenericParamKind::Const { ref ty } => {
2470                     s.word_space("const");
2471                     s.print_ident(param.ident);
2472                     s.s.space();
2473                     s.word_space(":");
2474                     s.print_type(ty);
2475                     s.print_type_bounds(":", &param.bounds)
2476                 }
2477             }
2478         });
2479
2480         self.s.word(">");
2481     }
2482
2483     crate fn print_where_clause(&mut self, where_clause: &ast::WhereClause) {
2484         if where_clause.predicates.is_empty() {
2485             return;
2486         }
2487
2488         self.s.space();
2489         self.word_space("where");
2490
2491         for (i, predicate) in where_clause.predicates.iter().enumerate() {
2492             if i != 0 {
2493                 self.word_space(",");
2494             }
2495
2496             match *predicate {
2497                 ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
2498                     ref bound_generic_params,
2499                     ref bounded_ty,
2500                     ref bounds,
2501                     ..
2502                 }) => {
2503                     self.print_formal_generic_params(bound_generic_params);
2504                     self.print_type(bounded_ty);
2505                     self.print_type_bounds(":", bounds);
2506                 }
2507                 ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
2508                     ref lifetime,
2509                     ref bounds,
2510                     ..
2511                 }) => {
2512                     self.print_lifetime_bounds(*lifetime, bounds);
2513                 }
2514                 ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
2515                     ref lhs_ty,
2516                     ref rhs_ty,
2517                     ..
2518                 }) => {
2519                     self.print_type(lhs_ty);
2520                     self.s.space();
2521                     self.word_space("=");
2522                     self.print_type(rhs_ty);
2523                 }
2524             }
2525         }
2526     }
2527
2528     crate fn print_use_tree(&mut self, tree: &ast::UseTree) {
2529         match tree.kind {
2530             ast::UseTreeKind::Simple(rename, ..) => {
2531                 self.print_path(&tree.prefix, false, 0);
2532                 if let Some(rename) = rename {
2533                     self.s.space();
2534                     self.word_space("as");
2535                     self.print_ident(rename);
2536                 }
2537             }
2538             ast::UseTreeKind::Glob => {
2539                 if !tree.prefix.segments.is_empty() {
2540                     self.print_path(&tree.prefix, false, 0);
2541                     self.s.word("::");
2542                 }
2543                 self.s.word("*");
2544             }
2545             ast::UseTreeKind::Nested(ref items) => {
2546                 if tree.prefix.segments.is_empty() {
2547                     self.s.word("{");
2548                 } else {
2549                     self.print_path(&tree.prefix, false, 0);
2550                     self.s.word("::{");
2551                 }
2552                 self.commasep(Inconsistent, &items[..], |this, &(ref tree, _)| {
2553                     this.print_use_tree(tree)
2554                 });
2555                 self.s.word("}");
2556             }
2557         }
2558     }
2559
2560     pub fn print_mutability(&mut self, mutbl: ast::Mutability, print_const: bool) {
2561         match mutbl {
2562             ast::Mutability::Mut => self.word_nbsp("mut"),
2563             ast::Mutability::Not => {
2564                 if print_const {
2565                     self.word_nbsp("const");
2566                 }
2567             }
2568         }
2569     }
2570
2571     crate fn print_mt(&mut self, mt: &ast::MutTy, print_const: bool) {
2572         self.print_mutability(mt.mutbl, print_const);
2573         self.print_type(&mt.ty)
2574     }
2575
2576     crate fn print_param(&mut self, input: &ast::Param, is_closure: bool) {
2577         self.ibox(INDENT_UNIT);
2578
2579         self.print_outer_attributes_inline(&input.attrs);
2580
2581         match input.ty.kind {
2582             ast::TyKind::Infer if is_closure => self.print_pat(&input.pat),
2583             _ => {
2584                 if let Some(eself) = input.to_self() {
2585                     self.print_explicit_self(&eself);
2586                 } else {
2587                     let invalid = if let PatKind::Ident(_, ident, _) = input.pat.kind {
2588                         ident.name == kw::Invalid
2589                     } else {
2590                         false
2591                     };
2592                     if !invalid {
2593                         self.print_pat(&input.pat);
2594                         self.s.word(":");
2595                         self.s.space();
2596                     }
2597                     self.print_type(&input.ty);
2598                 }
2599             }
2600         }
2601         self.end();
2602     }
2603
2604     crate fn print_fn_ret_ty(&mut self, fn_ret_ty: &ast::FnRetTy) {
2605         if let ast::FnRetTy::Ty(ty) = fn_ret_ty {
2606             self.space_if_not_bol();
2607             self.ibox(INDENT_UNIT);
2608             self.word_space("->");
2609             self.print_type(ty);
2610             self.end();
2611             self.maybe_print_comment(ty.span.lo());
2612         }
2613     }
2614
2615     crate fn print_ty_fn(
2616         &mut self,
2617         ext: ast::Extern,
2618         unsafety: ast::Unsafe,
2619         decl: &ast::FnDecl,
2620         name: Option<ast::Ident>,
2621         generic_params: &[ast::GenericParam],
2622     ) {
2623         self.ibox(INDENT_UNIT);
2624         if !generic_params.is_empty() {
2625             self.s.word("for");
2626             self.print_generic_params(generic_params);
2627         }
2628         let generics = ast::Generics {
2629             params: Vec::new(),
2630             where_clause: ast::WhereClause { predicates: Vec::new(), span: rustc_span::DUMMY_SP },
2631             span: rustc_span::DUMMY_SP,
2632         };
2633         let header = ast::FnHeader { unsafety, ext, ..ast::FnHeader::default() };
2634         self.print_fn(decl, header, name, &generics);
2635         self.end();
2636     }
2637
2638     crate fn maybe_print_trailing_comment(
2639         &mut self,
2640         span: rustc_span::Span,
2641         next_pos: Option<BytePos>,
2642     ) {
2643         if let Some(cmnts) = self.comments() {
2644             if let Some(cmnt) = cmnts.trailing_comment(span, next_pos) {
2645                 self.print_comment(&cmnt);
2646             }
2647         }
2648     }
2649
2650     crate fn print_remaining_comments(&mut self) {
2651         // If there aren't any remaining comments, then we need to manually
2652         // make sure there is a line break at the end.
2653         if self.next_comment().is_none() {
2654             self.s.hardbreak();
2655         }
2656         while let Some(ref cmnt) = self.next_comment() {
2657             self.print_comment(cmnt);
2658         }
2659     }
2660
2661     crate fn print_fn_header_info(&mut self, header: ast::FnHeader) {
2662         self.print_constness(header.constness);
2663         self.print_asyncness(header.asyncness);
2664         self.print_unsafety(header.unsafety);
2665
2666         match header.ext {
2667             ast::Extern::None => {}
2668             ast::Extern::Implicit => {
2669                 self.word_nbsp("extern");
2670             }
2671             ast::Extern::Explicit(abi) => {
2672                 self.word_nbsp("extern");
2673                 self.print_literal(&abi.as_lit());
2674                 self.nbsp();
2675             }
2676         }
2677
2678         self.s.word("fn")
2679     }
2680
2681     crate fn print_unsafety(&mut self, s: ast::Unsafe) {
2682         match s {
2683             ast::Unsafe::No => {}
2684             ast::Unsafe::Yes(_) => self.word_nbsp("unsafe"),
2685         }
2686     }
2687
2688     crate fn print_constness(&mut self, s: ast::Const) {
2689         match s {
2690             ast::Const::No => {}
2691             ast::Const::Yes(_) => self.word_nbsp("const"),
2692         }
2693     }
2694
2695     crate fn print_is_auto(&mut self, s: ast::IsAuto) {
2696         match s {
2697             ast::IsAuto::Yes => self.word_nbsp("auto"),
2698             ast::IsAuto::No => {}
2699         }
2700     }
2701 }