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 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 self.print_type_bounds(":", bounds);
118 self.print_where_clause_parts(where_clauses.0.0, before_predicates);
119 if let Some(ty) = ty {
121 self.word_space("=");
124 self.print_where_clause_parts(where_clauses.1.0, after_predicates);
126 self.end(); // end inner head-block
127 self.end(); // end outer head-block
130 /// Pretty-prints an item.
131 crate fn print_item(&mut self, item: &ast::Item) {
132 self.hardbreak_if_not_bol();
133 self.maybe_print_comment(item.span.lo());
134 self.print_outer_attributes(&item.attrs);
135 self.ann.pre(self, AnnNode::Item(item));
137 ast::ItemKind::ExternCrate(orig_name) => {
138 self.head(visibility_qualified(&item.vis, "extern crate"));
139 if let Some(orig_name) = orig_name {
140 self.print_name(orig_name);
145 self.print_ident(item.ident);
147 self.end(); // end inner head-block
148 self.end(); // end outer head-block
150 ast::ItemKind::Use(ref tree) => {
151 self.print_visibility(&item.vis);
152 self.word_nbsp("use");
153 self.print_use_tree(tree);
156 ast::ItemKind::Static(ref ty, mutbl, ref body) => {
157 let def = ast::Defaultness::Final;
158 self.print_item_const(item.ident, Some(mutbl), ty, body.as_deref(), &item.vis, def);
160 ast::ItemKind::Const(def, ref ty, ref body) => {
161 self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis, def);
163 ast::ItemKind::Fn(box ast::Fn { defaultness, ref sig, ref generics, ref body }) => {
164 let body = body.as_deref();
175 ast::ItemKind::Mod(unsafety, ref mod_kind) => {
176 self.head(Self::to_string(|s| {
177 s.print_visibility(&item.vis);
178 s.print_unsafety(unsafety);
181 self.print_ident(item.ident);
184 ModKind::Loaded(items, ..) => {
187 self.print_inner_attributes(&item.attrs);
189 self.print_item(item);
191 let empty = item.attrs.is_empty() && items.is_empty();
192 self.bclose(item.span, empty);
194 ModKind::Unloaded => {
196 self.end(); // end inner head-block
197 self.end(); // end outer head-block
201 ast::ItemKind::ForeignMod(ref nmod) => {
202 self.head(Self::to_string(|s| {
203 s.print_unsafety(nmod.unsafety);
206 if let Some(abi) = nmod.abi {
207 self.print_literal(&abi.as_lit());
211 self.print_foreign_mod(nmod, &item.attrs);
212 let empty = item.attrs.is_empty() && nmod.items.is_empty();
213 self.bclose(item.span, empty);
215 ast::ItemKind::GlobalAsm(ref asm) => {
216 self.head(visibility_qualified(&item.vis, "global_asm!"));
217 self.print_inline_asm(asm);
220 ast::ItemKind::TyAlias(box ast::TyAlias {
224 where_predicates_split,
228 let ty = ty.as_deref();
229 self.print_associated_type(
233 where_predicates_split,
240 ast::ItemKind::Enum(ref enum_definition, ref params) => {
241 self.print_enum_def(enum_definition, params, item.ident, item.span, &item.vis);
243 ast::ItemKind::Struct(ref struct_def, ref generics) => {
244 self.head(visibility_qualified(&item.vis, "struct"));
245 self.print_struct(struct_def, generics, item.ident, item.span, true);
247 ast::ItemKind::Union(ref struct_def, ref generics) => {
248 self.head(visibility_qualified(&item.vis, "union"));
249 self.print_struct(struct_def, generics, item.ident, item.span, true);
251 ast::ItemKind::Impl(box ast::Impl {
262 self.print_visibility(&item.vis);
263 self.print_defaultness(defaultness);
264 self.print_unsafety(unsafety);
267 if generics.params.is_empty() {
270 self.print_generic_params(&generics.params);
274 self.print_constness(constness);
276 if let ast::ImplPolarity::Negative(_) = polarity {
280 if let Some(ref t) = *of_trait {
281 self.print_trait_ref(t);
283 self.word_space("for");
286 self.print_type(self_ty);
287 self.print_where_clause(&generics.where_clause);
291 self.print_inner_attributes(&item.attrs);
292 for impl_item in items {
293 self.print_assoc_item(impl_item);
295 let empty = item.attrs.is_empty() && items.is_empty();
296 self.bclose(item.span, empty);
298 ast::ItemKind::Trait(box ast::Trait {
307 self.print_visibility(&item.vis);
308 self.print_unsafety(unsafety);
309 self.print_is_auto(is_auto);
310 self.word_nbsp("trait");
311 self.print_ident(item.ident);
312 self.print_generic_params(&generics.params);
313 let mut real_bounds = Vec::with_capacity(bounds.len());
314 for b in bounds.iter() {
315 if let GenericBound::Trait(ref ptr, ast::TraitBoundModifier::Maybe) = *b {
317 self.word_space("for ?");
318 self.print_trait_ref(&ptr.trait_ref);
320 real_bounds.push(b.clone());
323 self.print_type_bounds(":", &real_bounds);
324 self.print_where_clause(&generics.where_clause);
327 self.print_inner_attributes(&item.attrs);
328 for trait_item in items {
329 self.print_assoc_item(trait_item);
331 let empty = item.attrs.is_empty() && items.is_empty();
332 self.bclose(item.span, empty);
334 ast::ItemKind::TraitAlias(ref generics, ref bounds) => {
335 self.head(visibility_qualified(&item.vis, "trait"));
336 self.print_ident(item.ident);
337 self.print_generic_params(&generics.params);
338 let mut real_bounds = Vec::with_capacity(bounds.len());
339 // FIXME(durka) this seems to be some quite outdated syntax
340 for b in bounds.iter() {
341 if let GenericBound::Trait(ref ptr, ast::TraitBoundModifier::Maybe) = *b {
343 self.word_space("for ?");
344 self.print_trait_ref(&ptr.trait_ref);
346 real_bounds.push(b.clone());
350 self.print_type_bounds("=", &real_bounds);
351 self.print_where_clause(&generics.where_clause);
353 self.end(); // end inner head-block
354 self.end(); // end outer head-block
356 ast::ItemKind::MacCall(ref mac) => {
358 if mac.args.need_semicolon() {
362 ast::ItemKind::MacroDef(ref macro_def) => {
363 self.print_mac_def(macro_def, &item.ident, item.span, |state| {
364 state.print_visibility(&item.vis)
368 self.ann.post(self, AnnNode::Item(item))
373 enum_definition: &ast::EnumDef,
374 generics: &ast::Generics,
376 span: rustc_span::Span,
377 visibility: &ast::Visibility,
379 self.head(visibility_qualified(visibility, "enum"));
380 self.print_ident(ident);
381 self.print_generic_params(&generics.params);
382 self.print_where_clause(&generics.where_clause);
384 self.print_variants(&enum_definition.variants, span)
387 fn print_variants(&mut self, variants: &[ast::Variant], span: rustc_span::Span) {
390 self.space_if_not_bol();
391 self.maybe_print_comment(v.span.lo());
392 self.print_outer_attributes(&v.attrs);
394 self.print_variant(v);
397 self.maybe_print_trailing_comment(v.span, None);
399 let empty = variants.is_empty();
400 self.bclose(span, empty)
403 crate fn print_visibility(&mut self, vis: &ast::Visibility) {
405 ast::VisibilityKind::Public => self.word_nbsp("pub"),
406 ast::VisibilityKind::Crate(sugar) => match sugar {
407 ast::CrateSugar::PubCrate => self.word_nbsp("pub(crate)"),
408 ast::CrateSugar::JustCrate => self.word_nbsp("crate"),
410 ast::VisibilityKind::Restricted { ref path, .. } => {
411 let path = Self::to_string(|s| s.print_path(path, false, 0));
412 if path == "self" || path == "super" {
413 self.word_nbsp(format!("pub({})", path))
415 self.word_nbsp(format!("pub(in {})", path))
418 ast::VisibilityKind::Inherited => {}
422 fn print_defaultness(&mut self, defaultness: ast::Defaultness) {
423 if let ast::Defaultness::Default(_) = defaultness {
424 self.word_nbsp("default");
428 fn print_record_struct_body(&mut self, fields: &[ast::FieldDef], span: rustc_span::Span) {
432 let empty = fields.is_empty();
434 self.hardbreak_if_not_bol();
436 for field in fields {
437 self.hardbreak_if_not_bol();
438 self.maybe_print_comment(field.span.lo());
439 self.print_outer_attributes(&field.attrs);
440 self.print_visibility(&field.vis);
441 self.print_ident(field.ident.unwrap());
443 self.print_type(&field.ty);
448 self.bclose(span, empty);
453 struct_def: &ast::VariantData,
454 generics: &ast::Generics,
456 span: rustc_span::Span,
457 print_finalizer: bool,
459 self.print_ident(ident);
460 self.print_generic_params(&generics.params);
462 ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
463 if let ast::VariantData::Tuple(..) = struct_def {
465 self.commasep(Inconsistent, struct_def.fields(), |s, field| {
466 s.maybe_print_comment(field.span.lo());
467 s.print_outer_attributes(&field.attrs);
468 s.print_visibility(&field.vis);
469 s.print_type(&field.ty)
473 self.print_where_clause(&generics.where_clause);
478 self.end(); // Close the outer-box.
480 ast::VariantData::Struct(ref fields, ..) => {
481 self.print_where_clause(&generics.where_clause);
482 self.print_record_struct_body(fields, span);
487 crate fn print_variant(&mut self, v: &ast::Variant) {
489 self.print_visibility(&v.vis);
490 let generics = ast::Generics::default();
491 self.print_struct(&v.data, &generics, v.ident, v.span, false);
492 if let Some(ref d) = v.disr_expr {
494 self.word_space("=");
495 self.print_expr(&d.value)
499 fn print_assoc_item(&mut self, item: &ast::AssocItem) {
500 let ast::Item { id, span, ident, ref attrs, ref kind, ref vis, tokens: _ } = *item;
501 self.ann.pre(self, AnnNode::SubItem(id));
502 self.hardbreak_if_not_bol();
503 self.maybe_print_comment(span.lo());
504 self.print_outer_attributes(attrs);
506 ast::AssocItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => {
507 self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs);
509 ast::AssocItemKind::Const(def, ty, body) => {
510 self.print_item_const(ident, None, ty, body.as_deref(), vis, *def);
512 ast::AssocItemKind::TyAlias(box ast::TyAlias {
516 where_predicates_split,
520 self.print_associated_type(
524 *where_predicates_split,
531 ast::AssocItemKind::MacCall(m) => {
533 if m.args.need_semicolon() {
538 self.ann.post(self, AnnNode::SubItem(id))
545 generics: &ast::Generics,
546 vis: &ast::Visibility,
547 defaultness: ast::Defaultness,
548 body: Option<&ast::Block>,
549 attrs: &[ast::Attribute],
554 self.print_visibility(vis);
555 self.print_defaultness(defaultness);
556 self.print_fn(&sig.decl, sig.header, Some(name), generics);
557 if let Some(body) = body {
559 self.print_block_with_attrs(body, attrs);
568 header: ast::FnHeader,
570 generics: &ast::Generics,
572 self.print_fn_header_info(header);
573 if let Some(name) = name {
575 self.print_ident(name);
577 self.print_generic_params(&generics.params);
578 self.print_fn_params_and_ret(decl, false);
579 self.print_where_clause(&generics.where_clause)
582 crate fn print_fn_params_and_ret(&mut self, decl: &ast::FnDecl, is_closure: bool) {
583 let (open, close) = if is_closure { ("|", "|") } else { ("(", ")") };
585 self.commasep(Inconsistent, &decl.inputs, |s, param| s.print_param(param, is_closure));
587 self.print_fn_ret_ty(&decl.output)
590 fn print_where_clause(&mut self, where_clause: &ast::WhereClause) {
591 self.print_where_clause_parts(where_clause.has_where_token, &where_clause.predicates);
594 crate fn print_where_clause_parts(
596 has_where_token: bool,
597 predicates: &[ast::WherePredicate],
599 if predicates.is_empty() && !has_where_token {
604 self.word_space("where");
606 for (i, predicate) in predicates.iter().enumerate() {
608 self.word_space(",");
611 self.print_where_predicate(predicate);
615 pub fn print_where_predicate(&mut self, predicate: &ast::WherePredicate) {
617 ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
618 bound_generic_params,
623 self.print_formal_generic_params(bound_generic_params);
624 self.print_type(bounded_ty);
625 self.print_type_bounds(":", bounds);
627 ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
632 self.print_lifetime_bounds(*lifetime, bounds);
634 ast::WherePredicate::EqPredicate(ast::WhereEqPredicate { lhs_ty, rhs_ty, .. }) => {
635 self.print_type(lhs_ty);
637 self.word_space("=");
638 self.print_type(rhs_ty);
643 fn print_use_tree(&mut self, tree: &ast::UseTree) {
645 ast::UseTreeKind::Simple(rename, ..) => {
646 self.print_path(&tree.prefix, false, 0);
647 if let Some(rename) = rename {
649 self.word_nbsp("as");
650 self.print_ident(rename);
653 ast::UseTreeKind::Glob => {
654 if !tree.prefix.segments.is_empty() {
655 self.print_path(&tree.prefix, false, 0);
660 ast::UseTreeKind::Nested(ref items) => {
661 if !tree.prefix.segments.is_empty() {
662 self.print_path(&tree.prefix, false, 0);
665 if items.is_empty() {
667 } else if items.len() == 1 {
668 self.print_use_tree(&items[0].0);
670 self.cbox(INDENT_UNIT);
674 for use_tree in items.iter().delimited() {
675 self.print_use_tree(&use_tree.0);
676 if !use_tree.is_last {
678 if let ast::UseTreeKind::Nested(_) = use_tree.0.kind {
686 self.trailing_comma();
687 self.offset(-INDENT_UNIT);