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