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