1mod 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 reversed_comments: Vec<Comment>,
62}
63
64fn 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 res.extend(lines.next().map(|it| it.to_string()));
97 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 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
238pub 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 s.maybe_print_shebang();
284
285 if is_expanded && !krate.attrs.iter().any(|attr| attr.has_name(sym::no_core)) {
286 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 if edition.is_rust_2015() {
305 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
325fn 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 match (tt1, tt2) {
358 (Tok(Token { kind: DocComment(CommentKind::Line, ..), .. }, _), _) => false,
360
361 (Tok(Token { kind: Dot, .. }, _), tt2) if !is_punct(tt2) => false,
363
364 (Tok(Token { kind: Dollar, .. }, _), Tok(Token { kind: Ident(..), .. }, _)) => false,
366
367 (tt1, Tok(Token { kind: Comma | Semi | Dot, .. }, _)) if !is_punct(tt1) => false,
371
372 (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 (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 (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
460pub 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 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 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 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 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 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 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 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 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 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 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 self.bclose(span, !open_space, cb.unwrap());
885 }
886 delim => {
887 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) = ¯o_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 ¯o_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 let cb = self.cbox(INDENT_UNIT);
963 let ib = self.ibox(0);
965 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 self.replace_last_token_still_buffered(pp::Printer::hardbreak_tok_offset(off));
1003 }
1004 }
1005 }
1006 }
1007
1008 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 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 token::Literal(lit) => literal_to_string(lit).into(),
1078
1079 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 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 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 mut_restriction_to_string(&self, r: &ast::MutRestriction) -> String {
1157 Self::to_string(|s| s.print_mut_restriction(r))
1158 }
1159
1160 fn block_to_string(&self, blk: &ast::Block) -> String {
1161 Self::to_string(|s| {
1162 let (cb, ib) = s.head("");
1163 s.print_block(blk, cb, ib)
1164 })
1165 }
1166
1167 fn attr_item_to_string(&self, ai: &ast::AttrItem) -> String {
1168 Self::to_string(|s| s.print_attr_item(ai, ai.path.span))
1169 }
1170
1171 fn tts_to_string(&self, tokens: &TokenStream) -> String {
1172 Self::to_string(|s| s.print_tts(tokens, false))
1173 }
1174
1175 fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
1176 let mut printer = State::new();
1177 f(&mut printer);
1178 printer.s.eof()
1179 }
1180}
1181
1182impl<'a> PrintState<'a> for State<'a> {
1183 fn comments(&self) -> Option<&Comments<'a>> {
1184 self.comments.as_ref()
1185 }
1186
1187 fn comments_mut(&mut self) -> Option<&mut Comments<'a>> {
1188 self.comments.as_mut()
1189 }
1190
1191 fn ann_post(&mut self, ident: Ident) {
1192 self.ann.post(self, AnnNode::Ident(&ident));
1193 }
1194
1195 fn print_generic_args(&mut self, args: &ast::GenericArgs, colons_before_params: bool) {
1196 if colons_before_params {
1197 self.word("::")
1198 }
1199
1200 match args {
1201 ast::GenericArgs::AngleBracketed(data) => {
1202 self.word("<");
1203 self.commasep(Inconsistent, &data.args, |s, arg| match arg {
1204 ast::AngleBracketedArg::Arg(a) => s.print_generic_arg(a),
1205 ast::AngleBracketedArg::Constraint(c) => s.print_assoc_item_constraint(c),
1206 });
1207 self.word(">")
1208 }
1209
1210 ast::GenericArgs::Parenthesized(data) => {
1211 self.word("(");
1212 self.commasep(Inconsistent, &data.inputs, |s, ty| s.print_type(ty));
1213 self.word(")");
1214 self.print_fn_ret_ty(&data.output);
1215 }
1216 ast::GenericArgs::ParenthesizedElided(_) => {
1217 self.word("(");
1218 self.word("..");
1219 self.word(")");
1220 }
1221 }
1222 }
1223}
1224
1225impl<'a> State<'a> {
1226 pub fn new() -> State<'a> {
1227 State { s: pp::Printer::new(), comments: None, ann: &NoAnn, is_sdylib_interface: false }
1228 }
1229
1230 fn commasep_cmnt<T, F, G>(&mut self, b: Breaks, elts: &[T], mut op: F, mut get_span: G)
1231 where
1232 F: FnMut(&mut State<'_>, &T),
1233 G: FnMut(&T) -> rustc_span::Span,
1234 {
1235 let rb = self.rbox(0, b);
1236 let len = elts.len();
1237 let mut i = 0;
1238 for elt in elts {
1239 self.maybe_print_comment(get_span(elt).hi());
1240 op(self, elt);
1241 i += 1;
1242 if i < len {
1243 self.word(",");
1244 self.maybe_print_trailing_comment(get_span(elt), Some(get_span(&elts[i]).hi()));
1245 self.space_if_not_bol();
1246 }
1247 }
1248 self.end(rb);
1249 }
1250
1251 fn commasep_exprs(&mut self, b: Breaks, exprs: &[Box<ast::Expr>]) {
1252 self.commasep_cmnt(b, exprs, |s, e| s.print_expr(e, FixupContext::default()), |e| e.span)
1253 }
1254
1255 pub fn print_opt_lifetime(&mut self, lifetime: &Option<ast::Lifetime>) {
1256 if let Some(lt) = *lifetime {
1257 self.print_lifetime(lt);
1258 self.nbsp();
1259 }
1260 }
1261
1262 pub fn print_assoc_item_constraint(&mut self, constraint: &ast::AssocItemConstraint) {
1263 self.print_ident(constraint.ident);
1264 if let Some(args) = constraint.gen_args.as_ref() {
1265 self.print_generic_args(args, false)
1266 }
1267 self.space();
1268 match &constraint.kind {
1269 ast::AssocItemConstraintKind::Equality { term } => {
1270 self.word_space("=");
1271 match term {
1272 Term::Ty(ty) => self.print_type(ty),
1273 Term::Const(c) => self.print_expr_anon_const(c, &[]),
1274 }
1275 }
1276 ast::AssocItemConstraintKind::Bound { bounds } => {
1277 if !bounds.is_empty() {
1278 self.word_nbsp(":");
1279 self.print_type_bounds(bounds);
1280 }
1281 }
1282 }
1283 }
1284
1285 pub fn print_generic_arg(&mut self, generic_arg: &GenericArg) {
1286 match generic_arg {
1287 GenericArg::Lifetime(lt) => self.print_lifetime(*lt),
1288 GenericArg::Type(ty) => self.print_type(ty),
1289 GenericArg::Const(ct) => self.print_expr(&ct.value, FixupContext::default()),
1290 }
1291 }
1292
1293 pub fn print_ty_pat(&mut self, pat: &ast::TyPat) {
1294 match &pat.kind {
1295 rustc_ast::TyPatKind::Range(start, end, include_end) => {
1296 if let Some(start) = start {
1297 self.print_expr_anon_const(start, &[]);
1298 }
1299 self.word("..");
1300 if let Some(end) = end {
1301 if let RangeEnd::Included(_) = include_end.node {
1302 self.word("=");
1303 }
1304 self.print_expr_anon_const(end, &[]);
1305 }
1306 }
1307 rustc_ast::TyPatKind::NotNull => self.word("!null"),
1308 rustc_ast::TyPatKind::Or(variants) => {
1309 let mut first = true;
1310 for pat in variants {
1311 if first {
1312 first = false
1313 } else {
1314 self.word(" | ");
1315 }
1316 self.print_ty_pat(pat);
1317 }
1318 }
1319 rustc_ast::TyPatKind::Err(_) => {
1320 self.popen();
1321 self.word("/*ERROR*/");
1322 self.pclose();
1323 }
1324 }
1325 }
1326
1327 pub fn print_type(&mut self, ty: &ast::Ty) {
1328 self.maybe_print_comment(ty.span.lo());
1329 let ib = self.ibox(0);
1330 match &ty.kind {
1331 ast::TyKind::Slice(ty) => {
1332 self.word("[");
1333 self.print_type(ty);
1334 self.word("]");
1335 }
1336 ast::TyKind::Ptr(mt) => {
1337 self.word("*");
1338 self.print_mt(mt, true);
1339 }
1340 ast::TyKind::Ref(lifetime, mt) => {
1341 self.word("&");
1342 self.print_opt_lifetime(lifetime);
1343 self.print_mt(mt, false);
1344 }
1345 ast::TyKind::PinnedRef(lifetime, mt) => {
1346 self.word("&");
1347 self.print_opt_lifetime(lifetime);
1348 self.word("pin ");
1349 self.print_mt(mt, true);
1350 }
1351 ast::TyKind::Never => {
1352 self.word("!");
1353 }
1354 ast::TyKind::Tup(elts) => {
1355 self.popen();
1356 self.commasep(Inconsistent, elts, |s, ty| s.print_type(ty));
1357 if elts.len() == 1 {
1358 self.word(",");
1359 }
1360 self.pclose();
1361 }
1362 ast::TyKind::Paren(typ) => {
1363 self.popen();
1364 self.print_type(typ);
1365 self.pclose();
1366 }
1367 ast::TyKind::FnPtr(f) => {
1368 self.print_ty_fn(f.ext, f.safety, &f.decl, None, &f.generic_params);
1369 }
1370 ast::TyKind::UnsafeBinder(f) => {
1371 let ib = self.ibox(INDENT_UNIT);
1372 self.word("unsafe");
1373 self.print_generic_params(&f.generic_params);
1374 self.nbsp();
1375 self.print_type(&f.inner_ty);
1376 self.end(ib);
1377 }
1378 ast::TyKind::Path(None, path) => {
1379 self.print_path(path, false, 0);
1380 }
1381 ast::TyKind::Path(Some(qself), path) => self.print_qpath(path, qself, false),
1382 ast::TyKind::TraitObject(bounds, syntax) => {
1383 match syntax {
1384 ast::TraitObjectSyntax::Dyn => self.word_nbsp("dyn"),
1385 ast::TraitObjectSyntax::None => {}
1386 }
1387 self.print_type_bounds(bounds);
1388 }
1389 ast::TyKind::ImplTrait(_, bounds) => {
1390 self.word_nbsp("impl");
1391 self.print_type_bounds(bounds);
1392 }
1393 ast::TyKind::Array(ty, length) => {
1394 self.word("[");
1395 self.print_type(ty);
1396 self.word("; ");
1397 self.print_expr(&length.value, FixupContext::default());
1398 self.word("]");
1399 }
1400 ast::TyKind::Infer => {
1401 self.word("_");
1402 }
1403 ast::TyKind::Err(_) => {
1404 self.popen();
1405 self.word("/*ERROR*/");
1406 self.pclose();
1407 }
1408 ast::TyKind::Dummy => {
1409 self.popen();
1410 self.word("/*DUMMY*/");
1411 self.pclose();
1412 }
1413 ast::TyKind::ImplicitSelf => {
1414 self.word("Self");
1415 }
1416 ast::TyKind::MacCall(m) => {
1417 self.print_mac(m);
1418 }
1419 ast::TyKind::CVarArgs => {
1420 self.word("...");
1421 }
1422 ast::TyKind::Pat(ty, pat) => {
1423 self.print_type(ty);
1424 self.word(" is ");
1425 self.print_ty_pat(pat);
1426 }
1427 ast::TyKind::FieldOf(ty, variant, field) => {
1428 self.word("builtin # field_of");
1429 self.popen();
1430 let ib = self.ibox(0);
1431 self.print_type(ty);
1432 self.word(",");
1433 self.space();
1434
1435 if let Some(variant) = variant {
1436 self.print_ident(*variant);
1437 self.word(".");
1438 }
1439 self.print_ident(*field);
1440
1441 self.end(ib);
1442 self.pclose();
1443 }
1444 }
1445 self.end(ib);
1446 }
1447
1448 fn print_trait_ref(&mut self, t: &ast::TraitRef) {
1449 self.print_path(&t.path, false, 0)
1450 }
1451
1452 fn print_formal_generic_params(&mut self, generic_params: &[ast::GenericParam]) {
1453 if !generic_params.is_empty() {
1454 self.word("for");
1455 self.print_generic_params(generic_params);
1456 self.nbsp();
1457 }
1458 }
1459
1460 fn print_poly_trait_ref(&mut self, t: &ast::PolyTraitRef) {
1461 if let ast::Parens::Yes = t.parens {
1462 self.popen();
1463 }
1464 self.print_formal_generic_params(&t.bound_generic_params);
1465
1466 let ast::TraitBoundModifiers { constness, asyncness, polarity } = t.modifiers;
1467 match constness {
1468 ast::BoundConstness::Never => {}
1469 ast::BoundConstness::Always(_) | ast::BoundConstness::Maybe(_) => {
1470 self.word_space(constness.as_str());
1471 }
1472 }
1473 match asyncness {
1474 ast::BoundAsyncness::Normal => {}
1475 ast::BoundAsyncness::Async(_) => {
1476 self.word_space(asyncness.as_str());
1477 }
1478 }
1479 match polarity {
1480 ast::BoundPolarity::Positive => {}
1481 ast::BoundPolarity::Negative(_) | ast::BoundPolarity::Maybe(_) => {
1482 self.word(polarity.as_str());
1483 }
1484 }
1485
1486 self.print_trait_ref(&t.trait_ref);
1487 if let ast::Parens::Yes = t.parens {
1488 self.pclose();
1489 }
1490 }
1491
1492 fn print_stmt(&mut self, st: &ast::Stmt) {
1493 self.maybe_print_comment(st.span.lo());
1494 match &st.kind {
1495 ast::StmtKind::Let(loc) => {
1496 self.print_outer_attributes(&loc.attrs);
1497 self.space_if_not_bol();
1498 let ib1 = self.ibox(INDENT_UNIT);
1499 if loc.super_.is_some() {
1500 self.word_nbsp("super");
1501 }
1502 self.word_nbsp("let");
1503
1504 let ib2 = self.ibox(INDENT_UNIT);
1505 self.print_local_decl(loc);
1506 self.end(ib2);
1507 if let Some((init, els)) = loc.kind.init_else_opt() {
1508 self.nbsp();
1509 self.word_space("=");
1510 self.print_expr_cond_paren(
1511 init,
1512 els.is_some() && classify::expr_trailing_brace(init).is_some(),
1513 FixupContext::default(),
1514 );
1515 if let Some(els) = els {
1516 let cb = self.cbox(INDENT_UNIT);
1517 let ib = self.ibox(INDENT_UNIT);
1518 self.word(" else ");
1519 self.print_block(els, cb, ib);
1520 }
1521 }
1522 self.word(";");
1523 self.end(ib1);
1524 }
1525 ast::StmtKind::Item(item) => self.print_item(item),
1526 ast::StmtKind::Expr(expr) => {
1527 self.space_if_not_bol();
1528 self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1529 if classify::expr_requires_semi_to_be_stmt(expr) {
1530 self.word(";");
1531 }
1532 }
1533 ast::StmtKind::Semi(expr) => {
1534 self.space_if_not_bol();
1535 self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1536 self.word(";");
1537 }
1538 ast::StmtKind::Empty => {
1539 self.space_if_not_bol();
1540 self.word(";");
1541 }
1542 ast::StmtKind::MacCall(mac) => {
1543 self.space_if_not_bol();
1544 self.print_outer_attributes(&mac.attrs);
1545 self.print_mac(&mac.mac);
1546 if mac.style == ast::MacStmtStyle::Semicolon {
1547 self.word(";");
1548 }
1549 }
1550 }
1551 self.maybe_print_trailing_comment(st.span, None)
1552 }
1553
1554 fn print_block(&mut self, blk: &ast::Block, cb: BoxMarker, ib: BoxMarker) {
1555 self.print_block_with_attrs(blk, &[], cb, ib)
1556 }
1557
1558 fn print_block_unclosed_indent(&mut self, blk: &ast::Block, ib: BoxMarker) {
1559 self.print_block_maybe_unclosed(blk, &[], None, ib)
1560 }
1561
1562 fn print_block_with_attrs(
1563 &mut self,
1564 blk: &ast::Block,
1565 attrs: &[ast::Attribute],
1566 cb: BoxMarker,
1567 ib: BoxMarker,
1568 ) {
1569 self.print_block_maybe_unclosed(blk, attrs, Some(cb), ib)
1570 }
1571
1572 fn print_block_maybe_unclosed(
1573 &mut self,
1574 blk: &ast::Block,
1575 attrs: &[ast::Attribute],
1576 cb: Option<BoxMarker>,
1577 ib: BoxMarker,
1578 ) {
1579 match blk.rules {
1580 BlockCheckMode::Unsafe(..) => self.word_space("unsafe"),
1581 BlockCheckMode::Default => (),
1582 }
1583 self.maybe_print_comment(blk.span.lo());
1584 self.ann.pre(self, AnnNode::Block(blk));
1585 self.bopen(ib);
1586
1587 let has_attrs = self.print_inner_attributes(attrs);
1588
1589 for (i, st) in blk.stmts.iter().enumerate() {
1590 match &st.kind {
1591 ast::StmtKind::Expr(expr) if i == blk.stmts.len() - 1 => {
1592 self.maybe_print_comment(st.span.lo());
1593 self.space_if_not_bol();
1594 self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1595 self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi()));
1596 }
1597 _ => self.print_stmt(st),
1598 }
1599 }
1600
1601 let no_space = !has_attrs && blk.stmts.is_empty();
1602 self.bclose_maybe_open(blk.span, no_space, cb);
1603 self.ann.post(self, AnnNode::Block(blk))
1604 }
1605
1606 fn print_let(&mut self, pat: &ast::Pat, expr: &ast::Expr, fixup: FixupContext) {
1632 self.word("let ");
1633 self.print_pat(pat);
1634 self.space();
1635 self.word_space("=");
1636 self.print_expr_cond_paren(
1637 expr,
1638 fixup.needs_par_as_let_scrutinee(expr),
1639 FixupContext::default(),
1640 );
1641 }
1642
1643 fn print_mac(&mut self, m: &ast::MacCall) {
1644 self.print_mac_common(
1645 Some(MacHeader::Path(&m.path)),
1646 true,
1647 None,
1648 m.args.delim,
1649 None,
1650 &m.args.tokens,
1651 true,
1652 m.span(),
1653 );
1654 }
1655
1656 fn inline_asm_template_and_operands<'asm>(
1657 asm: &'asm ast::InlineAsm,
1658 ) -> (String, Vec<&'asm InlineAsmOperand>) {
1659 fn is_explicit_reg(op: &InlineAsmOperand) -> bool {
1660 match op {
1661 InlineAsmOperand::In { reg, .. }
1662 | InlineAsmOperand::Out { reg, .. }
1663 | InlineAsmOperand::InOut { reg, .. }
1664 | InlineAsmOperand::SplitInOut { reg, .. } => {
1665 #[allow(non_exhaustive_omitted_patterns)] match reg {
InlineAsmRegOrRegClass::Reg(_) => true,
_ => false,
}matches!(reg, InlineAsmRegOrRegClass::Reg(_))
1666 }
1667 InlineAsmOperand::Const { .. }
1668 | InlineAsmOperand::Sym { .. }
1669 | InlineAsmOperand::Label { .. } => false,
1670 }
1671 }
1672
1673 let needs_reorder = {
1680 let mut seen_explicit = false;
1681 asm.operands.iter().any(|(op, _)| {
1682 if is_explicit_reg(op) {
1683 seen_explicit = true;
1684 false
1685 } else {
1686 seen_explicit
1687 }
1688 })
1689 };
1690
1691 if !needs_reorder {
1692 let template = InlineAsmTemplatePiece::to_string(&asm.template);
1693 let operands = asm.operands.iter().map(|(op, _)| op).collect();
1694 return (template, operands);
1695 }
1696
1697 let mut non_explicit = Vec::new();
1698 let mut explicit = Vec::new();
1699 for (i, (op, _)) in asm.operands.iter().enumerate() {
1700 if is_explicit_reg(op) {
1701 explicit.push(i);
1702 } else {
1703 non_explicit.push(i);
1704 }
1705 }
1706 let order = non_explicit.into_iter().chain(explicit).collect::<Vec<_>>();
1707
1708 let mut old_to_new = ::alloc::vec::from_elem(0usize, asm.operands.len())vec![0usize; asm.operands.len()];
1710 for (new_idx, old_idx) in order.iter().copied().enumerate() {
1711 old_to_new[old_idx] = new_idx;
1712 }
1713
1714 let remapped = asm
1717 .template
1718 .iter()
1719 .map(|piece| match piece {
1720 InlineAsmTemplatePiece::Placeholder { operand_idx, modifier, span } => {
1721 InlineAsmTemplatePiece::Placeholder {
1722 operand_idx: old_to_new[*operand_idx],
1723 modifier: *modifier,
1724 span: *span,
1725 }
1726 }
1727 other => other.clone(),
1728 })
1729 .collect::<Vec<_>>();
1730 let template = InlineAsmTemplatePiece::to_string(&remapped);
1731 let operands = order.iter().map(|&idx| &asm.operands[idx].0).collect();
1732 (template, operands)
1733 }
1734
1735 fn print_inline_asm(&mut self, asm: &ast::InlineAsm) {
1736 enum AsmArg<'a> {
1737 Template(String),
1738 Operand(&'a InlineAsmOperand),
1739 ClobberAbi(Symbol),
1740 Options(InlineAsmOptions),
1741 }
1742
1743 let (template, operands) = Self::inline_asm_template_and_operands(asm);
1744 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)];
1745 args.extend(operands.into_iter().map(AsmArg::Operand));
1746 for (abi, _) in &asm.clobber_abis {
1747 args.push(AsmArg::ClobberAbi(*abi));
1748 }
1749 if !asm.options.is_empty() {
1750 args.push(AsmArg::Options(asm.options));
1751 }
1752
1753 self.popen();
1754 self.commasep(Consistent, &args, |s, arg| match arg {
1755 AsmArg::Template(template) => s.print_string(template, ast::StrStyle::Cooked),
1756 AsmArg::Operand(op) => {
1757 let print_reg_or_class = |s: &mut Self, r: &InlineAsmRegOrRegClass| match r {
1758 InlineAsmRegOrRegClass::Reg(r) => s.print_symbol(*r, ast::StrStyle::Cooked),
1759 InlineAsmRegOrRegClass::RegClass(r) => s.word(r.to_string()),
1760 };
1761 match op {
1762 InlineAsmOperand::In { reg, expr } => {
1763 s.word("in");
1764 s.popen();
1765 print_reg_or_class(s, reg);
1766 s.pclose();
1767 s.space();
1768 s.print_expr(expr, FixupContext::default());
1769 }
1770 InlineAsmOperand::Out { reg, late, expr } => {
1771 s.word(if *late { "lateout" } else { "out" });
1772 s.popen();
1773 print_reg_or_class(s, reg);
1774 s.pclose();
1775 s.space();
1776 match expr {
1777 Some(expr) => s.print_expr(expr, FixupContext::default()),
1778 None => s.word("_"),
1779 }
1780 }
1781 InlineAsmOperand::InOut { reg, late, expr } => {
1782 s.word(if *late { "inlateout" } else { "inout" });
1783 s.popen();
1784 print_reg_or_class(s, reg);
1785 s.pclose();
1786 s.space();
1787 s.print_expr(expr, FixupContext::default());
1788 }
1789 InlineAsmOperand::SplitInOut { reg, late, in_expr, out_expr } => {
1790 s.word(if *late { "inlateout" } else { "inout" });
1791 s.popen();
1792 print_reg_or_class(s, reg);
1793 s.pclose();
1794 s.space();
1795 s.print_expr(in_expr, FixupContext::default());
1796 s.space();
1797 s.word_space("=>");
1798 match out_expr {
1799 Some(out_expr) => s.print_expr(out_expr, FixupContext::default()),
1800 None => s.word("_"),
1801 }
1802 }
1803 InlineAsmOperand::Const { anon_const } => {
1804 s.word("const");
1805 s.space();
1806 s.print_expr(&anon_const.value, FixupContext::default());
1807 }
1808 InlineAsmOperand::Sym { sym } => {
1809 s.word("sym");
1810 s.space();
1811 if let Some(qself) = &sym.qself {
1812 s.print_qpath(&sym.path, qself, true);
1813 } else {
1814 s.print_path(&sym.path, true, 0);
1815 }
1816 }
1817 InlineAsmOperand::Label { block } => {
1818 let (cb, ib) = s.head("label");
1819 s.print_block(block, cb, ib);
1820 }
1821 }
1822 }
1823 AsmArg::ClobberAbi(abi) => {
1824 s.word("clobber_abi");
1825 s.popen();
1826 s.print_symbol(*abi, ast::StrStyle::Cooked);
1827 s.pclose();
1828 }
1829 AsmArg::Options(opts) => {
1830 s.word("options");
1831 s.popen();
1832 s.commasep(Inconsistent, &opts.human_readable_names(), |s, &opt| {
1833 s.word(opt);
1834 });
1835 s.pclose();
1836 }
1837 });
1838 self.pclose();
1839 }
1840
1841 fn print_local_decl(&mut self, loc: &ast::Local) {
1842 self.print_pat(&loc.pat);
1843 if let Some(ty) = &loc.ty {
1844 self.word_space(":");
1845 self.print_type(ty);
1846 }
1847 }
1848
1849 fn print_name(&mut self, name: Symbol) {
1850 self.word(name.to_string());
1851 self.ann.post(self, AnnNode::Name(&name))
1852 }
1853
1854 fn print_qpath(&mut self, path: &ast::Path, qself: &ast::QSelf, colons_before_params: bool) {
1855 self.word("<");
1856 self.print_type(&qself.ty);
1857 if qself.position > 0 {
1858 self.space();
1859 self.word_space("as");
1860 let depth = path.segments.len() - qself.position;
1861 self.print_path(path, false, depth);
1862 }
1863 self.word(">");
1864 for item_segment in &path.segments[qself.position..] {
1865 self.word("::");
1866 self.print_ident(item_segment.ident);
1867 if let Some(args) = &item_segment.args {
1868 self.print_generic_args(args, colons_before_params)
1869 }
1870 }
1871 }
1872
1873 fn print_pat_paren_if_or(&mut self, pat: &ast::Pat) {
1880 let needs_paren = #[allow(non_exhaustive_omitted_patterns)] match pat.kind {
PatKind::Or(..) => true,
_ => false,
}matches!(pat.kind, PatKind::Or(..));
1881 if needs_paren {
1882 self.popen();
1883 }
1884 self.print_pat(pat);
1885 if needs_paren {
1886 self.pclose();
1887 }
1888 }
1889
1890 fn print_pat(&mut self, pat: &ast::Pat) {
1891 self.maybe_print_comment(pat.span.lo());
1892 self.ann.pre(self, AnnNode::Pat(pat));
1893 match &pat.kind {
1895 PatKind::Missing => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
1896 PatKind::Wild => self.word("_"),
1897 PatKind::Never => self.word("!"),
1898 PatKind::Ident(BindingMode(by_ref, mutbl), ident, sub) => {
1899 if mutbl.is_mut() {
1900 self.word_nbsp("mut");
1901 }
1902 if let ByRef::Yes(pinnedness, rmutbl) = by_ref {
1903 self.word_nbsp("ref");
1904 if pinnedness.is_pinned() {
1905 self.word_nbsp("pin");
1906 }
1907 if rmutbl.is_mut() {
1908 self.word_nbsp("mut");
1909 } else if pinnedness.is_pinned() {
1910 self.word_nbsp("const");
1911 }
1912 }
1913 self.print_ident(*ident);
1914 if let Some(p) = sub {
1915 self.space();
1916 self.word_space("@");
1917 self.print_pat_paren_if_or(p);
1918 }
1919 }
1920 PatKind::TupleStruct(qself, path, elts) => {
1921 if let Some(qself) = qself {
1922 self.print_qpath(path, qself, true);
1923 } else {
1924 self.print_path(path, true, 0);
1925 }
1926 self.popen();
1927 self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));
1928 self.pclose();
1929 }
1930 PatKind::Or(pats) => {
1931 self.strsep("|", true, Inconsistent, pats, |s, p| s.print_pat(p));
1932 }
1933 PatKind::Path(None, path) => {
1934 self.print_path(path, true, 0);
1935 }
1936 PatKind::Path(Some(qself), path) => {
1937 self.print_qpath(path, qself, false);
1938 }
1939 PatKind::Struct(qself, path, fields, etc) => {
1940 if let Some(qself) = qself {
1941 self.print_qpath(path, qself, true);
1942 } else {
1943 self.print_path(path, true, 0);
1944 }
1945 self.nbsp();
1946 self.word("{");
1947 let empty = fields.is_empty() && *etc == ast::PatFieldsRest::None;
1948 if !empty {
1949 self.space();
1950 }
1951 self.commasep_cmnt(
1952 Consistent,
1953 fields,
1954 |s, f| {
1955 let cb = s.cbox(INDENT_UNIT);
1956 if !f.is_shorthand {
1957 s.print_ident(f.ident);
1958 s.word_nbsp(":");
1959 }
1960 s.print_pat(&f.pat);
1961 s.end(cb);
1962 },
1963 |f| f.pat.span,
1964 );
1965 if let ast::PatFieldsRest::Rest(_) | ast::PatFieldsRest::Recovered(_) = etc {
1966 if !fields.is_empty() {
1967 self.word_space(",");
1968 }
1969 self.word("..");
1970 if let ast::PatFieldsRest::Recovered(_) = etc {
1971 self.word("/* recovered parse error */");
1972 }
1973 }
1974 if !empty {
1975 self.space();
1976 }
1977 self.word("}");
1978 }
1979 PatKind::Tuple(elts) => {
1980 self.popen();
1981 self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));
1982 if elts.len() == 1 {
1983 self.word(",");
1984 }
1985 self.pclose();
1986 }
1987 PatKind::Box(inner) => {
1988 self.word("box ");
1989 self.print_pat_paren_if_or(inner);
1990 }
1991 PatKind::Deref(inner) => {
1992 self.word("deref!");
1993 self.popen();
1994 self.print_pat(inner);
1995 self.pclose();
1996 }
1997 PatKind::Ref(inner, pinned, mutbl) => {
1998 self.word("&");
1999 if pinned.is_pinned() {
2000 self.word("pin ");
2001 if mutbl.is_not() {
2002 self.word("const ");
2003 }
2004 }
2005 if mutbl.is_mut() {
2006 self.word("mut ");
2007 }
2008 if let PatKind::Ident(ast::BindingMode::MUT, ..) = inner.kind {
2009 self.popen();
2010 self.print_pat(inner);
2011 self.pclose();
2012 } else {
2013 self.print_pat_paren_if_or(inner);
2014 }
2015 }
2016 PatKind::Expr(e) => self.print_expr(e, FixupContext::default()),
2017 PatKind::Range(begin, end, Spanned { node: end_kind, .. }) => {
2018 if let Some(e) = begin {
2019 self.print_expr(e, FixupContext::default());
2020 }
2021 match end_kind {
2022 RangeEnd::Included(RangeSyntax::DotDotDot) => self.word("..."),
2023 RangeEnd::Included(RangeSyntax::DotDotEq) => self.word("..="),
2024 RangeEnd::Excluded => self.word(".."),
2025 }
2026 if let Some(e) = end {
2027 self.print_expr(e, FixupContext::default());
2028 }
2029 }
2030 PatKind::Guard(subpat, guard) => {
2031 self.popen();
2032 self.print_pat(subpat);
2033 self.space();
2034 self.word_space("if");
2035 self.print_expr(&guard.cond, FixupContext::default());
2036 self.pclose();
2037 }
2038 PatKind::Slice(elts) => {
2039 self.word("[");
2040 self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));
2041 self.word("]");
2042 }
2043 PatKind::Rest => self.word(".."),
2044 PatKind::Paren(inner) => {
2045 self.popen();
2046 self.print_pat(inner);
2047 self.pclose();
2048 }
2049 PatKind::MacCall(m) => self.print_mac(m),
2050 PatKind::Err(_) => {
2051 self.popen();
2052 self.word("/*ERROR*/");
2053 self.pclose();
2054 }
2055 }
2056 self.ann.post(self, AnnNode::Pat(pat))
2057 }
2058
2059 fn print_explicit_self(&mut self, explicit_self: &ast::ExplicitSelf) {
2060 match &explicit_self.node {
2061 SelfKind::Value(m) => {
2062 self.print_mutability(*m, false);
2063 self.word("self")
2064 }
2065 SelfKind::Region(lt, m) => {
2066 self.word("&");
2067 self.print_opt_lifetime(lt);
2068 self.print_mutability(*m, false);
2069 self.word("self")
2070 }
2071 SelfKind::Pinned(lt, m) => {
2072 self.word("&");
2073 self.print_opt_lifetime(lt);
2074 self.word("pin ");
2075 self.print_mutability(*m, true);
2076 self.word("self")
2077 }
2078 SelfKind::Explicit(typ, m) => {
2079 self.print_mutability(*m, false);
2080 self.word("self");
2081 self.word_space(":");
2082 self.print_type(typ)
2083 }
2084 }
2085 }
2086
2087 fn print_coroutine_kind(&mut self, coroutine_kind: ast::CoroutineKind) {
2088 match coroutine_kind {
2089 ast::CoroutineKind::Gen { .. } => {
2090 self.word_nbsp("gen");
2091 }
2092 ast::CoroutineKind::Async { .. } => {
2093 self.word_nbsp("async");
2094 }
2095 ast::CoroutineKind::AsyncGen { .. } => {
2096 self.word_nbsp("async");
2097 self.word_nbsp("gen");
2098 }
2099 }
2100 }
2101
2102 pub fn print_type_bounds(&mut self, bounds: &[ast::GenericBound]) {
2103 let mut first = true;
2104 for bound in bounds {
2105 if first {
2106 first = false;
2107 } else {
2108 self.nbsp();
2109 self.word_space("+");
2110 }
2111
2112 match bound {
2113 GenericBound::Trait(tref) => {
2114 self.print_poly_trait_ref(tref);
2115 }
2116 GenericBound::Outlives(lt) => self.print_lifetime(*lt),
2117 GenericBound::Use(args, _) => {
2118 self.word("use");
2119 self.word("<");
2120 self.commasep(Inconsistent, args, |s, arg| match arg {
2121 ast::PreciseCapturingArg::Arg(p, _) => s.print_path(p, false, 0),
2122 ast::PreciseCapturingArg::Lifetime(lt) => s.print_lifetime(*lt),
2123 });
2124 self.word(">")
2125 }
2126 }
2127 }
2128 }
2129
2130 fn print_lifetime(&mut self, lifetime: ast::Lifetime) {
2131 self.word(lifetime.ident.name.to_string());
2132 self.ann_post(lifetime.ident)
2133 }
2134
2135 fn print_lifetime_bounds(&mut self, bounds: &ast::GenericBounds) {
2136 for (i, bound) in bounds.iter().enumerate() {
2137 if i != 0 {
2138 self.word(" + ");
2139 }
2140 match bound {
2141 ast::GenericBound::Outlives(lt) => self.print_lifetime(*lt),
2142 _ => {
2143 {
::core::panicking::panic_fmt(format_args!("expected a lifetime bound, found a trait bound"));
}panic!("expected a lifetime bound, found a trait bound")
2144 }
2145 }
2146 }
2147 }
2148
2149 fn print_generic_params(&mut self, generic_params: &[ast::GenericParam]) {
2150 if generic_params.is_empty() {
2151 return;
2152 }
2153
2154 self.word("<");
2155
2156 self.commasep(Inconsistent, generic_params, |s, param| {
2157 s.print_outer_attributes_inline(¶m.attrs);
2158
2159 match ¶m.kind {
2160 ast::GenericParamKind::Lifetime => {
2161 let lt = ast::Lifetime { id: param.id, ident: param.ident };
2162 s.print_lifetime(lt);
2163 if !param.bounds.is_empty() {
2164 s.word_nbsp(":");
2165 s.print_lifetime_bounds(¶m.bounds)
2166 }
2167 }
2168 ast::GenericParamKind::Type { default } => {
2169 s.print_ident(param.ident);
2170 if !param.bounds.is_empty() {
2171 s.word_nbsp(":");
2172 s.print_type_bounds(¶m.bounds);
2173 }
2174 if let Some(default) = default {
2175 s.space();
2176 s.word_space("=");
2177 s.print_type(default)
2178 }
2179 }
2180 ast::GenericParamKind::Const { ty, default, .. } => {
2181 s.word_space("const");
2182 s.print_ident(param.ident);
2183 s.space();
2184 s.word_space(":");
2185 s.print_type(ty);
2186 if !param.bounds.is_empty() {
2187 s.word_nbsp(":");
2188 s.print_type_bounds(¶m.bounds);
2189 }
2190 if let Some(default) = default {
2191 s.space();
2192 s.word_space("=");
2193 s.print_expr(&default.value, FixupContext::default());
2194 }
2195 }
2196 }
2197 });
2198
2199 self.word(">");
2200 }
2201
2202 pub fn print_mutability(&mut self, mutbl: ast::Mutability, print_const: bool) {
2203 match mutbl {
2204 ast::Mutability::Mut => self.word_nbsp("mut"),
2205 ast::Mutability::Not => {
2206 if print_const {
2207 self.word_nbsp("const");
2208 }
2209 }
2210 }
2211 }
2212
2213 fn print_mt(&mut self, mt: &ast::MutTy, print_const: bool) {
2214 self.print_mutability(mt.mutbl, print_const);
2215 self.print_type(&mt.ty)
2216 }
2217
2218 fn print_param(&mut self, input: &ast::Param, is_closure: bool) {
2219 let ib = self.ibox(INDENT_UNIT);
2220
2221 self.print_outer_attributes_inline(&input.attrs);
2222
2223 match input.ty.kind {
2224 ast::TyKind::Infer if is_closure => self.print_pat(&input.pat),
2225 _ => {
2226 if let Some(eself) = input.to_self() {
2227 self.print_explicit_self(&eself);
2228 } else {
2229 if !#[allow(non_exhaustive_omitted_patterns)] match input.pat.kind {
PatKind::Missing => true,
_ => false,
}matches!(input.pat.kind, PatKind::Missing) {
2230 self.print_pat(&input.pat);
2231 self.word(":");
2232 self.space();
2233 }
2234 self.print_type(&input.ty);
2235 }
2236 }
2237 }
2238 self.end(ib);
2239 }
2240
2241 fn print_fn_ret_ty(&mut self, fn_ret_ty: &ast::FnRetTy) {
2242 if let ast::FnRetTy::Ty(ty) = fn_ret_ty {
2243 self.space_if_not_bol();
2244 let ib = self.ibox(INDENT_UNIT);
2245 self.word_space("->");
2246 self.print_type(ty);
2247 self.end(ib);
2248 self.maybe_print_comment(ty.span.lo());
2249 }
2250 }
2251
2252 fn print_ty_fn(
2253 &mut self,
2254 ext: ast::Extern,
2255 safety: ast::Safety,
2256 decl: &ast::FnDecl,
2257 name: Option<Ident>,
2258 generic_params: &[ast::GenericParam],
2259 ) {
2260 let ib = self.ibox(INDENT_UNIT);
2261 self.print_formal_generic_params(generic_params);
2262 let generics = ast::Generics::default();
2263 let header = ast::FnHeader { safety, ext, ..ast::FnHeader::default() };
2264 self.print_fn(decl, header, name, &generics);
2265 self.end(ib);
2266 }
2267
2268 fn print_fn_header_info(&mut self, header: ast::FnHeader) {
2269 self.print_constness(header.constness);
2270 header.coroutine_kind.map(|coroutine_kind| self.print_coroutine_kind(coroutine_kind));
2271 self.print_safety(header.safety);
2272
2273 match header.ext {
2274 ast::Extern::None => {}
2275 ast::Extern::Implicit(_) => {
2276 self.word_nbsp("extern");
2277 }
2278 ast::Extern::Explicit(abi, _) => {
2279 self.word_nbsp("extern");
2280 self.print_token_literal(abi.as_token_lit(), abi.span);
2281 self.nbsp();
2282 }
2283 }
2284
2285 self.word("fn")
2286 }
2287
2288 fn print_safety(&mut self, s: ast::Safety) {
2289 match s {
2290 ast::Safety::Default => {}
2291 ast::Safety::Safe(_) => self.word_nbsp("safe"),
2292 ast::Safety::Unsafe(_) => self.word_nbsp("unsafe"),
2293 }
2294 }
2295
2296 fn print_constness(&mut self, s: ast::Const) {
2297 match s {
2298 ast::Const::No => {}
2299 ast::Const::Yes(_) => self.word_nbsp("const"),
2300 }
2301 }
2302
2303 fn print_is_auto(&mut self, s: ast::IsAuto) {
2304 match s {
2305 ast::IsAuto::Yes => self.word_nbsp("auto"),
2306 ast::IsAuto::No => {}
2307 }
2308 }
2309
2310 fn print_meta_item_lit(&mut self, lit: &ast::MetaItemLit) {
2311 self.print_token_literal(lit.as_token_lit(), lit.span)
2312 }
2313
2314 fn print_token_literal(&mut self, token_lit: token::Lit, span: Span) {
2315 self.maybe_print_comment(span.lo());
2316 self.word(token_lit.to_string())
2317 }
2318
2319 fn print_symbol(&mut self, sym: Symbol, style: ast::StrStyle) {
2320 self.print_string(sym.as_str(), style);
2321 }
2322
2323 fn print_inner_attributes_no_trailing_hardbreak(&mut self, attrs: &[ast::Attribute]) -> bool {
2324 self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, false)
2325 }
2326
2327 fn print_outer_attributes_inline(&mut self, attrs: &[ast::Attribute]) -> bool {
2328 self.print_either_attributes(attrs, ast::AttrStyle::Outer, true, true)
2329 }
2330
2331 fn print_attribute(&mut self, attr: &ast::Attribute) {
2332 self.print_attribute_inline(attr, false);
2333 }
2334
2335 fn print_meta_list_item(&mut self, item: &ast::MetaItemInner) {
2336 match item {
2337 ast::MetaItemInner::MetaItem(mi) => self.print_meta_item(mi),
2338 ast::MetaItemInner::Lit(lit) => self.print_meta_item_lit(lit),
2339 }
2340 }
2341
2342 fn print_meta_item(&mut self, item: &ast::MetaItem) {
2343 let ib = self.ibox(INDENT_UNIT);
2344
2345 match item.unsafety {
2346 ast::Safety::Unsafe(_) => {
2347 self.word("unsafe");
2348 self.popen();
2349 }
2350 ast::Safety::Default | ast::Safety::Safe(_) => {}
2351 }
2352
2353 match &item.kind {
2354 ast::MetaItemKind::Word => self.print_path(&item.path, false, 0),
2355 ast::MetaItemKind::NameValue(value) => {
2356 self.print_path(&item.path, false, 0);
2357 self.space();
2358 self.word_space("=");
2359 self.print_meta_item_lit(value);
2360 }
2361 ast::MetaItemKind::List(items) => {
2362 self.print_path(&item.path, false, 0);
2363 self.popen();
2364 self.commasep(Consistent, items, |s, i| s.print_meta_list_item(i));
2365 self.pclose();
2366 }
2367 }
2368
2369 match item.unsafety {
2370 ast::Safety::Unsafe(_) => self.pclose(),
2371 ast::Safety::Default | ast::Safety::Safe(_) => {}
2372 }
2373
2374 self.end(ib);
2375 }
2376
2377 pub(crate) fn bounds_to_string(&self, bounds: &[ast::GenericBound]) -> String {
2378 Self::to_string(|s| s.print_type_bounds(bounds))
2379 }
2380
2381 pub(crate) fn where_bound_predicate_to_string(
2382 &self,
2383 where_bound_predicate: &ast::WhereBoundPredicate,
2384 ) -> String {
2385 Self::to_string(|s| s.print_where_bound_predicate(where_bound_predicate))
2386 }
2387
2388 pub(crate) fn tt_to_string(&self, tt: &TokenTree) -> String {
2389 Self::to_string(|s| {
2390 s.print_tt(tt, false);
2391 })
2392 }
2393
2394 pub(crate) fn path_segment_to_string(&self, p: &ast::PathSegment) -> String {
2395 Self::to_string(|s| s.print_path_segment(p, false))
2396 }
2397
2398 pub(crate) fn meta_list_item_to_string(&self, li: &ast::MetaItemInner) -> String {
2399 Self::to_string(|s| s.print_meta_list_item(li))
2400 }
2401
2402 pub(crate) fn attribute_to_string(&self, attr: &ast::Attribute) -> String {
2403 Self::to_string(|s| s.print_attribute(attr))
2404 }
2405}