refactor: dedupe & simplify ty alias formatting
This commit is contained in:
parent
4d50e7c760
commit
19c5c74951
166
src/items.rs
166
src/items.rs
@ -1185,18 +1185,6 @@ pub(crate) fn format_trait(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct OpaqueTypeBounds<'a> {
|
|
||||||
generic_bounds: &'a ast::GenericBounds,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Rewrite for OpaqueTypeBounds<'a> {
|
|
||||||
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
|
||||||
self.generic_bounds
|
|
||||||
.rewrite(context, shape)
|
|
||||||
.map(|s| format!("impl {}", s))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) struct TraitAliasBounds<'a> {
|
pub(crate) struct TraitAliasBounds<'a> {
|
||||||
generic_bounds: &'a ast::GenericBounds,
|
generic_bounds: &'a ast::GenericBounds,
|
||||||
generics: &'a ast::Generics,
|
generics: &'a ast::Generics,
|
||||||
@ -1518,17 +1506,79 @@ fn format_tuple_struct(
|
|||||||
Some(result)
|
Some(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn rewrite_type<R: Rewrite>(
|
pub(crate) enum ItemVisitorKind<'a> {
|
||||||
context: &RewriteContext<'_>,
|
Item(&'a ast::Item),
|
||||||
|
AssocTraitItem(&'a ast::AssocItem),
|
||||||
|
AssocImplItem(&'a ast::AssocItem),
|
||||||
|
ForeignItem(&'a ast::ForeignItem),
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TyAliasRewriteInfo<'c, 'g>(
|
||||||
|
&'c RewriteContext<'c>,
|
||||||
|
Indent,
|
||||||
|
&'g ast::Generics,
|
||||||
|
symbol::Ident,
|
||||||
|
Span,
|
||||||
|
);
|
||||||
|
|
||||||
|
pub(crate) fn rewrite_type_alias<'a, 'b>(
|
||||||
|
ty_alias_kind: &ast::TyAliasKind,
|
||||||
|
context: &RewriteContext<'a>,
|
||||||
indent: Indent,
|
indent: Indent,
|
||||||
ident: symbol::Ident,
|
visitor_kind: &ItemVisitorKind<'b>,
|
||||||
vis: &ast::Visibility,
|
|
||||||
generics: &ast::Generics,
|
|
||||||
generic_bounds_opt: Option<&ast::GenericBounds>,
|
|
||||||
rhs: Option<&R>,
|
|
||||||
span: Span,
|
span: Span,
|
||||||
|
) -> Option<String> {
|
||||||
|
use ItemVisitorKind::*;
|
||||||
|
|
||||||
|
let ast::TyAliasKind(defaultness, ref generics, ref generic_bounds, ref ty) = *ty_alias_kind;
|
||||||
|
let ty_opt = ty.as_ref().map(|t| &**t);
|
||||||
|
let (ident, vis) = match visitor_kind {
|
||||||
|
Item(i) => (i.ident, &i.vis),
|
||||||
|
AssocTraitItem(i) | AssocImplItem(i) => (i.ident, &i.vis),
|
||||||
|
ForeignItem(i) => (i.ident, &i.vis),
|
||||||
|
};
|
||||||
|
let rw_info = &TyAliasRewriteInfo(context, indent, generics, ident, span);
|
||||||
|
|
||||||
|
// Type Aliases are formatted slightly differently depending on the context
|
||||||
|
// in which they appear, whether they are opaque, and whether they are associated.
|
||||||
|
// https://rustc-dev-guide.rust-lang.org/opaque-types-type-alias-impl-trait.html
|
||||||
|
// https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/items.md#type-aliases
|
||||||
|
match (visitor_kind, ty_opt) {
|
||||||
|
(Item(_), None) => {
|
||||||
|
let op_ty = OpaqueType { generic_bounds };
|
||||||
|
rewrite_ty(rw_info, Some(generic_bounds), Some(&op_ty), vis)
|
||||||
|
}
|
||||||
|
(Item(_), Some(ty)) => rewrite_ty(rw_info, Some(generic_bounds), Some(&*ty), vis),
|
||||||
|
(AssocImplItem(_), _) => {
|
||||||
|
let result = if let Some(ast::Ty {
|
||||||
|
kind: ast::TyKind::ImplTrait(_, ref generic_bounds),
|
||||||
|
..
|
||||||
|
}) = ty_opt
|
||||||
|
{
|
||||||
|
let op_ty = OpaqueType { generic_bounds };
|
||||||
|
rewrite_ty(rw_info, None, Some(&op_ty), &DEFAULT_VISIBILITY)
|
||||||
|
} else {
|
||||||
|
rewrite_ty(rw_info, None, ty.as_ref(), vis)
|
||||||
|
}?;
|
||||||
|
match defaultness {
|
||||||
|
ast::Defaultness::Default(..) => Some(format!("default {}", result)),
|
||||||
|
_ => Some(result),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(AssocTraitItem(_), _) | (ForeignItem(_), _) => {
|
||||||
|
rewrite_ty(rw_info, Some(generic_bounds), ty.as_ref(), vis)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rewrite_ty<R: Rewrite>(
|
||||||
|
rw_info: &TyAliasRewriteInfo<'_, '_>,
|
||||||
|
generic_bounds_opt: Option<&ast::GenericBounds>,
|
||||||
|
rhs: Option<&R>,
|
||||||
|
vis: &ast::Visibility,
|
||||||
) -> Option<String> {
|
) -> Option<String> {
|
||||||
let mut result = String::with_capacity(128);
|
let mut result = String::with_capacity(128);
|
||||||
|
let TyAliasRewriteInfo(context, indent, generics, ident, span) = *rw_info;
|
||||||
result.push_str(&format!("{}type ", format_visibility(context, vis)));
|
result.push_str(&format!("{}type ", format_visibility(context, vis)));
|
||||||
let ident_str = rewrite_ident(context, ident);
|
let ident_str = rewrite_ident(context, ident);
|
||||||
|
|
||||||
@ -1616,28 +1666,6 @@ pub(crate) fn rewrite_type<R: Rewrite>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn rewrite_opaque_type(
|
|
||||||
context: &RewriteContext<'_>,
|
|
||||||
indent: Indent,
|
|
||||||
ident: symbol::Ident,
|
|
||||||
generic_bounds: &ast::GenericBounds,
|
|
||||||
generics: &ast::Generics,
|
|
||||||
vis: &ast::Visibility,
|
|
||||||
span: Span,
|
|
||||||
) -> Option<String> {
|
|
||||||
let opaque_type_bounds = OpaqueTypeBounds { generic_bounds };
|
|
||||||
rewrite_type(
|
|
||||||
context,
|
|
||||||
indent,
|
|
||||||
ident,
|
|
||||||
vis,
|
|
||||||
generics,
|
|
||||||
Some(generic_bounds),
|
|
||||||
Some(&opaque_type_bounds),
|
|
||||||
span,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn type_annotation_spacing(config: &Config) -> (&str, &str) {
|
fn type_annotation_spacing(config: &Config) -> (&str, &str) {
|
||||||
(
|
(
|
||||||
if config.space_before_colon() { " " } else { "" },
|
if config.space_before_colon() { " " } else { "" },
|
||||||
@ -1863,54 +1891,18 @@ fn rewrite_static(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
struct OpaqueType<'a> {
|
struct OpaqueType<'a> {
|
||||||
bounds: &'a ast::GenericBounds,
|
generic_bounds: &'a ast::GenericBounds,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Rewrite for OpaqueType<'a> {
|
impl<'a> Rewrite for OpaqueType<'a> {
|
||||||
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
||||||
let shape = shape.offset_left(5)?; // `impl `
|
let shape = shape.offset_left(5)?; // `impl `
|
||||||
self.bounds
|
self.generic_bounds
|
||||||
.rewrite(context, shape)
|
.rewrite(context, shape)
|
||||||
.map(|s| format!("impl {}", s))
|
.map(|s| format!("impl {}", s))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn rewrite_impl_type(
|
|
||||||
ident: symbol::Ident,
|
|
||||||
vis: &ast::Visibility,
|
|
||||||
defaultness: ast::Defaultness,
|
|
||||||
ty_opt: Option<&ptr::P<ast::Ty>>,
|
|
||||||
generics: &ast::Generics,
|
|
||||||
context: &RewriteContext<'_>,
|
|
||||||
indent: Indent,
|
|
||||||
span: Span,
|
|
||||||
) -> Option<String> {
|
|
||||||
// Opaque type
|
|
||||||
let result = if let Some(rustc_ast::ast::Ty {
|
|
||||||
kind: ast::TyKind::ImplTrait(_, ref bounds),
|
|
||||||
..
|
|
||||||
}) = ty_opt.map(|t| &**t)
|
|
||||||
{
|
|
||||||
rewrite_type(
|
|
||||||
context,
|
|
||||||
indent,
|
|
||||||
ident,
|
|
||||||
&DEFAULT_VISIBILITY,
|
|
||||||
generics,
|
|
||||||
None,
|
|
||||||
Some(&OpaqueType { bounds }),
|
|
||||||
span,
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
rewrite_type(context, indent, ident, vis, generics, None, ty_opt, span)
|
|
||||||
}?;
|
|
||||||
|
|
||||||
match defaultness {
|
|
||||||
ast::Defaultness::Default(..) => Some(format!("default {}", result)),
|
|
||||||
_ => Some(result),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Rewrite for ast::FnRetTy {
|
impl Rewrite for ast::FnRetTy {
|
||||||
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
||||||
match *self {
|
match *self {
|
||||||
@ -3176,19 +3168,9 @@ impl Rewrite for ast::ForeignItem {
|
|||||||
// 1 = ;
|
// 1 = ;
|
||||||
rewrite_assign_rhs(context, prefix, &**ty, shape.sub_width(1)?).map(|s| s + ";")
|
rewrite_assign_rhs(context, prefix, &**ty, shape.sub_width(1)?).map(|s| s + ";")
|
||||||
}
|
}
|
||||||
ast::ForeignItemKind::TyAlias(ref ty_alias_kind) => {
|
ast::ForeignItemKind::TyAlias(ref ty_alias) => {
|
||||||
let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref type_default) =
|
let (kind, span) = (&ItemVisitorKind::ForeignItem(&self), self.span);
|
||||||
**ty_alias_kind;
|
rewrite_type_alias(ty_alias, context, shape.indent, kind, span)
|
||||||
rewrite_type(
|
|
||||||
context,
|
|
||||||
shape.indent,
|
|
||||||
self.ident,
|
|
||||||
&self.vis,
|
|
||||||
generics,
|
|
||||||
Some(generic_bounds),
|
|
||||||
type_default.as_ref(),
|
|
||||||
self.span,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
ast::ForeignItemKind::MacCall(ref mac) => {
|
ast::ForeignItemKind::MacCall(ref mac) => {
|
||||||
rewrite_macro(mac, None, context, shape, MacroPosition::Item)
|
rewrite_macro(mac, None, context, shape, MacroPosition::Item)
|
||||||
|
@ -12,8 +12,7 @@ use crate::config::{BraceStyle, Config};
|
|||||||
use crate::coverage::transform_missing_snippet;
|
use crate::coverage::transform_missing_snippet;
|
||||||
use crate::items::{
|
use crate::items::{
|
||||||
format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item, rewrite_extern_crate,
|
format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item, rewrite_extern_crate,
|
||||||
rewrite_impl_type, rewrite_opaque_type, rewrite_type, FnBraceStyle, FnSig, StaticParts,
|
rewrite_type_alias, FnBraceStyle, FnSig, ItemVisitorKind, StaticParts, StructParts,
|
||||||
StructParts,
|
|
||||||
};
|
};
|
||||||
use crate::macros::{macro_style, rewrite_macro, rewrite_macro_def, MacroPosition};
|
use crate::macros::{macro_style, rewrite_macro, rewrite_macro_def, MacroPosition};
|
||||||
use crate::modules::Module;
|
use crate::modules::Module;
|
||||||
@ -576,35 +575,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast::ItemKind::TyAlias(ref alias_kind) => {
|
ast::ItemKind::TyAlias(ref alias_kind) => {
|
||||||
let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref ty) =
|
use ItemVisitorKind::Item;
|
||||||
**alias_kind;
|
self.visit_ty_alias_kind(alias_kind, &Item(&item), item.span);
|
||||||
match ty {
|
|
||||||
Some(ty) => {
|
|
||||||
let rewrite = rewrite_type(
|
|
||||||
&self.get_context(),
|
|
||||||
self.block_indent,
|
|
||||||
item.ident,
|
|
||||||
&item.vis,
|
|
||||||
generics,
|
|
||||||
Some(generic_bounds),
|
|
||||||
Some(&*ty),
|
|
||||||
item.span,
|
|
||||||
);
|
|
||||||
self.push_rewrite(item.span, rewrite);
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
let rewrite = rewrite_opaque_type(
|
|
||||||
&self.get_context(),
|
|
||||||
self.block_indent,
|
|
||||||
item.ident,
|
|
||||||
generic_bounds,
|
|
||||||
generics,
|
|
||||||
&item.vis,
|
|
||||||
item.span,
|
|
||||||
);
|
|
||||||
self.push_rewrite(item.span, rewrite);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ast::ItemKind::GlobalAsm(..) => {
|
ast::ItemKind::GlobalAsm(..) => {
|
||||||
let snippet = Some(self.snippet(item.span).to_owned());
|
let snippet = Some(self.snippet(item.span).to_owned());
|
||||||
@ -627,6 +599,22 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||||||
self.skip_context = skip_context_saved;
|
self.skip_context = skip_context_saved;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn visit_ty_alias_kind(
|
||||||
|
&mut self,
|
||||||
|
ty_kind: &ast::TyAliasKind,
|
||||||
|
visitor_kind: &ItemVisitorKind<'_>,
|
||||||
|
span: Span,
|
||||||
|
) {
|
||||||
|
let rewrite = rewrite_type_alias(
|
||||||
|
ty_kind,
|
||||||
|
&self.get_context(),
|
||||||
|
self.block_indent,
|
||||||
|
visitor_kind,
|
||||||
|
span,
|
||||||
|
);
|
||||||
|
self.push_rewrite(span, rewrite);
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn visit_trait_item(&mut self, ti: &ast::AssocItem) {
|
pub(crate) fn visit_trait_item(&mut self, ti: &ast::AssocItem) {
|
||||||
skip_out_of_file_lines_range_visitor!(self, ti.span);
|
skip_out_of_file_lines_range_visitor!(self, ti.span);
|
||||||
|
|
||||||
@ -659,19 +647,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast::AssocItemKind::TyAlias(ref ty_alias_kind) => {
|
ast::AssocItemKind::TyAlias(ref ty_alias_kind) => {
|
||||||
let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref type_default) =
|
use ItemVisitorKind::AssocTraitItem;
|
||||||
**ty_alias_kind;
|
self.visit_ty_alias_kind(ty_alias_kind, &AssocTraitItem(&ti), ti.span);
|
||||||
let rewrite = rewrite_type(
|
|
||||||
&self.get_context(),
|
|
||||||
self.block_indent,
|
|
||||||
ti.ident,
|
|
||||||
&ti.vis,
|
|
||||||
generics,
|
|
||||||
Some(generic_bounds),
|
|
||||||
type_default.as_ref(),
|
|
||||||
ti.span,
|
|
||||||
);
|
|
||||||
self.push_rewrite(ti.span, rewrite);
|
|
||||||
}
|
}
|
||||||
ast::AssocItemKind::MacCall(ref mac) => {
|
ast::AssocItemKind::MacCall(ref mac) => {
|
||||||
self.visit_mac(mac, Some(ti.ident), MacroPosition::Item);
|
self.visit_mac(mac, Some(ti.ident), MacroPosition::Item);
|
||||||
@ -710,20 +687,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||||||
}
|
}
|
||||||
ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_impl_item(ii)),
|
ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_impl_item(ii)),
|
||||||
ast::AssocItemKind::TyAlias(ref ty_alias_kind) => {
|
ast::AssocItemKind::TyAlias(ref ty_alias_kind) => {
|
||||||
let ast::TyAliasKind(defaultness, ref generics, _, ref ty) = **ty_alias_kind;
|
use ItemVisitorKind::AssocImplItem;
|
||||||
self.push_rewrite(
|
self.visit_ty_alias_kind(ty_alias_kind, &AssocImplItem(&ii), ii.span);
|
||||||
ii.span,
|
|
||||||
rewrite_impl_type(
|
|
||||||
ii.ident,
|
|
||||||
&ii.vis,
|
|
||||||
defaultness,
|
|
||||||
ty.as_ref(),
|
|
||||||
generics,
|
|
||||||
&self.get_context(),
|
|
||||||
self.block_indent,
|
|
||||||
ii.span,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
ast::AssocItemKind::MacCall(ref mac) => {
|
ast::AssocItemKind::MacCall(ref mac) => {
|
||||||
self.visit_mac(mac, Some(ii.ident), MacroPosition::Item);
|
self.visit_mac(mac, Some(ii.ident), MacroPosition::Item);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user