1use ast::StaticItem;
2use rustc_ast::{self as ast, EiiImpl, ModKind, Safety, TraitAlias};
3use rustc_span::Ident;
4
5use crate::pp::BoxMarker;
6use crate::pp::Breaks::Inconsistent;
7use crate::pprust::state::fixup::FixupContext;
8use crate::pprust::state::{AnnNode, INDENT_UNIT, PrintState, State};
9
10enum DelegationKind<'a> {
11 Single,
12 List(&'a [(Ident, Option<Ident>)]),
13 Glob,
14}
15
16fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String {
17 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}",
State::to_string(|s| s.print_visibility(vis)), s))
})format!("{}{}", State::to_string(|s| s.print_visibility(vis)), s)
18}
19
20impl<'a> State<'a> {
21 fn print_foreign_mod(&mut self, nmod: &ast::ForeignMod, attrs: &[ast::Attribute]) {
22 self.print_inner_attributes(attrs);
23 for item in &nmod.items {
24 self.print_foreign_item(item);
25 }
26 }
27
28 pub(crate) fn print_foreign_item(&mut self, item: &ast::ForeignItem) {
29 let ast::Item { id, span, ref attrs, ref kind, ref vis, tokens: _ } = *item;
30 self.ann.pre(self, AnnNode::SubItem(id));
31 self.hardbreak_if_not_bol();
32 self.maybe_print_comment(span.lo());
33 self.print_outer_attributes(attrs);
34 match kind {
35 ast::ForeignItemKind::Fn(func) => {
36 self.print_fn_full(vis, attrs, &*func);
37 }
38 ast::ForeignItemKind::Static(ast::StaticItem {
39 ident,
40 ty,
41 mutability,
42 expr,
43 safety,
44 define_opaque,
45 eii_impl,
46 }) => self.print_item_const(
47 *ident,
48 Some(*mutability),
49 &ast::Generics::default(),
50 ty,
51 expr.as_deref(),
52 vis,
53 *safety,
54 ast::Defaultness::Implicit,
55 define_opaque.as_deref(),
56 eii_impl.as_deref(),
57 ),
58 ast::ForeignItemKind::TyAlias(ast::TyAlias {
59 defaultness,
60 ident,
61 generics,
62 after_where_clause,
63 bounds,
64 ty,
65 }) => {
66 self.print_associated_type(
67 *ident,
68 generics,
69 after_where_clause,
70 bounds,
71 ty.as_deref(),
72 vis,
73 *defaultness,
74 );
75 }
76 ast::ForeignItemKind::MacCall(m) => {
77 self.print_mac(m);
78 if m.args.need_semicolon() {
79 self.word(";");
80 }
81 }
82 }
83 self.ann.post(self, AnnNode::SubItem(id))
84 }
85
86 fn print_item_const(
87 &mut self,
88 ident: Ident,
89 mutbl: Option<ast::Mutability>,
90 generics: &ast::Generics,
91 ty: &ast::Ty,
92 body: Option<&ast::Expr>,
93 vis: &ast::Visibility,
94 safety: ast::Safety,
95 defaultness: ast::Defaultness,
96 define_opaque: Option<&[(ast::NodeId, ast::Path)]>,
97 eii_impl: Option<&EiiImpl>,
98 ) {
99 self.print_define_opaques(define_opaque);
100 if let Some(eii_impl) = eii_impl {
101 self.print_eii_impl(eii_impl);
102 }
103 let (cb, ib) = self.head("");
104 self.print_visibility(vis);
105 self.print_safety(safety);
106 self.print_defaultness(defaultness);
107 let leading = match mutbl {
108 None => "const",
109 Some(ast::Mutability::Not) => "static",
110 Some(ast::Mutability::Mut) => "static mut",
111 };
112 self.word_space(leading);
113 self.print_ident(ident);
114 self.print_generic_params(&generics.params);
115 self.word_space(":");
116 self.print_type(ty);
117 if body.is_some() {
118 self.space();
119 }
120 self.end(ib);
121 if let Some(body) = body {
122 self.word_space("=");
123 self.print_expr(body, FixupContext::default());
124 }
125 self.print_where_clause(&generics.where_clause);
126 self.word(";");
127 self.end(cb);
128 }
129
130 fn print_associated_type(
131 &mut self,
132 ident: Ident,
133 generics: &ast::Generics,
134 after_where_clause: &ast::WhereClause,
135 bounds: &ast::GenericBounds,
136 ty: Option<&ast::Ty>,
137 vis: &ast::Visibility,
138 defaultness: ast::Defaultness,
139 ) {
140 let (cb, ib) = self.head("");
141 self.print_visibility(vis);
142 self.print_defaultness(defaultness);
143 self.word_space("type");
144 self.print_ident(ident);
145 self.print_generic_params(&generics.params);
146 if !bounds.is_empty() {
147 self.word_nbsp(":");
148 self.print_type_bounds(bounds);
149 }
150 self.print_where_clause(&generics.where_clause);
151 if let Some(ty) = ty {
152 self.space();
153 self.word_space("=");
154 self.print_type(ty);
155 }
156 self.print_where_clause(&after_where_clause);
157 self.word(";");
158 self.end(ib);
159 self.end(cb);
160 }
161
162 pub(crate) fn print_item(&mut self, item: &ast::Item) {
164 if self.is_sdylib_interface && item.span.is_dummy() {
165 return;
167 }
168 self.hardbreak_if_not_bol();
169 self.maybe_print_comment(item.span.lo());
170 self.print_outer_attributes(&item.attrs);
171 self.ann.pre(self, AnnNode::Item(item));
172 match &item.kind {
173 ast::ItemKind::ExternCrate(orig_name, ident) => {
174 let (cb, ib) = self.head(visibility_qualified(&item.vis, "extern crate"));
175 if let &Some(orig_name) = orig_name {
176 self.print_name(orig_name);
177 self.space();
178 self.word("as");
179 self.space();
180 }
181 self.print_ident(*ident);
182 self.word(";");
183 self.end(ib);
184 self.end(cb);
185 }
186 ast::ItemKind::Use(tree) => {
187 self.print_visibility(&item.vis);
188 self.word_nbsp("use");
189 self.print_use_tree(tree);
190 self.word(";");
191 }
192 ast::ItemKind::Static(StaticItem {
193 ident,
194 ty,
195 safety,
196 mutability: mutbl,
197 expr: body,
198 define_opaque,
199 eii_impl,
200 }) => {
201 self.print_safety(*safety);
202 self.print_item_const(
203 *ident,
204 Some(*mutbl),
205 &ast::Generics::default(),
206 ty,
207 body.as_deref(),
208 &item.vis,
209 ast::Safety::Default,
210 ast::Defaultness::Implicit,
211 define_opaque.as_deref(),
212 eii_impl.as_deref(),
213 );
214 }
215 ast::ItemKind::ConstBlock(ast::ConstBlockItem { id: _, span: _, block }) => {
216 let ib = self.ibox(INDENT_UNIT);
217 self.word("const");
218 self.nbsp();
219 {
220 let cb = self.cbox(0);
221 let ib = self.ibox(0);
222 self.print_block_with_attrs(block, &[], cb, ib);
223 }
224 self.end(ib);
225 }
226 ast::ItemKind::Const(ast::ConstItem {
227 defaultness,
228 ident,
229 generics,
230 ty,
231 body,
232 kind: _,
233 define_opaque,
234 }) => {
235 self.print_item_const(
236 *ident,
237 None,
238 generics,
239 ty,
240 body.as_deref(),
241 &item.vis,
242 ast::Safety::Default,
243 *defaultness,
244 define_opaque.as_deref(),
245 None,
246 );
247 }
248 ast::ItemKind::Fn(func) => {
249 self.print_fn_full(&item.vis, &item.attrs, &*func);
250 }
251 ast::ItemKind::Mod(safety, ident, mod_kind) => {
252 let (cb, ib) = self.head(Self::to_string(|s| {
253 s.print_visibility(&item.vis);
254 s.print_safety(*safety);
255 s.word("mod");
256 }));
257 self.print_ident(*ident);
258
259 match mod_kind {
260 ModKind::Loaded(items, ..) => {
261 self.nbsp();
262 self.bopen(ib);
263 self.print_inner_attributes(&item.attrs);
264 for item in items {
265 self.print_item(item);
266 }
267 let empty = item.attrs.is_empty() && items.is_empty();
268 self.bclose(item.span, empty, cb);
269 }
270 ModKind::Unloaded => {
271 self.word(";");
272 self.end(ib);
273 self.end(cb);
274 }
275 }
276 }
277 ast::ItemKind::ForeignMod(nmod) => {
278 let (cb, ib) = self.head(Self::to_string(|s| {
279 s.print_safety(nmod.safety);
280 s.word("extern");
281 }));
282 if let Some(abi) = nmod.abi {
283 self.print_token_literal(abi.as_token_lit(), abi.span);
284 self.nbsp();
285 }
286 self.bopen(ib);
287 self.print_foreign_mod(nmod, &item.attrs);
288 let empty = item.attrs.is_empty() && nmod.items.is_empty();
289 self.bclose(item.span, empty, cb);
290 }
291 ast::ItemKind::GlobalAsm(asm) => {
292 let (cb, ib) = self.head(visibility_qualified(&item.vis, "global_asm!"));
294 self.print_inline_asm(asm);
295 self.word(";");
296 self.end(ib);
297 self.end(cb);
298 }
299 ast::ItemKind::TyAlias(ast::TyAlias {
300 defaultness,
301 ident,
302 generics,
303 after_where_clause,
304 bounds,
305 ty,
306 }) => {
307 self.print_associated_type(
308 *ident,
309 generics,
310 after_where_clause,
311 bounds,
312 ty.as_deref(),
313 &item.vis,
314 *defaultness,
315 );
316 }
317 ast::ItemKind::Enum(ident, generics, enum_definition) => {
318 self.print_enum_def(enum_definition, generics, *ident, item.span, &item.vis);
319 }
320 ast::ItemKind::Struct(ident, generics, struct_def) => {
321 let (cb, ib) = self.head(visibility_qualified(&item.vis, "struct"));
322 self.print_struct(struct_def, generics, *ident, item.span, true, cb, ib);
323 }
324 ast::ItemKind::Union(ident, generics, struct_def) => {
325 let (cb, ib) = self.head(visibility_qualified(&item.vis, "union"));
326 self.print_struct(struct_def, generics, *ident, item.span, true, cb, ib);
327 }
328 ast::ItemKind::Impl(ast::Impl { generics, of_trait, self_ty, items, constness }) => {
329 let (cb, ib) = self.head("");
330 self.print_visibility(&item.vis);
331
332 let impl_generics = |this: &mut Self| {
333 this.word("impl");
334
335 if generics.params.is_empty() {
336 this.nbsp();
337 } else {
338 this.print_generic_params(&generics.params);
339 this.space();
340 }
341 };
342
343 if let Some(of_trait) = of_trait {
344 let ast::TraitImplHeader { defaultness, safety, polarity, ref trait_ref } =
345 *of_trait;
346 self.print_defaultness(defaultness);
347 self.print_constness(*constness);
348 self.print_safety(safety);
349 impl_generics(self);
350 if let ast::ImplPolarity::Negative(_) = polarity {
351 self.word("!");
352 }
353 self.print_trait_ref(trait_ref);
354 self.space();
355 self.word_space("for");
356 } else {
357 self.print_constness(*constness);
358 impl_generics(self);
359 }
360
361 self.print_type(self_ty);
362 self.print_where_clause(&generics.where_clause);
363
364 self.space();
365 self.bopen(ib);
366 self.print_inner_attributes(&item.attrs);
367 for impl_item in items {
368 self.print_assoc_item(impl_item);
369 }
370 let empty = item.attrs.is_empty() && items.is_empty();
371 self.bclose(item.span, empty, cb);
372 }
373 ast::ItemKind::Trait(ast::Trait {
374 impl_restriction,
375 constness,
376 safety,
377 is_auto,
378 ident,
379 generics,
380 bounds,
381 items,
382 }) => {
383 let (cb, ib) = self.head("");
384 self.print_visibility(&item.vis);
385 self.print_impl_restriction(impl_restriction);
386 self.print_constness(*constness);
387 self.print_safety(*safety);
388 self.print_is_auto(*is_auto);
389 self.word_nbsp("trait");
390 self.print_ident(*ident);
391 self.print_generic_params(&generics.params);
392 if !bounds.is_empty() {
393 self.word_nbsp(":");
394 self.print_type_bounds(bounds);
395 }
396 self.print_where_clause(&generics.where_clause);
397 self.word(" ");
398 self.bopen(ib);
399 self.print_inner_attributes(&item.attrs);
400 for trait_item in items {
401 self.print_assoc_item(trait_item);
402 }
403 let empty = item.attrs.is_empty() && items.is_empty();
404 self.bclose(item.span, empty, cb);
405 }
406 ast::ItemKind::TraitAlias(TraitAlias { constness, ident, generics, bounds }) => {
407 let (cb, ib) = self.head("");
408 self.print_visibility(&item.vis);
409 self.print_constness(*constness);
410 self.word_nbsp("trait");
411 self.print_ident(*ident);
412 self.print_generic_params(&generics.params);
413 self.nbsp();
414 if !bounds.is_empty() {
415 self.word_nbsp("=");
416 self.print_type_bounds(bounds);
417 }
418 self.print_where_clause(&generics.where_clause);
419 self.word(";");
420 self.end(ib);
421 self.end(cb);
422 }
423 ast::ItemKind::MacCall(mac) => {
424 self.print_mac(mac);
425 if mac.args.need_semicolon() {
426 self.word(";");
427 }
428 }
429 ast::ItemKind::MacroDef(ident, macro_def) => {
430 self.print_mac_def(macro_def, &ident, item.span, |state| {
431 state.print_visibility(&item.vis)
432 });
433 }
434 ast::ItemKind::Delegation(deleg) => self.print_delegation(
435 &item.attrs,
436 &item.vis,
437 &deleg.qself,
438 &deleg.path,
439 DelegationKind::Single,
440 &deleg.body,
441 ),
442 ast::ItemKind::DelegationMac(deleg) => self.print_delegation(
443 &item.attrs,
444 &item.vis,
445 &deleg.qself,
446 &deleg.prefix,
447 match &deleg.suffixes {
448 ast::DelegationSuffixes::List(s) => DelegationKind::List(s),
449 ast::DelegationSuffixes::Glob(_) => DelegationKind::Glob,
450 },
451 &deleg.body,
452 ),
453 }
454 self.ann.post(self, AnnNode::Item(item))
455 }
456
457 fn print_enum_def(
458 &mut self,
459 enum_definition: &ast::EnumDef,
460 generics: &ast::Generics,
461 ident: Ident,
462 span: rustc_span::Span,
463 visibility: &ast::Visibility,
464 ) {
465 let (cb, ib) = self.head(visibility_qualified(visibility, "enum"));
466 self.print_ident(ident);
467 self.print_generic_params(&generics.params);
468 self.print_where_clause(&generics.where_clause);
469 self.space();
470 self.bopen(ib);
471 for v in enum_definition.variants.iter() {
472 self.space_if_not_bol();
473 self.maybe_print_comment(v.span.lo());
474 self.print_outer_attributes(&v.attrs);
475 let ib = self.ibox(0);
476 self.print_variant(v);
477 self.word(",");
478 self.end(ib);
479 self.maybe_print_trailing_comment(v.span, None);
480 }
481 let empty = enum_definition.variants.is_empty();
482 self.bclose(span, empty, cb)
483 }
484
485 pub(crate) fn print_visibility(&mut self, vis: &ast::Visibility) {
486 match &vis.kind {
487 ast::VisibilityKind::Public => self.word_nbsp("pub"),
488 ast::VisibilityKind::Restricted { path, shorthand, .. } => {
489 let path = Self::to_string(|s| s.print_path(path, false, 0));
490 if *shorthand && (path == "crate" || path == "self" || path == "super") {
491 self.word_nbsp(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("pub({0})", path))
})format!("pub({path})"))
492 } else {
493 self.word_nbsp(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("pub(in {0})", path))
})format!("pub(in {path})"))
494 }
495 }
496 ast::VisibilityKind::Inherited => {}
497 }
498 }
499
500 pub(crate) fn print_impl_restriction(&mut self, impl_restriction: &ast::ImplRestriction) {
501 match &impl_restriction.kind {
502 ast::RestrictionKind::Restricted { path, shorthand, .. } => {
503 let path = Self::to_string(|s| s.print_path(path, false, 0));
504 if *shorthand {
505 self.word_nbsp(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("impl({0})", path))
})format!("impl({path})"))
506 } else {
507 self.word_nbsp(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("impl(in {0})", path))
})format!("impl(in {path})"))
508 }
509 }
510 ast::RestrictionKind::Unrestricted => {}
511 }
512 }
513
514 pub(crate) fn print_mut_restriction(&mut self, mut_restriction: &ast::MutRestriction) {
515 match &mut_restriction.kind {
516 ast::RestrictionKind::Restricted { path, shorthand, .. } => {
517 let path = Self::to_string(|s| s.print_path(path, false, 0));
518 if *shorthand {
519 self.word_nbsp(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("mut({0})", path))
})format!("mut({path})"))
520 } else {
521 self.word_nbsp(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("mut(in {0})", path))
})format!("mut(in {path})"))
522 }
523 }
524 ast::RestrictionKind::Unrestricted => {}
525 }
526 }
527
528 fn print_defaultness(&mut self, defaultness: ast::Defaultness) {
529 if let ast::Defaultness::Default(_) = defaultness {
530 self.word_nbsp("default");
531 }
532 }
533
534 fn print_struct(
535 &mut self,
536 struct_def: &ast::VariantData,
537 generics: &ast::Generics,
538 ident: Ident,
539 span: rustc_span::Span,
540 print_finalizer: bool,
541 cb: BoxMarker,
542 ib: BoxMarker,
543 ) {
544 self.print_ident(ident);
545 self.print_generic_params(&generics.params);
546 match &struct_def {
547 ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
548 if let ast::VariantData::Tuple(..) = struct_def {
549 self.popen();
550 self.commasep(Inconsistent, struct_def.fields(), |s, field| {
551 s.maybe_print_comment(field.span.lo());
552 s.print_outer_attributes(&field.attrs);
553 s.print_visibility(&field.vis);
554 s.print_mut_restriction(field.mut_restriction());
555 s.print_type(&field.ty)
556 });
557 self.pclose();
558 }
559 self.print_where_clause(&generics.where_clause);
560 if print_finalizer {
561 self.word(";");
562 }
563 self.end(ib);
564 self.end(cb);
565 }
566 ast::VariantData::Struct { fields, .. } => {
567 self.print_where_clause(&generics.where_clause);
568 self.nbsp();
569 self.bopen(ib);
570
571 let empty = fields.is_empty();
572 if !empty {
573 self.hardbreak_if_not_bol();
574
575 for field in fields {
576 self.hardbreak_if_not_bol();
577 self.maybe_print_comment(field.span.lo());
578 self.print_outer_attributes(&field.attrs);
579 self.print_visibility(&field.vis);
580 self.print_mut_restriction(field.mut_restriction());
581 self.print_ident(field.ident.unwrap());
582 self.word_nbsp(":");
583 self.print_type(&field.ty);
584 self.word(",");
585 }
586 }
587
588 self.bclose(span, empty, cb);
589 }
590 }
591 }
592
593 pub(crate) fn print_variant(&mut self, v: &ast::Variant) {
594 let (cb, ib) = self.head("");
595 self.print_visibility(&v.vis);
596 let generics = ast::Generics::default();
597 self.print_struct(&v.data, &generics, v.ident, v.span, false, cb, ib);
598 if let Some(d) = &v.disr_expr {
599 self.space();
600 self.word_space("=");
601 self.print_expr(&d.value, FixupContext::default())
602 }
603 }
604
605 pub(crate) fn print_assoc_item(&mut self, item: &ast::AssocItem) {
606 let ast::Item { id, span, ref attrs, ref kind, ref vis, tokens: _ } = *item;
607 self.ann.pre(self, AnnNode::SubItem(id));
608 self.hardbreak_if_not_bol();
609 self.maybe_print_comment(span.lo());
610 self.print_outer_attributes(attrs);
611 match kind {
612 ast::AssocItemKind::Fn(func) => {
613 self.print_fn_full(vis, attrs, &*func);
614 }
615 ast::AssocItemKind::Const(ast::ConstItem {
616 defaultness,
617 ident,
618 generics,
619 ty,
620 body,
621 kind: _,
622 define_opaque,
623 }) => {
624 self.print_item_const(
625 *ident,
626 None,
627 generics,
628 ty,
629 body.as_deref(),
630 vis,
631 ast::Safety::Default,
632 *defaultness,
633 define_opaque.as_deref(),
634 None,
635 );
636 }
637 ast::AssocItemKind::Type(ast::TyAlias {
638 defaultness,
639 ident,
640 generics,
641 after_where_clause,
642 bounds,
643 ty,
644 }) => {
645 self.print_associated_type(
646 *ident,
647 generics,
648 after_where_clause,
649 bounds,
650 ty.as_deref(),
651 vis,
652 *defaultness,
653 );
654 }
655 ast::AssocItemKind::MacCall(m) => {
656 self.print_mac(m);
657 if m.args.need_semicolon() {
658 self.word(";");
659 }
660 }
661 ast::AssocItemKind::Delegation(deleg) => self.print_delegation(
662 &item.attrs,
663 vis,
664 &deleg.qself,
665 &deleg.path,
666 DelegationKind::Single,
667 &deleg.body,
668 ),
669 ast::AssocItemKind::DelegationMac(deleg) => self.print_delegation(
670 &item.attrs,
671 vis,
672 &deleg.qself,
673 &deleg.prefix,
674 match &deleg.suffixes {
675 ast::DelegationSuffixes::List(s) => DelegationKind::List(s),
676 ast::DelegationSuffixes::Glob(_) => DelegationKind::Glob,
677 },
678 &deleg.body,
679 ),
680 }
681 self.ann.post(self, AnnNode::SubItem(id))
682 }
683
684 fn print_delegation(
685 &mut self,
686 attrs: &[ast::Attribute],
687 vis: &ast::Visibility,
688 qself: &Option<Box<ast::QSelf>>,
689 path: &ast::Path,
690 kind: DelegationKind<'_>,
691 body: &Option<Box<ast::Block>>,
692 ) {
693 let body_cb_ib = body.as_ref().map(|body| (body, self.head("")));
694 self.print_visibility(vis);
695 self.word_nbsp("reuse");
696
697 if let Some(qself) = qself {
698 self.print_qpath(path, qself, false);
699 } else {
700 self.print_path(path, false, 0);
701 }
702 match kind {
703 DelegationKind::Single => {}
704 DelegationKind::List(suffixes) => {
705 self.word("::");
706 self.word("{");
707 for (i, (ident, rename)) in suffixes.iter().enumerate() {
708 self.print_ident(*ident);
709 if let Some(rename) = rename {
710 self.nbsp();
711 self.word_nbsp("as");
712 self.print_ident(*rename);
713 }
714 if i != suffixes.len() - 1 {
715 self.word_space(",");
716 }
717 }
718 self.word("}");
719 }
720 DelegationKind::Glob => {
721 self.word("::");
722 self.word("*");
723 }
724 }
725 if let Some((body, (cb, ib))) = body_cb_ib {
726 self.nbsp();
727 self.print_block_with_attrs(body, attrs, cb, ib);
728 } else {
729 self.word(";");
730 }
731 }
732
733 fn print_fn_full(&mut self, vis: &ast::Visibility, attrs: &[ast::Attribute], func: &ast::Fn) {
734 let ast::Fn { defaultness, ident, generics, sig, contract, body, define_opaque, eii_impl } =
735 func;
736
737 self.print_define_opaques(define_opaque.as_deref());
738
739 if let Some(eii_impl) = eii_impl {
740 self.print_eii_impl(eii_impl);
741 }
742
743 let body_cb_ib = body.as_ref().map(|body| (body, self.head("")));
744
745 self.print_visibility(vis);
746 self.print_defaultness(*defaultness);
747 self.print_fn(&sig.decl, sig.header, Some(*ident), generics);
748 if let Some(contract) = &contract {
749 self.nbsp();
750 self.print_contract(contract);
751 }
752 if let Some((body, (cb, ib))) = body_cb_ib {
753 if self.is_sdylib_interface {
754 self.word(";");
755 self.end(ib); self.end(cb); return;
758 }
759
760 self.nbsp();
761 self.print_block_with_attrs(body, attrs, cb, ib);
762 } else {
763 self.word(";");
764 }
765 }
766
767 fn print_eii_impl(&mut self, eii: &ast::EiiImpl) {
768 self.word("#[");
769 if let Safety::Unsafe(..) = eii.impl_safety {
770 self.word("unsafe");
771 self.popen();
772 }
773 self.print_path(&eii.eii_macro_path, false, 0);
774 if let Safety::Unsafe(..) = eii.impl_safety {
775 self.pclose();
776 }
777 self.word("]");
778 self.hardbreak();
779 }
780
781 fn print_define_opaques(&mut self, define_opaque: Option<&[(ast::NodeId, ast::Path)]>) {
782 if let Some(define_opaque) = define_opaque {
783 self.word("#[define_opaque(");
784 for (i, (_, path)) in define_opaque.iter().enumerate() {
785 if i != 0 {
786 self.word_space(",");
787 }
788
789 self.print_path(path, false, 0);
790 }
791 self.word(")]");
792 }
793 self.hardbreak_if_not_bol();
794 }
795
796 fn print_contract(&mut self, contract: &ast::FnContract) {
797 if let Some(pred) = &contract.requires {
798 self.word("rustc_requires");
799 self.popen();
800 self.print_expr(pred, FixupContext::default());
801 self.pclose();
802 }
803 if let Some(pred) = &contract.ensures {
804 self.word("rustc_ensures");
805 self.popen();
806 self.print_expr(pred, FixupContext::default());
807 self.pclose();
808 }
809 }
810
811 pub(crate) fn print_fn(
812 &mut self,
813 decl: &ast::FnDecl,
814 header: ast::FnHeader,
815 ident: Option<Ident>,
816 generics: &ast::Generics,
817 ) {
818 self.print_fn_header_info(header);
819 if let Some(ident) = ident {
820 self.nbsp();
821 self.print_ident(ident);
822 }
823 self.print_generic_params(&generics.params);
824 self.print_fn_params_and_ret(decl, false);
825 self.print_where_clause(&generics.where_clause);
826 }
827
828 pub(crate) fn print_fn_params_and_ret(&mut self, decl: &ast::FnDecl, is_closure: bool) {
829 let (open, close) = if is_closure { ("|", "|") } else { ("(", ")") };
830 self.word(open);
831 self.commasep(Inconsistent, &decl.inputs, |s, param| s.print_param(param, is_closure));
832 self.word(close);
833 self.print_fn_ret_ty(&decl.output)
834 }
835
836 fn print_where_clause(&mut self, where_clause: &ast::WhereClause) {
837 let ast::WhereClause { has_where_token, ref predicates, span: _ } = *where_clause;
838 if predicates.is_empty() && !has_where_token {
839 return;
840 }
841
842 self.space();
843 self.word_space("where");
844
845 for (i, predicate) in predicates.iter().enumerate() {
846 if i != 0 {
847 self.word_space(",");
848 }
849
850 self.print_where_predicate(predicate);
851 }
852 }
853
854 pub fn print_where_predicate(&mut self, predicate: &ast::WherePredicate) {
855 let ast::WherePredicate { attrs, kind, id: _, span: _, is_placeholder: _ } = predicate;
856 self.print_outer_attributes(attrs);
857 match kind {
858 ast::WherePredicateKind::BoundPredicate(where_bound_predicate) => {
859 self.print_where_bound_predicate(where_bound_predicate);
860 }
861 ast::WherePredicateKind::RegionPredicate(ast::WhereRegionPredicate {
862 lifetime,
863 bounds,
864 ..
865 }) => {
866 self.print_lifetime(*lifetime);
867 self.word(":");
868 if !bounds.is_empty() {
869 self.nbsp();
870 self.print_lifetime_bounds(bounds);
871 }
872 }
873 }
874 }
875
876 pub(crate) fn print_where_bound_predicate(
877 &mut self,
878 where_bound_predicate: &ast::WhereBoundPredicate,
879 ) {
880 self.print_formal_generic_params(&where_bound_predicate.bound_generic_params);
881 self.print_type(&where_bound_predicate.bounded_ty);
882 self.word(":");
883 if !where_bound_predicate.bounds.is_empty() {
884 self.nbsp();
885 self.print_type_bounds(&where_bound_predicate.bounds);
886 }
887 }
888
889 fn print_use_tree(&mut self, tree: &ast::UseTree) {
890 match &tree.kind {
891 ast::UseTreeKind::Simple(rename) => {
892 self.print_path(&tree.prefix, false, 0);
893 if let &Some(rename) = rename {
894 self.nbsp();
895 self.word_nbsp("as");
896 self.print_ident(rename);
897 }
898 }
899 ast::UseTreeKind::Glob(_) => {
900 if !tree.prefix.segments.is_empty() {
901 self.print_path(&tree.prefix, false, 0);
902 self.word("::");
903 }
904 self.word("*");
905 }
906 ast::UseTreeKind::Nested { items, .. } => {
907 if !tree.prefix.segments.is_empty() {
908 self.print_path(&tree.prefix, false, 0);
909 self.word("::");
910 }
911 if items.is_empty() {
912 self.word("{}");
913 } else if let [(item, _)] = items.as_slice()
914 && !item
915 .prefix
916 .segments
917 .first()
918 .is_some_and(|seg| seg.ident.name == rustc_span::symbol::kw::SelfLower)
919 {
920 self.print_use_tree(item);
921 } else {
922 let cb = self.cbox(INDENT_UNIT);
923 self.word("{");
924 self.zerobreak();
925 let ib = self.ibox(0);
926 for (idx, use_tree) in items.iter().enumerate() {
927 let is_last = idx == items.len() - 1;
928 self.print_use_tree(&use_tree.0);
929 if !is_last {
930 self.word(",");
931 if let ast::UseTreeKind::Nested { .. } = use_tree.0.kind {
932 self.hardbreak();
933 } else {
934 self.space();
935 }
936 }
937 }
938 self.end(ib);
939 self.trailing_comma();
940 self.offset(-INDENT_UNIT);
941 self.word("}");
942 self.end(cb);
943 }
944 }
945 }
946 }
947}