rustc_ast_pretty/pprust/
state.rs

1//! AST pretty printing.
2//!
3//! Note that HIR pretty printing is layered on top of this crate.
4
5mod expr;
6mod fixup;
7mod item;
8
9use std::borrow::Cow;
10use std::sync::Arc;
11
12use rustc_ast::attr::AttrIdGenerator;
13use rustc_ast::token::{self, CommentKind, Delimiter, DocFragmentKind, Token, TokenKind};
14use rustc_ast::tokenstream::{Spacing, TokenStream, TokenTree};
15use rustc_ast::util::classify;
16use rustc_ast::util::comments::{Comment, CommentStyle};
17use rustc_ast::{
18    self as ast, AttrArgs, BindingMode, BlockCheckMode, ByRef, DelimArgs, GenericArg, GenericBound,
19    InlineAsmOperand, InlineAsmOptions, InlineAsmRegOrRegClass, InlineAsmTemplatePiece, PatKind,
20    RangeEnd, RangeSyntax, Safety, SelfKind, Term, attr,
21};
22use rustc_span::edition::Edition;
23use rustc_span::source_map::{SourceMap, Spanned};
24use rustc_span::symbol::IdentPrinter;
25use rustc_span::{BytePos, CharPos, DUMMY_SP, FileName, Ident, Pos, Span, Symbol, kw, sym};
26
27use crate::pp::Breaks::{Consistent, Inconsistent};
28use crate::pp::{self, BoxMarker, Breaks};
29use crate::pprust::state::fixup::FixupContext;
30
31pub enum MacHeader<'a> {
32    Path(&'a ast::Path),
33    Keyword(&'static str),
34}
35
36pub enum AnnNode<'a> {
37    Ident(&'a Ident),
38    Name(&'a Symbol),
39    Block(&'a ast::Block),
40    Item(&'a ast::Item),
41    SubItem(ast::NodeId),
42    Expr(&'a ast::Expr),
43    Pat(&'a ast::Pat),
44    Crate(&'a ast::Crate),
45}
46
47pub trait PpAnn {
48    fn pre(&self, _state: &mut State<'_>, _node: AnnNode<'_>) {}
49    fn post(&self, _state: &mut State<'_>, _node: AnnNode<'_>) {}
50}
51
52struct NoAnn;
53
54impl PpAnn for NoAnn {}
55
56pub struct Comments<'a> {
57    sm: &'a SourceMap,
58    // Stored in reverse order so we can consume them by popping.
59    reversed_comments: Vec<Comment>,
60}
61
62/// Returns `None` if the first `col` chars of `s` contain a non-whitespace char.
63/// Otherwise returns `Some(k)` where `k` is first char offset after that leading
64/// whitespace. Note that `k` may be outside bounds of `s`.
65fn all_whitespace(s: &str, col: CharPos) -> Option<usize> {
66    let mut idx = 0;
67    for (i, ch) in s.char_indices().take(col.to_usize()) {
68        if !ch.is_whitespace() {
69            return None;
70        }
71        idx = i + ch.len_utf8();
72    }
73    Some(idx)
74}
75
76fn trim_whitespace_prefix(s: &str, col: CharPos) -> &str {
77    let len = s.len();
78    match all_whitespace(s, col) {
79        Some(col) => {
80            if col < len {
81                &s[col..]
82            } else {
83                ""
84            }
85        }
86        None => s,
87    }
88}
89
90fn split_block_comment_into_lines(text: &str, col: CharPos) -> Vec<String> {
91    let mut res: Vec<String> = vec![];
92    let mut lines = text.lines();
93    // just push the first line
94    res.extend(lines.next().map(|it| it.to_string()));
95    // for other lines, strip common whitespace prefix
96    for line in lines {
97        res.push(trim_whitespace_prefix(line, col).to_string())
98    }
99    res
100}
101
102fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comment> {
103    let sm = SourceMap::new(sm.path_mapping().clone());
104    let source_file = sm.new_source_file(path, src);
105    let text = Arc::clone(&(*source_file.src.as_ref().unwrap()));
106
107    let text: &str = text.as_str();
108    let start_bpos = source_file.start_pos;
109    let mut pos = 0;
110    let mut comments: Vec<Comment> = Vec::new();
111    let mut code_to_the_left = false;
112
113    if let Some(shebang_len) = rustc_lexer::strip_shebang(text) {
114        comments.push(Comment {
115            style: CommentStyle::Isolated,
116            lines: vec![text[..shebang_len].to_string()],
117            pos: start_bpos,
118        });
119        pos += shebang_len;
120    }
121
122    for token in rustc_lexer::tokenize(&text[pos..], rustc_lexer::FrontmatterAllowed::Yes) {
123        let token_text = &text[pos..pos + token.len as usize];
124        match token.kind {
125            rustc_lexer::TokenKind::Whitespace => {
126                if let Some(mut idx) = token_text.find('\n') {
127                    code_to_the_left = false;
128                    while let Some(next_newline) = &token_text[idx + 1..].find('\n') {
129                        idx += 1 + next_newline;
130                        comments.push(Comment {
131                            style: CommentStyle::BlankLine,
132                            lines: vec![],
133                            pos: start_bpos + BytePos((pos + idx) as u32),
134                        });
135                    }
136                }
137            }
138            rustc_lexer::TokenKind::BlockComment { doc_style, .. } => {
139                if doc_style.is_none() {
140                    let code_to_the_right = !matches!(
141                        text[pos + token.len as usize..].chars().next(),
142                        Some('\r' | '\n')
143                    );
144                    let style = match (code_to_the_left, code_to_the_right) {
145                        (_, true) => CommentStyle::Mixed,
146                        (false, false) => CommentStyle::Isolated,
147                        (true, false) => CommentStyle::Trailing,
148                    };
149
150                    // Count the number of chars since the start of the line by rescanning.
151                    let pos_in_file = start_bpos + BytePos(pos as u32);
152                    let line_begin_in_file = source_file.line_begin_pos(pos_in_file);
153                    let line_begin_pos = (line_begin_in_file - start_bpos).to_usize();
154                    let col = CharPos(text[line_begin_pos..pos].chars().count());
155
156                    let lines = split_block_comment_into_lines(token_text, col);
157                    comments.push(Comment { style, lines, pos: pos_in_file })
158                }
159            }
160            rustc_lexer::TokenKind::LineComment { doc_style } => {
161                if doc_style.is_none() {
162                    comments.push(Comment {
163                        style: if code_to_the_left {
164                            CommentStyle::Trailing
165                        } else {
166                            CommentStyle::Isolated
167                        },
168                        lines: vec![token_text.to_string()],
169                        pos: start_bpos + BytePos(pos as u32),
170                    })
171                }
172            }
173            rustc_lexer::TokenKind::Frontmatter { .. } => {
174                code_to_the_left = false;
175                comments.push(Comment {
176                    style: CommentStyle::Isolated,
177                    lines: vec![token_text.to_string()],
178                    pos: start_bpos + BytePos(pos as u32),
179                });
180            }
181            _ => {
182                code_to_the_left = true;
183            }
184        }
185        pos += token.len as usize;
186    }
187
188    comments
189}
190
191impl<'a> Comments<'a> {
192    pub fn new(sm: &'a SourceMap, filename: FileName, input: String) -> Comments<'a> {
193        let mut comments = gather_comments(sm, filename, input);
194        comments.reverse();
195        Comments { sm, reversed_comments: comments }
196    }
197
198    fn peek(&self) -> Option<&Comment> {
199        self.reversed_comments.last()
200    }
201
202    fn next(&mut self) -> Option<Comment> {
203        self.reversed_comments.pop()
204    }
205
206    fn trailing_comment(
207        &mut self,
208        span: rustc_span::Span,
209        next_pos: Option<BytePos>,
210    ) -> Option<Comment> {
211        if let Some(cmnt) = self.peek() {
212            if cmnt.style != CommentStyle::Trailing {
213                return None;
214            }
215            let span_line = self.sm.lookup_char_pos(span.hi());
216            let comment_line = self.sm.lookup_char_pos(cmnt.pos);
217            let next = next_pos.unwrap_or_else(|| cmnt.pos + BytePos(1));
218            if span.hi() < cmnt.pos && cmnt.pos < next && span_line.line == comment_line.line {
219                return Some(self.next().unwrap());
220            }
221        }
222
223        None
224    }
225}
226
227pub struct State<'a> {
228    pub s: pp::Printer,
229    comments: Option<Comments<'a>>,
230    ann: &'a (dyn PpAnn + 'a),
231    is_sdylib_interface: bool,
232}
233
234const INDENT_UNIT: isize = 4;
235
236/// Requires you to pass an input filename and reader so that
237/// it can scan the input text for comments to copy forward.
238pub fn print_crate<'a>(
239    sm: &'a SourceMap,
240    krate: &ast::Crate,
241    filename: FileName,
242    input: String,
243    ann: &'a dyn PpAnn,
244    is_expanded: bool,
245    edition: Edition,
246    g: &AttrIdGenerator,
247) -> String {
248    let mut s = State {
249        s: pp::Printer::new(),
250        comments: Some(Comments::new(sm, filename, input)),
251        ann,
252        is_sdylib_interface: false,
253    };
254
255    print_crate_inner(&mut s, krate, is_expanded, edition, g);
256    s.s.eof()
257}
258
259pub fn print_crate_as_interface(
260    krate: &ast::Crate,
261    edition: Edition,
262    g: &AttrIdGenerator,
263) -> String {
264    let mut s =
265        State { s: pp::Printer::new(), comments: None, ann: &NoAnn, is_sdylib_interface: true };
266
267    print_crate_inner(&mut s, krate, false, edition, g);
268    s.s.eof()
269}
270
271fn print_crate_inner<'a>(
272    s: &mut State<'a>,
273    krate: &ast::Crate,
274    is_expanded: bool,
275    edition: Edition,
276    g: &AttrIdGenerator,
277) {
278    // We need to print shebang before anything else
279    // otherwise the resulting code will not compile
280    // and shebang will be useless.
281    s.maybe_print_shebang();
282
283    if is_expanded && !krate.attrs.iter().any(|attr| attr.has_name(sym::no_core)) {
284        // We need to print `#![no_std]` (and its feature gate) so that
285        // compiling pretty-printed source won't inject libstd again.
286        // However, we don't want these attributes in the AST because
287        // of the feature gate, so we fake them up here.
288
289        // `#![feature(prelude_import)]`
290        let fake_attr = attr::mk_attr_nested_word(
291            g,
292            ast::AttrStyle::Inner,
293            Safety::Default,
294            sym::feature,
295            sym::prelude_import,
296            DUMMY_SP,
297        );
298        s.print_attribute(&fake_attr);
299
300        // Currently, in Rust 2018 we don't have `extern crate std;` at the crate
301        // root, so this is not needed, and actually breaks things.
302        if edition.is_rust_2015() {
303            // `#![no_std]`
304            let fake_attr = attr::mk_attr_word(
305                g,
306                ast::AttrStyle::Inner,
307                Safety::Default,
308                sym::no_std,
309                DUMMY_SP,
310            );
311            s.print_attribute(&fake_attr);
312        }
313    }
314
315    s.print_inner_attributes(&krate.attrs);
316    for item in &krate.items {
317        s.print_item(item);
318    }
319    s.print_remaining_comments();
320    s.ann.post(s, AnnNode::Crate(krate));
321}
322
323/// Should two consecutive tokens be printed with a space between them?
324///
325/// Note: some old proc macros parse pretty-printed output, so changes here can
326/// break old code. For example:
327/// - #63896: `#[allow(unused,` must be printed rather than `#[allow(unused ,`
328/// - #73345: `#[allow(unused)]` must be printed rather than `# [allow(unused)]`
329///
330fn space_between(tt1: &TokenTree, tt2: &TokenTree) -> bool {
331    use Delimiter::*;
332    use TokenTree::{Delimited as Del, Token as Tok};
333    use token::*;
334
335    fn is_punct(tt: &TokenTree) -> bool {
336        matches!(tt, TokenTree::Token(tok, _) if tok.is_punct())
337    }
338
339    // Each match arm has one or more examples in comments. The default is to
340    // insert space between adjacent tokens, except for the cases listed in
341    // this match.
342    match (tt1, tt2) {
343        // No space after line doc comments.
344        (Tok(Token { kind: DocComment(CommentKind::Line, ..), .. }, _), _) => false,
345
346        // `.` + NON-PUNCT: `x.y`, `tup.0`
347        (Tok(Token { kind: Dot, .. }, _), tt2) if !is_punct(tt2) => false,
348
349        // `$` + IDENT: `$e`
350        (Tok(Token { kind: Dollar, .. }, _), Tok(Token { kind: Ident(..), .. }, _)) => false,
351
352        // NON-PUNCT + `,`: `foo,`
353        // NON-PUNCT + `;`: `x = 3;`, `[T; 3]`
354        // NON-PUNCT + `.`: `x.y`, `tup.0`
355        (tt1, Tok(Token { kind: Comma | Semi | Dot, .. }, _)) if !is_punct(tt1) => false,
356
357        // IDENT + `!`: `println!()`, but `if !x { ... }` needs a space after the `if`
358        (Tok(Token { kind: Ident(sym, is_raw), span }, _), Tok(Token { kind: Bang, .. }, _))
359            if !Ident::new(*sym, *span).is_reserved() || matches!(is_raw, IdentIsRaw::Yes) =>
360        {
361            false
362        }
363
364        // IDENT|`fn`|`Self`|`pub` + `(`: `f(3)`, `fn(x: u8)`, `Self()`, `pub(crate)`,
365        //      but `let (a, b) = (1, 2)` needs a space after the `let`
366        (Tok(Token { kind: Ident(sym, is_raw), span }, _), Del(_, _, Parenthesis, _))
367            if !Ident::new(*sym, *span).is_reserved()
368                || *sym == kw::Fn
369                || *sym == kw::SelfUpper
370                || *sym == kw::Pub
371                || matches!(is_raw, IdentIsRaw::Yes) =>
372        {
373            false
374        }
375
376        // `#` + `[`: `#[attr]`
377        (Tok(Token { kind: Pound, .. }, _), Del(_, _, Bracket, _)) => false,
378
379        _ => true,
380    }
381}
382
383pub fn doc_comment_to_string(
384    fragment_kind: DocFragmentKind,
385    attr_style: ast::AttrStyle,
386    data: Symbol,
387) -> String {
388    match fragment_kind {
389        DocFragmentKind::Sugared(comment_kind) => match (comment_kind, attr_style) {
390            (CommentKind::Line, ast::AttrStyle::Outer) => format!("///{data}"),
391            (CommentKind::Line, ast::AttrStyle::Inner) => format!("//!{data}"),
392            (CommentKind::Block, ast::AttrStyle::Outer) => format!("/**{data}*/"),
393            (CommentKind::Block, ast::AttrStyle::Inner) => format!("/*!{data}*/"),
394        },
395        DocFragmentKind::Raw(_) => {
396            format!(
397                "#{}[doc = {:?}]",
398                if attr_style == ast::AttrStyle::Inner { "!" } else { "" },
399                data.to_string(),
400            )
401        }
402    }
403}
404
405fn literal_to_string(lit: token::Lit) -> String {
406    let token::Lit { kind, symbol, suffix } = lit;
407    let mut out = match kind {
408        token::Byte => format!("b'{symbol}'"),
409        token::Char => format!("'{symbol}'"),
410        token::Str => format!("\"{symbol}\""),
411        token::StrRaw(n) => {
412            format!("r{delim}\"{string}\"{delim}", delim = "#".repeat(n as usize), string = symbol)
413        }
414        token::ByteStr => format!("b\"{symbol}\""),
415        token::ByteStrRaw(n) => {
416            format!("br{delim}\"{string}\"{delim}", delim = "#".repeat(n as usize), string = symbol)
417        }
418        token::CStr => format!("c\"{symbol}\""),
419        token::CStrRaw(n) => {
420            format!("cr{delim}\"{symbol}\"{delim}", delim = "#".repeat(n as usize))
421        }
422        token::Integer | token::Float | token::Bool | token::Err(_) => symbol.to_string(),
423    };
424
425    if let Some(suffix) = suffix {
426        out.push_str(suffix.as_str())
427    }
428
429    out
430}
431
432impl std::ops::Deref for State<'_> {
433    type Target = pp::Printer;
434    fn deref(&self) -> &Self::Target {
435        &self.s
436    }
437}
438
439impl std::ops::DerefMut for State<'_> {
440    fn deref_mut(&mut self) -> &mut Self::Target {
441        &mut self.s
442    }
443}
444
445/// This trait is used for both AST and HIR pretty-printing.
446pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::DerefMut {
447    fn comments(&self) -> Option<&Comments<'a>>;
448    fn comments_mut(&mut self) -> Option<&mut Comments<'a>>;
449    fn ann_post(&mut self, ident: Ident);
450    fn print_generic_args(&mut self, args: &ast::GenericArgs, colons_before_params: bool);
451
452    fn print_ident(&mut self, ident: Ident) {
453        self.word(IdentPrinter::for_ast_ident(ident, ident.guess_print_mode()).to_string());
454        self.ann_post(ident)
455    }
456
457    fn strsep<'x, T: 'x, F, I>(
458        &mut self,
459        sep: &'static str,
460        space_before: bool,
461        b: Breaks,
462        elts: I,
463        mut op: F,
464    ) where
465        F: FnMut(&mut Self, &T),
466        I: IntoIterator<Item = &'x T>,
467    {
468        let mut it = elts.into_iter();
469
470        let rb = self.rbox(0, b);
471        if let Some(first) = it.next() {
472            op(self, first);
473            for elt in it {
474                if space_before {
475                    self.space();
476                }
477                self.word_space(sep);
478                op(self, elt);
479            }
480        }
481        self.end(rb);
482    }
483
484    fn commasep<'x, T: 'x, F, I>(&mut self, b: Breaks, elts: I, op: F)
485    where
486        F: FnMut(&mut Self, &T),
487        I: IntoIterator<Item = &'x T>,
488    {
489        self.strsep(",", false, b, elts, op)
490    }
491
492    fn maybe_print_comment(&mut self, pos: BytePos) -> bool {
493        let mut has_comment = false;
494        while let Some(cmnt) = self.peek_comment() {
495            if cmnt.pos >= pos {
496                break;
497            }
498            has_comment = true;
499            let cmnt = self.next_comment().unwrap();
500            self.print_comment(cmnt);
501        }
502        has_comment
503    }
504
505    fn print_comment(&mut self, cmnt: Comment) {
506        match cmnt.style {
507            CommentStyle::Mixed => {
508                if !self.is_beginning_of_line() {
509                    self.zerobreak();
510                }
511                if let Some((last, lines)) = cmnt.lines.split_last() {
512                    let ib = self.ibox(0);
513
514                    for line in lines {
515                        self.word(line.clone());
516                        self.hardbreak()
517                    }
518
519                    self.word(last.clone());
520                    self.space();
521
522                    self.end(ib);
523                }
524                self.zerobreak()
525            }
526            CommentStyle::Isolated => {
527                self.hardbreak_if_not_bol();
528                for line in &cmnt.lines {
529                    // Don't print empty lines because they will end up as trailing
530                    // whitespace.
531                    if !line.is_empty() {
532                        self.word(line.clone());
533                    }
534                    self.hardbreak();
535                }
536            }
537            CommentStyle::Trailing => {
538                if !self.is_beginning_of_line() {
539                    self.word(" ");
540                }
541                if let [line] = cmnt.lines.as_slice() {
542                    self.word(line.clone());
543                    self.hardbreak()
544                } else {
545                    let vb = self.visual_align();
546                    for line in &cmnt.lines {
547                        if !line.is_empty() {
548                            self.word(line.clone());
549                        }
550                        self.hardbreak();
551                    }
552                    self.end(vb);
553                }
554            }
555            CommentStyle::BlankLine => {
556                // We need to do at least one, possibly two hardbreaks.
557                let twice = match self.last_token() {
558                    Some(pp::Token::String(s)) => ";" == s,
559                    Some(pp::Token::Begin(_)) => true,
560                    Some(pp::Token::End) => true,
561                    _ => false,
562                };
563                if twice {
564                    self.hardbreak();
565                }
566                self.hardbreak();
567            }
568        }
569    }
570
571    fn peek_comment<'b>(&'b self) -> Option<&'b Comment>
572    where
573        'a: 'b,
574    {
575        self.comments().and_then(|c| c.peek())
576    }
577
578    fn next_comment(&mut self) -> Option<Comment> {
579        self.comments_mut().and_then(|c| c.next())
580    }
581
582    fn maybe_print_trailing_comment(&mut self, span: rustc_span::Span, next_pos: Option<BytePos>) {
583        if let Some(cmnts) = self.comments_mut()
584            && let Some(cmnt) = cmnts.trailing_comment(span, next_pos)
585        {
586            self.print_comment(cmnt);
587        }
588    }
589
590    fn print_remaining_comments(&mut self) {
591        // If there aren't any remaining comments, then we need to manually
592        // make sure there is a line break at the end.
593        if self.peek_comment().is_none() {
594            self.hardbreak();
595        }
596        while let Some(cmnt) = self.next_comment() {
597            self.print_comment(cmnt)
598        }
599    }
600
601    fn print_string(&mut self, st: &str, style: ast::StrStyle) {
602        let st = match style {
603            ast::StrStyle::Cooked => format!("\"{}\"", st.escape_debug()),
604            ast::StrStyle::Raw(n) => {
605                format!("r{delim}\"{string}\"{delim}", delim = "#".repeat(n as usize), string = st)
606            }
607        };
608        self.word(st)
609    }
610
611    fn maybe_print_shebang(&mut self) {
612        if let Some(cmnt) = self.peek_comment() {
613            // Comment is a shebang if it's:
614            // Isolated, starts with #! and doesn't continue with `[`
615            // See [rustc_lexer::strip_shebang] and [gather_comments] from pprust/state.rs for details
616            if cmnt.style == CommentStyle::Isolated
617                && cmnt.lines.first().map_or(false, |l| l.starts_with("#!"))
618            {
619                let cmnt = self.next_comment().unwrap();
620                self.print_comment(cmnt);
621            }
622        }
623    }
624
625    fn print_inner_attributes(&mut self, attrs: &[ast::Attribute]) -> bool {
626        self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, true)
627    }
628
629    fn print_outer_attributes(&mut self, attrs: &[ast::Attribute]) -> bool {
630        self.print_either_attributes(attrs, ast::AttrStyle::Outer, false, true)
631    }
632
633    fn print_either_attributes(
634        &mut self,
635        attrs: &[ast::Attribute],
636        kind: ast::AttrStyle,
637        is_inline: bool,
638        trailing_hardbreak: bool,
639    ) -> bool {
640        let mut printed = false;
641        for attr in attrs {
642            if attr.style == kind {
643                if self.print_attribute_inline(attr, is_inline) {
644                    if is_inline {
645                        self.nbsp();
646                    }
647                    printed = true;
648                }
649            }
650        }
651        if printed && trailing_hardbreak && !is_inline {
652            self.hardbreak_if_not_bol();
653        }
654        printed
655    }
656
657    fn print_attribute_inline(&mut self, attr: &ast::Attribute, is_inline: bool) -> bool {
658        if attr.has_name(sym::cfg_trace) || attr.has_name(sym::cfg_attr_trace) {
659            // It's not a valid identifier, so avoid printing it
660            // to keep the printed code reasonably parse-able.
661            return false;
662        }
663        if !is_inline {
664            self.hardbreak_if_not_bol();
665        }
666        self.maybe_print_comment(attr.span.lo());
667        match &attr.kind {
668            ast::AttrKind::Normal(normal) => {
669                match attr.style {
670                    ast::AttrStyle::Inner => self.word("#!["),
671                    ast::AttrStyle::Outer => self.word("#["),
672                }
673                self.print_attr_item(&normal.item, attr.span);
674                self.word("]");
675            }
676            ast::AttrKind::DocComment(comment_kind, data) => {
677                self.word(doc_comment_to_string(
678                    DocFragmentKind::Sugared(*comment_kind),
679                    attr.style,
680                    *data,
681                ));
682                self.hardbreak()
683            }
684        }
685        true
686    }
687
688    fn print_attr_item(&mut self, item: &ast::AttrItem, span: Span) {
689        let ib = self.ibox(0);
690        match item.unsafety {
691            ast::Safety::Unsafe(_) => {
692                self.word("unsafe");
693                self.popen();
694            }
695            ast::Safety::Default | ast::Safety::Safe(_) => {}
696        }
697        match &item.args {
698            AttrArgs::Delimited(DelimArgs { dspan: _, delim, tokens }) => self.print_mac_common(
699                Some(MacHeader::Path(&item.path)),
700                false,
701                None,
702                *delim,
703                None,
704                tokens,
705                true,
706                span,
707            ),
708            AttrArgs::Empty => {
709                self.print_path(&item.path, false, 0);
710            }
711            AttrArgs::Eq { expr, .. } => {
712                self.print_path(&item.path, false, 0);
713                self.space();
714                self.word_space("=");
715                let token_str = self.expr_to_string(expr);
716                self.word(token_str);
717            }
718        }
719        match item.unsafety {
720            ast::Safety::Unsafe(_) => self.pclose(),
721            ast::Safety::Default | ast::Safety::Safe(_) => {}
722        }
723        self.end(ib);
724    }
725
726    /// This doesn't deserve to be called "pretty" printing, but it should be
727    /// meaning-preserving. A quick hack that might help would be to look at the
728    /// spans embedded in the TTs to decide where to put spaces and newlines.
729    /// But it'd be better to parse these according to the grammar of the
730    /// appropriate macro, transcribe back into the grammar we just parsed from,
731    /// and then pretty-print the resulting AST nodes (so, e.g., we print
732    /// expression arguments as expressions). It can be done! I think.
733    fn print_tt(&mut self, tt: &TokenTree, convert_dollar_crate: bool) -> Spacing {
734        match tt {
735            TokenTree::Token(token, spacing) => {
736                let token_str = self.token_to_string_ext(token, convert_dollar_crate);
737                self.word(token_str);
738                if let token::DocComment(..) = token.kind {
739                    self.hardbreak()
740                }
741                *spacing
742            }
743            TokenTree::Delimited(dspan, spacing, delim, tts) => {
744                self.print_mac_common(
745                    None,
746                    false,
747                    None,
748                    *delim,
749                    Some(spacing.open),
750                    tts,
751                    convert_dollar_crate,
752                    dspan.entire(),
753                );
754                spacing.close
755            }
756        }
757    }
758
759    // The easiest way to implement token stream pretty printing would be to
760    // print each token followed by a single space. But that would produce ugly
761    // output, so we go to some effort to do better.
762    //
763    // First, we track whether each token that appears in source code is
764    // followed by a space, with `Spacing`, and reproduce that in the output.
765    // This works well in a lot of cases. E.g. `stringify!(x + y)` produces
766    // "x + y" and `stringify!(x+y)` produces "x+y".
767    //
768    // But this doesn't work for code produced by proc macros (which have no
769    // original source text representation) nor for code produced by decl
770    // macros (which are tricky because the whitespace after tokens appearing
771    // in macro rules isn't always what you want in the produced output). For
772    // these we mostly use `Spacing::Alone`, which is the conservative choice.
773    //
774    // So we have a backup mechanism for when `Spacing::Alone` occurs between a
775    // pair of tokens: we check if that pair of tokens can obviously go
776    // together without a space between them. E.g. token `x` followed by token
777    // `,` is better printed as `x,` than `x ,`. (Even if the original source
778    // code was `x ,`.)
779    //
780    // Finally, we must be careful about changing the output. Token pretty
781    // printing is used by `stringify!` and `impl Display for
782    // proc_macro::TokenStream`, and some programs rely on the output having a
783    // particular form, even though they shouldn't. In particular, some proc
784    // macros do `format!({stream})` on a token stream and then "parse" the
785    // output with simple string matching that can't handle whitespace changes.
786    // E.g. we have seen cases where a proc macro can handle `a :: b` but not
787    // `a::b`. See #117433 for some examples.
788    fn print_tts(&mut self, tts: &TokenStream, convert_dollar_crate: bool) {
789        let mut iter = tts.iter().peekable();
790        while let Some(tt) = iter.next() {
791            let spacing = self.print_tt(tt, convert_dollar_crate);
792            if let Some(next) = iter.peek() {
793                if spacing == Spacing::Alone && space_between(tt, next) {
794                    self.space();
795                }
796            }
797        }
798    }
799
800    fn print_mac_common(
801        &mut self,
802        header: Option<MacHeader<'_>>,
803        has_bang: bool,
804        ident: Option<Ident>,
805        delim: Delimiter,
806        open_spacing: Option<Spacing>,
807        tts: &TokenStream,
808        convert_dollar_crate: bool,
809        span: Span,
810    ) {
811        let cb = (delim == Delimiter::Brace).then(|| self.cbox(INDENT_UNIT));
812        match header {
813            Some(MacHeader::Path(path)) => self.print_path(path, false, 0),
814            Some(MacHeader::Keyword(kw)) => self.word(kw),
815            None => {}
816        }
817        if has_bang {
818            self.word("!");
819        }
820        if let Some(ident) = ident {
821            self.nbsp();
822            self.print_ident(ident);
823        }
824        match delim {
825            Delimiter::Brace => {
826                if header.is_some() || has_bang || ident.is_some() {
827                    self.nbsp();
828                }
829                self.word("{");
830
831                // Respect `Alone`, if provided, and print a space. Unless the list is empty.
832                let open_space = (open_spacing == None || open_spacing == Some(Spacing::Alone))
833                    && !tts.is_empty();
834                if open_space {
835                    self.space();
836                }
837                let ib = self.ibox(0);
838                self.print_tts(tts, convert_dollar_crate);
839                self.end(ib);
840
841                // Use `open_space` for the spacing *before* the closing delim.
842                // Because spacing on delimiters is lost when going through
843                // proc macros, and otherwise we can end up with ugly cases
844                // like `{ x}`. Symmetry is better.
845                self.bclose(span, !open_space, cb.unwrap());
846            }
847            delim => {
848                // `open_spacing` is ignored. We never print spaces after
849                // non-brace opening delims or before non-brace closing delims.
850                let token_str = self.token_kind_to_string(&delim.as_open_token_kind());
851                self.word(token_str);
852                let ib = self.ibox(0);
853                self.print_tts(tts, convert_dollar_crate);
854                self.end(ib);
855                let token_str = self.token_kind_to_string(&delim.as_close_token_kind());
856                self.word(token_str);
857            }
858        }
859    }
860
861    fn print_mac_def(
862        &mut self,
863        macro_def: &ast::MacroDef,
864        ident: &Ident,
865        sp: Span,
866        print_visibility: impl FnOnce(&mut Self),
867    ) {
868        if let Some(eii_extern_target) = &macro_def.eii_extern_target {
869            self.word("#[eii_extern_target(");
870            self.print_path(&eii_extern_target.extern_item_path, false, 0);
871            if eii_extern_target.impl_unsafe {
872                self.word(",");
873                self.space();
874                self.word("unsafe");
875            }
876            self.word(")]");
877            self.hardbreak();
878        }
879        let (kw, has_bang) = if macro_def.macro_rules {
880            ("macro_rules", true)
881        } else {
882            print_visibility(self);
883            ("macro", false)
884        };
885        self.print_mac_common(
886            Some(MacHeader::Keyword(kw)),
887            has_bang,
888            Some(*ident),
889            macro_def.body.delim,
890            None,
891            &macro_def.body.tokens,
892            true,
893            sp,
894        );
895        if macro_def.body.need_semicolon() {
896            self.word(";");
897        }
898    }
899
900    fn print_path(&mut self, path: &ast::Path, colons_before_params: bool, depth: usize) {
901        self.maybe_print_comment(path.span.lo());
902
903        for (i, segment) in path.segments[..path.segments.len() - depth].iter().enumerate() {
904            if i > 0 {
905                self.word("::")
906            }
907            self.print_path_segment(segment, colons_before_params);
908        }
909    }
910
911    fn print_path_segment(&mut self, segment: &ast::PathSegment, colons_before_params: bool) {
912        if segment.ident.name != kw::PathRoot {
913            self.print_ident(segment.ident);
914            if let Some(args) = &segment.args {
915                self.print_generic_args(args, colons_before_params);
916            }
917        }
918    }
919
920    fn head<S: Into<Cow<'static, str>>>(&mut self, w: S) -> (BoxMarker, BoxMarker) {
921        let w = w.into();
922        // Outer-box is consistent.
923        let cb = self.cbox(INDENT_UNIT);
924        // Head-box is inconsistent.
925        let ib = self.ibox(0);
926        // Keyword that starts the head.
927        if !w.is_empty() {
928            self.word_nbsp(w);
929        }
930        (cb, ib)
931    }
932
933    fn bopen(&mut self, ib: BoxMarker) {
934        self.word("{");
935        self.end(ib);
936    }
937
938    fn bclose_maybe_open(&mut self, span: rustc_span::Span, no_space: bool, cb: Option<BoxMarker>) {
939        let has_comment = self.maybe_print_comment(span.hi());
940        if !no_space || has_comment {
941            self.break_offset_if_not_bol(1, -INDENT_UNIT);
942        }
943        self.word("}");
944        if let Some(cb) = cb {
945            self.end(cb);
946        }
947    }
948
949    fn bclose(&mut self, span: rustc_span::Span, no_space: bool, cb: BoxMarker) {
950        let cb = Some(cb);
951        self.bclose_maybe_open(span, no_space, cb)
952    }
953
954    fn break_offset_if_not_bol(&mut self, n: usize, off: isize) {
955        if !self.is_beginning_of_line() {
956            self.break_offset(n, off)
957        } else if off != 0 {
958            if let Some(last_token) = self.last_token_still_buffered() {
959                if last_token.is_hardbreak_tok() {
960                    // We do something pretty sketchy here: tuck the nonzero
961                    // offset-adjustment we were going to deposit along with the
962                    // break into the previous hardbreak.
963                    self.replace_last_token_still_buffered(pp::Printer::hardbreak_tok_offset(off));
964                }
965            }
966        }
967    }
968
969    /// Print the token kind precisely, without converting `$crate` into its respective crate name.
970    fn token_kind_to_string(&self, tok: &TokenKind) -> Cow<'static, str> {
971        self.token_kind_to_string_ext(tok, None)
972    }
973
974    fn token_kind_to_string_ext(
975        &self,
976        tok: &TokenKind,
977        convert_dollar_crate: Option<Span>,
978    ) -> Cow<'static, str> {
979        match *tok {
980            token::Eq => "=".into(),
981            token::Lt => "<".into(),
982            token::Le => "<=".into(),
983            token::EqEq => "==".into(),
984            token::Ne => "!=".into(),
985            token::Ge => ">=".into(),
986            token::Gt => ">".into(),
987            token::Bang => "!".into(),
988            token::Tilde => "~".into(),
989            token::OrOr => "||".into(),
990            token::AndAnd => "&&".into(),
991            token::Plus => "+".into(),
992            token::Minus => "-".into(),
993            token::Star => "*".into(),
994            token::Slash => "/".into(),
995            token::Percent => "%".into(),
996            token::Caret => "^".into(),
997            token::And => "&".into(),
998            token::Or => "|".into(),
999            token::Shl => "<<".into(),
1000            token::Shr => ">>".into(),
1001            token::PlusEq => "+=".into(),
1002            token::MinusEq => "-=".into(),
1003            token::StarEq => "*=".into(),
1004            token::SlashEq => "/=".into(),
1005            token::PercentEq => "%=".into(),
1006            token::CaretEq => "^=".into(),
1007            token::AndEq => "&=".into(),
1008            token::OrEq => "|=".into(),
1009            token::ShlEq => "<<=".into(),
1010            token::ShrEq => ">>=".into(),
1011
1012            /* Structural symbols */
1013            token::At => "@".into(),
1014            token::Dot => ".".into(),
1015            token::DotDot => "..".into(),
1016            token::DotDotDot => "...".into(),
1017            token::DotDotEq => "..=".into(),
1018            token::Comma => ",".into(),
1019            token::Semi => ";".into(),
1020            token::Colon => ":".into(),
1021            token::PathSep => "::".into(),
1022            token::RArrow => "->".into(),
1023            token::LArrow => "<-".into(),
1024            token::FatArrow => "=>".into(),
1025            token::OpenParen => "(".into(),
1026            token::CloseParen => ")".into(),
1027            token::OpenBracket => "[".into(),
1028            token::CloseBracket => "]".into(),
1029            token::OpenBrace => "{".into(),
1030            token::CloseBrace => "}".into(),
1031            token::OpenInvisible(_) | token::CloseInvisible(_) => "".into(),
1032            token::Pound => "#".into(),
1033            token::Dollar => "$".into(),
1034            token::Question => "?".into(),
1035            token::SingleQuote => "'".into(),
1036
1037            /* Literals */
1038            token::Literal(lit) => literal_to_string(lit).into(),
1039
1040            /* Name components */
1041            token::Ident(name, is_raw) => {
1042                IdentPrinter::new(name, is_raw.to_print_mode_ident(), convert_dollar_crate)
1043                    .to_string()
1044                    .into()
1045            }
1046            token::NtIdent(ident, is_raw) => {
1047                IdentPrinter::for_ast_ident(ident, is_raw.to_print_mode_ident()).to_string().into()
1048            }
1049
1050            token::Lifetime(name, is_raw) | token::NtLifetime(Ident { name, .. }, is_raw) => {
1051                IdentPrinter::new(name, is_raw.to_print_mode_lifetime(), None).to_string().into()
1052            }
1053
1054            /* Other */
1055            token::DocComment(comment_kind, attr_style, data) => {
1056                doc_comment_to_string(DocFragmentKind::Sugared(comment_kind), attr_style, data)
1057                    .into()
1058            }
1059            token::Eof => "<eof>".into(),
1060        }
1061    }
1062
1063    /// Print the token precisely, without converting `$crate` into its respective crate name.
1064    fn token_to_string(&self, token: &Token) -> Cow<'static, str> {
1065        self.token_to_string_ext(token, false)
1066    }
1067
1068    fn token_to_string_ext(&self, token: &Token, convert_dollar_crate: bool) -> Cow<'static, str> {
1069        let convert_dollar_crate = convert_dollar_crate.then_some(token.span);
1070        self.token_kind_to_string_ext(&token.kind, convert_dollar_crate)
1071    }
1072
1073    fn ty_to_string(&self, ty: &ast::Ty) -> String {
1074        Self::to_string(|s| s.print_type(ty))
1075    }
1076
1077    fn pat_to_string(&self, pat: &ast::Pat) -> String {
1078        Self::to_string(|s| s.print_pat(pat))
1079    }
1080
1081    fn expr_to_string(&self, e: &ast::Expr) -> String {
1082        Self::to_string(|s| s.print_expr(e, FixupContext::default()))
1083    }
1084
1085    fn meta_item_lit_to_string(&self, lit: &ast::MetaItemLit) -> String {
1086        Self::to_string(|s| s.print_meta_item_lit(lit))
1087    }
1088
1089    fn stmt_to_string(&self, stmt: &ast::Stmt) -> String {
1090        Self::to_string(|s| s.print_stmt(stmt))
1091    }
1092
1093    fn item_to_string(&self, i: &ast::Item) -> String {
1094        Self::to_string(|s| s.print_item(i))
1095    }
1096
1097    fn assoc_item_to_string(&self, i: &ast::AssocItem) -> String {
1098        Self::to_string(|s| s.print_assoc_item(i))
1099    }
1100
1101    fn foreign_item_to_string(&self, i: &ast::ForeignItem) -> String {
1102        Self::to_string(|s| s.print_foreign_item(i))
1103    }
1104
1105    fn path_to_string(&self, p: &ast::Path) -> String {
1106        Self::to_string(|s| s.print_path(p, false, 0))
1107    }
1108
1109    fn vis_to_string(&self, v: &ast::Visibility) -> String {
1110        Self::to_string(|s| s.print_visibility(v))
1111    }
1112
1113    fn block_to_string(&self, blk: &ast::Block) -> String {
1114        Self::to_string(|s| {
1115            let (cb, ib) = s.head("");
1116            s.print_block(blk, cb, ib)
1117        })
1118    }
1119
1120    fn attr_item_to_string(&self, ai: &ast::AttrItem) -> String {
1121        Self::to_string(|s| s.print_attr_item(ai, ai.path.span))
1122    }
1123
1124    fn tts_to_string(&self, tokens: &TokenStream) -> String {
1125        Self::to_string(|s| s.print_tts(tokens, false))
1126    }
1127
1128    fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
1129        let mut printer = State::new();
1130        f(&mut printer);
1131        printer.s.eof()
1132    }
1133}
1134
1135impl<'a> PrintState<'a> for State<'a> {
1136    fn comments(&self) -> Option<&Comments<'a>> {
1137        self.comments.as_ref()
1138    }
1139
1140    fn comments_mut(&mut self) -> Option<&mut Comments<'a>> {
1141        self.comments.as_mut()
1142    }
1143
1144    fn ann_post(&mut self, ident: Ident) {
1145        self.ann.post(self, AnnNode::Ident(&ident));
1146    }
1147
1148    fn print_generic_args(&mut self, args: &ast::GenericArgs, colons_before_params: bool) {
1149        if colons_before_params {
1150            self.word("::")
1151        }
1152
1153        match args {
1154            ast::GenericArgs::AngleBracketed(data) => {
1155                self.word("<");
1156                self.commasep(Inconsistent, &data.args, |s, arg| match arg {
1157                    ast::AngleBracketedArg::Arg(a) => s.print_generic_arg(a),
1158                    ast::AngleBracketedArg::Constraint(c) => s.print_assoc_item_constraint(c),
1159                });
1160                self.word(">")
1161            }
1162
1163            ast::GenericArgs::Parenthesized(data) => {
1164                self.word("(");
1165                self.commasep(Inconsistent, &data.inputs, |s, ty| s.print_type(ty));
1166                self.word(")");
1167                self.print_fn_ret_ty(&data.output);
1168            }
1169            ast::GenericArgs::ParenthesizedElided(_) => {
1170                self.word("(");
1171                self.word("..");
1172                self.word(")");
1173            }
1174        }
1175    }
1176}
1177
1178impl<'a> State<'a> {
1179    pub fn new() -> State<'a> {
1180        State { s: pp::Printer::new(), comments: None, ann: &NoAnn, is_sdylib_interface: false }
1181    }
1182
1183    fn commasep_cmnt<T, F, G>(&mut self, b: Breaks, elts: &[T], mut op: F, mut get_span: G)
1184    where
1185        F: FnMut(&mut State<'_>, &T),
1186        G: FnMut(&T) -> rustc_span::Span,
1187    {
1188        let rb = self.rbox(0, b);
1189        let len = elts.len();
1190        let mut i = 0;
1191        for elt in elts {
1192            self.maybe_print_comment(get_span(elt).hi());
1193            op(self, elt);
1194            i += 1;
1195            if i < len {
1196                self.word(",");
1197                self.maybe_print_trailing_comment(get_span(elt), Some(get_span(&elts[i]).hi()));
1198                self.space_if_not_bol();
1199            }
1200        }
1201        self.end(rb);
1202    }
1203
1204    fn commasep_exprs(&mut self, b: Breaks, exprs: &[Box<ast::Expr>]) {
1205        self.commasep_cmnt(b, exprs, |s, e| s.print_expr(e, FixupContext::default()), |e| e.span)
1206    }
1207
1208    pub fn print_opt_lifetime(&mut self, lifetime: &Option<ast::Lifetime>) {
1209        if let Some(lt) = *lifetime {
1210            self.print_lifetime(lt);
1211            self.nbsp();
1212        }
1213    }
1214
1215    pub fn print_assoc_item_constraint(&mut self, constraint: &ast::AssocItemConstraint) {
1216        self.print_ident(constraint.ident);
1217        if let Some(args) = constraint.gen_args.as_ref() {
1218            self.print_generic_args(args, false)
1219        }
1220        self.space();
1221        match &constraint.kind {
1222            ast::AssocItemConstraintKind::Equality { term } => {
1223                self.word_space("=");
1224                match term {
1225                    Term::Ty(ty) => self.print_type(ty),
1226                    Term::Const(c) => self.print_expr_anon_const(c, &[]),
1227                }
1228            }
1229            ast::AssocItemConstraintKind::Bound { bounds } => {
1230                if !bounds.is_empty() {
1231                    self.word_nbsp(":");
1232                    self.print_type_bounds(bounds);
1233                }
1234            }
1235        }
1236    }
1237
1238    pub fn print_generic_arg(&mut self, generic_arg: &GenericArg) {
1239        match generic_arg {
1240            GenericArg::Lifetime(lt) => self.print_lifetime(*lt),
1241            GenericArg::Type(ty) => self.print_type(ty),
1242            GenericArg::Const(ct) => self.print_expr(&ct.value, FixupContext::default()),
1243        }
1244    }
1245
1246    pub fn print_ty_pat(&mut self, pat: &ast::TyPat) {
1247        match &pat.kind {
1248            rustc_ast::TyPatKind::Range(start, end, include_end) => {
1249                if let Some(start) = start {
1250                    self.print_expr_anon_const(start, &[]);
1251                }
1252                self.word("..");
1253                if let Some(end) = end {
1254                    if let RangeEnd::Included(_) = include_end.node {
1255                        self.word("=");
1256                    }
1257                    self.print_expr_anon_const(end, &[]);
1258                }
1259            }
1260            rustc_ast::TyPatKind::NotNull => self.word("!null"),
1261            rustc_ast::TyPatKind::Or(variants) => {
1262                let mut first = true;
1263                for pat in variants {
1264                    if first {
1265                        first = false
1266                    } else {
1267                        self.word(" | ");
1268                    }
1269                    self.print_ty_pat(pat);
1270                }
1271            }
1272            rustc_ast::TyPatKind::Err(_) => {
1273                self.popen();
1274                self.word("/*ERROR*/");
1275                self.pclose();
1276            }
1277        }
1278    }
1279
1280    pub fn print_type(&mut self, ty: &ast::Ty) {
1281        self.maybe_print_comment(ty.span.lo());
1282        let ib = self.ibox(0);
1283        match &ty.kind {
1284            ast::TyKind::Slice(ty) => {
1285                self.word("[");
1286                self.print_type(ty);
1287                self.word("]");
1288            }
1289            ast::TyKind::Ptr(mt) => {
1290                self.word("*");
1291                self.print_mt(mt, true);
1292            }
1293            ast::TyKind::Ref(lifetime, mt) => {
1294                self.word("&");
1295                self.print_opt_lifetime(lifetime);
1296                self.print_mt(mt, false);
1297            }
1298            ast::TyKind::PinnedRef(lifetime, mt) => {
1299                self.word("&");
1300                self.print_opt_lifetime(lifetime);
1301                self.word("pin ");
1302                self.print_mt(mt, true);
1303            }
1304            ast::TyKind::Never => {
1305                self.word("!");
1306            }
1307            ast::TyKind::Tup(elts) => {
1308                self.popen();
1309                self.commasep(Inconsistent, elts, |s, ty| s.print_type(ty));
1310                if elts.len() == 1 {
1311                    self.word(",");
1312                }
1313                self.pclose();
1314            }
1315            ast::TyKind::Paren(typ) => {
1316                self.popen();
1317                self.print_type(typ);
1318                self.pclose();
1319            }
1320            ast::TyKind::FnPtr(f) => {
1321                self.print_ty_fn(f.ext, f.safety, &f.decl, None, &f.generic_params);
1322            }
1323            ast::TyKind::UnsafeBinder(f) => {
1324                let ib = self.ibox(INDENT_UNIT);
1325                self.word("unsafe");
1326                self.print_generic_params(&f.generic_params);
1327                self.nbsp();
1328                self.print_type(&f.inner_ty);
1329                self.end(ib);
1330            }
1331            ast::TyKind::Path(None, path) => {
1332                self.print_path(path, false, 0);
1333            }
1334            ast::TyKind::Path(Some(qself), path) => self.print_qpath(path, qself, false),
1335            ast::TyKind::TraitObject(bounds, syntax) => {
1336                match syntax {
1337                    ast::TraitObjectSyntax::Dyn => self.word_nbsp("dyn"),
1338                    ast::TraitObjectSyntax::None => {}
1339                }
1340                self.print_type_bounds(bounds);
1341            }
1342            ast::TyKind::ImplTrait(_, bounds) => {
1343                self.word_nbsp("impl");
1344                self.print_type_bounds(bounds);
1345            }
1346            ast::TyKind::Array(ty, length) => {
1347                self.word("[");
1348                self.print_type(ty);
1349                self.word("; ");
1350                self.print_expr(&length.value, FixupContext::default());
1351                self.word("]");
1352            }
1353            ast::TyKind::Infer => {
1354                self.word("_");
1355            }
1356            ast::TyKind::Err(_) => {
1357                self.popen();
1358                self.word("/*ERROR*/");
1359                self.pclose();
1360            }
1361            ast::TyKind::Dummy => {
1362                self.popen();
1363                self.word("/*DUMMY*/");
1364                self.pclose();
1365            }
1366            ast::TyKind::ImplicitSelf => {
1367                self.word("Self");
1368            }
1369            ast::TyKind::MacCall(m) => {
1370                self.print_mac(m);
1371            }
1372            ast::TyKind::CVarArgs => {
1373                self.word("...");
1374            }
1375            ast::TyKind::Pat(ty, pat) => {
1376                self.print_type(ty);
1377                self.word(" is ");
1378                self.print_ty_pat(pat);
1379            }
1380        }
1381        self.end(ib);
1382    }
1383
1384    fn print_trait_ref(&mut self, t: &ast::TraitRef) {
1385        self.print_path(&t.path, false, 0)
1386    }
1387
1388    fn print_formal_generic_params(&mut self, generic_params: &[ast::GenericParam]) {
1389        if !generic_params.is_empty() {
1390            self.word("for");
1391            self.print_generic_params(generic_params);
1392            self.nbsp();
1393        }
1394    }
1395
1396    fn print_poly_trait_ref(&mut self, t: &ast::PolyTraitRef) {
1397        self.print_formal_generic_params(&t.bound_generic_params);
1398
1399        let ast::TraitBoundModifiers { constness, asyncness, polarity } = t.modifiers;
1400        match constness {
1401            ast::BoundConstness::Never => {}
1402            ast::BoundConstness::Always(_) | ast::BoundConstness::Maybe(_) => {
1403                self.word_space(constness.as_str());
1404            }
1405        }
1406        match asyncness {
1407            ast::BoundAsyncness::Normal => {}
1408            ast::BoundAsyncness::Async(_) => {
1409                self.word_space(asyncness.as_str());
1410            }
1411        }
1412        match polarity {
1413            ast::BoundPolarity::Positive => {}
1414            ast::BoundPolarity::Negative(_) | ast::BoundPolarity::Maybe(_) => {
1415                self.word(polarity.as_str());
1416            }
1417        }
1418
1419        self.print_trait_ref(&t.trait_ref)
1420    }
1421
1422    fn print_stmt(&mut self, st: &ast::Stmt) {
1423        self.maybe_print_comment(st.span.lo());
1424        match &st.kind {
1425            ast::StmtKind::Let(loc) => {
1426                self.print_outer_attributes(&loc.attrs);
1427                self.space_if_not_bol();
1428                let ib1 = self.ibox(INDENT_UNIT);
1429                if loc.super_.is_some() {
1430                    self.word_nbsp("super");
1431                }
1432                self.word_nbsp("let");
1433
1434                let ib2 = self.ibox(INDENT_UNIT);
1435                self.print_local_decl(loc);
1436                self.end(ib2);
1437                if let Some((init, els)) = loc.kind.init_else_opt() {
1438                    self.nbsp();
1439                    self.word_space("=");
1440                    self.print_expr_cond_paren(
1441                        init,
1442                        els.is_some() && classify::expr_trailing_brace(init).is_some(),
1443                        FixupContext::default(),
1444                    );
1445                    if let Some(els) = els {
1446                        let cb = self.cbox(INDENT_UNIT);
1447                        let ib = self.ibox(INDENT_UNIT);
1448                        self.word(" else ");
1449                        self.print_block(els, cb, ib);
1450                    }
1451                }
1452                self.word(";");
1453                self.end(ib1);
1454            }
1455            ast::StmtKind::Item(item) => self.print_item(item),
1456            ast::StmtKind::Expr(expr) => {
1457                self.space_if_not_bol();
1458                self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1459                if classify::expr_requires_semi_to_be_stmt(expr) {
1460                    self.word(";");
1461                }
1462            }
1463            ast::StmtKind::Semi(expr) => {
1464                self.space_if_not_bol();
1465                self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1466                self.word(";");
1467            }
1468            ast::StmtKind::Empty => {
1469                self.space_if_not_bol();
1470                self.word(";");
1471            }
1472            ast::StmtKind::MacCall(mac) => {
1473                self.space_if_not_bol();
1474                self.print_outer_attributes(&mac.attrs);
1475                self.print_mac(&mac.mac);
1476                if mac.style == ast::MacStmtStyle::Semicolon {
1477                    self.word(";");
1478                }
1479            }
1480        }
1481        self.maybe_print_trailing_comment(st.span, None)
1482    }
1483
1484    fn print_block(&mut self, blk: &ast::Block, cb: BoxMarker, ib: BoxMarker) {
1485        self.print_block_with_attrs(blk, &[], cb, ib)
1486    }
1487
1488    fn print_block_unclosed_indent(&mut self, blk: &ast::Block, ib: BoxMarker) {
1489        self.print_block_maybe_unclosed(blk, &[], None, ib)
1490    }
1491
1492    fn print_block_with_attrs(
1493        &mut self,
1494        blk: &ast::Block,
1495        attrs: &[ast::Attribute],
1496        cb: BoxMarker,
1497        ib: BoxMarker,
1498    ) {
1499        self.print_block_maybe_unclosed(blk, attrs, Some(cb), ib)
1500    }
1501
1502    fn print_block_maybe_unclosed(
1503        &mut self,
1504        blk: &ast::Block,
1505        attrs: &[ast::Attribute],
1506        cb: Option<BoxMarker>,
1507        ib: BoxMarker,
1508    ) {
1509        match blk.rules {
1510            BlockCheckMode::Unsafe(..) => self.word_space("unsafe"),
1511            BlockCheckMode::Default => (),
1512        }
1513        self.maybe_print_comment(blk.span.lo());
1514        self.ann.pre(self, AnnNode::Block(blk));
1515        self.bopen(ib);
1516
1517        let has_attrs = self.print_inner_attributes(attrs);
1518
1519        for (i, st) in blk.stmts.iter().enumerate() {
1520            match &st.kind {
1521                ast::StmtKind::Expr(expr) if i == blk.stmts.len() - 1 => {
1522                    self.maybe_print_comment(st.span.lo());
1523                    self.space_if_not_bol();
1524                    self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1525                    self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi()));
1526                }
1527                _ => self.print_stmt(st),
1528            }
1529        }
1530
1531        let no_space = !has_attrs && blk.stmts.is_empty();
1532        self.bclose_maybe_open(blk.span, no_space, cb);
1533        self.ann.post(self, AnnNode::Block(blk))
1534    }
1535
1536    /// Print a `let pat = expr` expression.
1537    ///
1538    /// Parentheses are inserted surrounding `expr` if a round-trip through the
1539    /// parser would otherwise work out the wrong way in a condition position.
1540    ///
1541    /// For example each of the following would mean the wrong thing without
1542    /// parentheses.
1543    ///
1544    /// ```ignore (illustrative)
1545    /// if let _ = (Struct {}) {}
1546    ///
1547    /// if let _ = (true && false) {}
1548    /// ```
1549    ///
1550    /// In a match guard, the second case still requires parens, but the first
1551    /// case no longer does because anything until `=>` is considered part of
1552    /// the match guard expression. Parsing of the expression is not terminated
1553    /// by `{` in that position.
1554    ///
1555    /// ```ignore (illustrative)
1556    /// match () {
1557    ///     () if let _ = Struct {} => {}
1558    ///     () if let _ = (true && false) => {}
1559    /// }
1560    /// ```
1561    fn print_let(&mut self, pat: &ast::Pat, expr: &ast::Expr, fixup: FixupContext) {
1562        self.word("let ");
1563        self.print_pat(pat);
1564        self.space();
1565        self.word_space("=");
1566        self.print_expr_cond_paren(
1567            expr,
1568            fixup.needs_par_as_let_scrutinee(expr),
1569            FixupContext::default(),
1570        );
1571    }
1572
1573    fn print_mac(&mut self, m: &ast::MacCall) {
1574        self.print_mac_common(
1575            Some(MacHeader::Path(&m.path)),
1576            true,
1577            None,
1578            m.args.delim,
1579            None,
1580            &m.args.tokens,
1581            true,
1582            m.span(),
1583        );
1584    }
1585
1586    fn print_inline_asm(&mut self, asm: &ast::InlineAsm) {
1587        enum AsmArg<'a> {
1588            Template(String),
1589            Operand(&'a InlineAsmOperand),
1590            ClobberAbi(Symbol),
1591            Options(InlineAsmOptions),
1592        }
1593
1594        let mut args = vec![AsmArg::Template(InlineAsmTemplatePiece::to_string(&asm.template))];
1595        args.extend(asm.operands.iter().map(|(o, _)| AsmArg::Operand(o)));
1596        for (abi, _) in &asm.clobber_abis {
1597            args.push(AsmArg::ClobberAbi(*abi));
1598        }
1599        if !asm.options.is_empty() {
1600            args.push(AsmArg::Options(asm.options));
1601        }
1602
1603        self.popen();
1604        self.commasep(Consistent, &args, |s, arg| match arg {
1605            AsmArg::Template(template) => s.print_string(template, ast::StrStyle::Cooked),
1606            AsmArg::Operand(op) => {
1607                let print_reg_or_class = |s: &mut Self, r: &InlineAsmRegOrRegClass| match r {
1608                    InlineAsmRegOrRegClass::Reg(r) => s.print_symbol(*r, ast::StrStyle::Cooked),
1609                    InlineAsmRegOrRegClass::RegClass(r) => s.word(r.to_string()),
1610                };
1611                match op {
1612                    InlineAsmOperand::In { reg, expr } => {
1613                        s.word("in");
1614                        s.popen();
1615                        print_reg_or_class(s, reg);
1616                        s.pclose();
1617                        s.space();
1618                        s.print_expr(expr, FixupContext::default());
1619                    }
1620                    InlineAsmOperand::Out { reg, late, expr } => {
1621                        s.word(if *late { "lateout" } else { "out" });
1622                        s.popen();
1623                        print_reg_or_class(s, reg);
1624                        s.pclose();
1625                        s.space();
1626                        match expr {
1627                            Some(expr) => s.print_expr(expr, FixupContext::default()),
1628                            None => s.word("_"),
1629                        }
1630                    }
1631                    InlineAsmOperand::InOut { reg, late, expr } => {
1632                        s.word(if *late { "inlateout" } else { "inout" });
1633                        s.popen();
1634                        print_reg_or_class(s, reg);
1635                        s.pclose();
1636                        s.space();
1637                        s.print_expr(expr, FixupContext::default());
1638                    }
1639                    InlineAsmOperand::SplitInOut { reg, late, in_expr, out_expr } => {
1640                        s.word(if *late { "inlateout" } else { "inout" });
1641                        s.popen();
1642                        print_reg_or_class(s, reg);
1643                        s.pclose();
1644                        s.space();
1645                        s.print_expr(in_expr, FixupContext::default());
1646                        s.space();
1647                        s.word_space("=>");
1648                        match out_expr {
1649                            Some(out_expr) => s.print_expr(out_expr, FixupContext::default()),
1650                            None => s.word("_"),
1651                        }
1652                    }
1653                    InlineAsmOperand::Const { anon_const } => {
1654                        s.word("const");
1655                        s.space();
1656                        s.print_expr(&anon_const.value, FixupContext::default());
1657                    }
1658                    InlineAsmOperand::Sym { sym } => {
1659                        s.word("sym");
1660                        s.space();
1661                        if let Some(qself) = &sym.qself {
1662                            s.print_qpath(&sym.path, qself, true);
1663                        } else {
1664                            s.print_path(&sym.path, true, 0);
1665                        }
1666                    }
1667                    InlineAsmOperand::Label { block } => {
1668                        let (cb, ib) = s.head("label");
1669                        s.print_block(block, cb, ib);
1670                    }
1671                }
1672            }
1673            AsmArg::ClobberAbi(abi) => {
1674                s.word("clobber_abi");
1675                s.popen();
1676                s.print_symbol(*abi, ast::StrStyle::Cooked);
1677                s.pclose();
1678            }
1679            AsmArg::Options(opts) => {
1680                s.word("options");
1681                s.popen();
1682                s.commasep(Inconsistent, &opts.human_readable_names(), |s, &opt| {
1683                    s.word(opt);
1684                });
1685                s.pclose();
1686            }
1687        });
1688        self.pclose();
1689    }
1690
1691    fn print_local_decl(&mut self, loc: &ast::Local) {
1692        self.print_pat(&loc.pat);
1693        if let Some(ty) = &loc.ty {
1694            self.word_space(":");
1695            self.print_type(ty);
1696        }
1697    }
1698
1699    fn print_name(&mut self, name: Symbol) {
1700        self.word(name.to_string());
1701        self.ann.post(self, AnnNode::Name(&name))
1702    }
1703
1704    fn print_qpath(&mut self, path: &ast::Path, qself: &ast::QSelf, colons_before_params: bool) {
1705        self.word("<");
1706        self.print_type(&qself.ty);
1707        if qself.position > 0 {
1708            self.space();
1709            self.word_space("as");
1710            let depth = path.segments.len() - qself.position;
1711            self.print_path(path, false, depth);
1712        }
1713        self.word(">");
1714        for item_segment in &path.segments[qself.position..] {
1715            self.word("::");
1716            self.print_ident(item_segment.ident);
1717            if let Some(args) = &item_segment.args {
1718                self.print_generic_args(args, colons_before_params)
1719            }
1720        }
1721    }
1722
1723    fn print_pat(&mut self, pat: &ast::Pat) {
1724        self.maybe_print_comment(pat.span.lo());
1725        self.ann.pre(self, AnnNode::Pat(pat));
1726        /* Pat isn't normalized, but the beauty of it is that it doesn't matter */
1727        match &pat.kind {
1728            PatKind::Missing => unreachable!(),
1729            PatKind::Wild => self.word("_"),
1730            PatKind::Never => self.word("!"),
1731            PatKind::Ident(BindingMode(by_ref, mutbl), ident, sub) => {
1732                if mutbl.is_mut() {
1733                    self.word_nbsp("mut");
1734                }
1735                if let ByRef::Yes(pinnedness, rmutbl) = by_ref {
1736                    self.word_nbsp("ref");
1737                    if pinnedness.is_pinned() {
1738                        self.word_nbsp("pin");
1739                    }
1740                    if rmutbl.is_mut() {
1741                        self.word_nbsp("mut");
1742                    } else if pinnedness.is_pinned() {
1743                        self.word_nbsp("const");
1744                    }
1745                }
1746                self.print_ident(*ident);
1747                if let Some(p) = sub {
1748                    self.space();
1749                    self.word_space("@");
1750                    self.print_pat(p);
1751                }
1752            }
1753            PatKind::TupleStruct(qself, path, elts) => {
1754                if let Some(qself) = qself {
1755                    self.print_qpath(path, qself, true);
1756                } else {
1757                    self.print_path(path, true, 0);
1758                }
1759                self.popen();
1760                self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));
1761                self.pclose();
1762            }
1763            PatKind::Or(pats) => {
1764                self.strsep("|", true, Inconsistent, pats, |s, p| s.print_pat(p));
1765            }
1766            PatKind::Path(None, path) => {
1767                self.print_path(path, true, 0);
1768            }
1769            PatKind::Path(Some(qself), path) => {
1770                self.print_qpath(path, qself, false);
1771            }
1772            PatKind::Struct(qself, path, fields, etc) => {
1773                if let Some(qself) = qself {
1774                    self.print_qpath(path, qself, true);
1775                } else {
1776                    self.print_path(path, true, 0);
1777                }
1778                self.nbsp();
1779                self.word("{");
1780                let empty = fields.is_empty() && *etc == ast::PatFieldsRest::None;
1781                if !empty {
1782                    self.space();
1783                }
1784                self.commasep_cmnt(
1785                    Consistent,
1786                    fields,
1787                    |s, f| {
1788                        let cb = s.cbox(INDENT_UNIT);
1789                        if !f.is_shorthand {
1790                            s.print_ident(f.ident);
1791                            s.word_nbsp(":");
1792                        }
1793                        s.print_pat(&f.pat);
1794                        s.end(cb);
1795                    },
1796                    |f| f.pat.span,
1797                );
1798                if let ast::PatFieldsRest::Rest(_) | ast::PatFieldsRest::Recovered(_) = etc {
1799                    if !fields.is_empty() {
1800                        self.word_space(",");
1801                    }
1802                    self.word("..");
1803                    if let ast::PatFieldsRest::Recovered(_) = etc {
1804                        self.word("/* recovered parse error */");
1805                    }
1806                }
1807                if !empty {
1808                    self.space();
1809                }
1810                self.word("}");
1811            }
1812            PatKind::Tuple(elts) => {
1813                self.popen();
1814                self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));
1815                if elts.len() == 1 {
1816                    self.word(",");
1817                }
1818                self.pclose();
1819            }
1820            PatKind::Box(inner) => {
1821                self.word("box ");
1822                self.print_pat(inner);
1823            }
1824            PatKind::Deref(inner) => {
1825                self.word("deref!");
1826                self.popen();
1827                self.print_pat(inner);
1828                self.pclose();
1829            }
1830            PatKind::Ref(inner, pinned, mutbl) => {
1831                self.word("&");
1832                if pinned.is_pinned() {
1833                    self.word("pin ");
1834                    if mutbl.is_not() {
1835                        self.word("const ");
1836                    }
1837                }
1838                if mutbl.is_mut() {
1839                    self.word("mut ");
1840                }
1841                if let PatKind::Ident(ast::BindingMode::MUT, ..) = inner.kind {
1842                    self.popen();
1843                    self.print_pat(inner);
1844                    self.pclose();
1845                } else {
1846                    self.print_pat(inner);
1847                }
1848            }
1849            PatKind::Expr(e) => self.print_expr(e, FixupContext::default()),
1850            PatKind::Range(begin, end, Spanned { node: end_kind, .. }) => {
1851                if let Some(e) = begin {
1852                    self.print_expr(e, FixupContext::default());
1853                }
1854                match end_kind {
1855                    RangeEnd::Included(RangeSyntax::DotDotDot) => self.word("..."),
1856                    RangeEnd::Included(RangeSyntax::DotDotEq) => self.word("..="),
1857                    RangeEnd::Excluded => self.word(".."),
1858                }
1859                if let Some(e) = end {
1860                    self.print_expr(e, FixupContext::default());
1861                }
1862            }
1863            PatKind::Guard(subpat, condition) => {
1864                self.popen();
1865                self.print_pat(subpat);
1866                self.space();
1867                self.word_space("if");
1868                self.print_expr(condition, FixupContext::default());
1869                self.pclose();
1870            }
1871            PatKind::Slice(elts) => {
1872                self.word("[");
1873                self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));
1874                self.word("]");
1875            }
1876            PatKind::Rest => self.word(".."),
1877            PatKind::Paren(inner) => {
1878                self.popen();
1879                self.print_pat(inner);
1880                self.pclose();
1881            }
1882            PatKind::MacCall(m) => self.print_mac(m),
1883            PatKind::Err(_) => {
1884                self.popen();
1885                self.word("/*ERROR*/");
1886                self.pclose();
1887            }
1888        }
1889        self.ann.post(self, AnnNode::Pat(pat))
1890    }
1891
1892    fn print_explicit_self(&mut self, explicit_self: &ast::ExplicitSelf) {
1893        match &explicit_self.node {
1894            SelfKind::Value(m) => {
1895                self.print_mutability(*m, false);
1896                self.word("self")
1897            }
1898            SelfKind::Region(lt, m) => {
1899                self.word("&");
1900                self.print_opt_lifetime(lt);
1901                self.print_mutability(*m, false);
1902                self.word("self")
1903            }
1904            SelfKind::Pinned(lt, m) => {
1905                self.word("&");
1906                self.print_opt_lifetime(lt);
1907                self.word("pin ");
1908                self.print_mutability(*m, true);
1909                self.word("self")
1910            }
1911            SelfKind::Explicit(typ, m) => {
1912                self.print_mutability(*m, false);
1913                self.word("self");
1914                self.word_space(":");
1915                self.print_type(typ)
1916            }
1917        }
1918    }
1919
1920    fn print_coroutine_kind(&mut self, coroutine_kind: ast::CoroutineKind) {
1921        match coroutine_kind {
1922            ast::CoroutineKind::Gen { .. } => {
1923                self.word_nbsp("gen");
1924            }
1925            ast::CoroutineKind::Async { .. } => {
1926                self.word_nbsp("async");
1927            }
1928            ast::CoroutineKind::AsyncGen { .. } => {
1929                self.word_nbsp("async");
1930                self.word_nbsp("gen");
1931            }
1932        }
1933    }
1934
1935    pub fn print_type_bounds(&mut self, bounds: &[ast::GenericBound]) {
1936        let mut first = true;
1937        for bound in bounds {
1938            if first {
1939                first = false;
1940            } else {
1941                self.nbsp();
1942                self.word_space("+");
1943            }
1944
1945            match bound {
1946                GenericBound::Trait(tref) => {
1947                    self.print_poly_trait_ref(tref);
1948                }
1949                GenericBound::Outlives(lt) => self.print_lifetime(*lt),
1950                GenericBound::Use(args, _) => {
1951                    self.word("use");
1952                    self.word("<");
1953                    self.commasep(Inconsistent, args, |s, arg| match arg {
1954                        ast::PreciseCapturingArg::Arg(p, _) => s.print_path(p, false, 0),
1955                        ast::PreciseCapturingArg::Lifetime(lt) => s.print_lifetime(*lt),
1956                    });
1957                    self.word(">")
1958                }
1959            }
1960        }
1961    }
1962
1963    fn print_lifetime(&mut self, lifetime: ast::Lifetime) {
1964        self.print_name(lifetime.ident.name)
1965    }
1966
1967    fn print_lifetime_bounds(&mut self, bounds: &ast::GenericBounds) {
1968        for (i, bound) in bounds.iter().enumerate() {
1969            if i != 0 {
1970                self.word(" + ");
1971            }
1972            match bound {
1973                ast::GenericBound::Outlives(lt) => self.print_lifetime(*lt),
1974                _ => {
1975                    panic!("expected a lifetime bound, found a trait bound")
1976                }
1977            }
1978        }
1979    }
1980
1981    fn print_generic_params(&mut self, generic_params: &[ast::GenericParam]) {
1982        if generic_params.is_empty() {
1983            return;
1984        }
1985
1986        self.word("<");
1987
1988        self.commasep(Inconsistent, generic_params, |s, param| {
1989            s.print_outer_attributes_inline(&param.attrs);
1990
1991            match &param.kind {
1992                ast::GenericParamKind::Lifetime => {
1993                    let lt = ast::Lifetime { id: param.id, ident: param.ident };
1994                    s.print_lifetime(lt);
1995                    if !param.bounds.is_empty() {
1996                        s.word_nbsp(":");
1997                        s.print_lifetime_bounds(&param.bounds)
1998                    }
1999                }
2000                ast::GenericParamKind::Type { default } => {
2001                    s.print_ident(param.ident);
2002                    if !param.bounds.is_empty() {
2003                        s.word_nbsp(":");
2004                        s.print_type_bounds(&param.bounds);
2005                    }
2006                    if let Some(default) = default {
2007                        s.space();
2008                        s.word_space("=");
2009                        s.print_type(default)
2010                    }
2011                }
2012                ast::GenericParamKind::Const { ty, default, .. } => {
2013                    s.word_space("const");
2014                    s.print_ident(param.ident);
2015                    s.space();
2016                    s.word_space(":");
2017                    s.print_type(ty);
2018                    if !param.bounds.is_empty() {
2019                        s.word_nbsp(":");
2020                        s.print_type_bounds(&param.bounds);
2021                    }
2022                    if let Some(default) = default {
2023                        s.space();
2024                        s.word_space("=");
2025                        s.print_expr(&default.value, FixupContext::default());
2026                    }
2027                }
2028            }
2029        });
2030
2031        self.word(">");
2032    }
2033
2034    pub fn print_mutability(&mut self, mutbl: ast::Mutability, print_const: bool) {
2035        match mutbl {
2036            ast::Mutability::Mut => self.word_nbsp("mut"),
2037            ast::Mutability::Not => {
2038                if print_const {
2039                    self.word_nbsp("const");
2040                }
2041            }
2042        }
2043    }
2044
2045    fn print_mt(&mut self, mt: &ast::MutTy, print_const: bool) {
2046        self.print_mutability(mt.mutbl, print_const);
2047        self.print_type(&mt.ty)
2048    }
2049
2050    fn print_param(&mut self, input: &ast::Param, is_closure: bool) {
2051        let ib = self.ibox(INDENT_UNIT);
2052
2053        self.print_outer_attributes_inline(&input.attrs);
2054
2055        match input.ty.kind {
2056            ast::TyKind::Infer if is_closure => self.print_pat(&input.pat),
2057            _ => {
2058                if let Some(eself) = input.to_self() {
2059                    self.print_explicit_self(&eself);
2060                } else {
2061                    if !matches!(input.pat.kind, PatKind::Missing) {
2062                        self.print_pat(&input.pat);
2063                        self.word(":");
2064                        self.space();
2065                    }
2066                    self.print_type(&input.ty);
2067                }
2068            }
2069        }
2070        self.end(ib);
2071    }
2072
2073    fn print_fn_ret_ty(&mut self, fn_ret_ty: &ast::FnRetTy) {
2074        if let ast::FnRetTy::Ty(ty) = fn_ret_ty {
2075            self.space_if_not_bol();
2076            let ib = self.ibox(INDENT_UNIT);
2077            self.word_space("->");
2078            self.print_type(ty);
2079            self.end(ib);
2080            self.maybe_print_comment(ty.span.lo());
2081        }
2082    }
2083
2084    fn print_ty_fn(
2085        &mut self,
2086        ext: ast::Extern,
2087        safety: ast::Safety,
2088        decl: &ast::FnDecl,
2089        name: Option<Ident>,
2090        generic_params: &[ast::GenericParam],
2091    ) {
2092        let ib = self.ibox(INDENT_UNIT);
2093        self.print_formal_generic_params(generic_params);
2094        let generics = ast::Generics::default();
2095        let header = ast::FnHeader { safety, ext, ..ast::FnHeader::default() };
2096        self.print_fn(decl, header, name, &generics);
2097        self.end(ib);
2098    }
2099
2100    fn print_fn_header_info(&mut self, header: ast::FnHeader) {
2101        self.print_constness(header.constness);
2102        header.coroutine_kind.map(|coroutine_kind| self.print_coroutine_kind(coroutine_kind));
2103        self.print_safety(header.safety);
2104
2105        match header.ext {
2106            ast::Extern::None => {}
2107            ast::Extern::Implicit(_) => {
2108                self.word_nbsp("extern");
2109            }
2110            ast::Extern::Explicit(abi, _) => {
2111                self.word_nbsp("extern");
2112                self.print_token_literal(abi.as_token_lit(), abi.span);
2113                self.nbsp();
2114            }
2115        }
2116
2117        self.word("fn")
2118    }
2119
2120    fn print_safety(&mut self, s: ast::Safety) {
2121        match s {
2122            ast::Safety::Default => {}
2123            ast::Safety::Safe(_) => self.word_nbsp("safe"),
2124            ast::Safety::Unsafe(_) => self.word_nbsp("unsafe"),
2125        }
2126    }
2127
2128    fn print_constness(&mut self, s: ast::Const) {
2129        match s {
2130            ast::Const::No => {}
2131            ast::Const::Yes(_) => self.word_nbsp("const"),
2132        }
2133    }
2134
2135    fn print_is_auto(&mut self, s: ast::IsAuto) {
2136        match s {
2137            ast::IsAuto::Yes => self.word_nbsp("auto"),
2138            ast::IsAuto::No => {}
2139        }
2140    }
2141
2142    fn print_meta_item_lit(&mut self, lit: &ast::MetaItemLit) {
2143        self.print_token_literal(lit.as_token_lit(), lit.span)
2144    }
2145
2146    fn print_token_literal(&mut self, token_lit: token::Lit, span: Span) {
2147        self.maybe_print_comment(span.lo());
2148        self.word(token_lit.to_string())
2149    }
2150
2151    fn print_symbol(&mut self, sym: Symbol, style: ast::StrStyle) {
2152        self.print_string(sym.as_str(), style);
2153    }
2154
2155    fn print_inner_attributes_no_trailing_hardbreak(&mut self, attrs: &[ast::Attribute]) -> bool {
2156        self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, false)
2157    }
2158
2159    fn print_outer_attributes_inline(&mut self, attrs: &[ast::Attribute]) -> bool {
2160        self.print_either_attributes(attrs, ast::AttrStyle::Outer, true, true)
2161    }
2162
2163    fn print_attribute(&mut self, attr: &ast::Attribute) {
2164        self.print_attribute_inline(attr, false);
2165    }
2166
2167    fn print_meta_list_item(&mut self, item: &ast::MetaItemInner) {
2168        match item {
2169            ast::MetaItemInner::MetaItem(mi) => self.print_meta_item(mi),
2170            ast::MetaItemInner::Lit(lit) => self.print_meta_item_lit(lit),
2171        }
2172    }
2173
2174    fn print_meta_item(&mut self, item: &ast::MetaItem) {
2175        let ib = self.ibox(INDENT_UNIT);
2176
2177        match item.unsafety {
2178            ast::Safety::Unsafe(_) => {
2179                self.word("unsafe");
2180                self.popen();
2181            }
2182            ast::Safety::Default | ast::Safety::Safe(_) => {}
2183        }
2184
2185        match &item.kind {
2186            ast::MetaItemKind::Word => self.print_path(&item.path, false, 0),
2187            ast::MetaItemKind::NameValue(value) => {
2188                self.print_path(&item.path, false, 0);
2189                self.space();
2190                self.word_space("=");
2191                self.print_meta_item_lit(value);
2192            }
2193            ast::MetaItemKind::List(items) => {
2194                self.print_path(&item.path, false, 0);
2195                self.popen();
2196                self.commasep(Consistent, items, |s, i| s.print_meta_list_item(i));
2197                self.pclose();
2198            }
2199        }
2200
2201        match item.unsafety {
2202            ast::Safety::Unsafe(_) => self.pclose(),
2203            ast::Safety::Default | ast::Safety::Safe(_) => {}
2204        }
2205
2206        self.end(ib);
2207    }
2208
2209    pub(crate) fn bounds_to_string(&self, bounds: &[ast::GenericBound]) -> String {
2210        Self::to_string(|s| s.print_type_bounds(bounds))
2211    }
2212
2213    pub(crate) fn where_bound_predicate_to_string(
2214        &self,
2215        where_bound_predicate: &ast::WhereBoundPredicate,
2216    ) -> String {
2217        Self::to_string(|s| s.print_where_bound_predicate(where_bound_predicate))
2218    }
2219
2220    pub(crate) fn tt_to_string(&self, tt: &TokenTree) -> String {
2221        Self::to_string(|s| {
2222            s.print_tt(tt, false);
2223        })
2224    }
2225
2226    pub(crate) fn path_segment_to_string(&self, p: &ast::PathSegment) -> String {
2227        Self::to_string(|s| s.print_path_segment(p, false))
2228    }
2229
2230    pub(crate) fn meta_list_item_to_string(&self, li: &ast::MetaItemInner) -> String {
2231        Self::to_string(|s| s.print_meta_list_item(li))
2232    }
2233
2234    pub(crate) fn attribute_to_string(&self, attr: &ast::Attribute) -> String {
2235        Self::to_string(|s| s.print_attribute(attr))
2236    }
2237}