rustc_ast_pretty/pprust/state/
item.rs

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    /// Pretty-prints an item.
158    pub(crate) fn print_item(&mut self, item: &ast::Item) {
159        if self.is_sdylib_interface && item.span.is_dummy() {
160            // Do not print prelude for interface files.
161            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                expr,
214                define_opaque,
215            }) => {
216                self.print_item_const(
217                    *ident,
218                    None,
219                    generics,
220                    ty,
221                    expr.as_deref(),
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                // FIXME: Print `builtin # global_asm` once macro `global_asm` uses `builtin_syntax`.
273                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 }) => {
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 {
325                        defaultness,
326                        safety,
327                        constness,
328                        polarity,
329                        ref trait_ref,
330                    } = *of_trait;
331                    self.print_defaultness(defaultness);
332                    self.print_safety(safety);
333                    impl_generics(self);
334                    self.print_constness(constness);
335                    if let ast::ImplPolarity::Negative(_) = polarity {
336                        self.word("!");
337                    }
338                    self.print_trait_ref(trait_ref);
339                    self.space();
340                    self.word_space("for");
341                } else {
342                    impl_generics(self);
343                }
344
345                self.print_type(self_ty);
346                self.print_where_clause(&generics.where_clause);
347
348                self.space();
349                self.bopen(ib);
350                self.print_inner_attributes(&item.attrs);
351                for impl_item in items {
352                    self.print_assoc_item(impl_item);
353                }
354                let empty = item.attrs.is_empty() && items.is_empty();
355                self.bclose(item.span, empty, cb);
356            }
357            ast::ItemKind::Trait(box ast::Trait {
358                constness,
359                safety,
360                is_auto,
361                ident,
362                generics,
363                bounds,
364                items,
365            }) => {
366                let (cb, ib) = self.head("");
367                self.print_visibility(&item.vis);
368                self.print_constness(*constness);
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(box TraitAlias { constness, ident, generics, bounds }) => {
389                let (cb, ib) = self.head("");
390                self.print_visibility(&item.vis);
391                self.print_constness(*constness);
392                self.word_nbsp("trait");
393                self.print_ident(*ident);
394                self.print_generic_params(&generics.params);
395                self.nbsp();
396                if !bounds.is_empty() {
397                    self.word_nbsp("=");
398                    self.print_type_bounds(bounds);
399                }
400                self.print_where_clause(&generics.where_clause);
401                self.word(";");
402                self.end(ib);
403                self.end(cb);
404            }
405            ast::ItemKind::MacCall(mac) => {
406                self.print_mac(mac);
407                if mac.args.need_semicolon() {
408                    self.word(";");
409                }
410            }
411            ast::ItemKind::MacroDef(ident, macro_def) => {
412                self.print_mac_def(macro_def, &ident, item.span, |state| {
413                    state.print_visibility(&item.vis)
414                });
415            }
416            ast::ItemKind::Delegation(deleg) => self.print_delegation(
417                &item.attrs,
418                &item.vis,
419                &deleg.qself,
420                &deleg.path,
421                DelegationKind::Single,
422                &deleg.body,
423            ),
424            ast::ItemKind::DelegationMac(deleg) => self.print_delegation(
425                &item.attrs,
426                &item.vis,
427                &deleg.qself,
428                &deleg.prefix,
429                deleg.suffixes.as_ref().map_or(DelegationKind::Glob, |s| DelegationKind::List(s)),
430                &deleg.body,
431            ),
432        }
433        self.ann.post(self, AnnNode::Item(item))
434    }
435
436    fn print_enum_def(
437        &mut self,
438        enum_definition: &ast::EnumDef,
439        generics: &ast::Generics,
440        ident: Ident,
441        span: rustc_span::Span,
442        visibility: &ast::Visibility,
443    ) {
444        let (cb, ib) = self.head(visibility_qualified(visibility, "enum"));
445        self.print_ident(ident);
446        self.print_generic_params(&generics.params);
447        self.print_where_clause(&generics.where_clause);
448        self.space();
449        self.bopen(ib);
450        for v in enum_definition.variants.iter() {
451            self.space_if_not_bol();
452            self.maybe_print_comment(v.span.lo());
453            self.print_outer_attributes(&v.attrs);
454            let ib = self.ibox(0);
455            self.print_variant(v);
456            self.word(",");
457            self.end(ib);
458            self.maybe_print_trailing_comment(v.span, None);
459        }
460        let empty = enum_definition.variants.is_empty();
461        self.bclose(span, empty, cb)
462    }
463
464    pub(crate) fn print_visibility(&mut self, vis: &ast::Visibility) {
465        match &vis.kind {
466            ast::VisibilityKind::Public => self.word_nbsp("pub"),
467            ast::VisibilityKind::Restricted { path, shorthand, .. } => {
468                let path = Self::to_string(|s| s.print_path(path, false, 0));
469                if *shorthand && (path == "crate" || path == "self" || path == "super") {
470                    self.word_nbsp(format!("pub({path})"))
471                } else {
472                    self.word_nbsp(format!("pub(in {path})"))
473                }
474            }
475            ast::VisibilityKind::Inherited => {}
476        }
477    }
478
479    fn print_defaultness(&mut self, defaultness: ast::Defaultness) {
480        if let ast::Defaultness::Default(_) = defaultness {
481            self.word_nbsp("default");
482        }
483    }
484
485    fn print_struct(
486        &mut self,
487        struct_def: &ast::VariantData,
488        generics: &ast::Generics,
489        ident: Ident,
490        span: rustc_span::Span,
491        print_finalizer: bool,
492        cb: BoxMarker,
493        ib: BoxMarker,
494    ) {
495        self.print_ident(ident);
496        self.print_generic_params(&generics.params);
497        match &struct_def {
498            ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
499                if let ast::VariantData::Tuple(..) = struct_def {
500                    self.popen();
501                    self.commasep(Inconsistent, struct_def.fields(), |s, field| {
502                        s.maybe_print_comment(field.span.lo());
503                        s.print_outer_attributes(&field.attrs);
504                        s.print_visibility(&field.vis);
505                        s.print_type(&field.ty)
506                    });
507                    self.pclose();
508                }
509                self.print_where_clause(&generics.where_clause);
510                if print_finalizer {
511                    self.word(";");
512                }
513                self.end(ib);
514                self.end(cb);
515            }
516            ast::VariantData::Struct { fields, .. } => {
517                self.print_where_clause(&generics.where_clause);
518                self.nbsp();
519                self.bopen(ib);
520
521                let empty = fields.is_empty();
522                if !empty {
523                    self.hardbreak_if_not_bol();
524
525                    for field in fields {
526                        self.hardbreak_if_not_bol();
527                        self.maybe_print_comment(field.span.lo());
528                        self.print_outer_attributes(&field.attrs);
529                        self.print_visibility(&field.vis);
530                        self.print_ident(field.ident.unwrap());
531                        self.word_nbsp(":");
532                        self.print_type(&field.ty);
533                        self.word(",");
534                    }
535                }
536
537                self.bclose(span, empty, cb);
538            }
539        }
540    }
541
542    pub(crate) fn print_variant(&mut self, v: &ast::Variant) {
543        let (cb, ib) = self.head("");
544        self.print_visibility(&v.vis);
545        let generics = ast::Generics::default();
546        self.print_struct(&v.data, &generics, v.ident, v.span, false, cb, ib);
547        if let Some(d) = &v.disr_expr {
548            self.space();
549            self.word_space("=");
550            self.print_expr(&d.value, FixupContext::default())
551        }
552    }
553
554    pub(crate) fn print_assoc_item(&mut self, item: &ast::AssocItem) {
555        let ast::Item { id, span, ref attrs, ref kind, ref vis, tokens: _ } = *item;
556        self.ann.pre(self, AnnNode::SubItem(id));
557        self.hardbreak_if_not_bol();
558        self.maybe_print_comment(span.lo());
559        self.print_outer_attributes(attrs);
560        match kind {
561            ast::AssocItemKind::Fn(func) => {
562                self.print_fn_full(vis, attrs, &*func);
563            }
564            ast::AssocItemKind::Const(box ast::ConstItem {
565                defaultness,
566                ident,
567                generics,
568                ty,
569                expr,
570                define_opaque,
571            }) => {
572                self.print_item_const(
573                    *ident,
574                    None,
575                    generics,
576                    ty,
577                    expr.as_deref(),
578                    vis,
579                    ast::Safety::Default,
580                    *defaultness,
581                    define_opaque.as_deref(),
582                );
583            }
584            ast::AssocItemKind::Type(box ast::TyAlias {
585                defaultness,
586                ident,
587                generics,
588                after_where_clause,
589                bounds,
590                ty,
591            }) => {
592                self.print_associated_type(
593                    *ident,
594                    generics,
595                    after_where_clause,
596                    bounds,
597                    ty.as_deref(),
598                    vis,
599                    *defaultness,
600                );
601            }
602            ast::AssocItemKind::MacCall(m) => {
603                self.print_mac(m);
604                if m.args.need_semicolon() {
605                    self.word(";");
606                }
607            }
608            ast::AssocItemKind::Delegation(deleg) => self.print_delegation(
609                &item.attrs,
610                vis,
611                &deleg.qself,
612                &deleg.path,
613                DelegationKind::Single,
614                &deleg.body,
615            ),
616            ast::AssocItemKind::DelegationMac(deleg) => self.print_delegation(
617                &item.attrs,
618                vis,
619                &deleg.qself,
620                &deleg.prefix,
621                deleg.suffixes.as_ref().map_or(DelegationKind::Glob, |s| DelegationKind::List(s)),
622                &deleg.body,
623            ),
624        }
625        self.ann.post(self, AnnNode::SubItem(id))
626    }
627
628    fn print_delegation(
629        &mut self,
630        attrs: &[ast::Attribute],
631        vis: &ast::Visibility,
632        qself: &Option<Box<ast::QSelf>>,
633        path: &ast::Path,
634        kind: DelegationKind<'_>,
635        body: &Option<Box<ast::Block>>,
636    ) {
637        let body_cb_ib = body.as_ref().map(|body| (body, self.head("")));
638        self.print_visibility(vis);
639        self.word_nbsp("reuse");
640
641        if let Some(qself) = qself {
642            self.print_qpath(path, qself, false);
643        } else {
644            self.print_path(path, false, 0);
645        }
646        match kind {
647            DelegationKind::Single => {}
648            DelegationKind::List(suffixes) => {
649                self.word("::");
650                self.word("{");
651                for (i, (ident, rename)) in suffixes.iter().enumerate() {
652                    self.print_ident(*ident);
653                    if let Some(rename) = rename {
654                        self.nbsp();
655                        self.word_nbsp("as");
656                        self.print_ident(*rename);
657                    }
658                    if i != suffixes.len() - 1 {
659                        self.word_space(",");
660                    }
661                }
662                self.word("}");
663            }
664            DelegationKind::Glob => {
665                self.word("::");
666                self.word("*");
667            }
668        }
669        if let Some((body, (cb, ib))) = body_cb_ib {
670            self.nbsp();
671            self.print_block_with_attrs(body, attrs, cb, ib);
672        } else {
673            self.word(";");
674        }
675    }
676
677    fn print_fn_full(&mut self, vis: &ast::Visibility, attrs: &[ast::Attribute], func: &ast::Fn) {
678        let ast::Fn { defaultness, ident, generics, sig, contract, body, define_opaque } = func;
679
680        self.print_define_opaques(define_opaque.as_deref());
681
682        let body_cb_ib = body.as_ref().map(|body| (body, self.head("")));
683
684        self.print_visibility(vis);
685        self.print_defaultness(*defaultness);
686        self.print_fn(&sig.decl, sig.header, Some(*ident), generics);
687        if let Some(contract) = &contract {
688            self.nbsp();
689            self.print_contract(contract);
690        }
691        if let Some((body, (cb, ib))) = body_cb_ib {
692            if self.is_sdylib_interface {
693                self.word(";");
694                self.end(ib); // end inner head-block
695                self.end(cb); // end outer head-block
696                return;
697            }
698
699            self.nbsp();
700            self.print_block_with_attrs(body, attrs, cb, ib);
701        } else {
702            self.word(";");
703        }
704    }
705
706    fn print_define_opaques(&mut self, define_opaque: Option<&[(ast::NodeId, ast::Path)]>) {
707        if let Some(define_opaque) = define_opaque {
708            self.word("#[define_opaque(");
709            for (i, (_, path)) in define_opaque.iter().enumerate() {
710                if i != 0 {
711                    self.word_space(",");
712                }
713
714                self.print_path(path, false, 0);
715            }
716            self.word(")]");
717        }
718        self.hardbreak_if_not_bol();
719    }
720
721    fn print_contract(&mut self, contract: &ast::FnContract) {
722        if let Some(pred) = &contract.requires {
723            self.word("rustc_requires");
724            self.popen();
725            self.print_expr(pred, FixupContext::default());
726            self.pclose();
727        }
728        if let Some(pred) = &contract.ensures {
729            self.word("rustc_ensures");
730            self.popen();
731            self.print_expr(pred, FixupContext::default());
732            self.pclose();
733        }
734    }
735
736    pub(crate) fn print_fn(
737        &mut self,
738        decl: &ast::FnDecl,
739        header: ast::FnHeader,
740        ident: Option<Ident>,
741        generics: &ast::Generics,
742    ) {
743        self.print_fn_header_info(header);
744        if let Some(ident) = ident {
745            self.nbsp();
746            self.print_ident(ident);
747        }
748        self.print_generic_params(&generics.params);
749        self.print_fn_params_and_ret(decl, false);
750        self.print_where_clause(&generics.where_clause);
751    }
752
753    pub(crate) fn print_fn_params_and_ret(&mut self, decl: &ast::FnDecl, is_closure: bool) {
754        let (open, close) = if is_closure { ("|", "|") } else { ("(", ")") };
755        self.word(open);
756        self.commasep(Inconsistent, &decl.inputs, |s, param| s.print_param(param, is_closure));
757        self.word(close);
758        self.print_fn_ret_ty(&decl.output)
759    }
760
761    fn print_where_clause(&mut self, where_clause: &ast::WhereClause) {
762        let ast::WhereClause { has_where_token, ref predicates, span: _ } = *where_clause;
763        if predicates.is_empty() && !has_where_token {
764            return;
765        }
766
767        self.space();
768        self.word_space("where");
769
770        for (i, predicate) in predicates.iter().enumerate() {
771            if i != 0 {
772                self.word_space(",");
773            }
774
775            self.print_where_predicate(predicate);
776        }
777    }
778
779    pub fn print_where_predicate(&mut self, predicate: &ast::WherePredicate) {
780        let ast::WherePredicate { attrs, kind, id: _, span: _, is_placeholder: _ } = predicate;
781        self.print_outer_attributes(attrs);
782        match kind {
783            ast::WherePredicateKind::BoundPredicate(where_bound_predicate) => {
784                self.print_where_bound_predicate(where_bound_predicate);
785            }
786            ast::WherePredicateKind::RegionPredicate(ast::WhereRegionPredicate {
787                lifetime,
788                bounds,
789                ..
790            }) => {
791                self.print_lifetime(*lifetime);
792                self.word(":");
793                if !bounds.is_empty() {
794                    self.nbsp();
795                    self.print_lifetime_bounds(bounds);
796                }
797            }
798            ast::WherePredicateKind::EqPredicate(ast::WhereEqPredicate {
799                lhs_ty, rhs_ty, ..
800            }) => {
801                self.print_type(lhs_ty);
802                self.space();
803                self.word_space("=");
804                self.print_type(rhs_ty);
805            }
806        }
807    }
808
809    pub(crate) fn print_where_bound_predicate(
810        &mut self,
811        where_bound_predicate: &ast::WhereBoundPredicate,
812    ) {
813        self.print_formal_generic_params(&where_bound_predicate.bound_generic_params);
814        self.print_type(&where_bound_predicate.bounded_ty);
815        self.word(":");
816        if !where_bound_predicate.bounds.is_empty() {
817            self.nbsp();
818            self.print_type_bounds(&where_bound_predicate.bounds);
819        }
820    }
821
822    fn print_use_tree(&mut self, tree: &ast::UseTree) {
823        match &tree.kind {
824            ast::UseTreeKind::Simple(rename) => {
825                self.print_path(&tree.prefix, false, 0);
826                if let &Some(rename) = rename {
827                    self.nbsp();
828                    self.word_nbsp("as");
829                    self.print_ident(rename);
830                }
831            }
832            ast::UseTreeKind::Glob => {
833                if !tree.prefix.segments.is_empty() {
834                    self.print_path(&tree.prefix, false, 0);
835                    self.word("::");
836                }
837                self.word("*");
838            }
839            ast::UseTreeKind::Nested { items, .. } => {
840                if !tree.prefix.segments.is_empty() {
841                    self.print_path(&tree.prefix, false, 0);
842                    self.word("::");
843                }
844                if items.is_empty() {
845                    self.word("{}");
846                } else if let [(item, _)] = items.as_slice() {
847                    self.print_use_tree(item);
848                } else {
849                    let cb = self.cbox(INDENT_UNIT);
850                    self.word("{");
851                    self.zerobreak();
852                    let ib = self.ibox(0);
853                    for (pos, use_tree) in items.iter().with_position() {
854                        let is_last = matches!(pos, Position::Last | Position::Only);
855                        self.print_use_tree(&use_tree.0);
856                        if !is_last {
857                            self.word(",");
858                            if let ast::UseTreeKind::Nested { .. } = use_tree.0.kind {
859                                self.hardbreak();
860                            } else {
861                                self.space();
862                            }
863                        }
864                    }
865                    self.end(ib);
866                    self.trailing_comma();
867                    self.offset(-INDENT_UNIT);
868                    self.word("}");
869                    self.end(cb);
870                }
871            }
872        }
873    }
874}