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