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(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(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(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(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(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(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(ast::Trait {
374                impl_restriction,
375                constness,
376                safety,
377                is_auto,
378                ident,
379                generics,
380                bounds,
381                items,
382            }) => {
383                let (cb, ib) = self.head("");
384                self.print_visibility(&item.vis);
385                self.print_impl_restriction(impl_restriction);
386                self.print_constness(*constness);
387                self.print_safety(*safety);
388                self.print_is_auto(*is_auto);
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(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    pub(crate) fn print_mut_restriction(&mut self, mut_restriction: &ast::MutRestriction) {
515        match &mut_restriction.kind {
516            ast::RestrictionKind::Restricted { path, shorthand, .. } => {
517                let path = Self::to_string(|s| s.print_path(path, false, 0));
518                if *shorthand {
519                    self.word_nbsp(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("mut({0})", path))
    })format!("mut({path})"))
520                } else {
521                    self.word_nbsp(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("mut(in {0})", path))
    })format!("mut(in {path})"))
522                }
523            }
524            ast::RestrictionKind::Unrestricted => {}
525        }
526    }
527
528    fn print_defaultness(&mut self, defaultness: ast::Defaultness) {
529        if let ast::Defaultness::Default(_) = defaultness {
530            self.word_nbsp("default");
531        }
532    }
533
534    fn print_struct(
535        &mut self,
536        struct_def: &ast::VariantData,
537        generics: &ast::Generics,
538        ident: Ident,
539        span: rustc_span::Span,
540        print_finalizer: bool,
541        cb: BoxMarker,
542        ib: BoxMarker,
543    ) {
544        self.print_ident(ident);
545        self.print_generic_params(&generics.params);
546        match &struct_def {
547            ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
548                if let ast::VariantData::Tuple(..) = struct_def {
549                    self.popen();
550                    self.commasep(Inconsistent, struct_def.fields(), |s, field| {
551                        s.maybe_print_comment(field.span.lo());
552                        s.print_outer_attributes(&field.attrs);
553                        s.print_visibility(&field.vis);
554                        s.print_mut_restriction(&field.mut_restriction);
555                        s.print_type(&field.ty)
556                    });
557                    self.pclose();
558                }
559                self.print_where_clause(&generics.where_clause);
560                if print_finalizer {
561                    self.word(";");
562                }
563                self.end(ib);
564                self.end(cb);
565            }
566            ast::VariantData::Struct { fields, .. } => {
567                self.print_where_clause(&generics.where_clause);
568                self.nbsp();
569                self.bopen(ib);
570
571                let empty = fields.is_empty();
572                if !empty {
573                    self.hardbreak_if_not_bol();
574
575                    for field in fields {
576                        self.hardbreak_if_not_bol();
577                        self.maybe_print_comment(field.span.lo());
578                        self.print_outer_attributes(&field.attrs);
579                        self.print_visibility(&field.vis);
580                        self.print_mut_restriction(&field.mut_restriction);
581                        self.print_ident(field.ident.unwrap());
582                        self.word_nbsp(":");
583                        self.print_type(&field.ty);
584                        self.word(",");
585                    }
586                }
587
588                self.bclose(span, empty, cb);
589            }
590        }
591    }
592
593    pub(crate) fn print_variant(&mut self, v: &ast::Variant) {
594        let (cb, ib) = self.head("");
595        self.print_visibility(&v.vis);
596        let generics = ast::Generics::default();
597        self.print_struct(&v.data, &generics, v.ident, v.span, false, cb, ib);
598        if let Some(d) = &v.disr_expr {
599            self.space();
600            self.word_space("=");
601            self.print_expr(&d.value, FixupContext::default())
602        }
603    }
604
605    pub(crate) fn print_assoc_item(&mut self, item: &ast::AssocItem) {
606        let ast::Item { id, span, ref attrs, ref kind, ref vis, tokens: _ } = *item;
607        self.ann.pre(self, AnnNode::SubItem(id));
608        self.hardbreak_if_not_bol();
609        self.maybe_print_comment(span.lo());
610        self.print_outer_attributes(attrs);
611        match kind {
612            ast::AssocItemKind::Fn(func) => {
613                self.print_fn_full(vis, attrs, &*func);
614            }
615            ast::AssocItemKind::Const(ast::ConstItem {
616                defaultness,
617                ident,
618                generics,
619                ty,
620                rhs_kind,
621                define_opaque,
622            }) => {
623                self.print_item_const(
624                    *ident,
625                    None,
626                    generics,
627                    ty,
628                    rhs_kind.expr(),
629                    vis,
630                    ast::Safety::Default,
631                    *defaultness,
632                    define_opaque.as_deref(),
633                    &[],
634                );
635            }
636            ast::AssocItemKind::Type(ast::TyAlias {
637                defaultness,
638                ident,
639                generics,
640                after_where_clause,
641                bounds,
642                ty,
643            }) => {
644                self.print_associated_type(
645                    *ident,
646                    generics,
647                    after_where_clause,
648                    bounds,
649                    ty.as_deref(),
650                    vis,
651                    *defaultness,
652                );
653            }
654            ast::AssocItemKind::MacCall(m) => {
655                self.print_mac(m);
656                if m.args.need_semicolon() {
657                    self.word(";");
658                }
659            }
660            ast::AssocItemKind::Delegation(deleg) => self.print_delegation(
661                &item.attrs,
662                vis,
663                &deleg.qself,
664                &deleg.path,
665                DelegationKind::Single,
666                &deleg.body,
667            ),
668            ast::AssocItemKind::DelegationMac(deleg) => self.print_delegation(
669                &item.attrs,
670                vis,
671                &deleg.qself,
672                &deleg.prefix,
673                match &deleg.suffixes {
674                    ast::DelegationSuffixes::List(s) => DelegationKind::List(s),
675                    ast::DelegationSuffixes::Glob(_) => DelegationKind::Glob,
676                },
677                &deleg.body,
678            ),
679        }
680        self.ann.post(self, AnnNode::SubItem(id))
681    }
682
683    fn print_delegation(
684        &mut self,
685        attrs: &[ast::Attribute],
686        vis: &ast::Visibility,
687        qself: &Option<Box<ast::QSelf>>,
688        path: &ast::Path,
689        kind: DelegationKind<'_>,
690        body: &Option<Box<ast::Block>>,
691    ) {
692        let body_cb_ib = body.as_ref().map(|body| (body, self.head("")));
693        self.print_visibility(vis);
694        self.word_nbsp("reuse");
695
696        if let Some(qself) = qself {
697            self.print_qpath(path, qself, false);
698        } else {
699            self.print_path(path, false, 0);
700        }
701        match kind {
702            DelegationKind::Single => {}
703            DelegationKind::List(suffixes) => {
704                self.word("::");
705                self.word("{");
706                for (i, (ident, rename)) in suffixes.iter().enumerate() {
707                    self.print_ident(*ident);
708                    if let Some(rename) = rename {
709                        self.nbsp();
710                        self.word_nbsp("as");
711                        self.print_ident(*rename);
712                    }
713                    if i != suffixes.len() - 1 {
714                        self.word_space(",");
715                    }
716                }
717                self.word("}");
718            }
719            DelegationKind::Glob => {
720                self.word("::");
721                self.word("*");
722            }
723        }
724        if let Some((body, (cb, ib))) = body_cb_ib {
725            self.nbsp();
726            self.print_block_with_attrs(body, attrs, cb, ib);
727        } else {
728            self.word(";");
729        }
730    }
731
732    fn print_fn_full(&mut self, vis: &ast::Visibility, attrs: &[ast::Attribute], func: &ast::Fn) {
733        let ast::Fn { defaultness, ident, generics, sig, contract, body, define_opaque, eii_impls } =
734            func;
735
736        self.print_define_opaques(define_opaque.as_deref());
737
738        for eii_impl in eii_impls {
739            self.print_eii_impl(eii_impl);
740        }
741
742        let body_cb_ib = body.as_ref().map(|body| (body, self.head("")));
743
744        self.print_visibility(vis);
745        self.print_defaultness(*defaultness);
746        self.print_fn(&sig.decl, sig.header, Some(*ident), generics);
747        if let Some(contract) = &contract {
748            self.nbsp();
749            self.print_contract(contract);
750        }
751        if let Some((body, (cb, ib))) = body_cb_ib {
752            if self.is_sdylib_interface {
753                self.word(";");
754                self.end(ib); // end inner head-block
755                self.end(cb); // end outer head-block
756                return;
757            }
758
759            self.nbsp();
760            self.print_block_with_attrs(body, attrs, cb, ib);
761        } else {
762            self.word(";");
763        }
764    }
765
766    fn print_eii_impl(&mut self, eii: &ast::EiiImpl) {
767        self.word("#[");
768        if let Safety::Unsafe(..) = eii.impl_safety {
769            self.word("unsafe");
770            self.popen();
771        }
772        self.print_path(&eii.eii_macro_path, false, 0);
773        if let Safety::Unsafe(..) = eii.impl_safety {
774            self.pclose();
775        }
776        self.word("]");
777        self.hardbreak();
778    }
779
780    fn print_define_opaques(&mut self, define_opaque: Option<&[(ast::NodeId, ast::Path)]>) {
781        if let Some(define_opaque) = define_opaque {
782            self.word("#[define_opaque(");
783            for (i, (_, path)) in define_opaque.iter().enumerate() {
784                if i != 0 {
785                    self.word_space(",");
786                }
787
788                self.print_path(path, false, 0);
789            }
790            self.word(")]");
791        }
792        self.hardbreak_if_not_bol();
793    }
794
795    fn print_contract(&mut self, contract: &ast::FnContract) {
796        if let Some(pred) = &contract.requires {
797            self.word("rustc_requires");
798            self.popen();
799            self.print_expr(pred, FixupContext::default());
800            self.pclose();
801        }
802        if let Some(pred) = &contract.ensures {
803            self.word("rustc_ensures");
804            self.popen();
805            self.print_expr(pred, FixupContext::default());
806            self.pclose();
807        }
808    }
809
810    pub(crate) fn print_fn(
811        &mut self,
812        decl: &ast::FnDecl,
813        header: ast::FnHeader,
814        ident: Option<Ident>,
815        generics: &ast::Generics,
816    ) {
817        self.print_fn_header_info(header);
818        if let Some(ident) = ident {
819            self.nbsp();
820            self.print_ident(ident);
821        }
822        self.print_generic_params(&generics.params);
823        self.print_fn_params_and_ret(decl, false);
824        self.print_where_clause(&generics.where_clause);
825    }
826
827    pub(crate) fn print_fn_params_and_ret(&mut self, decl: &ast::FnDecl, is_closure: bool) {
828        let (open, close) = if is_closure { ("|", "|") } else { ("(", ")") };
829        self.word(open);
830        self.commasep(Inconsistent, &decl.inputs, |s, param| s.print_param(param, is_closure));
831        self.word(close);
832        self.print_fn_ret_ty(&decl.output)
833    }
834
835    fn print_where_clause(&mut self, where_clause: &ast::WhereClause) {
836        let ast::WhereClause { has_where_token, ref predicates, span: _ } = *where_clause;
837        if predicates.is_empty() && !has_where_token {
838            return;
839        }
840
841        self.space();
842        self.word_space("where");
843
844        for (i, predicate) in predicates.iter().enumerate() {
845            if i != 0 {
846                self.word_space(",");
847            }
848
849            self.print_where_predicate(predicate);
850        }
851    }
852
853    pub fn print_where_predicate(&mut self, predicate: &ast::WherePredicate) {
854        let ast::WherePredicate { attrs, kind, id: _, span: _, is_placeholder: _ } = predicate;
855        self.print_outer_attributes(attrs);
856        match kind {
857            ast::WherePredicateKind::BoundPredicate(where_bound_predicate) => {
858                self.print_where_bound_predicate(where_bound_predicate);
859            }
860            ast::WherePredicateKind::RegionPredicate(ast::WhereRegionPredicate {
861                lifetime,
862                bounds,
863                ..
864            }) => {
865                self.print_lifetime(*lifetime);
866                self.word(":");
867                if !bounds.is_empty() {
868                    self.nbsp();
869                    self.print_lifetime_bounds(bounds);
870                }
871            }
872            ast::WherePredicateKind::EqPredicate(ast::WhereEqPredicate {
873                lhs_ty, rhs_ty, ..
874            }) => {
875                self.print_type(lhs_ty);
876                self.space();
877                self.word_space("=");
878                self.print_type(rhs_ty);
879            }
880        }
881    }
882
883    pub(crate) fn print_where_bound_predicate(
884        &mut self,
885        where_bound_predicate: &ast::WhereBoundPredicate,
886    ) {
887        self.print_formal_generic_params(&where_bound_predicate.bound_generic_params);
888        self.print_type(&where_bound_predicate.bounded_ty);
889        self.word(":");
890        if !where_bound_predicate.bounds.is_empty() {
891            self.nbsp();
892            self.print_type_bounds(&where_bound_predicate.bounds);
893        }
894    }
895
896    fn print_use_tree(&mut self, tree: &ast::UseTree) {
897        match &tree.kind {
898            ast::UseTreeKind::Simple(rename) => {
899                self.print_path(&tree.prefix, false, 0);
900                if let &Some(rename) = rename {
901                    self.nbsp();
902                    self.word_nbsp("as");
903                    self.print_ident(rename);
904                }
905            }
906            ast::UseTreeKind::Glob(_) => {
907                if !tree.prefix.segments.is_empty() {
908                    self.print_path(&tree.prefix, false, 0);
909                    self.word("::");
910                }
911                self.word("*");
912            }
913            ast::UseTreeKind::Nested { items, .. } => {
914                if !tree.prefix.segments.is_empty() {
915                    self.print_path(&tree.prefix, false, 0);
916                    self.word("::");
917                }
918                if items.is_empty() {
919                    self.word("{}");
920                } else if let [(item, _)] = items.as_slice()
921                    && !item
922                        .prefix
923                        .segments
924                        .first()
925                        .is_some_and(|seg| seg.ident.name == rustc_span::symbol::kw::SelfLower)
926                {
927                    self.print_use_tree(item);
928                } else {
929                    let cb = self.cbox(INDENT_UNIT);
930                    self.word("{");
931                    self.zerobreak();
932                    let ib = self.ibox(0);
933                    for (pos, use_tree) in items.iter().with_position() {
934                        let is_last = #[allow(non_exhaustive_omitted_patterns)] match pos {
    Position::Last | Position::Only => true,
    _ => false,
}matches!(pos, Position::Last | Position::Only);
935                        self.print_use_tree(&use_tree.0);
936                        if !is_last {
937                            self.word(",");
938                            if let ast::UseTreeKind::Nested { .. } = use_tree.0.kind {
939                                self.hardbreak();
940                            } else {
941                                self.space();
942                            }
943                        }
944                    }
945                    self.end(ib);
946                    self.trailing_comma();
947                    self.offset(-INDENT_UNIT);
948                    self.word("}");
949                    self.end(cb);
950                }
951            }
952        }
953    }
954}