1 use crate::pp
::Breaks
::Inconsistent
;
2 use crate::pprust
::state
::delimited
::IterDelimited
;
3 use crate::pprust
::state
::{AnnNode, PrintState, State, INDENT_UNIT}
;
6 use rustc_ast
::GenericBound
;
7 use rustc_ast
::ModKind
;
8 use rustc_span
::symbol
::Ident
;
10 fn visibility_qualified(vis
: &ast
::Visibility
, s
: &str) -> String
{
11 format
!("{}{}", State
::to_string(|s
| s
.print_visibility(vis
)), s
)
15 fn print_foreign_mod(&mut self, nmod
: &ast
::ForeignMod
, attrs
: &[ast
::Attribute
]) {
16 self.print_inner_attributes(attrs
);
17 for item
in &nmod
.items
{
18 self.print_foreign_item(item
);
22 pub(crate) fn print_foreign_item(&mut self, item
: &ast
::ForeignItem
) {
23 let ast
::Item { id, span, ident, ref attrs, ref kind, ref vis, tokens: _ }
= *item
;
24 self.ann
.pre(self, AnnNode
::SubItem(id
));
25 self.hardbreak_if_not_bol();
26 self.maybe_print_comment(span
.lo());
27 self.print_outer_attributes(attrs
);
29 ast
::ForeignItemKind
::Fn(box ast
::Fn { defaultness, sig, generics, body }
) => {
30 self.print_fn_full(sig
, ident
, generics
, vis
, *defaultness
, body
.as_deref(), attrs
);
32 ast
::ForeignItemKind
::Static(ty
, mutbl
, body
) => {
33 let def
= ast
::Defaultness
::Final
;
34 self.print_item_const(ident
, Some(*mutbl
), ty
, body
.as_deref(), vis
, def
);
36 ast
::ForeignItemKind
::TyAlias(box ast
::TyAlias
{
40 where_predicates_split
,
44 self.print_associated_type(
48 *where_predicates_split
,
55 ast
::ForeignItemKind
::MacCall(m
) => {
57 if m
.args
.need_semicolon() {
62 self.ann
.post(self, AnnNode
::SubItem(id
))
68 mutbl
: Option
<ast
::Mutability
>,
70 body
: Option
<&ast
::Expr
>,
71 vis
: &ast
::Visibility
,
72 defaultness
: ast
::Defaultness
,
75 self.print_visibility(vis
);
76 self.print_defaultness(defaultness
);
77 let leading
= match mutbl
{
79 Some(ast
::Mutability
::Not
) => "static",
80 Some(ast
::Mutability
::Mut
) => "static mut",
82 self.word_space(leading
);
83 self.print_ident(ident
);
89 self.end(); // end the head-ibox
90 if let Some(body
) = body
{
92 self.print_expr(body
);
95 self.end(); // end the outer cbox
98 fn print_associated_type(
101 generics
: &ast
::Generics
,
102 where_clauses
: (ast
::TyAliasWhereClause
, ast
::TyAliasWhereClause
),
103 where_predicates_split
: usize,
104 bounds
: &ast
::GenericBounds
,
105 ty
: Option
<&ast
::Ty
>,
106 vis
: &ast
::Visibility
,
107 defaultness
: ast
::Defaultness
,
109 let (before_predicates
, after_predicates
) =
110 generics
.where_clause
.predicates
.split_at(where_predicates_split
);
112 self.print_visibility(vis
);
113 self.print_defaultness(defaultness
);
114 self.word_space("type");
115 self.print_ident(ident
);
116 self.print_generic_params(&generics
.params
);
117 if !bounds
.is_empty() {
119 self.print_type_bounds(bounds
);
121 self.print_where_clause_parts(where_clauses
.0.0, before_predicates
);
122 if let Some(ty
) = ty
{
124 self.word_space("=");
127 self.print_where_clause_parts(where_clauses
.1.0, after_predicates
);
129 self.end(); // end inner head-block
130 self.end(); // end outer head-block
133 /// Pretty-prints an item.
134 pub(crate) fn print_item(&mut self, item
: &ast
::Item
) {
135 self.hardbreak_if_not_bol();
136 self.maybe_print_comment(item
.span
.lo());
137 self.print_outer_attributes(&item
.attrs
);
138 self.ann
.pre(self, AnnNode
::Item(item
));
140 ast
::ItemKind
::ExternCrate(orig_name
) => {
141 self.head(visibility_qualified(&item
.vis
, "extern crate"));
142 if let &Some(orig_name
) = orig_name
{
143 self.print_name(orig_name
);
148 self.print_ident(item
.ident
);
150 self.end(); // end inner head-block
151 self.end(); // end outer head-block
153 ast
::ItemKind
::Use(tree
) => {
154 self.print_visibility(&item
.vis
);
155 self.word_nbsp("use");
156 self.print_use_tree(tree
);
159 ast
::ItemKind
::Static(ty
, mutbl
, body
) => {
160 let def
= ast
::Defaultness
::Final
;
161 self.print_item_const(
170 ast
::ItemKind
::Const(def
, ty
, body
) => {
171 self.print_item_const(item
.ident
, None
, ty
, body
.as_deref(), &item
.vis
, *def
);
173 ast
::ItemKind
::Fn(box ast
::Fn { defaultness, sig, generics, body }
) => {
184 ast
::ItemKind
::Mod(unsafety
, mod_kind
) => {
185 self.head(Self::to_string(|s
| {
186 s
.print_visibility(&item
.vis
);
187 s
.print_unsafety(*unsafety
);
190 self.print_ident(item
.ident
);
193 ModKind
::Loaded(items
, ..) => {
196 self.print_inner_attributes(&item
.attrs
);
198 self.print_item(item
);
200 let empty
= item
.attrs
.is_empty() && items
.is_empty();
201 self.bclose(item
.span
, empty
);
203 ModKind
::Unloaded
=> {
205 self.end(); // end inner head-block
206 self.end(); // end outer head-block
210 ast
::ItemKind
::ForeignMod(nmod
) => {
211 self.head(Self::to_string(|s
| {
212 s
.print_unsafety(nmod
.unsafety
);
215 if let Some(abi
) = nmod
.abi
{
216 self.print_token_literal(abi
.as_token_lit(), abi
.span
);
220 self.print_foreign_mod(nmod
, &item
.attrs
);
221 let empty
= item
.attrs
.is_empty() && nmod
.items
.is_empty();
222 self.bclose(item
.span
, empty
);
224 ast
::ItemKind
::GlobalAsm(asm
) => {
225 self.head(visibility_qualified(&item
.vis
, "global_asm!"));
226 self.print_inline_asm(asm
);
231 ast
::ItemKind
::TyAlias(box ast
::TyAlias
{
235 where_predicates_split
,
239 self.print_associated_type(
243 *where_predicates_split
,
250 ast
::ItemKind
::Enum(enum_definition
, params
) => {
251 self.print_enum_def(enum_definition
, params
, item
.ident
, item
.span
, &item
.vis
);
253 ast
::ItemKind
::Struct(struct_def
, generics
) => {
254 self.head(visibility_qualified(&item
.vis
, "struct"));
255 self.print_struct(struct_def
, generics
, item
.ident
, item
.span
, true);
257 ast
::ItemKind
::Union(struct_def
, generics
) => {
258 self.head(visibility_qualified(&item
.vis
, "union"));
259 self.print_struct(struct_def
, generics
, item
.ident
, item
.span
, true);
261 ast
::ItemKind
::Impl(box ast
::Impl
{
272 self.print_visibility(&item
.vis
);
273 self.print_defaultness(*defaultness
);
274 self.print_unsafety(*unsafety
);
277 if generics
.params
.is_empty() {
280 self.print_generic_params(&generics
.params
);
284 self.print_constness(*constness
);
286 if let ast
::ImplPolarity
::Negative(_
) = polarity
{
290 if let Some(t
) = of_trait
{
291 self.print_trait_ref(t
);
293 self.word_space("for");
296 self.print_type(self_ty
);
297 self.print_where_clause(&generics
.where_clause
);
301 self.print_inner_attributes(&item
.attrs
);
302 for impl_item
in items
{
303 self.print_assoc_item(impl_item
);
305 let empty
= item
.attrs
.is_empty() && items
.is_empty();
306 self.bclose(item
.span
, empty
);
308 ast
::ItemKind
::Trait(box ast
::Trait
{
317 self.print_visibility(&item
.vis
);
318 self.print_unsafety(*unsafety
);
319 self.print_is_auto(*is_auto
);
320 self.word_nbsp("trait");
321 self.print_ident(item
.ident
);
322 self.print_generic_params(&generics
.params
);
323 let mut real_bounds
= Vec
::with_capacity(bounds
.len());
324 for b
in bounds
.iter() {
325 if let GenericBound
::Trait(ptr
, ast
::TraitBoundModifier
::Maybe
) = b
{
327 self.word_space("for ?");
328 self.print_trait_ref(&ptr
.trait_ref
);
330 real_bounds
.push(b
.clone());
333 if !real_bounds
.is_empty() {
335 self.print_type_bounds(&real_bounds
);
337 self.print_where_clause(&generics
.where_clause
);
340 self.print_inner_attributes(&item
.attrs
);
341 for trait_item
in items
{
342 self.print_assoc_item(trait_item
);
344 let empty
= item
.attrs
.is_empty() && items
.is_empty();
345 self.bclose(item
.span
, empty
);
347 ast
::ItemKind
::TraitAlias(generics
, bounds
) => {
348 self.head(visibility_qualified(&item
.vis
, "trait"));
349 self.print_ident(item
.ident
);
350 self.print_generic_params(&generics
.params
);
352 if !bounds
.is_empty() {
354 self.print_type_bounds(&bounds
);
356 self.print_where_clause(&generics
.where_clause
);
358 self.end(); // end inner head-block
359 self.end(); // end outer head-block
361 ast
::ItemKind
::MacCall(mac
) => {
363 if mac
.args
.need_semicolon() {
367 ast
::ItemKind
::MacroDef(macro_def
) => {
368 self.print_mac_def(macro_def
, &item
.ident
, item
.span
, |state
| {
369 state
.print_visibility(&item
.vis
)
373 self.ann
.post(self, AnnNode
::Item(item
))
378 enum_definition
: &ast
::EnumDef
,
379 generics
: &ast
::Generics
,
381 span
: rustc_span
::Span
,
382 visibility
: &ast
::Visibility
,
384 self.head(visibility_qualified(visibility
, "enum"));
385 self.print_ident(ident
);
386 self.print_generic_params(&generics
.params
);
387 self.print_where_clause(&generics
.where_clause
);
389 self.print_variants(&enum_definition
.variants
, span
)
392 fn print_variants(&mut self, variants
: &[ast
::Variant
], span
: rustc_span
::Span
) {
395 self.space_if_not_bol();
396 self.maybe_print_comment(v
.span
.lo());
397 self.print_outer_attributes(&v
.attrs
);
399 self.print_variant(v
);
402 self.maybe_print_trailing_comment(v
.span
, None
);
404 let empty
= variants
.is_empty();
405 self.bclose(span
, empty
)
408 pub(crate) fn print_visibility(&mut self, vis
: &ast
::Visibility
) {
410 ast
::VisibilityKind
::Public
=> self.word_nbsp("pub"),
411 ast
::VisibilityKind
::Restricted { path, shorthand, .. }
=> {
412 let path
= Self::to_string(|s
| s
.print_path(path
, false, 0));
413 if *shorthand
&& (path
== "crate" || path
== "self" || path
== "super") {
414 self.word_nbsp(format
!("pub({path})"))
416 self.word_nbsp(format
!("pub(in {path})"))
419 ast
::VisibilityKind
::Inherited
=> {}
423 fn print_defaultness(&mut self, defaultness
: ast
::Defaultness
) {
424 if let ast
::Defaultness
::Default(_
) = defaultness
{
425 self.word_nbsp("default");
429 fn print_record_struct_body(&mut self, fields
: &[ast
::FieldDef
], span
: rustc_span
::Span
) {
433 let empty
= fields
.is_empty();
435 self.hardbreak_if_not_bol();
437 for field
in fields
{
438 self.hardbreak_if_not_bol();
439 self.maybe_print_comment(field
.span
.lo());
440 self.print_outer_attributes(&field
.attrs
);
441 self.print_visibility(&field
.vis
);
442 self.print_ident(field
.ident
.unwrap());
444 self.print_type(&field
.ty
);
449 self.bclose(span
, empty
);
454 struct_def
: &ast
::VariantData
,
455 generics
: &ast
::Generics
,
457 span
: rustc_span
::Span
,
458 print_finalizer
: bool
,
460 self.print_ident(ident
);
461 self.print_generic_params(&generics
.params
);
463 ast
::VariantData
::Tuple(..) | ast
::VariantData
::Unit(..) => {
464 if let ast
::VariantData
::Tuple(..) = struct_def
{
466 self.commasep(Inconsistent
, struct_def
.fields(), |s
, field
| {
467 s
.maybe_print_comment(field
.span
.lo());
468 s
.print_outer_attributes(&field
.attrs
);
469 s
.print_visibility(&field
.vis
);
470 s
.print_type(&field
.ty
)
474 self.print_where_clause(&generics
.where_clause
);
479 self.end(); // Close the outer-box.
481 ast
::VariantData
::Struct(fields
, ..) => {
482 self.print_where_clause(&generics
.where_clause
);
483 self.print_record_struct_body(fields
, span
);
488 pub(crate) fn print_variant(&mut self, v
: &ast
::Variant
) {
490 self.print_visibility(&v
.vis
);
491 let generics
= ast
::Generics
::default();
492 self.print_struct(&v
.data
, &generics
, v
.ident
, v
.span
, false);
493 if let Some(d
) = &v
.disr_expr
{
495 self.word_space("=");
496 self.print_expr(&d
.value
)
500 pub(crate) fn print_assoc_item(&mut self, item
: &ast
::AssocItem
) {
501 let ast
::Item { id, span, ident, ref attrs, ref kind, ref vis, tokens: _ }
= *item
;
502 self.ann
.pre(self, AnnNode
::SubItem(id
));
503 self.hardbreak_if_not_bol();
504 self.maybe_print_comment(span
.lo());
505 self.print_outer_attributes(attrs
);
507 ast
::AssocItemKind
::Fn(box ast
::Fn { defaultness, sig, generics, body }
) => {
508 self.print_fn_full(sig
, ident
, generics
, vis
, *defaultness
, body
.as_deref(), attrs
);
510 ast
::AssocItemKind
::Const(def
, ty
, body
) => {
511 self.print_item_const(ident
, None
, ty
, body
.as_deref(), vis
, *def
);
513 ast
::AssocItemKind
::Type(box ast
::TyAlias
{
517 where_predicates_split
,
521 self.print_associated_type(
525 *where_predicates_split
,
532 ast
::AssocItemKind
::MacCall(m
) => {
534 if m
.args
.need_semicolon() {
539 self.ann
.post(self, AnnNode
::SubItem(id
))
546 generics
: &ast
::Generics
,
547 vis
: &ast
::Visibility
,
548 defaultness
: ast
::Defaultness
,
549 body
: Option
<&ast
::Block
>,
550 attrs
: &[ast
::Attribute
],
555 self.print_visibility(vis
);
556 self.print_defaultness(defaultness
);
557 self.print_fn(&sig
.decl
, sig
.header
, Some(name
), generics
);
558 if let Some(body
) = body
{
560 self.print_block_with_attrs(body
, attrs
);
566 pub(crate) fn print_fn(
569 header
: ast
::FnHeader
,
571 generics
: &ast
::Generics
,
573 self.print_fn_header_info(header
);
574 if let Some(name
) = name
{
576 self.print_ident(name
);
578 self.print_generic_params(&generics
.params
);
579 self.print_fn_params_and_ret(decl
, false);
580 self.print_where_clause(&generics
.where_clause
)
583 pub(crate) fn print_fn_params_and_ret(&mut self, decl
: &ast
::FnDecl
, is_closure
: bool
) {
584 let (open
, close
) = if is_closure { ("|", "|") }
else { ("(", ")") }
;
586 self.commasep(Inconsistent
, &decl
.inputs
, |s
, param
| s
.print_param(param
, is_closure
));
588 self.print_fn_ret_ty(&decl
.output
)
591 fn print_where_clause(&mut self, where_clause
: &ast
::WhereClause
) {
592 self.print_where_clause_parts(where_clause
.has_where_token
, &where_clause
.predicates
);
595 pub(crate) fn print_where_clause_parts(
597 has_where_token
: bool
,
598 predicates
: &[ast
::WherePredicate
],
600 if predicates
.is_empty() && !has_where_token
{
605 self.word_space("where");
607 for (i
, predicate
) in predicates
.iter().enumerate() {
609 self.word_space(",");
612 self.print_where_predicate(predicate
);
616 pub fn print_where_predicate(&mut self, predicate
: &ast
::WherePredicate
) {
618 ast
::WherePredicate
::BoundPredicate(ast
::WhereBoundPredicate
{
619 bound_generic_params
,
624 self.print_formal_generic_params(bound_generic_params
);
625 self.print_type(bounded_ty
);
627 if !bounds
.is_empty() {
629 self.print_type_bounds(bounds
);
632 ast
::WherePredicate
::RegionPredicate(ast
::WhereRegionPredicate
{
637 self.print_lifetime(*lifetime
);
639 if !bounds
.is_empty() {
641 self.print_lifetime_bounds(bounds
);
644 ast
::WherePredicate
::EqPredicate(ast
::WhereEqPredicate { lhs_ty, rhs_ty, .. }
) => {
645 self.print_type(lhs_ty
);
647 self.word_space("=");
648 self.print_type(rhs_ty
);
653 fn print_use_tree(&mut self, tree
: &ast
::UseTree
) {
655 ast
::UseTreeKind
::Simple(rename
) => {
656 self.print_path(&tree
.prefix
, false, 0);
657 if let &Some(rename
) = rename
{
659 self.word_nbsp("as");
660 self.print_ident(rename
);
663 ast
::UseTreeKind
::Glob
=> {
664 if !tree
.prefix
.segments
.is_empty() {
665 self.print_path(&tree
.prefix
, false, 0);
670 ast
::UseTreeKind
::Nested(items
) => {
671 if !tree
.prefix
.segments
.is_empty() {
672 self.print_path(&tree
.prefix
, false, 0);
675 if items
.is_empty() {
677 } else if items
.len() == 1 {
678 self.print_use_tree(&items
[0].0);
680 self.cbox(INDENT_UNIT
);
684 for use_tree
in items
.iter().delimited() {
685 self.print_use_tree(&use_tree
.0);
686 if !use_tree
.is_last
{
688 if let ast
::UseTreeKind
::Nested(_
) = use_tree
.0.kind
{
696 self.trailing_comma();
697 self.offset(-INDENT_UNIT
);