Skip to content

Commit e243be6

Browse files
committed
Allow formatting Anonymous{Struct, Union} declarations
1 parent d67f1a4 commit e243be6

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

src/items.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::cmp::{max, min, Ordering};
66
use regex::Regex;
77
use rustc_ast::visit;
88
use rustc_ast::{ast, ptr};
9-
use rustc_span::{symbol, BytePos, Span, DUMMY_SP};
9+
use rustc_span::{symbol, BytePos, Span};
1010

1111
use crate::attr::filter_inline_attrs;
1212
use crate::comment::{
@@ -31,12 +31,7 @@ use crate::stmt::Stmt;
3131
use crate::utils::*;
3232
use crate::vertical::rewrite_with_alignment;
3333
use crate::visitor::FmtVisitor;
34-
35-
const DEFAULT_VISIBILITY: ast::Visibility = ast::Visibility {
36-
kind: ast::VisibilityKind::Inherited,
37-
span: DUMMY_SP,
38-
tokens: None,
39-
};
34+
use crate::DEFAULT_VISIBILITY;
4035

4136
fn type_annotation_separator(config: &Config) -> &str {
4237
colon_spaces(config)
@@ -976,7 +971,7 @@ impl<'a> StructParts<'a> {
976971
format_header(context, self.prefix, self.ident, self.vis, offset)
977972
}
978973

979-
fn from_variant(variant: &'a ast::Variant) -> Self {
974+
pub(crate) fn from_variant(variant: &'a ast::Variant) -> Self {
980975
StructParts {
981976
prefix: "",
982977
ident: variant.ident,

src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use std::rc::Rc;
3131

3232
use ignore;
3333
use rustc_ast::ast;
34-
use rustc_span::symbol;
34+
use rustc_span::{symbol, DUMMY_SP};
3535
use thiserror::Error;
3636

3737
use crate::comment::LineClasses;
@@ -95,6 +95,11 @@ mod types;
9595
mod vertical;
9696
pub(crate) mod visitor;
9797

98+
const DEFAULT_VISIBILITY: ast::Visibility = ast::Visibility {
99+
kind: ast::VisibilityKind::Inherited,
100+
span: DUMMY_SP,
101+
tokens: None,
102+
};
98103
/// The various errors that can occur during formatting. Note that not all of
99104
/// these can currently be propagated to clients.
100105
#[derive(Error, Debug)]

src/types.rs

+55-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use std::iter::ExactSizeIterator;
22
use std::ops::Deref;
33

44
use rustc_ast::ast::{self, FnRetTy, Mutability};
5-
use rustc_span::{symbol::kw, BytePos, Pos, Span};
5+
use rustc_span::{symbol::kw, symbol::Ident, BytePos, Pos, Span};
66

7-
use crate::comment::{combine_strs_with_missing_comments, contains_comment};
87
use crate::config::lists::*;
98
use crate::config::{IndentStyle, TypeDensity, Version};
109
use crate::expr::{
1110
format_expr, rewrite_assign_rhs, rewrite_call, rewrite_tuple, rewrite_unary_prefix, ExprType,
1211
};
12+
use crate::items::StructParts;
1313
use crate::lists::{
1414
definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
1515
};
@@ -24,6 +24,11 @@ use crate::utils::{
2424
colon_spaces, extra_offset, first_line_width, format_extern, format_mutability,
2525
last_line_extendable, last_line_width, mk_sp, rewrite_ident,
2626
};
27+
use crate::DEFAULT_VISIBILITY;
28+
use crate::{
29+
comment::{combine_strs_with_missing_comments, contains_comment},
30+
items::format_struct_struct,
31+
};
2732

2833
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
2934
pub(crate) enum PathContext {
@@ -764,6 +769,54 @@ impl Rewrite for ast::Ty {
764769
ast::TyKind::Tup(ref items) => {
765770
rewrite_tuple(context, items.iter(), self.span, shape, items.len() == 1)
766771
}
772+
ast::TyKind::AnonymousStruct(ref fields, recovered) => {
773+
let ident = Ident::new(
774+
kw::Struct,
775+
mk_sp(self.span.lo(), self.span.lo() + BytePos(6)),
776+
);
777+
let data = ast::VariantData::Struct(fields.clone(), recovered);
778+
let variant = ast::Variant {
779+
attrs: vec![],
780+
id: self.id,
781+
span: self.span,
782+
vis: DEFAULT_VISIBILITY,
783+
ident,
784+
data,
785+
disr_expr: None,
786+
is_placeholder: false,
787+
};
788+
format_struct_struct(
789+
&context,
790+
&StructParts::from_variant(&variant),
791+
fields,
792+
shape.indent,
793+
None,
794+
)
795+
}
796+
ast::TyKind::AnonymousUnion(ref fields, recovered) => {
797+
let ident = Ident::new(
798+
kw::Union,
799+
mk_sp(self.span.lo(), self.span.lo() + BytePos(5)),
800+
);
801+
let data = ast::VariantData::Struct(fields.clone(), recovered);
802+
let variant = ast::Variant {
803+
attrs: vec![],
804+
id: self.id,
805+
span: self.span,
806+
vis: DEFAULT_VISIBILITY,
807+
ident,
808+
data,
809+
disr_expr: None,
810+
is_placeholder: false,
811+
};
812+
format_struct_struct(
813+
&context,
814+
&StructParts::from_variant(&variant),
815+
fields,
816+
shape.indent,
817+
None,
818+
)
819+
}
767820
ast::TyKind::Path(ref q_self, ref path) => {
768821
rewrite_path(context, PathContext::Type, q_self.as_ref(), path, shape)
769822
}

0 commit comments

Comments
 (0)