rustc_ast_pretty/pprust/state/
item.rs

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::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    fn print_foreign_item(&mut self, item: &ast::ForeignItem) {
31        let ast::Item { id, span, ident, 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(ident, vis, attrs, &*func);
39            }
40            ast::ForeignItemKind::Static(box ast::StaticItem {
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                generics,
60                where_clauses,
61                bounds,
62                ty,
63            }) => {
64                self.print_associated_type(
65                    ident,
66                    generics,
67                    *where_clauses,
68                    bounds,
69                    ty.as_deref(),
70                    vis,
71                    *defaultness,
72                );
73            }
74            ast::ForeignItemKind::MacCall(m) => {
75                self.print_mac(m);
76                if m.args.need_semicolon() {
77                    self.word(";");
78                }
79            }
80        }
81        self.ann.post(self, AnnNode::SubItem(id))
82    }
83
84    fn print_item_const(
85        &mut self,
86        ident: Ident,
87        mutbl: Option<ast::Mutability>,
88        generics: &ast::Generics,
89        ty: &ast::Ty,
90        body: Option<&ast::Expr>,
91        vis: &ast::Visibility,
92        safety: ast::Safety,
93        defaultness: ast::Defaultness,
94        define_opaque: Option<&[(ast::NodeId, ast::Path)]>,
95    ) {
96        self.print_define_opaques(define_opaque);
97        self.head("");
98        self.print_visibility(vis);
99        self.print_safety(safety);
100        self.print_defaultness(defaultness);
101        let leading = match mutbl {
102            None => "const",
103            Some(ast::Mutability::Not) => "static",
104            Some(ast::Mutability::Mut) => "static mut",
105        };
106        self.word_space(leading);
107        self.print_ident(ident);
108        self.print_generic_params(&generics.params);
109        self.word_space(":");
110        self.print_type(ty);
111        if body.is_some() {
112            self.space();
113        }
114        self.end(); // end the head-ibox
115        if let Some(body) = body {
116            self.word_space("=");
117            self.print_expr(body, FixupContext::default());
118        }
119        self.print_where_clause(&generics.where_clause);
120        self.word(";");
121        self.end(); // end the outer cbox
122    }
123
124    fn print_associated_type(
125        &mut self,
126        ident: Ident,
127        generics: &ast::Generics,
128        where_clauses: ast::TyAliasWhereClauses,
129        bounds: &ast::GenericBounds,
130        ty: Option<&ast::Ty>,
131        vis: &ast::Visibility,
132        defaultness: ast::Defaultness,
133    ) {
134        let (before_predicates, after_predicates) =
135            generics.where_clause.predicates.split_at(where_clauses.split);
136        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_parts(where_clauses.before.has_where_token, before_predicates);
147        if let Some(ty) = ty {
148            self.space();
149            self.word_space("=");
150            self.print_type(ty);
151        }
152        self.print_where_clause_parts(where_clauses.after.has_where_token, after_predicates);
153        self.word(";");
154        self.end(); // end inner head-block
155        self.end(); // end outer head-block
156    }
157
158    /// Pretty-prints an item.
159    pub(crate) fn print_item(&mut self, item: &ast::Item) {
160        self.hardbreak_if_not_bol();
161        self.maybe_print_comment(item.span.lo());
162        self.print_outer_attributes(&item.attrs);
163        self.ann.pre(self, AnnNode::Item(item));
164        match &item.kind {
165            ast::ItemKind::ExternCrate(orig_name) => {
166                self.head(visibility_qualified(&item.vis, "extern crate"));
167                if let &Some(orig_name) = orig_name {
168                    self.print_name(orig_name);
169                    self.space();
170                    self.word("as");
171                    self.space();
172                }
173                self.print_ident(item.ident);
174                self.word(";");
175                self.end(); // end inner head-block
176                self.end(); // end outer head-block
177            }
178            ast::ItemKind::Use(tree) => {
179                self.print_visibility(&item.vis);
180                self.word_nbsp("use");
181                self.print_use_tree(tree);
182                self.word(";");
183            }
184            ast::ItemKind::Static(box StaticItem {
185                ty,
186                safety,
187                mutability: mutbl,
188                expr: body,
189                define_opaque,
190            }) => {
191                self.print_safety(*safety);
192                self.print_item_const(
193                    item.ident,
194                    Some(*mutbl),
195                    &ast::Generics::default(),
196                    ty,
197                    body.as_deref(),
198                    &item.vis,
199                    ast::Safety::Default,
200                    ast::Defaultness::Final,
201                    define_opaque.as_deref(),
202                );
203            }
204            ast::ItemKind::Const(box ast::ConstItem {
205                defaultness,
206                generics,
207                ty,
208                expr,
209                define_opaque,
210            }) => {
211                self.print_item_const(
212                    item.ident,
213                    None,
214                    generics,
215                    ty,
216                    expr.as_deref(),
217                    &item.vis,
218                    ast::Safety::Default,
219                    *defaultness,
220                    define_opaque.as_deref(),
221                );
222            }
223            ast::ItemKind::Fn(func) => {
224                self.print_fn_full(item.ident, &item.vis, &item.attrs, &*func);
225            }
226            ast::ItemKind::Mod(safety, mod_kind) => {
227                self.head(Self::to_string(|s| {
228                    s.print_visibility(&item.vis);
229                    s.print_safety(*safety);
230                    s.word("mod");
231                }));
232                self.print_ident(item.ident);
233
234                match mod_kind {
235                    ModKind::Loaded(items, ..) => {
236                        self.nbsp();
237                        self.bopen();
238                        self.print_inner_attributes(&item.attrs);
239                        for item in items {
240                            self.print_item(item);
241                        }
242                        let empty = item.attrs.is_empty() && items.is_empty();
243                        self.bclose(item.span, empty);
244                    }
245                    ModKind::Unloaded => {
246                        self.word(";");
247                        self.end(); // end inner head-block
248                        self.end(); // end outer head-block
249                    }
250                }
251            }
252            ast::ItemKind::ForeignMod(nmod) => {
253                self.head(Self::to_string(|s| {
254                    s.print_safety(nmod.safety);
255                    s.word("extern");
256                }));
257                if let Some(abi) = nmod.abi {
258                    self.print_token_literal(abi.as_token_lit(), abi.span);
259                    self.nbsp();
260                }
261                self.bopen();
262                self.print_foreign_mod(nmod, &item.attrs);
263                let empty = item.attrs.is_empty() && nmod.items.is_empty();
264                self.bclose(item.span, empty);
265            }
266            ast::ItemKind::GlobalAsm(asm) => {
267                // FIXME: Print `builtin # global_asm` once macro `global_asm` uses `builtin_syntax`.
268                self.head(visibility_qualified(&item.vis, "global_asm!"));
269                self.print_inline_asm(asm);
270                self.word(";");
271                self.end();
272                self.end();
273            }
274            ast::ItemKind::TyAlias(box ast::TyAlias {
275                defaultness,
276                generics,
277                where_clauses,
278                bounds,
279                ty,
280            }) => {
281                self.print_associated_type(
282                    item.ident,
283                    generics,
284                    *where_clauses,
285                    bounds,
286                    ty.as_deref(),
287                    &item.vis,
288                    *defaultness,
289                );
290            }
291            ast::ItemKind::Enum(enum_definition, params) => {
292                self.print_enum_def(enum_definition, params, item.ident, item.span, &item.vis);
293            }
294            ast::ItemKind::Struct(struct_def, generics) => {
295                self.head(visibility_qualified(&item.vis, "struct"));
296                self.print_struct(struct_def, generics, item.ident, item.span, true);
297            }
298            ast::ItemKind::Union(struct_def, generics) => {
299                self.head(visibility_qualified(&item.vis, "union"));
300                self.print_struct(struct_def, generics, item.ident, item.span, true);
301            }
302            ast::ItemKind::Impl(box ast::Impl {
303                safety,
304                polarity,
305                defaultness,
306                constness,
307                generics,
308                of_trait,
309                self_ty,
310                items,
311            }) => {
312                self.head("");
313                self.print_visibility(&item.vis);
314                self.print_defaultness(*defaultness);
315                self.print_safety(*safety);
316                self.word("impl");
317
318                if generics.params.is_empty() {
319                    self.nbsp();
320                } else {
321                    self.print_generic_params(&generics.params);
322                    self.space();
323                }
324
325                self.print_constness(*constness);
326
327                if let ast::ImplPolarity::Negative(_) = polarity {
328                    self.word("!");
329                }
330
331                if let Some(t) = of_trait {
332                    self.print_trait_ref(t);
333                    self.space();
334                    self.word_space("for");
335                }
336
337                self.print_type(self_ty);
338                self.print_where_clause(&generics.where_clause);
339
340                self.space();
341                self.bopen();
342                self.print_inner_attributes(&item.attrs);
343                for impl_item in items {
344                    self.print_assoc_item(impl_item);
345                }
346                let empty = item.attrs.is_empty() && items.is_empty();
347                self.bclose(item.span, empty);
348            }
349            ast::ItemKind::Trait(box ast::Trait {
350                is_auto,
351                safety,
352                generics,
353                bounds,
354                items,
355                ..
356            }) => {
357                self.head("");
358                self.print_visibility(&item.vis);
359                self.print_safety(*safety);
360                self.print_is_auto(*is_auto);
361                self.word_nbsp("trait");
362                self.print_ident(item.ident);
363                self.print_generic_params(&generics.params);
364                if !bounds.is_empty() {
365                    self.word_nbsp(":");
366                    self.print_type_bounds(bounds);
367                }
368                self.print_where_clause(&generics.where_clause);
369                self.word(" ");
370                self.bopen();
371                self.print_inner_attributes(&item.attrs);
372                for trait_item in items {
373                    self.print_assoc_item(trait_item);
374                }
375                let empty = item.attrs.is_empty() && items.is_empty();
376                self.bclose(item.span, empty);
377            }
378            ast::ItemKind::TraitAlias(generics, bounds) => {
379                self.head(visibility_qualified(&item.vis, "trait"));
380                self.print_ident(item.ident);
381                self.print_generic_params(&generics.params);
382                self.nbsp();
383                if !bounds.is_empty() {
384                    self.word_nbsp("=");
385                    self.print_type_bounds(bounds);
386                }
387                self.print_where_clause(&generics.where_clause);
388                self.word(";");
389                self.end(); // end inner head-block
390                self.end(); // end outer head-block
391            }
392            ast::ItemKind::MacCall(mac) => {
393                self.print_mac(mac);
394                if mac.args.need_semicolon() {
395                    self.word(";");
396                }
397            }
398            ast::ItemKind::MacroDef(macro_def) => {
399                self.print_mac_def(macro_def, &item.ident, item.span, |state| {
400                    state.print_visibility(&item.vis)
401                });
402            }
403            ast::ItemKind::Delegation(deleg) => self.print_delegation(
404                &item.attrs,
405                &item.vis,
406                &deleg.qself,
407                &deleg.path,
408                DelegationKind::Single,
409                &deleg.body,
410            ),
411            ast::ItemKind::DelegationMac(deleg) => self.print_delegation(
412                &item.attrs,
413                &item.vis,
414                &deleg.qself,
415                &deleg.prefix,
416                deleg.suffixes.as_ref().map_or(DelegationKind::Glob, |s| DelegationKind::List(s)),
417                &deleg.body,
418            ),
419        }
420        self.ann.post(self, AnnNode::Item(item))
421    }
422
423    fn print_enum_def(
424        &mut self,
425        enum_definition: &ast::EnumDef,
426        generics: &ast::Generics,
427        ident: Ident,
428        span: rustc_span::Span,
429        visibility: &ast::Visibility,
430    ) {
431        self.head(visibility_qualified(visibility, "enum"));
432        self.print_ident(ident);
433        self.print_generic_params(&generics.params);
434        self.print_where_clause(&generics.where_clause);
435        self.space();
436        self.print_variants(&enum_definition.variants, span)
437    }
438
439    fn print_variants(&mut self, variants: &[ast::Variant], span: rustc_span::Span) {
440        self.bopen();
441        for v in variants {
442            self.space_if_not_bol();
443            self.maybe_print_comment(v.span.lo());
444            self.print_outer_attributes(&v.attrs);
445            self.ibox(0);
446            self.print_variant(v);
447            self.word(",");
448            self.end();
449            self.maybe_print_trailing_comment(v.span, None);
450        }
451        let empty = variants.is_empty();
452        self.bclose(span, empty)
453    }
454
455    pub(crate) fn print_visibility(&mut self, vis: &ast::Visibility) {
456        match &vis.kind {
457            ast::VisibilityKind::Public => self.word_nbsp("pub"),
458            ast::VisibilityKind::Restricted { path, shorthand, .. } => {
459                let path = Self::to_string(|s| s.print_path(path, false, 0));
460                if *shorthand && (path == "crate" || path == "self" || path == "super") {
461                    self.word_nbsp(format!("pub({path})"))
462                } else {
463                    self.word_nbsp(format!("pub(in {path})"))
464                }
465            }
466            ast::VisibilityKind::Inherited => {}
467        }
468    }
469
470    fn print_defaultness(&mut self, defaultness: ast::Defaultness) {
471        if let ast::Defaultness::Default(_) = defaultness {
472            self.word_nbsp("default");
473        }
474    }
475
476    pub(crate) fn print_record_struct_body(
477        &mut self,
478        fields: &[ast::FieldDef],
479        span: rustc_span::Span,
480    ) {
481        self.nbsp();
482        self.bopen();
483
484        let empty = fields.is_empty();
485        if !empty {
486            self.hardbreak_if_not_bol();
487
488            for field in fields {
489                self.hardbreak_if_not_bol();
490                self.maybe_print_comment(field.span.lo());
491                self.print_outer_attributes(&field.attrs);
492                self.print_visibility(&field.vis);
493                self.print_ident(field.ident.unwrap());
494                self.word_nbsp(":");
495                self.print_type(&field.ty);
496                self.word(",");
497            }
498        }
499
500        self.bclose(span, empty);
501    }
502
503    fn print_struct(
504        &mut self,
505        struct_def: &ast::VariantData,
506        generics: &ast::Generics,
507        ident: Ident,
508        span: rustc_span::Span,
509        print_finalizer: bool,
510    ) {
511        self.print_ident(ident);
512        self.print_generic_params(&generics.params);
513        match &struct_def {
514            ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
515                if let ast::VariantData::Tuple(..) = struct_def {
516                    self.popen();
517                    self.commasep(Inconsistent, struct_def.fields(), |s, field| {
518                        s.maybe_print_comment(field.span.lo());
519                        s.print_outer_attributes(&field.attrs);
520                        s.print_visibility(&field.vis);
521                        s.print_type(&field.ty)
522                    });
523                    self.pclose();
524                }
525                self.print_where_clause(&generics.where_clause);
526                if print_finalizer {
527                    self.word(";");
528                }
529                self.end();
530                self.end(); // Close the outer-box.
531            }
532            ast::VariantData::Struct { fields, .. } => {
533                self.print_where_clause(&generics.where_clause);
534                self.print_record_struct_body(fields, span);
535            }
536        }
537    }
538
539    pub(crate) fn print_variant(&mut self, v: &ast::Variant) {
540        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);
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, ident, 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(ident, vis, attrs, &*func);
560            }
561            ast::AssocItemKind::Const(box ast::ConstItem {
562                defaultness,
563                generics,
564                ty,
565                expr,
566                define_opaque,
567            }) => {
568                self.print_item_const(
569                    ident,
570                    None,
571                    generics,
572                    ty,
573                    expr.as_deref(),
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                generics,
583                where_clauses,
584                bounds,
585                ty,
586            }) => {
587                self.print_associated_type(
588                    ident,
589                    generics,
590                    *where_clauses,
591                    bounds,
592                    ty.as_deref(),
593                    vis,
594                    *defaultness,
595                );
596            }
597            ast::AssocItemKind::MacCall(m) => {
598                self.print_mac(m);
599                if m.args.need_semicolon() {
600                    self.word(";");
601                }
602            }
603            ast::AssocItemKind::Delegation(deleg) => self.print_delegation(
604                &item.attrs,
605                vis,
606                &deleg.qself,
607                &deleg.path,
608                DelegationKind::Single,
609                &deleg.body,
610            ),
611            ast::AssocItemKind::DelegationMac(deleg) => self.print_delegation(
612                &item.attrs,
613                vis,
614                &deleg.qself,
615                &deleg.prefix,
616                deleg.suffixes.as_ref().map_or(DelegationKind::Glob, |s| DelegationKind::List(s)),
617                &deleg.body,
618            ),
619        }
620        self.ann.post(self, AnnNode::SubItem(id))
621    }
622
623    fn print_delegation(
624        &mut self,
625        attrs: &[ast::Attribute],
626        vis: &ast::Visibility,
627        qself: &Option<P<ast::QSelf>>,
628        path: &ast::Path,
629        kind: DelegationKind<'_>,
630        body: &Option<P<ast::Block>>,
631    ) {
632        if body.is_some() {
633            self.head("");
634        }
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) = body {
667            self.nbsp();
668            self.print_block_with_attrs(body, attrs);
669        } else {
670            self.word(";");
671        }
672    }
673
674    fn print_fn_full(
675        &mut self,
676        name: Ident,
677        vis: &ast::Visibility,
678        attrs: &[ast::Attribute],
679        func: &ast::Fn,
680    ) {
681        let ast::Fn { defaultness, generics, sig, contract, body, define_opaque } = func;
682
683        self.print_define_opaques(define_opaque.as_deref());
684
685        if body.is_some() {
686            self.head("");
687        }
688        self.print_visibility(vis);
689        self.print_defaultness(*defaultness);
690        self.print_fn(&sig.decl, sig.header, Some(name), generics);
691        if let Some(contract) = &contract {
692            self.nbsp();
693            self.print_contract(contract);
694        }
695        if let Some(body) = body {
696            self.nbsp();
697            self.print_block_with_attrs(body, attrs);
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        name: Option<Ident>,
738        generics: &ast::Generics,
739    ) {
740        self.print_fn_header_info(header);
741        if let Some(name) = name {
742            self.nbsp();
743            self.print_ident(name);
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                    self.cbox(INDENT_UNIT);
854                    self.word("{");
855                    self.zerobreak();
856                    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();
870                    self.trailing_comma();
871                    self.offset(-INDENT_UNIT);
872                    self.word("}");
873                    self.end();
874                }
875            }
876        }
877    }
878}