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