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        self.print_formal_generic_params(&t.bound_generic_params);
1419
1420        let ast::TraitBoundModifiers { constness, asyncness, polarity } = t.modifiers;
1421        match constness {
1422            ast::BoundConstness::Never => {}
1423            ast::BoundConstness::Always(_) | ast::BoundConstness::Maybe(_) => {
1424                self.word_space(constness.as_str());
1425            }
1426        }
1427        match asyncness {
1428            ast::BoundAsyncness::Normal => {}
1429            ast::BoundAsyncness::Async(_) => {
1430                self.word_space(asyncness.as_str());
1431            }
1432        }
1433        match polarity {
1434            ast::BoundPolarity::Positive => {}
1435            ast::BoundPolarity::Negative(_) | ast::BoundPolarity::Maybe(_) => {
1436                self.word(polarity.as_str());
1437            }
1438        }
1439
1440        self.print_trait_ref(&t.trait_ref)
1441    }
1442
1443    fn print_stmt(&mut self, st: &ast::Stmt) {
1444        self.maybe_print_comment(st.span.lo());
1445        match &st.kind {
1446            ast::StmtKind::Let(loc) => {
1447                self.print_outer_attributes(&loc.attrs);
1448                self.space_if_not_bol();
1449                let ib1 = self.ibox(INDENT_UNIT);
1450                if loc.super_.is_some() {
1451                    self.word_nbsp("super");
1452                }
1453                self.word_nbsp("let");
1454
1455                let ib2 = self.ibox(INDENT_UNIT);
1456                self.print_local_decl(loc);
1457                self.end(ib2);
1458                if let Some((init, els)) = loc.kind.init_else_opt() {
1459                    self.nbsp();
1460                    self.word_space("=");
1461                    self.print_expr_cond_paren(
1462                        init,
1463                        els.is_some() && classify::expr_trailing_brace(init).is_some(),
1464                        FixupContext::default(),
1465                    );
1466                    if let Some(els) = els {
1467                        let cb = self.cbox(INDENT_UNIT);
1468                        let ib = self.ibox(INDENT_UNIT);
1469                        self.word(" else ");
1470                        self.print_block(els, cb, ib);
1471                    }
1472                }
1473                self.word(";");
1474                self.end(ib1);
1475            }
1476            ast::StmtKind::Item(item) => self.print_item(item),
1477            ast::StmtKind::Expr(expr) => {
1478                self.space_if_not_bol();
1479                self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1480                if classify::expr_requires_semi_to_be_stmt(expr) {
1481                    self.word(";");
1482                }
1483            }
1484            ast::StmtKind::Semi(expr) => {
1485                self.space_if_not_bol();
1486                self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1487                self.word(";");
1488            }
1489            ast::StmtKind::Empty => {
1490                self.space_if_not_bol();
1491                self.word(";");
1492            }
1493            ast::StmtKind::MacCall(mac) => {
1494                self.space_if_not_bol();
1495                self.print_outer_attributes(&mac.attrs);
1496                self.print_mac(&mac.mac);
1497                if mac.style == ast::MacStmtStyle::Semicolon {
1498                    self.word(";");
1499                }
1500            }
1501        }
1502        self.maybe_print_trailing_comment(st.span, None)
1503    }
1504
1505    fn print_block(&mut self, blk: &ast::Block, cb: BoxMarker, ib: BoxMarker) {
1506        self.print_block_with_attrs(blk, &[], cb, ib)
1507    }
1508
1509    fn print_block_unclosed_indent(&mut self, blk: &ast::Block, ib: BoxMarker) {
1510        self.print_block_maybe_unclosed(blk, &[], None, ib)
1511    }
1512
1513    fn print_block_with_attrs(
1514        &mut self,
1515        blk: &ast::Block,
1516        attrs: &[ast::Attribute],
1517        cb: BoxMarker,
1518        ib: BoxMarker,
1519    ) {
1520        self.print_block_maybe_unclosed(blk, attrs, Some(cb), ib)
1521    }
1522
1523    fn print_block_maybe_unclosed(
1524        &mut self,
1525        blk: &ast::Block,
1526        attrs: &[ast::Attribute],
1527        cb: Option<BoxMarker>,
1528        ib: BoxMarker,
1529    ) {
1530        match blk.rules {
1531            BlockCheckMode::Unsafe(..) => self.word_space("unsafe"),
1532            BlockCheckMode::Default => (),
1533        }
1534        self.maybe_print_comment(blk.span.lo());
1535        self.ann.pre(self, AnnNode::Block(blk));
1536        self.bopen(ib);
1537
1538        let has_attrs = self.print_inner_attributes(attrs);
1539
1540        for (i, st) in blk.stmts.iter().enumerate() {
1541            match &st.kind {
1542                ast::StmtKind::Expr(expr) if i == blk.stmts.len() - 1 => {
1543                    self.maybe_print_comment(st.span.lo());
1544                    self.space_if_not_bol();
1545                    self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1546                    self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi()));
1547                }
1548                _ => self.print_stmt(st),
1549            }
1550        }
1551
1552        let no_space = !has_attrs && blk.stmts.is_empty();
1553        self.bclose_maybe_open(blk.span, no_space, cb);
1554        self.ann.post(self, AnnNode::Block(blk))
1555    }
1556
1557    /// Print a `let pat = expr` expression.
1558    ///
1559    /// Parentheses are inserted surrounding `expr` if a round-trip through the
1560    /// parser would otherwise work out the wrong way in a condition position.
1561    ///
1562    /// For example each of the following would mean the wrong thing without
1563    /// parentheses.
1564    ///
1565    /// ```ignore (illustrative)
1566    /// if let _ = (Struct {}) {}
1567    ///
1568    /// if let _ = (true && false) {}
1569    /// ```
1570    ///
1571    /// In a match guard, the second case still requires parens, but the first
1572    /// case no longer does because anything until `=>` is considered part of
1573    /// the match guard expression. Parsing of the expression is not terminated
1574    /// by `{` in that position.
1575    ///
1576    /// ```ignore (illustrative)
1577    /// match () {
1578    ///     () if let _ = Struct {} => {}
1579    ///     () if let _ = (true && false) => {}
1580    /// }
1581    /// ```
1582    fn print_let(&mut self, pat: &ast::Pat, expr: &ast::Expr, fixup: FixupContext) {
1583        self.word("let ");
1584        self.print_pat(pat);
1585        self.space();
1586        self.word_space("=");
1587        self.print_expr_cond_paren(
1588            expr,
1589            fixup.needs_par_as_let_scrutinee(expr),
1590            FixupContext::default(),
1591        );
1592    }
1593
1594    fn print_mac(&mut self, m: &ast::MacCall) {
1595        self.print_mac_common(
1596            Some(MacHeader::Path(&m.path)),
1597            true,
1598            None,
1599            m.args.delim,
1600            None,
1601            &m.args.tokens,
1602            true,
1603            m.span(),
1604        );
1605    }
1606
1607    fn print_inline_asm(&mut self, asm: &ast::InlineAsm) {
1608        enum AsmArg<'a> {
1609            Template(String),
1610            Operand(&'a InlineAsmOperand),
1611            ClobberAbi(Symbol),
1612            Options(InlineAsmOptions),
1613        }
1614
1615        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))];
1616        args.extend(asm.operands.iter().map(|(o, _)| AsmArg::Operand(o)));
1617        for (abi, _) in &asm.clobber_abis {
1618            args.push(AsmArg::ClobberAbi(*abi));
1619        }
1620        if !asm.options.is_empty() {
1621            args.push(AsmArg::Options(asm.options));
1622        }
1623
1624        self.popen();
1625        self.commasep(Consistent, &args, |s, arg| match arg {
1626            AsmArg::Template(template) => s.print_string(template, ast::StrStyle::Cooked),
1627            AsmArg::Operand(op) => {
1628                let print_reg_or_class = |s: &mut Self, r: &InlineAsmRegOrRegClass| match r {
1629                    InlineAsmRegOrRegClass::Reg(r) => s.print_symbol(*r, ast::StrStyle::Cooked),
1630                    InlineAsmRegOrRegClass::RegClass(r) => s.word(r.to_string()),
1631                };
1632                match op {
1633                    InlineAsmOperand::In { reg, expr } => {
1634                        s.word("in");
1635                        s.popen();
1636                        print_reg_or_class(s, reg);
1637                        s.pclose();
1638                        s.space();
1639                        s.print_expr(expr, FixupContext::default());
1640                    }
1641                    InlineAsmOperand::Out { reg, late, expr } => {
1642                        s.word(if *late { "lateout" } else { "out" });
1643                        s.popen();
1644                        print_reg_or_class(s, reg);
1645                        s.pclose();
1646                        s.space();
1647                        match expr {
1648                            Some(expr) => s.print_expr(expr, FixupContext::default()),
1649                            None => s.word("_"),
1650                        }
1651                    }
1652                    InlineAsmOperand::InOut { reg, late, expr } => {
1653                        s.word(if *late { "inlateout" } else { "inout" });
1654                        s.popen();
1655                        print_reg_or_class(s, reg);
1656                        s.pclose();
1657                        s.space();
1658                        s.print_expr(expr, FixupContext::default());
1659                    }
1660                    InlineAsmOperand::SplitInOut { reg, late, in_expr, out_expr } => {
1661                        s.word(if *late { "inlateout" } else { "inout" });
1662                        s.popen();
1663                        print_reg_or_class(s, reg);
1664                        s.pclose();
1665                        s.space();
1666                        s.print_expr(in_expr, FixupContext::default());
1667                        s.space();
1668                        s.word_space("=>");
1669                        match out_expr {
1670                            Some(out_expr) => s.print_expr(out_expr, FixupContext::default()),
1671                            None => s.word("_"),
1672                        }
1673                    }
1674                    InlineAsmOperand::Const { anon_const } => {
1675                        s.word("const");
1676                        s.space();
1677                        s.print_expr(&anon_const.value, FixupContext::default());
1678                    }
1679                    InlineAsmOperand::Sym { sym } => {
1680                        s.word("sym");
1681                        s.space();
1682                        if let Some(qself) = &sym.qself {
1683                            s.print_qpath(&sym.path, qself, true);
1684                        } else {
1685                            s.print_path(&sym.path, true, 0);
1686                        }
1687                    }
1688                    InlineAsmOperand::Label { block } => {
1689                        let (cb, ib) = s.head("label");
1690                        s.print_block(block, cb, ib);
1691                    }
1692                }
1693            }
1694            AsmArg::ClobberAbi(abi) => {
1695                s.word("clobber_abi");
1696                s.popen();
1697                s.print_symbol(*abi, ast::StrStyle::Cooked);
1698                s.pclose();
1699            }
1700            AsmArg::Options(opts) => {
1701                s.word("options");
1702                s.popen();
1703                s.commasep(Inconsistent, &opts.human_readable_names(), |s, &opt| {
1704                    s.word(opt);
1705                });
1706                s.pclose();
1707            }
1708        });
1709        self.pclose();
1710    }
1711
1712    fn print_local_decl(&mut self, loc: &ast::Local) {
1713        self.print_pat(&loc.pat);
1714        if let Some(ty) = &loc.ty {
1715            self.word_space(":");
1716            self.print_type(ty);
1717        }
1718    }
1719
1720    fn print_name(&mut self, name: Symbol) {
1721        self.word(name.to_string());
1722        self.ann.post(self, AnnNode::Name(&name))
1723    }
1724
1725    fn print_qpath(&mut self, path: &ast::Path, qself: &ast::QSelf, colons_before_params: bool) {
1726        self.word("<");
1727        self.print_type(&qself.ty);
1728        if qself.position > 0 {
1729            self.space();
1730            self.word_space("as");
1731            let depth = path.segments.len() - qself.position;
1732            self.print_path(path, false, depth);
1733        }
1734        self.word(">");
1735        for item_segment in &path.segments[qself.position..] {
1736            self.word("::");
1737            self.print_ident(item_segment.ident);
1738            if let Some(args) = &item_segment.args {
1739                self.print_generic_args(args, colons_before_params)
1740            }
1741        }
1742    }
1743
1744    fn print_pat(&mut self, pat: &ast::Pat) {
1745        self.maybe_print_comment(pat.span.lo());
1746        self.ann.pre(self, AnnNode::Pat(pat));
1747        /* Pat isn't normalized, but the beauty of it is that it doesn't matter */
1748        match &pat.kind {
1749            PatKind::Missing => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
1750            PatKind::Wild => self.word("_"),
1751            PatKind::Never => self.word("!"),
1752            PatKind::Ident(BindingMode(by_ref, mutbl), ident, sub) => {
1753                if mutbl.is_mut() {
1754                    self.word_nbsp("mut");
1755                }
1756                if let ByRef::Yes(pinnedness, rmutbl) = by_ref {
1757                    self.word_nbsp("ref");
1758                    if pinnedness.is_pinned() {
1759                        self.word_nbsp("pin");
1760                    }
1761                    if rmutbl.is_mut() {
1762                        self.word_nbsp("mut");
1763                    } else if pinnedness.is_pinned() {
1764                        self.word_nbsp("const");
1765                    }
1766                }
1767                self.print_ident(*ident);
1768                if let Some(p) = sub {
1769                    self.space();
1770                    self.word_space("@");
1771                    self.print_pat(p);
1772                }
1773            }
1774            PatKind::TupleStruct(qself, path, elts) => {
1775                if let Some(qself) = qself {
1776                    self.print_qpath(path, qself, true);
1777                } else {
1778                    self.print_path(path, true, 0);
1779                }
1780                self.popen();
1781                self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));
1782                self.pclose();
1783            }
1784            PatKind::Or(pats) => {
1785                self.strsep("|", true, Inconsistent, pats, |s, p| s.print_pat(p));
1786            }
1787            PatKind::Path(None, path) => {
1788                self.print_path(path, true, 0);
1789            }
1790            PatKind::Path(Some(qself), path) => {
1791                self.print_qpath(path, qself, false);
1792            }
1793            PatKind::Struct(qself, path, fields, etc) => {
1794                if let Some(qself) = qself {
1795                    self.print_qpath(path, qself, true);
1796                } else {
1797                    self.print_path(path, true, 0);
1798                }
1799                self.nbsp();
1800                self.word("{");
1801                let empty = fields.is_empty() && *etc == ast::PatFieldsRest::None;
1802                if !empty {
1803                    self.space();
1804                }
1805                self.commasep_cmnt(
1806                    Consistent,
1807                    fields,
1808                    |s, f| {
1809                        let cb = s.cbox(INDENT_UNIT);
1810                        if !f.is_shorthand {
1811                            s.print_ident(f.ident);
1812                            s.word_nbsp(":");
1813                        }
1814                        s.print_pat(&f.pat);
1815                        s.end(cb);
1816                    },
1817                    |f| f.pat.span,
1818                );
1819                if let ast::PatFieldsRest::Rest(_) | ast::PatFieldsRest::Recovered(_) = etc {
1820                    if !fields.is_empty() {
1821                        self.word_space(",");
1822                    }
1823                    self.word("..");
1824                    if let ast::PatFieldsRest::Recovered(_) = etc {
1825                        self.word("/* recovered parse error */");
1826                    }
1827                }
1828                if !empty {
1829                    self.space();
1830                }
1831                self.word("}");
1832            }
1833            PatKind::Tuple(elts) => {
1834                self.popen();
1835                self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));
1836                if elts.len() == 1 {
1837                    self.word(",");
1838                }
1839                self.pclose();
1840            }
1841            PatKind::Box(inner) => {
1842                self.word("box ");
1843                self.print_pat(inner);
1844            }
1845            PatKind::Deref(inner) => {
1846                self.word("deref!");
1847                self.popen();
1848                self.print_pat(inner);
1849                self.pclose();
1850            }
1851            PatKind::Ref(inner, pinned, mutbl) => {
1852                self.word("&");
1853                if pinned.is_pinned() {
1854                    self.word("pin ");
1855                    if mutbl.is_not() {
1856                        self.word("const ");
1857                    }
1858                }
1859                if mutbl.is_mut() {
1860                    self.word("mut ");
1861                }
1862                if let PatKind::Ident(ast::BindingMode::MUT, ..) = inner.kind {
1863                    self.popen();
1864                    self.print_pat(inner);
1865                    self.pclose();
1866                } else {
1867                    self.print_pat(inner);
1868                }
1869            }
1870            PatKind::Expr(e) => self.print_expr(e, FixupContext::default()),
1871            PatKind::Range(begin, end, Spanned { node: end_kind, .. }) => {
1872                if let Some(e) = begin {
1873                    self.print_expr(e, FixupContext::default());
1874                }
1875                match end_kind {
1876                    RangeEnd::Included(RangeSyntax::DotDotDot) => self.word("..."),
1877                    RangeEnd::Included(RangeSyntax::DotDotEq) => self.word("..="),
1878                    RangeEnd::Excluded => self.word(".."),
1879                }
1880                if let Some(e) = end {
1881                    self.print_expr(e, FixupContext::default());
1882                }
1883            }
1884            PatKind::Guard(subpat, condition) => {
1885                self.popen();
1886                self.print_pat(subpat);
1887                self.space();
1888                self.word_space("if");
1889                self.print_expr(condition, FixupContext::default());
1890                self.pclose();
1891            }
1892            PatKind::Slice(elts) => {
1893                self.word("[");
1894                self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));
1895                self.word("]");
1896            }
1897            PatKind::Rest => self.word(".."),
1898            PatKind::Paren(inner) => {
1899                self.popen();
1900                self.print_pat(inner);
1901                self.pclose();
1902            }
1903            PatKind::MacCall(m) => self.print_mac(m),
1904            PatKind::Err(_) => {
1905                self.popen();
1906                self.word("/*ERROR*/");
1907                self.pclose();
1908            }
1909        }
1910        self.ann.post(self, AnnNode::Pat(pat))
1911    }
1912
1913    fn print_explicit_self(&mut self, explicit_self: &ast::ExplicitSelf) {
1914        match &explicit_self.node {
1915            SelfKind::Value(m) => {
1916                self.print_mutability(*m, false);
1917                self.word("self")
1918            }
1919            SelfKind::Region(lt, m) => {
1920                self.word("&");
1921                self.print_opt_lifetime(lt);
1922                self.print_mutability(*m, false);
1923                self.word("self")
1924            }
1925            SelfKind::Pinned(lt, m) => {
1926                self.word("&");
1927                self.print_opt_lifetime(lt);
1928                self.word("pin ");
1929                self.print_mutability(*m, true);
1930                self.word("self")
1931            }
1932            SelfKind::Explicit(typ, m) => {
1933                self.print_mutability(*m, false);
1934                self.word("self");
1935                self.word_space(":");
1936                self.print_type(typ)
1937            }
1938        }
1939    }
1940
1941    fn print_coroutine_kind(&mut self, coroutine_kind: ast::CoroutineKind) {
1942        match coroutine_kind {
1943            ast::CoroutineKind::Gen { .. } => {
1944                self.word_nbsp("gen");
1945            }
1946            ast::CoroutineKind::Async { .. } => {
1947                self.word_nbsp("async");
1948            }
1949            ast::CoroutineKind::AsyncGen { .. } => {
1950                self.word_nbsp("async");
1951                self.word_nbsp("gen");
1952            }
1953        }
1954    }
1955
1956    pub fn print_type_bounds(&mut self, bounds: &[ast::GenericBound]) {
1957        let mut first = true;
1958        for bound in bounds {
1959            if first {
1960                first = false;
1961            } else {
1962                self.nbsp();
1963                self.word_space("+");
1964            }
1965
1966            match bound {
1967                GenericBound::Trait(tref) => {
1968                    self.print_poly_trait_ref(tref);
1969                }
1970                GenericBound::Outlives(lt) => self.print_lifetime(*lt),
1971                GenericBound::Use(args, _) => {
1972                    self.word("use");
1973                    self.word("<");
1974                    self.commasep(Inconsistent, args, |s, arg| match arg {
1975                        ast::PreciseCapturingArg::Arg(p, _) => s.print_path(p, false, 0),
1976                        ast::PreciseCapturingArg::Lifetime(lt) => s.print_lifetime(*lt),
1977                    });
1978                    self.word(">")
1979                }
1980            }
1981        }
1982    }
1983
1984    fn print_lifetime(&mut self, lifetime: ast::Lifetime) {
1985        self.word(lifetime.ident.name.to_string());
1986        self.ann_post(lifetime.ident)
1987    }
1988
1989    fn print_lifetime_bounds(&mut self, bounds: &ast::GenericBounds) {
1990        for (i, bound) in bounds.iter().enumerate() {
1991            if i != 0 {
1992                self.word(" + ");
1993            }
1994            match bound {
1995                ast::GenericBound::Outlives(lt) => self.print_lifetime(*lt),
1996                _ => {
1997                    {
    ::core::panicking::panic_fmt(format_args!("expected a lifetime bound, found a trait bound"));
}panic!("expected a lifetime bound, found a trait bound")
1998                }
1999            }
2000        }
2001    }
2002
2003    fn print_generic_params(&mut self, generic_params: &[ast::GenericParam]) {
2004        if generic_params.is_empty() {
2005            return;
2006        }
2007
2008        self.word("<");
2009
2010        self.commasep(Inconsistent, generic_params, |s, param| {
2011            s.print_outer_attributes_inline(&param.attrs);
2012
2013            match &param.kind {
2014                ast::GenericParamKind::Lifetime => {
2015                    let lt = ast::Lifetime { id: param.id, ident: param.ident };
2016                    s.print_lifetime(lt);
2017                    if !param.bounds.is_empty() {
2018                        s.word_nbsp(":");
2019                        s.print_lifetime_bounds(&param.bounds)
2020                    }
2021                }
2022                ast::GenericParamKind::Type { default } => {
2023                    s.print_ident(param.ident);
2024                    if !param.bounds.is_empty() {
2025                        s.word_nbsp(":");
2026                        s.print_type_bounds(&param.bounds);
2027                    }
2028                    if let Some(default) = default {
2029                        s.space();
2030                        s.word_space("=");
2031                        s.print_type(default)
2032                    }
2033                }
2034                ast::GenericParamKind::Const { ty, default, .. } => {
2035                    s.word_space("const");
2036                    s.print_ident(param.ident);
2037                    s.space();
2038                    s.word_space(":");
2039                    s.print_type(ty);
2040                    if !param.bounds.is_empty() {
2041                        s.word_nbsp(":");
2042                        s.print_type_bounds(&param.bounds);
2043                    }
2044                    if let Some(default) = default {
2045                        s.space();
2046                        s.word_space("=");
2047                        s.print_expr(&default.value, FixupContext::default());
2048                    }
2049                }
2050            }
2051        });
2052
2053        self.word(">");
2054    }
2055
2056    pub fn print_mutability(&mut self, mutbl: ast::Mutability, print_const: bool) {
2057        match mutbl {
2058            ast::Mutability::Mut => self.word_nbsp("mut"),
2059            ast::Mutability::Not => {
2060                if print_const {
2061                    self.word_nbsp("const");
2062                }
2063            }
2064        }
2065    }
2066
2067    fn print_mt(&mut self, mt: &ast::MutTy, print_const: bool) {
2068        self.print_mutability(mt.mutbl, print_const);
2069        self.print_type(&mt.ty)
2070    }
2071
2072    fn print_param(&mut self, input: &ast::Param, is_closure: bool) {
2073        let ib = self.ibox(INDENT_UNIT);
2074
2075        self.print_outer_attributes_inline(&input.attrs);
2076
2077        match input.ty.kind {
2078            ast::TyKind::Infer if is_closure => self.print_pat(&input.pat),
2079            _ => {
2080                if let Some(eself) = input.to_self() {
2081                    self.print_explicit_self(&eself);
2082                } else {
2083                    if !#[allow(non_exhaustive_omitted_patterns)] match input.pat.kind {
    PatKind::Missing => true,
    _ => false,
}matches!(input.pat.kind, PatKind::Missing) {
2084                        self.print_pat(&input.pat);
2085                        self.word(":");
2086                        self.space();
2087                    }
2088                    self.print_type(&input.ty);
2089                }
2090            }
2091        }
2092        self.end(ib);
2093    }
2094
2095    fn print_fn_ret_ty(&mut self, fn_ret_ty: &ast::FnRetTy) {
2096        if let ast::FnRetTy::Ty(ty) = fn_ret_ty {
2097            self.space_if_not_bol();
2098            let ib = self.ibox(INDENT_UNIT);
2099            self.word_space("->");
2100            self.print_type(ty);
2101            self.end(ib);
2102            self.maybe_print_comment(ty.span.lo());
2103        }
2104    }
2105
2106    fn print_ty_fn(
2107        &mut self,
2108        ext: ast::Extern,
2109        safety: ast::Safety,
2110        decl: &ast::FnDecl,
2111        name: Option<Ident>,
2112        generic_params: &[ast::GenericParam],
2113    ) {
2114        let ib = self.ibox(INDENT_UNIT);
2115        self.print_formal_generic_params(generic_params);
2116        let generics = ast::Generics::default();
2117        let header = ast::FnHeader { safety, ext, ..ast::FnHeader::default() };
2118        self.print_fn(decl, header, name, &generics);
2119        self.end(ib);
2120    }
2121
2122    fn print_fn_header_info(&mut self, header: ast::FnHeader) {
2123        self.print_constness(header.constness);
2124        header.coroutine_kind.map(|coroutine_kind| self.print_coroutine_kind(coroutine_kind));
2125        self.print_safety(header.safety);
2126
2127        match header.ext {
2128            ast::Extern::None => {}
2129            ast::Extern::Implicit(_) => {
2130                self.word_nbsp("extern");
2131            }
2132            ast::Extern::Explicit(abi, _) => {
2133                self.word_nbsp("extern");
2134                self.print_token_literal(abi.as_token_lit(), abi.span);
2135                self.nbsp();
2136            }
2137        }
2138
2139        self.word("fn")
2140    }
2141
2142    fn print_safety(&mut self, s: ast::Safety) {
2143        match s {
2144            ast::Safety::Default => {}
2145            ast::Safety::Safe(_) => self.word_nbsp("safe"),
2146            ast::Safety::Unsafe(_) => self.word_nbsp("unsafe"),
2147        }
2148    }
2149
2150    fn print_constness(&mut self, s: ast::Const) {
2151        match s {
2152            ast::Const::No => {}
2153            ast::Const::Yes(_) => self.word_nbsp("const"),
2154        }
2155    }
2156
2157    fn print_is_auto(&mut self, s: ast::IsAuto) {
2158        match s {
2159            ast::IsAuto::Yes => self.word_nbsp("auto"),
2160            ast::IsAuto::No => {}
2161        }
2162    }
2163
2164    fn print_meta_item_lit(&mut self, lit: &ast::MetaItemLit) {
2165        self.print_token_literal(lit.as_token_lit(), lit.span)
2166    }
2167
2168    fn print_token_literal(&mut self, token_lit: token::Lit, span: Span) {
2169        self.maybe_print_comment(span.lo());
2170        self.word(token_lit.to_string())
2171    }
2172
2173    fn print_symbol(&mut self, sym: Symbol, style: ast::StrStyle) {
2174        self.print_string(sym.as_str(), style);
2175    }
2176
2177    fn print_inner_attributes_no_trailing_hardbreak(&mut self, attrs: &[ast::Attribute]) -> bool {
2178        self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, false)
2179    }
2180
2181    fn print_outer_attributes_inline(&mut self, attrs: &[ast::Attribute]) -> bool {
2182        self.print_either_attributes(attrs, ast::AttrStyle::Outer, true, true)
2183    }
2184
2185    fn print_attribute(&mut self, attr: &ast::Attribute) {
2186        self.print_attribute_inline(attr, false);
2187    }
2188
2189    fn print_meta_list_item(&mut self, item: &ast::MetaItemInner) {
2190        match item {
2191            ast::MetaItemInner::MetaItem(mi) => self.print_meta_item(mi),
2192            ast::MetaItemInner::Lit(lit) => self.print_meta_item_lit(lit),
2193        }
2194    }
2195
2196    fn print_meta_item(&mut self, item: &ast::MetaItem) {
2197        let ib = self.ibox(INDENT_UNIT);
2198
2199        match item.unsafety {
2200            ast::Safety::Unsafe(_) => {
2201                self.word("unsafe");
2202                self.popen();
2203            }
2204            ast::Safety::Default | ast::Safety::Safe(_) => {}
2205        }
2206
2207        match &item.kind {
2208            ast::MetaItemKind::Word => self.print_path(&item.path, false, 0),
2209            ast::MetaItemKind::NameValue(value) => {
2210                self.print_path(&item.path, false, 0);
2211                self.space();
2212                self.word_space("=");
2213                self.print_meta_item_lit(value);
2214            }
2215            ast::MetaItemKind::List(items) => {
2216                self.print_path(&item.path, false, 0);
2217                self.popen();
2218                self.commasep(Consistent, items, |s, i| s.print_meta_list_item(i));
2219                self.pclose();
2220            }
2221        }
2222
2223        match item.unsafety {
2224            ast::Safety::Unsafe(_) => self.pclose(),
2225            ast::Safety::Default | ast::Safety::Safe(_) => {}
2226        }
2227
2228        self.end(ib);
2229    }
2230
2231    pub(crate) fn bounds_to_string(&self, bounds: &[ast::GenericBound]) -> String {
2232        Self::to_string(|s| s.print_type_bounds(bounds))
2233    }
2234
2235    pub(crate) fn where_bound_predicate_to_string(
2236        &self,
2237        where_bound_predicate: &ast::WhereBoundPredicate,
2238    ) -> String {
2239        Self::to_string(|s| s.print_where_bound_predicate(where_bound_predicate))
2240    }
2241
2242    pub(crate) fn tt_to_string(&self, tt: &TokenTree) -> String {
2243        Self::to_string(|s| {
2244            s.print_tt(tt, false);
2245        })
2246    }
2247
2248    pub(crate) fn path_segment_to_string(&self, p: &ast::PathSegment) -> String {
2249        Self::to_string(|s| s.print_path_segment(p, false))
2250    }
2251
2252    pub(crate) fn meta_list_item_to_string(&self, li: &ast::MetaItemInner) -> String {
2253        Self::to_string(|s| s.print_meta_list_item(li))
2254    }
2255
2256    pub(crate) fn attribute_to_string(&self, attr: &ast::Attribute) -> String {
2257        Self::to_string(|s| s.print_attribute(attr))
2258    }
2259}