ast: Fix naming conventions in AST structures
TraitKind -> Trait TyAliasKind -> TyAlias ImplKind -> Impl FnKind -> Fn All `*Kind`s in AST are supposed to be enums. Tuple structs are converted to braced structs for the types above, and fields are reordered in syntactic order. Also, mutable AST visitor now correctly visit spans in defaultness, unsafety, impl polarity and constness.
This commit is contained in:
parent
051e2b40c9
commit
bf3c52f355
64
src/items.rs
64
src/items.rs
@ -622,7 +622,7 @@ fn both_opaque(
|
||||
fn need_empty_line(a: &ast::AssocItemKind, b: &ast::AssocItemKind) -> bool {
|
||||
match (a, b) {
|
||||
(TyAlias(lty), TyAlias(rty))
|
||||
if both_type(<y.3, &rty.3) || both_opaque(<y.3, &rty.3) =>
|
||||
if both_type(<y.ty, &rty.ty) || both_opaque(<y.ty, &rty.ty) =>
|
||||
{
|
||||
false
|
||||
}
|
||||
@ -633,7 +633,7 @@ fn need_empty_line(a: &ast::AssocItemKind, b: &ast::AssocItemKind) -> bool {
|
||||
|
||||
buffer.sort_by(|(_, a), (_, b)| match (&a.kind, &b.kind) {
|
||||
(TyAlias(lty), TyAlias(rty))
|
||||
if both_type(<y.3, &rty.3) || both_opaque(<y.3, &rty.3) =>
|
||||
if both_type(<y.ty, &rty.ty) || both_opaque(<y.ty, &rty.ty) =>
|
||||
{
|
||||
a.ident.as_str().cmp(&b.ident.as_str())
|
||||
}
|
||||
@ -641,8 +641,8 @@ fn need_empty_line(a: &ast::AssocItemKind, b: &ast::AssocItemKind) -> bool {
|
||||
a.ident.as_str().cmp(&b.ident.as_str())
|
||||
}
|
||||
(Fn(..), Fn(..)) => a.span.lo().cmp(&b.span.lo()),
|
||||
(TyAlias(ty), _) if is_type(&ty.3) => Ordering::Less,
|
||||
(_, TyAlias(ty)) if is_type(&ty.3) => Ordering::Greater,
|
||||
(TyAlias(ty), _) if is_type(&ty.ty) => Ordering::Less,
|
||||
(_, TyAlias(ty)) if is_type(&ty.ty) => Ordering::Greater,
|
||||
(TyAlias(..), _) => Ordering::Less,
|
||||
(_, TyAlias(..)) => Ordering::Greater,
|
||||
(Const(..), _) => Ordering::Less,
|
||||
@ -679,7 +679,7 @@ pub(crate) fn format_impl(
|
||||
offset: Indent,
|
||||
) -> Option<String> {
|
||||
if let ast::ItemKind::Impl(impl_kind) = &item.kind {
|
||||
let ast::ImplKind {
|
||||
let ast::Impl {
|
||||
ref generics,
|
||||
ref self_ty,
|
||||
ref items,
|
||||
@ -833,7 +833,7 @@ fn format_impl_ref_and_type(
|
||||
offset: Indent,
|
||||
) -> Option<String> {
|
||||
if let ast::ItemKind::Impl(impl_kind) = &item.kind {
|
||||
let ast::ImplKind {
|
||||
let ast::Impl {
|
||||
unsafety,
|
||||
polarity,
|
||||
defaultness,
|
||||
@ -1029,8 +1029,13 @@ pub(crate) fn format_trait(
|
||||
offset: Indent,
|
||||
) -> Option<String> {
|
||||
if let ast::ItemKind::Trait(trait_kind) = &item.kind {
|
||||
let ast::TraitKind(is_auto, unsafety, ref generics, ref generic_bounds, ref trait_items) =
|
||||
**trait_kind;
|
||||
let ast::Trait {
|
||||
is_auto,
|
||||
unsafety,
|
||||
ref generics,
|
||||
ref bounds,
|
||||
ref items,
|
||||
} = **trait_kind;
|
||||
let mut result = String::with_capacity(128);
|
||||
let header = format!(
|
||||
"{}{}{}trait ",
|
||||
@ -1048,11 +1053,11 @@ pub(crate) fn format_trait(
|
||||
result.push_str(&generics_str);
|
||||
|
||||
// FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
|
||||
if !generic_bounds.is_empty() {
|
||||
if !bounds.is_empty() {
|
||||
let ident_hi = context
|
||||
.snippet_provider
|
||||
.span_after(item.span, &item.ident.as_str());
|
||||
let bound_hi = generic_bounds.last().unwrap().span().hi();
|
||||
let bound_hi = bounds.last().unwrap().span().hi();
|
||||
let snippet = context.snippet(mk_sp(ident_hi, bound_hi));
|
||||
if contains_comment(snippet) {
|
||||
return None;
|
||||
@ -1061,7 +1066,7 @@ pub(crate) fn format_trait(
|
||||
result = rewrite_assign_rhs_with(
|
||||
context,
|
||||
result + ":",
|
||||
generic_bounds,
|
||||
bounds,
|
||||
shape,
|
||||
RhsTactics::ForceNextLineWithoutIndent,
|
||||
)?;
|
||||
@ -1072,10 +1077,10 @@ pub(crate) fn format_trait(
|
||||
let where_on_new_line = context.config.indent_style() != IndentStyle::Block;
|
||||
|
||||
let where_budget = context.budget(last_line_width(&result));
|
||||
let pos_before_where = if generic_bounds.is_empty() {
|
||||
let pos_before_where = if bounds.is_empty() {
|
||||
generics.where_clause.span.lo()
|
||||
} else {
|
||||
generic_bounds[generic_bounds.len() - 1].span().hi()
|
||||
bounds[bounds.len() - 1].span().hi()
|
||||
};
|
||||
let option = WhereClauseOption::snuggled(&generics_str);
|
||||
let where_clause_str = rewrite_where_clause(
|
||||
@ -1134,7 +1139,7 @@ pub(crate) fn format_trait(
|
||||
BraceStyle::PreferSameLine => result.push(' '),
|
||||
BraceStyle::SameLineWhere => {
|
||||
if result.contains('\n')
|
||||
|| (!generics.where_clause.predicates.is_empty() && !trait_items.is_empty())
|
||||
|| (!generics.where_clause.predicates.is_empty() && !items.is_empty())
|
||||
{
|
||||
result.push_str(&offset.to_string_with_newline(context.config));
|
||||
} else {
|
||||
@ -1149,12 +1154,12 @@ pub(crate) fn format_trait(
|
||||
let open_pos = snippet.find_uncommented("{")? + 1;
|
||||
let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
|
||||
|
||||
if !trait_items.is_empty() || contains_comment(&snippet[open_pos..]) {
|
||||
if !items.is_empty() || contains_comment(&snippet[open_pos..]) {
|
||||
let mut visitor = FmtVisitor::from_context(context);
|
||||
visitor.block_indent = offset.block_only().block_indent(context.config);
|
||||
visitor.last_pos = block_span.lo() + BytePos(open_pos as u32);
|
||||
|
||||
for item in trait_items {
|
||||
for item in items {
|
||||
visitor.visit_trait_item(item);
|
||||
}
|
||||
|
||||
@ -3125,17 +3130,22 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>
|
||||
|
||||
let item_str = match self.kind {
|
||||
ast::ForeignItemKind::Fn(ref fn_kind) => {
|
||||
let ast::FnKind(defaultness, ref fn_sig, ref generics, ref block) = **fn_kind;
|
||||
if let Some(ref body) = block {
|
||||
let ast::Fn {
|
||||
defaultness,
|
||||
ref sig,
|
||||
ref generics,
|
||||
ref body,
|
||||
} = **fn_kind;
|
||||
if let Some(ref body) = body {
|
||||
let mut visitor = FmtVisitor::from_context(context);
|
||||
visitor.block_indent = shape.indent;
|
||||
visitor.last_pos = self.span.lo();
|
||||
let inner_attrs = inner_attributes(&self.attrs);
|
||||
let fn_ctxt = visit::FnCtxt::Foreign;
|
||||
visitor.visit_fn(
|
||||
visit::FnKind::Fn(fn_ctxt, self.ident, &fn_sig, &self.vis, Some(body)),
|
||||
visit::FnKind::Fn(fn_ctxt, self.ident, &sig, &self.vis, Some(body)),
|
||||
generics,
|
||||
&fn_sig.decl,
|
||||
&sig.decl,
|
||||
self.span,
|
||||
defaultness,
|
||||
Some(&inner_attrs),
|
||||
@ -3146,7 +3156,7 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>
|
||||
context,
|
||||
shape.indent,
|
||||
self.ident,
|
||||
&FnSig::from_method_sig(&fn_sig, generics, &self.vis),
|
||||
&FnSig::from_method_sig(&sig, generics, &self.vis),
|
||||
span,
|
||||
FnBraceStyle::None,
|
||||
)
|
||||
@ -3168,16 +3178,20 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>
|
||||
rewrite_assign_rhs(context, prefix, &**ty, shape.sub_width(1)?).map(|s| s + ";")
|
||||
}
|
||||
ast::ForeignItemKind::TyAlias(ref ty_alias_kind) => {
|
||||
let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref type_default) =
|
||||
**ty_alias_kind;
|
||||
let ast::TyAlias {
|
||||
ref generics,
|
||||
ref bounds,
|
||||
ref ty,
|
||||
..
|
||||
} = **ty_alias_kind;
|
||||
rewrite_type(
|
||||
&context,
|
||||
shape.indent,
|
||||
self.ident,
|
||||
&self.vis,
|
||||
generics,
|
||||
Some(generic_bounds),
|
||||
type_default.as_ref(),
|
||||
Some(bounds),
|
||||
ty.as_ref(),
|
||||
self.span,
|
||||
)
|
||||
}
|
||||
|
@ -540,24 +540,22 @@ pub(crate) fn visit_item(&mut self, item: &ast::Item) {
|
||||
self.visit_static(&StaticParts::from_item(item));
|
||||
}
|
||||
ast::ItemKind::Fn(ref fn_kind) => {
|
||||
let ast::FnKind(defaultness, ref fn_signature, ref generics, ref block) =
|
||||
**fn_kind;
|
||||
if let Some(ref body) = block {
|
||||
let ast::Fn {
|
||||
defaultness,
|
||||
ref sig,
|
||||
ref generics,
|
||||
ref body,
|
||||
} = **fn_kind;
|
||||
if let Some(ref body) = body {
|
||||
let inner_attrs = inner_attributes(&item.attrs);
|
||||
let fn_ctxt = match fn_signature.header.ext {
|
||||
let fn_ctxt = match sig.header.ext {
|
||||
ast::Extern::None => visit::FnCtxt::Free,
|
||||
_ => visit::FnCtxt::Foreign,
|
||||
};
|
||||
self.visit_fn(
|
||||
visit::FnKind::Fn(
|
||||
fn_ctxt,
|
||||
item.ident,
|
||||
&fn_signature,
|
||||
&item.vis,
|
||||
Some(body),
|
||||
),
|
||||
visit::FnKind::Fn(fn_ctxt, item.ident, &sig, &item.vis, Some(body)),
|
||||
generics,
|
||||
&fn_signature.decl,
|
||||
&sig.decl,
|
||||
item.span,
|
||||
defaultness,
|
||||
Some(&inner_attrs),
|
||||
@ -565,19 +563,18 @@ pub(crate) fn visit_item(&mut self, item: &ast::Item) {
|
||||
} else {
|
||||
let indent = self.block_indent;
|
||||
let rewrite = self.rewrite_required_fn(
|
||||
indent,
|
||||
item.ident,
|
||||
&fn_signature,
|
||||
&item.vis,
|
||||
generics,
|
||||
item.span,
|
||||
indent, item.ident, &sig, &item.vis, generics, item.span,
|
||||
);
|
||||
self.push_rewrite(item.span, rewrite);
|
||||
}
|
||||
}
|
||||
ast::ItemKind::TyAlias(ref alias_kind) => {
|
||||
let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref ty) =
|
||||
**alias_kind;
|
||||
let ast::TyAlias {
|
||||
ref generics,
|
||||
ref bounds,
|
||||
ref ty,
|
||||
..
|
||||
} = **alias_kind;
|
||||
match ty {
|
||||
Some(ty) => {
|
||||
let rewrite = rewrite_type(
|
||||
@ -586,7 +583,7 @@ pub(crate) fn visit_item(&mut self, item: &ast::Item) {
|
||||
item.ident,
|
||||
&item.vis,
|
||||
generics,
|
||||
Some(generic_bounds),
|
||||
Some(bounds),
|
||||
Some(&*ty),
|
||||
item.span,
|
||||
);
|
||||
@ -597,7 +594,7 @@ pub(crate) fn visit_item(&mut self, item: &ast::Item) {
|
||||
&self.get_context(),
|
||||
self.block_indent,
|
||||
item.ident,
|
||||
generic_bounds,
|
||||
bounds,
|
||||
generics,
|
||||
&item.vis,
|
||||
item.span,
|
||||
@ -639,8 +636,13 @@ pub(crate) fn visit_trait_item(&mut self, ti: &ast::AssocItem) {
|
||||
match ti.kind {
|
||||
ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_trait_item(ti)),
|
||||
ast::AssocItemKind::Fn(ref fn_kind) => {
|
||||
let ast::FnKind(defaultness, ref sig, ref generics, ref block) = **fn_kind;
|
||||
if let Some(ref body) = block {
|
||||
let ast::Fn {
|
||||
defaultness,
|
||||
ref sig,
|
||||
ref generics,
|
||||
ref body,
|
||||
} = **fn_kind;
|
||||
if let Some(ref body) = body {
|
||||
let inner_attrs = inner_attributes(&ti.attrs);
|
||||
let fn_ctxt = visit::FnCtxt::Assoc(visit::AssocCtxt::Trait);
|
||||
self.visit_fn(
|
||||
@ -659,16 +661,20 @@ pub(crate) fn visit_trait_item(&mut self, ti: &ast::AssocItem) {
|
||||
}
|
||||
}
|
||||
ast::AssocItemKind::TyAlias(ref ty_alias_kind) => {
|
||||
let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref type_default) =
|
||||
**ty_alias_kind;
|
||||
let ast::TyAlias {
|
||||
ref generics,
|
||||
ref bounds,
|
||||
ref ty,
|
||||
..
|
||||
} = **ty_alias_kind;
|
||||
let rewrite = rewrite_type(
|
||||
&self.get_context(),
|
||||
self.block_indent,
|
||||
ti.ident,
|
||||
&ti.vis,
|
||||
generics,
|
||||
Some(generic_bounds),
|
||||
type_default.as_ref(),
|
||||
Some(bounds),
|
||||
ty.as_ref(),
|
||||
ti.span,
|
||||
);
|
||||
self.push_rewrite(ti.span, rewrite);
|
||||
@ -689,8 +695,13 @@ pub(crate) fn visit_impl_item(&mut self, ii: &ast::AssocItem) {
|
||||
|
||||
match ii.kind {
|
||||
ast::AssocItemKind::Fn(ref fn_kind) => {
|
||||
let ast::FnKind(defaultness, ref sig, ref generics, ref block) = **fn_kind;
|
||||
if let Some(ref body) = block {
|
||||
let ast::Fn {
|
||||
defaultness,
|
||||
ref sig,
|
||||
ref generics,
|
||||
ref body,
|
||||
} = **fn_kind;
|
||||
if let Some(ref body) = body {
|
||||
let inner_attrs = inner_attributes(&ii.attrs);
|
||||
let fn_ctxt = visit::FnCtxt::Assoc(visit::AssocCtxt::Impl);
|
||||
self.visit_fn(
|
||||
@ -710,7 +721,12 @@ pub(crate) fn visit_impl_item(&mut self, ii: &ast::AssocItem) {
|
||||
}
|
||||
ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_impl_item(ii)),
|
||||
ast::AssocItemKind::TyAlias(ref ty_alias_kind) => {
|
||||
let ast::TyAliasKind(defaultness, ref generics, _, ref ty) = **ty_alias_kind;
|
||||
let ast::TyAlias {
|
||||
defaultness,
|
||||
ref generics,
|
||||
ref ty,
|
||||
..
|
||||
} = **ty_alias_kind;
|
||||
self.push_rewrite(
|
||||
ii.span,
|
||||
rewrite_impl_type(
|
||||
|
Loading…
Reference in New Issue
Block a user