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