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 path_to_string(&self, p: &ast::Path) -> String {
1067        Self::to_string(|s| s.print_path(p, false, 0))
1068    }
1069
1070    fn vis_to_string(&self, v: &ast::Visibility) -> String {
1071        Self::to_string(|s| s.print_visibility(v))
1072    }
1073
1074    fn block_to_string(&self, blk: &ast::Block) -> String {
1075        Self::to_string(|s| {
1076            let (cb, ib) = s.head("");
1077            s.print_block(blk, cb, ib)
1078        })
1079    }
1080
1081    fn attr_item_to_string(&self, ai: &ast::AttrItem) -> String {
1082        Self::to_string(|s| s.print_attr_item(ai, ai.path.span))
1083    }
1084
1085    fn tts_to_string(&self, tokens: &TokenStream) -> String {
1086        Self::to_string(|s| s.print_tts(tokens, false))
1087    }
1088
1089    fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
1090        let mut printer = State::new();
1091        f(&mut printer);
1092        printer.s.eof()
1093    }
1094}
1095
1096impl<'a> PrintState<'a> for State<'a> {
1097    fn comments(&self) -> Option<&Comments<'a>> {
1098        self.comments.as_ref()
1099    }
1100
1101    fn comments_mut(&mut self) -> Option<&mut Comments<'a>> {
1102        self.comments.as_mut()
1103    }
1104
1105    fn ann_post(&mut self, ident: Ident) {
1106        self.ann.post(self, AnnNode::Ident(&ident));
1107    }
1108
1109    fn print_generic_args(&mut self, args: &ast::GenericArgs, colons_before_params: bool) {
1110        if colons_before_params {
1111            self.word("::")
1112        }
1113
1114        match args {
1115            ast::GenericArgs::AngleBracketed(data) => {
1116                self.word("<");
1117                self.commasep(Inconsistent, &data.args, |s, arg| match arg {
1118                    ast::AngleBracketedArg::Arg(a) => s.print_generic_arg(a),
1119                    ast::AngleBracketedArg::Constraint(c) => s.print_assoc_item_constraint(c),
1120                });
1121                self.word(">")
1122            }
1123
1124            ast::GenericArgs::Parenthesized(data) => {
1125                self.word("(");
1126                self.commasep(Inconsistent, &data.inputs, |s, ty| s.print_type(ty));
1127                self.word(")");
1128                self.print_fn_ret_ty(&data.output);
1129            }
1130            ast::GenericArgs::ParenthesizedElided(_) => {
1131                self.word("(");
1132                self.word("..");
1133                self.word(")");
1134            }
1135        }
1136    }
1137}
1138
1139impl<'a> State<'a> {
1140    pub fn new() -> State<'a> {
1141        State { s: pp::Printer::new(), comments: None, ann: &NoAnn, is_sdylib_interface: false }
1142    }
1143
1144    fn commasep_cmnt<T, F, G>(&mut self, b: Breaks, elts: &[T], mut op: F, mut get_span: G)
1145    where
1146        F: FnMut(&mut State<'_>, &T),
1147        G: FnMut(&T) -> rustc_span::Span,
1148    {
1149        let rb = self.rbox(0, b);
1150        let len = elts.len();
1151        let mut i = 0;
1152        for elt in elts {
1153            self.maybe_print_comment(get_span(elt).hi());
1154            op(self, elt);
1155            i += 1;
1156            if i < len {
1157                self.word(",");
1158                self.maybe_print_trailing_comment(get_span(elt), Some(get_span(&elts[i]).hi()));
1159                self.space_if_not_bol();
1160            }
1161        }
1162        self.end(rb);
1163    }
1164
1165    fn commasep_exprs(&mut self, b: Breaks, exprs: &[P<ast::Expr>]) {
1166        self.commasep_cmnt(b, exprs, |s, e| s.print_expr(e, FixupContext::default()), |e| e.span)
1167    }
1168
1169    pub fn print_opt_lifetime(&mut self, lifetime: &Option<ast::Lifetime>) {
1170        if let Some(lt) = *lifetime {
1171            self.print_lifetime(lt);
1172            self.nbsp();
1173        }
1174    }
1175
1176    pub fn print_assoc_item_constraint(&mut self, constraint: &ast::AssocItemConstraint) {
1177        self.print_ident(constraint.ident);
1178        if let Some(args) = constraint.gen_args.as_ref() {
1179            self.print_generic_args(args, false)
1180        }
1181        self.space();
1182        match &constraint.kind {
1183            ast::AssocItemConstraintKind::Equality { term } => {
1184                self.word_space("=");
1185                match term {
1186                    Term::Ty(ty) => self.print_type(ty),
1187                    Term::Const(c) => self.print_expr_anon_const(c, &[]),
1188                }
1189            }
1190            ast::AssocItemConstraintKind::Bound { bounds } => {
1191                if !bounds.is_empty() {
1192                    self.word_nbsp(":");
1193                    self.print_type_bounds(bounds);
1194                }
1195            }
1196        }
1197    }
1198
1199    pub fn print_generic_arg(&mut self, generic_arg: &GenericArg) {
1200        match generic_arg {
1201            GenericArg::Lifetime(lt) => self.print_lifetime(*lt),
1202            GenericArg::Type(ty) => self.print_type(ty),
1203            GenericArg::Const(ct) => self.print_expr(&ct.value, FixupContext::default()),
1204        }
1205    }
1206
1207    pub fn print_ty_pat(&mut self, pat: &ast::TyPat) {
1208        match &pat.kind {
1209            rustc_ast::TyPatKind::Range(start, end, include_end) => {
1210                if let Some(start) = start {
1211                    self.print_expr_anon_const(start, &[]);
1212                }
1213                self.word("..");
1214                if let Some(end) = end {
1215                    if let RangeEnd::Included(_) = include_end.node {
1216                        self.word("=");
1217                    }
1218                    self.print_expr_anon_const(end, &[]);
1219                }
1220            }
1221            rustc_ast::TyPatKind::Or(variants) => {
1222                let mut first = true;
1223                for pat in variants {
1224                    if first {
1225                        first = false
1226                    } else {
1227                        self.word(" | ");
1228                    }
1229                    self.print_ty_pat(pat);
1230                }
1231            }
1232            rustc_ast::TyPatKind::Err(_) => {
1233                self.popen();
1234                self.word("/*ERROR*/");
1235                self.pclose();
1236            }
1237        }
1238    }
1239
1240    pub fn print_type(&mut self, ty: &ast::Ty) {
1241        self.maybe_print_comment(ty.span.lo());
1242        let ib = self.ibox(0);
1243        match &ty.kind {
1244            ast::TyKind::Slice(ty) => {
1245                self.word("[");
1246                self.print_type(ty);
1247                self.word("]");
1248            }
1249            ast::TyKind::Ptr(mt) => {
1250                self.word("*");
1251                self.print_mt(mt, true);
1252            }
1253            ast::TyKind::Ref(lifetime, mt) => {
1254                self.word("&");
1255                self.print_opt_lifetime(lifetime);
1256                self.print_mt(mt, false);
1257            }
1258            ast::TyKind::PinnedRef(lifetime, mt) => {
1259                self.word("&");
1260                self.print_opt_lifetime(lifetime);
1261                self.word("pin ");
1262                self.print_mt(mt, true);
1263            }
1264            ast::TyKind::Never => {
1265                self.word("!");
1266            }
1267            ast::TyKind::Tup(elts) => {
1268                self.popen();
1269                self.commasep(Inconsistent, elts, |s, ty| s.print_type(ty));
1270                if elts.len() == 1 {
1271                    self.word(",");
1272                }
1273                self.pclose();
1274            }
1275            ast::TyKind::Paren(typ) => {
1276                self.popen();
1277                self.print_type(typ);
1278                self.pclose();
1279            }
1280            ast::TyKind::BareFn(f) => {
1281                self.print_ty_fn(f.ext, f.safety, &f.decl, None, &f.generic_params);
1282            }
1283            ast::TyKind::UnsafeBinder(f) => {
1284                let ib = self.ibox(INDENT_UNIT);
1285                self.word("unsafe");
1286                self.print_generic_params(&f.generic_params);
1287                self.nbsp();
1288                self.print_type(&f.inner_ty);
1289                self.end(ib);
1290            }
1291            ast::TyKind::Path(None, path) => {
1292                self.print_path(path, false, 0);
1293            }
1294            ast::TyKind::Path(Some(qself), path) => self.print_qpath(path, qself, false),
1295            ast::TyKind::TraitObject(bounds, syntax) => {
1296                match syntax {
1297                    ast::TraitObjectSyntax::Dyn => self.word_nbsp("dyn"),
1298                    ast::TraitObjectSyntax::DynStar => self.word_nbsp("dyn*"),
1299                    ast::TraitObjectSyntax::None => {}
1300                }
1301                self.print_type_bounds(bounds);
1302            }
1303            ast::TyKind::ImplTrait(_, bounds) => {
1304                self.word_nbsp("impl");
1305                self.print_type_bounds(bounds);
1306            }
1307            ast::TyKind::Array(ty, length) => {
1308                self.word("[");
1309                self.print_type(ty);
1310                self.word("; ");
1311                self.print_expr(&length.value, FixupContext::default());
1312                self.word("]");
1313            }
1314            ast::TyKind::Typeof(e) => {
1315                self.word("typeof(");
1316                self.print_expr(&e.value, FixupContext::default());
1317                self.word(")");
1318            }
1319            ast::TyKind::Infer => {
1320                self.word("_");
1321            }
1322            ast::TyKind::Err(_) => {
1323                self.popen();
1324                self.word("/*ERROR*/");
1325                self.pclose();
1326            }
1327            ast::TyKind::Dummy => {
1328                self.popen();
1329                self.word("/*DUMMY*/");
1330                self.pclose();
1331            }
1332            ast::TyKind::ImplicitSelf => {
1333                self.word("Self");
1334            }
1335            ast::TyKind::MacCall(m) => {
1336                self.print_mac(m);
1337            }
1338            ast::TyKind::CVarArgs => {
1339                self.word("...");
1340            }
1341            ast::TyKind::Pat(ty, pat) => {
1342                self.print_type(ty);
1343                self.word(" is ");
1344                self.print_ty_pat(pat);
1345            }
1346        }
1347        self.end(ib);
1348    }
1349
1350    fn print_trait_ref(&mut self, t: &ast::TraitRef) {
1351        self.print_path(&t.path, false, 0)
1352    }
1353
1354    fn print_formal_generic_params(&mut self, generic_params: &[ast::GenericParam]) {
1355        if !generic_params.is_empty() {
1356            self.word("for");
1357            self.print_generic_params(generic_params);
1358            self.nbsp();
1359        }
1360    }
1361
1362    fn print_poly_trait_ref(&mut self, t: &ast::PolyTraitRef) {
1363        self.print_formal_generic_params(&t.bound_generic_params);
1364
1365        let ast::TraitBoundModifiers { constness, asyncness, polarity } = t.modifiers;
1366        match constness {
1367            ast::BoundConstness::Never => {}
1368            ast::BoundConstness::Always(_) | ast::BoundConstness::Maybe(_) => {
1369                self.word_space(constness.as_str());
1370            }
1371        }
1372        match asyncness {
1373            ast::BoundAsyncness::Normal => {}
1374            ast::BoundAsyncness::Async(_) => {
1375                self.word_space(asyncness.as_str());
1376            }
1377        }
1378        match polarity {
1379            ast::BoundPolarity::Positive => {}
1380            ast::BoundPolarity::Negative(_) | ast::BoundPolarity::Maybe(_) => {
1381                self.word(polarity.as_str());
1382            }
1383        }
1384
1385        self.print_trait_ref(&t.trait_ref)
1386    }
1387
1388    fn print_stmt(&mut self, st: &ast::Stmt) {
1389        self.maybe_print_comment(st.span.lo());
1390        match &st.kind {
1391            ast::StmtKind::Let(loc) => {
1392                self.print_outer_attributes(&loc.attrs);
1393                self.space_if_not_bol();
1394                let ib1 = self.ibox(INDENT_UNIT);
1395                if loc.super_.is_some() {
1396                    self.word_nbsp("super");
1397                }
1398                self.word_nbsp("let");
1399
1400                let ib2 = self.ibox(INDENT_UNIT);
1401                self.print_local_decl(loc);
1402                self.end(ib2);
1403                if let Some((init, els)) = loc.kind.init_else_opt() {
1404                    self.nbsp();
1405                    self.word_space("=");
1406                    self.print_expr_cond_paren(
1407                        init,
1408                        els.is_some() && classify::expr_trailing_brace(init).is_some(),
1409                        FixupContext::default(),
1410                    );
1411                    if let Some(els) = els {
1412                        let cb = self.cbox(INDENT_UNIT);
1413                        let ib = self.ibox(INDENT_UNIT);
1414                        self.word(" else ");
1415                        self.print_block(els, cb, ib);
1416                    }
1417                }
1418                self.word(";");
1419                self.end(ib1);
1420            }
1421            ast::StmtKind::Item(item) => self.print_item(item),
1422            ast::StmtKind::Expr(expr) => {
1423                self.space_if_not_bol();
1424                self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1425                if classify::expr_requires_semi_to_be_stmt(expr) {
1426                    self.word(";");
1427                }
1428            }
1429            ast::StmtKind::Semi(expr) => {
1430                self.space_if_not_bol();
1431                self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1432                self.word(";");
1433            }
1434            ast::StmtKind::Empty => {
1435                self.space_if_not_bol();
1436                self.word(";");
1437            }
1438            ast::StmtKind::MacCall(mac) => {
1439                self.space_if_not_bol();
1440                self.print_outer_attributes(&mac.attrs);
1441                self.print_mac(&mac.mac);
1442                if mac.style == ast::MacStmtStyle::Semicolon {
1443                    self.word(";");
1444                }
1445            }
1446        }
1447        self.maybe_print_trailing_comment(st.span, None)
1448    }
1449
1450    fn print_block(&mut self, blk: &ast::Block, cb: BoxMarker, ib: BoxMarker) {
1451        self.print_block_with_attrs(blk, &[], cb, ib)
1452    }
1453
1454    fn print_block_unclosed_indent(&mut self, blk: &ast::Block, ib: BoxMarker) {
1455        self.print_block_maybe_unclosed(blk, &[], None, ib)
1456    }
1457
1458    fn print_block_with_attrs(
1459        &mut self,
1460        blk: &ast::Block,
1461        attrs: &[ast::Attribute],
1462        cb: BoxMarker,
1463        ib: BoxMarker,
1464    ) {
1465        self.print_block_maybe_unclosed(blk, attrs, Some(cb), ib)
1466    }
1467
1468    fn print_block_maybe_unclosed(
1469        &mut self,
1470        blk: &ast::Block,
1471        attrs: &[ast::Attribute],
1472        cb: Option<BoxMarker>,
1473        ib: BoxMarker,
1474    ) {
1475        match blk.rules {
1476            BlockCheckMode::Unsafe(..) => self.word_space("unsafe"),
1477            BlockCheckMode::Default => (),
1478        }
1479        self.maybe_print_comment(blk.span.lo());
1480        self.ann.pre(self, AnnNode::Block(blk));
1481        self.bopen(ib);
1482
1483        let has_attrs = self.print_inner_attributes(attrs);
1484
1485        for (i, st) in blk.stmts.iter().enumerate() {
1486            match &st.kind {
1487                ast::StmtKind::Expr(expr) if i == blk.stmts.len() - 1 => {
1488                    self.maybe_print_comment(st.span.lo());
1489                    self.space_if_not_bol();
1490                    self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1491                    self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi()));
1492                }
1493                _ => self.print_stmt(st),
1494            }
1495        }
1496
1497        let no_space = !has_attrs && blk.stmts.is_empty();
1498        self.bclose_maybe_open(blk.span, no_space, cb);
1499        self.ann.post(self, AnnNode::Block(blk))
1500    }
1501
1502    /// Print a `let pat = expr` expression.
1503    ///
1504    /// Parentheses are inserted surrounding `expr` if a round-trip through the
1505    /// parser would otherwise work out the wrong way in a condition position.
1506    ///
1507    /// For example each of the following would mean the wrong thing without
1508    /// parentheses.
1509    ///
1510    /// ```ignore (illustrative)
1511    /// if let _ = (Struct {}) {}
1512    ///
1513    /// if let _ = (true && false) {}
1514    /// ```
1515    ///
1516    /// In a match guard, the second case still requires parens, but the first
1517    /// case no longer does because anything until `=>` is considered part of
1518    /// the match guard expression. Parsing of the expression is not terminated
1519    /// by `{` in that position.
1520    ///
1521    /// ```ignore (illustrative)
1522    /// match () {
1523    ///     () if let _ = Struct {} => {}
1524    ///     () if let _ = (true && false) => {}
1525    /// }
1526    /// ```
1527    fn print_let(&mut self, pat: &ast::Pat, expr: &ast::Expr, fixup: FixupContext) {
1528        self.word("let ");
1529        self.print_pat(pat);
1530        self.space();
1531        self.word_space("=");
1532        self.print_expr_cond_paren(
1533            expr,
1534            fixup.needs_par_as_let_scrutinee(expr),
1535            FixupContext::default(),
1536        );
1537    }
1538
1539    fn print_mac(&mut self, m: &ast::MacCall) {
1540        self.print_mac_common(
1541            Some(MacHeader::Path(&m.path)),
1542            true,
1543            None,
1544            m.args.delim,
1545            None,
1546            &m.args.tokens,
1547            true,
1548            m.span(),
1549        );
1550    }
1551
1552    fn print_inline_asm(&mut self, asm: &ast::InlineAsm) {
1553        enum AsmArg<'a> {
1554            Template(String),
1555            Operand(&'a InlineAsmOperand),
1556            ClobberAbi(Symbol),
1557            Options(InlineAsmOptions),
1558        }
1559
1560        let mut args = vec![AsmArg::Template(InlineAsmTemplatePiece::to_string(&asm.template))];
1561        args.extend(asm.operands.iter().map(|(o, _)| AsmArg::Operand(o)));
1562        for (abi, _) in &asm.clobber_abis {
1563            args.push(AsmArg::ClobberAbi(*abi));
1564        }
1565        if !asm.options.is_empty() {
1566            args.push(AsmArg::Options(asm.options));
1567        }
1568
1569        self.popen();
1570        self.commasep(Consistent, &args, |s, arg| match arg {
1571            AsmArg::Template(template) => s.print_string(template, ast::StrStyle::Cooked),
1572            AsmArg::Operand(op) => {
1573                let print_reg_or_class = |s: &mut Self, r: &InlineAsmRegOrRegClass| match r {
1574                    InlineAsmRegOrRegClass::Reg(r) => s.print_symbol(*r, ast::StrStyle::Cooked),
1575                    InlineAsmRegOrRegClass::RegClass(r) => s.word(r.to_string()),
1576                };
1577                match op {
1578                    InlineAsmOperand::In { reg, expr } => {
1579                        s.word("in");
1580                        s.popen();
1581                        print_reg_or_class(s, reg);
1582                        s.pclose();
1583                        s.space();
1584                        s.print_expr(expr, FixupContext::default());
1585                    }
1586                    InlineAsmOperand::Out { reg, late, expr } => {
1587                        s.word(if *late { "lateout" } else { "out" });
1588                        s.popen();
1589                        print_reg_or_class(s, reg);
1590                        s.pclose();
1591                        s.space();
1592                        match expr {
1593                            Some(expr) => s.print_expr(expr, FixupContext::default()),
1594                            None => s.word("_"),
1595                        }
1596                    }
1597                    InlineAsmOperand::InOut { reg, late, expr } => {
1598                        s.word(if *late { "inlateout" } else { "inout" });
1599                        s.popen();
1600                        print_reg_or_class(s, reg);
1601                        s.pclose();
1602                        s.space();
1603                        s.print_expr(expr, FixupContext::default());
1604                    }
1605                    InlineAsmOperand::SplitInOut { reg, late, in_expr, out_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(in_expr, FixupContext::default());
1612                        s.space();
1613                        s.word_space("=>");
1614                        match out_expr {
1615                            Some(out_expr) => s.print_expr(out_expr, FixupContext::default()),
1616                            None => s.word("_"),
1617                        }
1618                    }
1619                    InlineAsmOperand::Const { anon_const } => {
1620                        s.word("const");
1621                        s.space();
1622                        s.print_expr(&anon_const.value, FixupContext::default());
1623                    }
1624                    InlineAsmOperand::Sym { sym } => {
1625                        s.word("sym");
1626                        s.space();
1627                        if let Some(qself) = &sym.qself {
1628                            s.print_qpath(&sym.path, qself, true);
1629                        } else {
1630                            s.print_path(&sym.path, true, 0);
1631                        }
1632                    }
1633                    InlineAsmOperand::Label { block } => {
1634                        let (cb, ib) = s.head("label");
1635                        s.print_block(block, cb, ib);
1636                    }
1637                }
1638            }
1639            AsmArg::ClobberAbi(abi) => {
1640                s.word("clobber_abi");
1641                s.popen();
1642                s.print_symbol(*abi, ast::StrStyle::Cooked);
1643                s.pclose();
1644            }
1645            AsmArg::Options(opts) => {
1646                s.word("options");
1647                s.popen();
1648                s.commasep(Inconsistent, &opts.human_readable_names(), |s, &opt| {
1649                    s.word(opt);
1650                });
1651                s.pclose();
1652            }
1653        });
1654        self.pclose();
1655    }
1656
1657    fn print_local_decl(&mut self, loc: &ast::Local) {
1658        self.print_pat(&loc.pat);
1659        if let Some(ty) = &loc.ty {
1660            self.word_space(":");
1661            self.print_type(ty);
1662        }
1663    }
1664
1665    fn print_name(&mut self, name: Symbol) {
1666        self.word(name.to_string());
1667        self.ann.post(self, AnnNode::Name(&name))
1668    }
1669
1670    fn print_qpath(&mut self, path: &ast::Path, qself: &ast::QSelf, colons_before_params: bool) {
1671        self.word("<");
1672        self.print_type(&qself.ty);
1673        if qself.position > 0 {
1674            self.space();
1675            self.word_space("as");
1676            let depth = path.segments.len() - qself.position;
1677            self.print_path(path, false, depth);
1678        }
1679        self.word(">");
1680        for item_segment in &path.segments[qself.position..] {
1681            self.word("::");
1682            self.print_ident(item_segment.ident);
1683            if let Some(args) = &item_segment.args {
1684                self.print_generic_args(args, colons_before_params)
1685            }
1686        }
1687    }
1688
1689    fn print_pat(&mut self, pat: &ast::Pat) {
1690        self.maybe_print_comment(pat.span.lo());
1691        self.ann.pre(self, AnnNode::Pat(pat));
1692        /* Pat isn't normalized, but the beauty of it is that it doesn't matter */
1693        match &pat.kind {
1694            PatKind::Missing => unreachable!(),
1695            PatKind::Wild => self.word("_"),
1696            PatKind::Never => self.word("!"),
1697            PatKind::Ident(BindingMode(by_ref, mutbl), ident, sub) => {
1698                if mutbl.is_mut() {
1699                    self.word_nbsp("mut");
1700                }
1701                if let ByRef::Yes(rmutbl) = by_ref {
1702                    self.word_nbsp("ref");
1703                    if rmutbl.is_mut() {
1704                        self.word_nbsp("mut");
1705                    }
1706                }
1707                self.print_ident(*ident);
1708                if let Some(p) = sub {
1709                    self.space();
1710                    self.word_space("@");
1711                    self.print_pat(p);
1712                }
1713            }
1714            PatKind::TupleStruct(qself, path, elts) => {
1715                if let Some(qself) = qself {
1716                    self.print_qpath(path, qself, true);
1717                } else {
1718                    self.print_path(path, true, 0);
1719                }
1720                self.popen();
1721                self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));
1722                self.pclose();
1723            }
1724            PatKind::Or(pats) => {
1725                self.strsep("|", true, Inconsistent, pats, |s, p| s.print_pat(p));
1726            }
1727            PatKind::Path(None, path) => {
1728                self.print_path(path, true, 0);
1729            }
1730            PatKind::Path(Some(qself), path) => {
1731                self.print_qpath(path, qself, false);
1732            }
1733            PatKind::Struct(qself, path, fields, etc) => {
1734                if let Some(qself) = qself {
1735                    self.print_qpath(path, qself, true);
1736                } else {
1737                    self.print_path(path, true, 0);
1738                }
1739                self.nbsp();
1740                self.word("{");
1741                let empty = fields.is_empty() && *etc == ast::PatFieldsRest::None;
1742                if !empty {
1743                    self.space();
1744                }
1745                self.commasep_cmnt(
1746                    Consistent,
1747                    fields,
1748                    |s, f| {
1749                        let cb = s.cbox(INDENT_UNIT);
1750                        if !f.is_shorthand {
1751                            s.print_ident(f.ident);
1752                            s.word_nbsp(":");
1753                        }
1754                        s.print_pat(&f.pat);
1755                        s.end(cb);
1756                    },
1757                    |f| f.pat.span,
1758                );
1759                if let ast::PatFieldsRest::Rest | ast::PatFieldsRest::Recovered(_) = etc {
1760                    if !fields.is_empty() {
1761                        self.word_space(",");
1762                    }
1763                    self.word("..");
1764                    if let ast::PatFieldsRest::Recovered(_) = etc {
1765                        self.word("/* recovered parse error */");
1766                    }
1767                }
1768                if !empty {
1769                    self.space();
1770                }
1771                self.word("}");
1772            }
1773            PatKind::Tuple(elts) => {
1774                self.popen();
1775                self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));
1776                if elts.len() == 1 {
1777                    self.word(",");
1778                }
1779                self.pclose();
1780            }
1781            PatKind::Box(inner) => {
1782                self.word("box ");
1783                self.print_pat(inner);
1784            }
1785            PatKind::Deref(inner) => {
1786                self.word("deref!");
1787                self.popen();
1788                self.print_pat(inner);
1789                self.pclose();
1790            }
1791            PatKind::Ref(inner, mutbl) => {
1792                self.word("&");
1793                if mutbl.is_mut() {
1794                    self.word("mut ");
1795                }
1796                if let PatKind::Ident(ast::BindingMode::MUT, ..) = inner.kind {
1797                    self.popen();
1798                    self.print_pat(inner);
1799                    self.pclose();
1800                } else {
1801                    self.print_pat(inner);
1802                }
1803            }
1804            PatKind::Expr(e) => self.print_expr(e, FixupContext::default()),
1805            PatKind::Range(begin, end, Spanned { node: end_kind, .. }) => {
1806                if let Some(e) = begin {
1807                    self.print_expr(e, FixupContext::default());
1808                }
1809                match end_kind {
1810                    RangeEnd::Included(RangeSyntax::DotDotDot) => self.word("..."),
1811                    RangeEnd::Included(RangeSyntax::DotDotEq) => self.word("..="),
1812                    RangeEnd::Excluded => self.word(".."),
1813                }
1814                if let Some(e) = end {
1815                    self.print_expr(e, FixupContext::default());
1816                }
1817            }
1818            PatKind::Guard(subpat, condition) => {
1819                self.popen();
1820                self.print_pat(subpat);
1821                self.space();
1822                self.word_space("if");
1823                self.print_expr(condition, FixupContext::default());
1824                self.pclose();
1825            }
1826            PatKind::Slice(elts) => {
1827                self.word("[");
1828                self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));
1829                self.word("]");
1830            }
1831            PatKind::Rest => self.word(".."),
1832            PatKind::Paren(inner) => {
1833                self.popen();
1834                self.print_pat(inner);
1835                self.pclose();
1836            }
1837            PatKind::MacCall(m) => self.print_mac(m),
1838            PatKind::Err(_) => {
1839                self.popen();
1840                self.word("/*ERROR*/");
1841                self.pclose();
1842            }
1843        }
1844        self.ann.post(self, AnnNode::Pat(pat))
1845    }
1846
1847    fn print_explicit_self(&mut self, explicit_self: &ast::ExplicitSelf) {
1848        match &explicit_self.node {
1849            SelfKind::Value(m) => {
1850                self.print_mutability(*m, false);
1851                self.word("self")
1852            }
1853            SelfKind::Region(lt, m) => {
1854                self.word("&");
1855                self.print_opt_lifetime(lt);
1856                self.print_mutability(*m, false);
1857                self.word("self")
1858            }
1859            SelfKind::Pinned(lt, m) => {
1860                self.word("&");
1861                self.print_opt_lifetime(lt);
1862                self.word("pin ");
1863                self.print_mutability(*m, true);
1864                self.word("self")
1865            }
1866            SelfKind::Explicit(typ, m) => {
1867                self.print_mutability(*m, false);
1868                self.word("self");
1869                self.word_space(":");
1870                self.print_type(typ)
1871            }
1872        }
1873    }
1874
1875    fn print_coroutine_kind(&mut self, coroutine_kind: ast::CoroutineKind) {
1876        match coroutine_kind {
1877            ast::CoroutineKind::Gen { .. } => {
1878                self.word_nbsp("gen");
1879            }
1880            ast::CoroutineKind::Async { .. } => {
1881                self.word_nbsp("async");
1882            }
1883            ast::CoroutineKind::AsyncGen { .. } => {
1884                self.word_nbsp("async");
1885                self.word_nbsp("gen");
1886            }
1887        }
1888    }
1889
1890    pub fn print_type_bounds(&mut self, bounds: &[ast::GenericBound]) {
1891        let mut first = true;
1892        for bound in bounds {
1893            if first {
1894                first = false;
1895            } else {
1896                self.nbsp();
1897                self.word_space("+");
1898            }
1899
1900            match bound {
1901                GenericBound::Trait(tref) => {
1902                    self.print_poly_trait_ref(tref);
1903                }
1904                GenericBound::Outlives(lt) => self.print_lifetime(*lt),
1905                GenericBound::Use(args, _) => {
1906                    self.word("use");
1907                    self.word("<");
1908                    self.commasep(Inconsistent, args, |s, arg| match arg {
1909                        ast::PreciseCapturingArg::Arg(p, _) => s.print_path(p, false, 0),
1910                        ast::PreciseCapturingArg::Lifetime(lt) => s.print_lifetime(*lt),
1911                    });
1912                    self.word(">")
1913                }
1914            }
1915        }
1916    }
1917
1918    fn print_lifetime(&mut self, lifetime: ast::Lifetime) {
1919        self.print_name(lifetime.ident.name)
1920    }
1921
1922    fn print_lifetime_bounds(&mut self, bounds: &ast::GenericBounds) {
1923        for (i, bound) in bounds.iter().enumerate() {
1924            if i != 0 {
1925                self.word(" + ");
1926            }
1927            match bound {
1928                ast::GenericBound::Outlives(lt) => self.print_lifetime(*lt),
1929                _ => {
1930                    panic!("expected a lifetime bound, found a trait bound")
1931                }
1932            }
1933        }
1934    }
1935
1936    fn print_generic_params(&mut self, generic_params: &[ast::GenericParam]) {
1937        if generic_params.is_empty() {
1938            return;
1939        }
1940
1941        self.word("<");
1942
1943        self.commasep(Inconsistent, generic_params, |s, param| {
1944            s.print_outer_attributes_inline(&param.attrs);
1945
1946            match &param.kind {
1947                ast::GenericParamKind::Lifetime => {
1948                    let lt = ast::Lifetime { id: param.id, ident: param.ident };
1949                    s.print_lifetime(lt);
1950                    if !param.bounds.is_empty() {
1951                        s.word_nbsp(":");
1952                        s.print_lifetime_bounds(&param.bounds)
1953                    }
1954                }
1955                ast::GenericParamKind::Type { default } => {
1956                    s.print_ident(param.ident);
1957                    if !param.bounds.is_empty() {
1958                        s.word_nbsp(":");
1959                        s.print_type_bounds(&param.bounds);
1960                    }
1961                    if let Some(default) = default {
1962                        s.space();
1963                        s.word_space("=");
1964                        s.print_type(default)
1965                    }
1966                }
1967                ast::GenericParamKind::Const { ty, default, .. } => {
1968                    s.word_space("const");
1969                    s.print_ident(param.ident);
1970                    s.space();
1971                    s.word_space(":");
1972                    s.print_type(ty);
1973                    if !param.bounds.is_empty() {
1974                        s.word_nbsp(":");
1975                        s.print_type_bounds(&param.bounds);
1976                    }
1977                    if let Some(default) = default {
1978                        s.space();
1979                        s.word_space("=");
1980                        s.print_expr(&default.value, FixupContext::default());
1981                    }
1982                }
1983            }
1984        });
1985
1986        self.word(">");
1987    }
1988
1989    pub fn print_mutability(&mut self, mutbl: ast::Mutability, print_const: bool) {
1990        match mutbl {
1991            ast::Mutability::Mut => self.word_nbsp("mut"),
1992            ast::Mutability::Not => {
1993                if print_const {
1994                    self.word_nbsp("const");
1995                }
1996            }
1997        }
1998    }
1999
2000    fn print_mt(&mut self, mt: &ast::MutTy, print_const: bool) {
2001        self.print_mutability(mt.mutbl, print_const);
2002        self.print_type(&mt.ty)
2003    }
2004
2005    fn print_param(&mut self, input: &ast::Param, is_closure: bool) {
2006        let ib = self.ibox(INDENT_UNIT);
2007
2008        self.print_outer_attributes_inline(&input.attrs);
2009
2010        match input.ty.kind {
2011            ast::TyKind::Infer if is_closure => self.print_pat(&input.pat),
2012            _ => {
2013                if let Some(eself) = input.to_self() {
2014                    self.print_explicit_self(&eself);
2015                } else {
2016                    if !matches!(input.pat.kind, PatKind::Missing) {
2017                        self.print_pat(&input.pat);
2018                        self.word(":");
2019                        self.space();
2020                    }
2021                    self.print_type(&input.ty);
2022                }
2023            }
2024        }
2025        self.end(ib);
2026    }
2027
2028    fn print_fn_ret_ty(&mut self, fn_ret_ty: &ast::FnRetTy) {
2029        if let ast::FnRetTy::Ty(ty) = fn_ret_ty {
2030            self.space_if_not_bol();
2031            let ib = self.ibox(INDENT_UNIT);
2032            self.word_space("->");
2033            self.print_type(ty);
2034            self.end(ib);
2035            self.maybe_print_comment(ty.span.lo());
2036        }
2037    }
2038
2039    fn print_ty_fn(
2040        &mut self,
2041        ext: ast::Extern,
2042        safety: ast::Safety,
2043        decl: &ast::FnDecl,
2044        name: Option<Ident>,
2045        generic_params: &[ast::GenericParam],
2046    ) {
2047        let ib = self.ibox(INDENT_UNIT);
2048        self.print_formal_generic_params(generic_params);
2049        let generics = ast::Generics::default();
2050        let header = ast::FnHeader { safety, ext, ..ast::FnHeader::default() };
2051        self.print_fn(decl, header, name, &generics);
2052        self.end(ib);
2053    }
2054
2055    fn print_fn_header_info(&mut self, header: ast::FnHeader) {
2056        self.print_constness(header.constness);
2057        header.coroutine_kind.map(|coroutine_kind| self.print_coroutine_kind(coroutine_kind));
2058        self.print_safety(header.safety);
2059
2060        match header.ext {
2061            ast::Extern::None => {}
2062            ast::Extern::Implicit(_) => {
2063                self.word_nbsp("extern");
2064            }
2065            ast::Extern::Explicit(abi, _) => {
2066                self.word_nbsp("extern");
2067                self.print_token_literal(abi.as_token_lit(), abi.span);
2068                self.nbsp();
2069            }
2070        }
2071
2072        self.word("fn")
2073    }
2074
2075    fn print_safety(&mut self, s: ast::Safety) {
2076        match s {
2077            ast::Safety::Default => {}
2078            ast::Safety::Safe(_) => self.word_nbsp("safe"),
2079            ast::Safety::Unsafe(_) => self.word_nbsp("unsafe"),
2080        }
2081    }
2082
2083    fn print_constness(&mut self, s: ast::Const) {
2084        match s {
2085            ast::Const::No => {}
2086            ast::Const::Yes(_) => self.word_nbsp("const"),
2087        }
2088    }
2089
2090    fn print_is_auto(&mut self, s: ast::IsAuto) {
2091        match s {
2092            ast::IsAuto::Yes => self.word_nbsp("auto"),
2093            ast::IsAuto::No => {}
2094        }
2095    }
2096
2097    fn print_meta_item_lit(&mut self, lit: &ast::MetaItemLit) {
2098        self.print_token_literal(lit.as_token_lit(), lit.span)
2099    }
2100
2101    fn print_token_literal(&mut self, token_lit: token::Lit, span: Span) {
2102        self.maybe_print_comment(span.lo());
2103        self.word(token_lit.to_string())
2104    }
2105
2106    fn print_symbol(&mut self, sym: Symbol, style: ast::StrStyle) {
2107        self.print_string(sym.as_str(), style);
2108    }
2109
2110    fn print_inner_attributes_no_trailing_hardbreak(&mut self, attrs: &[ast::Attribute]) -> bool {
2111        self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, false)
2112    }
2113
2114    fn print_outer_attributes_inline(&mut self, attrs: &[ast::Attribute]) -> bool {
2115        self.print_either_attributes(attrs, ast::AttrStyle::Outer, true, true)
2116    }
2117
2118    fn print_attribute(&mut self, attr: &ast::Attribute) {
2119        self.print_attribute_inline(attr, false);
2120    }
2121
2122    fn print_meta_list_item(&mut self, item: &ast::MetaItemInner) {
2123        match item {
2124            ast::MetaItemInner::MetaItem(mi) => self.print_meta_item(mi),
2125            ast::MetaItemInner::Lit(lit) => self.print_meta_item_lit(lit),
2126        }
2127    }
2128
2129    fn print_meta_item(&mut self, item: &ast::MetaItem) {
2130        let ib = self.ibox(INDENT_UNIT);
2131        match &item.kind {
2132            ast::MetaItemKind::Word => self.print_path(&item.path, false, 0),
2133            ast::MetaItemKind::NameValue(value) => {
2134                self.print_path(&item.path, false, 0);
2135                self.space();
2136                self.word_space("=");
2137                self.print_meta_item_lit(value);
2138            }
2139            ast::MetaItemKind::List(items) => {
2140                self.print_path(&item.path, false, 0);
2141                self.popen();
2142                self.commasep(Consistent, items, |s, i| s.print_meta_list_item(i));
2143                self.pclose();
2144            }
2145        }
2146        self.end(ib);
2147    }
2148
2149    pub(crate) fn bounds_to_string(&self, bounds: &[ast::GenericBound]) -> String {
2150        Self::to_string(|s| s.print_type_bounds(bounds))
2151    }
2152
2153    pub(crate) fn where_bound_predicate_to_string(
2154        &self,
2155        where_bound_predicate: &ast::WhereBoundPredicate,
2156    ) -> String {
2157        Self::to_string(|s| s.print_where_bound_predicate(where_bound_predicate))
2158    }
2159
2160    pub(crate) fn tt_to_string(&self, tt: &TokenTree) -> String {
2161        Self::to_string(|s| {
2162            s.print_tt(tt, false);
2163        })
2164    }
2165
2166    pub(crate) fn path_segment_to_string(&self, p: &ast::PathSegment) -> String {
2167        Self::to_string(|s| s.print_path_segment(p, false))
2168    }
2169
2170    pub(crate) fn meta_list_item_to_string(&self, li: &ast::MetaItemInner) -> String {
2171        Self::to_string(|s| s.print_meta_list_item(li))
2172    }
2173
2174    pub(crate) fn attribute_to_string(&self, attr: &ast::Attribute) -> String {
2175        Self::to_string(|s| s.print_attribute(attr))
2176    }
2177}