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