Skip to main content

rustc_ast_pretty/pprust/
state.rs

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