Skip to main content

rustc_ast_pretty/pprust/state/
item.rs

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