Skip to content

Commit 19c5c74

Browse files
refactor: dedupe & simplify ty alias formatting
1 parent 4d50e7c commit 19c5c74

File tree

2 files changed

+96
-149
lines changed

2 files changed

+96
-149
lines changed

Diff for: src/items.rs

+73-91
Original file line numberDiff line numberDiff line change
@@ -1185,18 +1185,6 @@ pub(crate) fn format_trait(
11851185
}
11861186
}
11871187

1188-
struct OpaqueTypeBounds<'a> {
1189-
generic_bounds: &'a ast::GenericBounds,
1190-
}
1191-
1192-
impl<'a> Rewrite for OpaqueTypeBounds<'a> {
1193-
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1194-
self.generic_bounds
1195-
.rewrite(context, shape)
1196-
.map(|s| format!("impl {}", s))
1197-
}
1198-
}
1199-
12001188
pub(crate) struct TraitAliasBounds<'a> {
12011189
generic_bounds: &'a ast::GenericBounds,
12021190
generics: &'a ast::Generics,
@@ -1518,17 +1506,79 @@ fn format_tuple_struct(
15181506
Some(result)
15191507
}
15201508

1521-
pub(crate) fn rewrite_type<R: Rewrite>(
1522-
context: &RewriteContext<'_>,
1509+
pub(crate) enum ItemVisitorKind<'a> {
1510+
Item(&'a ast::Item),
1511+
AssocTraitItem(&'a ast::AssocItem),
1512+
AssocImplItem(&'a ast::AssocItem),
1513+
ForeignItem(&'a ast::ForeignItem),
1514+
}
1515+
1516+
struct TyAliasRewriteInfo<'c, 'g>(
1517+
&'c RewriteContext<'c>,
1518+
Indent,
1519+
&'g ast::Generics,
1520+
symbol::Ident,
1521+
Span,
1522+
);
1523+
1524+
pub(crate) fn rewrite_type_alias<'a, 'b>(
1525+
ty_alias_kind: &ast::TyAliasKind,
1526+
context: &RewriteContext<'a>,
15231527
indent: Indent,
1524-
ident: symbol::Ident,
1525-
vis: &ast::Visibility,
1526-
generics: &ast::Generics,
1528+
visitor_kind: &ItemVisitorKind<'b>,
1529+
span: Span,
1530+
) -> Option<String> {
1531+
use ItemVisitorKind::*;
1532+
1533+
let ast::TyAliasKind(defaultness, ref generics, ref generic_bounds, ref ty) = *ty_alias_kind;
1534+
let ty_opt = ty.as_ref().map(|t| &**t);
1535+
let (ident, vis) = match visitor_kind {
1536+
Item(i) => (i.ident, &i.vis),
1537+
AssocTraitItem(i) | AssocImplItem(i) => (i.ident, &i.vis),
1538+
ForeignItem(i) => (i.ident, &i.vis),
1539+
};
1540+
let rw_info = &TyAliasRewriteInfo(context, indent, generics, ident, span);
1541+
1542+
// Type Aliases are formatted slightly differently depending on the context
1543+
// in which they appear, whether they are opaque, and whether they are associated.
1544+
// https://rustc-dev-guide.rust-lang.org/opaque-types-type-alias-impl-trait.html
1545+
// https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/items.md#type-aliases
1546+
match (visitor_kind, ty_opt) {
1547+
(Item(_), None) => {
1548+
let op_ty = OpaqueType { generic_bounds };
1549+
rewrite_ty(rw_info, Some(generic_bounds), Some(&op_ty), vis)
1550+
}
1551+
(Item(_), Some(ty)) => rewrite_ty(rw_info, Some(generic_bounds), Some(&*ty), vis),
1552+
(AssocImplItem(_), _) => {
1553+
let result = if let Some(ast::Ty {
1554+
kind: ast::TyKind::ImplTrait(_, ref generic_bounds),
1555+
..
1556+
}) = ty_opt
1557+
{
1558+
let op_ty = OpaqueType { generic_bounds };
1559+
rewrite_ty(rw_info, None, Some(&op_ty), &DEFAULT_VISIBILITY)
1560+
} else {
1561+
rewrite_ty(rw_info, None, ty.as_ref(), vis)
1562+
}?;
1563+
match defaultness {
1564+
ast::Defaultness::Default(..) => Some(format!("default {}", result)),
1565+
_ => Some(result),
1566+
}
1567+
}
1568+
(AssocTraitItem(_), _) | (ForeignItem(_), _) => {
1569+
rewrite_ty(rw_info, Some(generic_bounds), ty.as_ref(), vis)
1570+
}
1571+
}
1572+
}
1573+
1574+
fn rewrite_ty<R: Rewrite>(
1575+
rw_info: &TyAliasRewriteInfo<'_, '_>,
15271576
generic_bounds_opt: Option<&ast::GenericBounds>,
15281577
rhs: Option<&R>,
1529-
span: Span,
1578+
vis: &ast::Visibility,
15301579
) -> Option<String> {
15311580
let mut result = String::with_capacity(128);
1581+
let TyAliasRewriteInfo(context, indent, generics, ident, span) = *rw_info;
15321582
result.push_str(&format!("{}type ", format_visibility(context, vis)));
15331583
let ident_str = rewrite_ident(context, ident);
15341584

@@ -1616,28 +1666,6 @@ pub(crate) fn rewrite_type<R: Rewrite>(
16161666
}
16171667
}
16181668

1619-
pub(crate) fn rewrite_opaque_type(
1620-
context: &RewriteContext<'_>,
1621-
indent: Indent,
1622-
ident: symbol::Ident,
1623-
generic_bounds: &ast::GenericBounds,
1624-
generics: &ast::Generics,
1625-
vis: &ast::Visibility,
1626-
span: Span,
1627-
) -> Option<String> {
1628-
let opaque_type_bounds = OpaqueTypeBounds { generic_bounds };
1629-
rewrite_type(
1630-
context,
1631-
indent,
1632-
ident,
1633-
vis,
1634-
generics,
1635-
Some(generic_bounds),
1636-
Some(&opaque_type_bounds),
1637-
span,
1638-
)
1639-
}
1640-
16411669
fn type_annotation_spacing(config: &Config) -> (&str, &str) {
16421670
(
16431671
if config.space_before_colon() { " " } else { "" },
@@ -1863,54 +1891,18 @@ fn rewrite_static(
18631891
}
18641892
}
18651893
struct OpaqueType<'a> {
1866-
bounds: &'a ast::GenericBounds,
1894+
generic_bounds: &'a ast::GenericBounds,
18671895
}
18681896

18691897
impl<'a> Rewrite for OpaqueType<'a> {
18701898
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
18711899
let shape = shape.offset_left(5)?; // `impl `
1872-
self.bounds
1900+
self.generic_bounds
18731901
.rewrite(context, shape)
18741902
.map(|s| format!("impl {}", s))
18751903
}
18761904
}
18771905

1878-
pub(crate) fn rewrite_impl_type(
1879-
ident: symbol::Ident,
1880-
vis: &ast::Visibility,
1881-
defaultness: ast::Defaultness,
1882-
ty_opt: Option<&ptr::P<ast::Ty>>,
1883-
generics: &ast::Generics,
1884-
context: &RewriteContext<'_>,
1885-
indent: Indent,
1886-
span: Span,
1887-
) -> Option<String> {
1888-
// Opaque type
1889-
let result = if let Some(rustc_ast::ast::Ty {
1890-
kind: ast::TyKind::ImplTrait(_, ref bounds),
1891-
..
1892-
}) = ty_opt.map(|t| &**t)
1893-
{
1894-
rewrite_type(
1895-
context,
1896-
indent,
1897-
ident,
1898-
&DEFAULT_VISIBILITY,
1899-
generics,
1900-
None,
1901-
Some(&OpaqueType { bounds }),
1902-
span,
1903-
)
1904-
} else {
1905-
rewrite_type(context, indent, ident, vis, generics, None, ty_opt, span)
1906-
}?;
1907-
1908-
match defaultness {
1909-
ast::Defaultness::Default(..) => Some(format!("default {}", result)),
1910-
_ => Some(result),
1911-
}
1912-
}
1913-
19141906
impl Rewrite for ast::FnRetTy {
19151907
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
19161908
match *self {
@@ -3176,19 +3168,9 @@ impl Rewrite for ast::ForeignItem {
31763168
// 1 = ;
31773169
rewrite_assign_rhs(context, prefix, &**ty, shape.sub_width(1)?).map(|s| s + ";")
31783170
}
3179-
ast::ForeignItemKind::TyAlias(ref ty_alias_kind) => {
3180-
let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref type_default) =
3181-
**ty_alias_kind;
3182-
rewrite_type(
3183-
context,
3184-
shape.indent,
3185-
self.ident,
3186-
&self.vis,
3187-
generics,
3188-
Some(generic_bounds),
3189-
type_default.as_ref(),
3190-
self.span,
3191-
)
3171+
ast::ForeignItemKind::TyAlias(ref ty_alias) => {
3172+
let (kind, span) = (&ItemVisitorKind::ForeignItem(&self), self.span);
3173+
rewrite_type_alias(ty_alias, context, shape.indent, kind, span)
31923174
}
31933175
ast::ForeignItemKind::MacCall(ref mac) => {
31943176
rewrite_macro(mac, None, context, shape, MacroPosition::Item)

Diff for: src/visitor.rs

+23-58
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ use crate::config::{BraceStyle, Config};
1212
use crate::coverage::transform_missing_snippet;
1313
use crate::items::{
1414
format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item, rewrite_extern_crate,
15-
rewrite_impl_type, rewrite_opaque_type, rewrite_type, FnBraceStyle, FnSig, StaticParts,
16-
StructParts,
15+
rewrite_type_alias, FnBraceStyle, FnSig, ItemVisitorKind, StaticParts, StructParts,
1716
};
1817
use crate::macros::{macro_style, rewrite_macro, rewrite_macro_def, MacroPosition};
1918
use crate::modules::Module;
@@ -576,35 +575,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
576575
}
577576
}
578577
ast::ItemKind::TyAlias(ref alias_kind) => {
579-
let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref ty) =
580-
**alias_kind;
581-
match ty {
582-
Some(ty) => {
583-
let rewrite = rewrite_type(
584-
&self.get_context(),
585-
self.block_indent,
586-
item.ident,
587-
&item.vis,
588-
generics,
589-
Some(generic_bounds),
590-
Some(&*ty),
591-
item.span,
592-
);
593-
self.push_rewrite(item.span, rewrite);
594-
}
595-
None => {
596-
let rewrite = rewrite_opaque_type(
597-
&self.get_context(),
598-
self.block_indent,
599-
item.ident,
600-
generic_bounds,
601-
generics,
602-
&item.vis,
603-
item.span,
604-
);
605-
self.push_rewrite(item.span, rewrite);
606-
}
607-
}
578+
use ItemVisitorKind::Item;
579+
self.visit_ty_alias_kind(alias_kind, &Item(&item), item.span);
608580
}
609581
ast::ItemKind::GlobalAsm(..) => {
610582
let snippet = Some(self.snippet(item.span).to_owned());
@@ -627,6 +599,22 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
627599
self.skip_context = skip_context_saved;
628600
}
629601

602+
fn visit_ty_alias_kind(
603+
&mut self,
604+
ty_kind: &ast::TyAliasKind,
605+
visitor_kind: &ItemVisitorKind<'_>,
606+
span: Span,
607+
) {
608+
let rewrite = rewrite_type_alias(
609+
ty_kind,
610+
&self.get_context(),
611+
self.block_indent,
612+
visitor_kind,
613+
span,
614+
);
615+
self.push_rewrite(span, rewrite);
616+
}
617+
630618
pub(crate) fn visit_trait_item(&mut self, ti: &ast::AssocItem) {
631619
skip_out_of_file_lines_range_visitor!(self, ti.span);
632620

@@ -659,19 +647,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
659647
}
660648
}
661649
ast::AssocItemKind::TyAlias(ref ty_alias_kind) => {
662-
let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref type_default) =
663-
**ty_alias_kind;
664-
let rewrite = rewrite_type(
665-
&self.get_context(),
666-
self.block_indent,
667-
ti.ident,
668-
&ti.vis,
669-
generics,
670-
Some(generic_bounds),
671-
type_default.as_ref(),
672-
ti.span,
673-
);
674-
self.push_rewrite(ti.span, rewrite);
650+
use ItemVisitorKind::AssocTraitItem;
651+
self.visit_ty_alias_kind(ty_alias_kind, &AssocTraitItem(&ti), ti.span);
675652
}
676653
ast::AssocItemKind::MacCall(ref mac) => {
677654
self.visit_mac(mac, Some(ti.ident), MacroPosition::Item);
@@ -710,20 +687,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
710687
}
711688
ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_impl_item(ii)),
712689
ast::AssocItemKind::TyAlias(ref ty_alias_kind) => {
713-
let ast::TyAliasKind(defaultness, ref generics, _, ref ty) = **ty_alias_kind;
714-
self.push_rewrite(
715-
ii.span,
716-
rewrite_impl_type(
717-
ii.ident,
718-
&ii.vis,
719-
defaultness,
720-
ty.as_ref(),
721-
generics,
722-
&self.get_context(),
723-
self.block_indent,
724-
ii.span,
725-
),
726-
);
690+
use ItemVisitorKind::AssocImplItem;
691+
self.visit_ty_alias_kind(ty_alias_kind, &AssocImplItem(&ii), ii.span);
727692
}
728693
ast::AssocItemKind::MacCall(ref mac) => {
729694
self.visit_mac(mac, Some(ii.ident), MacroPosition::Item);

0 commit comments

Comments
 (0)