Skip to content

Rustfmt-ing librustc_front. #29075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 26 additions & 33 deletions src/librustc_front/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,14 +431,16 @@ pub fn noop_fold_foreign_mod<T: Folder>(ForeignMod { abi, items }: ForeignMod,
}

pub fn noop_fold_variant<T: Folder>(v: P<Variant>, fld: &mut T) -> P<Variant> {
v.map(|Spanned {node: Variant_ {name, attrs, data, disr_expr}, span}| Spanned {
node: Variant_ {
name: name,
attrs: fold_attrs(attrs, fld),
data: fld.fold_variant_data(data),
disr_expr: disr_expr.map(|e| fld.fold_expr(e)),
},
span: fld.new_span(span),
v.map(|Spanned { node: Variant_ { name, attrs, data, disr_expr }, span }| {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I dislike the current formatting of struct literals/struct patterns. I'd even say it discourages their use somewhat.
Compare struct patterns and tuple patterns:

S { name, attrs, data, disr_expr } // those damn 3 extra spaces! I need to type them (okay, rustfmt solve this problem) and they shift the text closer to 100 character limit
S { .. } // even worse, 37.5% of unnecessary symbols
S(name, attrs, data, disr_expr) // nice
S(..) // nice
S{name, attrs, data, disr_expr} // almost as nice
S{..} // almost as nice

Spanned {
node: Variant_ {
name: name,
attrs: fold_attrs(attrs, fld),
data: fld.fold_variant_data(data),
disr_expr: disr_expr.map(|e| fld.fold_expr(e)),
},
span: fld.new_span(span),
}
})
}

Expand Down Expand Up @@ -697,12 +699,14 @@ pub fn noop_fold_struct_def<T: Folder>(struct_def: P<VariantData>, fld: &mut T)
struct_def.map(|vdata| {
match vdata {
VariantData::Struct(fields, id) => {
VariantData::Struct(fields.move_map(|f| fld.fold_struct_field(f)), fld.new_id(id))
VariantData::Struct(fields.move_map(|f| fld.fold_struct_field(f)),
fld.new_id(id))
}
VariantData::Tuple(fields, id) => {
VariantData::Tuple(fields.move_map(|f| fld.fold_struct_field(f)), fld.new_id(id))
VariantData::Tuple(fields.move_map(|f| fld.fold_struct_field(f)),
fld.new_id(id))
}
VariantData::Unit(id) => VariantData::Unit(fld.new_id(id))
VariantData::Unit(id) => VariantData::Unit(fld.new_id(id)),
}
})
}
Expand Down Expand Up @@ -742,8 +746,7 @@ pub fn noop_fold_struct_field<T: Folder>(f: StructField, fld: &mut T) -> StructF

pub fn noop_fold_field<T: Folder>(Field { name, expr, span }: Field, folder: &mut T) -> Field {
Field {
name: respan(folder.new_span(name.span),
folder.fold_name(name.node)),
name: respan(folder.new_span(name.span), folder.fold_name(name.node)),
expr: folder.fold_expr(expr),
span: folder.new_span(span),
}
Expand Down Expand Up @@ -814,8 +817,7 @@ pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ {
ItemStruct(struct_def, folder.fold_generics(generics))
}
ItemDefaultImpl(unsafety, ref trait_ref) => {
ItemDefaultImpl(unsafety,
folder.fold_trait_ref((*trait_ref).clone()))
ItemDefaultImpl(unsafety, folder.fold_trait_ref((*trait_ref).clone()))
}
ItemImpl(unsafety, polarity, generics, ifce, ty, impl_items) => {
let new_impl_items = impl_items.into_iter()
Expand Down Expand Up @@ -856,8 +858,7 @@ pub fn noop_fold_trait_item<T: Folder>(i: P<TraitItem>,
attrs: fold_attrs(attrs, folder),
node: match node {
ConstTraitItem(ty, default) => {
ConstTraitItem(folder.fold_ty(ty),
default.map(|x| folder.fold_expr(x)))
ConstTraitItem(folder.fold_ty(ty), default.map(|x| folder.fold_expr(x)))
}
MethodTraitItem(sig, body) => {
MethodTraitItem(noop_fold_method_sig(sig, folder),
Expand Down Expand Up @@ -885,8 +886,7 @@ pub fn noop_fold_impl_item<T: Folder>(i: P<ImplItem>, folder: &mut T) -> SmallVe
ConstImplItem(folder.fold_ty(ty), folder.fold_expr(expr))
}
MethodImplItem(sig, body) => {
MethodImplItem(noop_fold_method_sig(sig, folder),
folder.fold_block(body))
MethodImplItem(noop_fold_method_sig(sig, folder), folder.fold_block(body))
}
TypeImplItem(ty) => TypeImplItem(folder.fold_ty(ty)),
},
Expand Down Expand Up @@ -983,8 +983,7 @@ pub fn noop_fold_foreign_item<T: Folder>(ni: P<ForeignItem>, folder: &mut T) ->
attrs: fold_attrs(attrs, folder),
node: match node {
ForeignItemFn(fdec, generics) => {
ForeignItemFn(folder.fold_fn_decl(fdec),
folder.fold_generics(generics))
ForeignItemFn(folder.fold_fn_decl(fdec), folder.fold_generics(generics))
}
ForeignItemStatic(t, m) => {
ForeignItemStatic(folder.fold_ty(t), m)
Expand Down Expand Up @@ -1076,12 +1075,10 @@ pub fn noop_fold_expr<T: Folder>(Expr { id, node, span }: Expr, folder: &mut T)
}
ExprTup(elts) => ExprTup(elts.move_map(|x| folder.fold_expr(x))),
ExprCall(f, args) => {
ExprCall(folder.fold_expr(f),
args.move_map(|x| folder.fold_expr(x)))
ExprCall(folder.fold_expr(f), args.move_map(|x| folder.fold_expr(x)))
}
ExprMethodCall(name, tps, args) => {
ExprMethodCall(respan(folder.new_span(name.span),
folder.fold_name(name.node)),
ExprMethodCall(respan(folder.new_span(name.span), folder.fold_name(name.node)),
tps.move_map(|x| folder.fold_ty(x)),
args.move_map(|x| folder.fold_expr(x)))
}
Expand Down Expand Up @@ -1129,13 +1126,11 @@ pub fn noop_fold_expr<T: Folder>(Expr { id, node, span }: Expr, folder: &mut T)
}
ExprField(el, name) => {
ExprField(folder.fold_expr(el),
respan(folder.new_span(name.span),
folder.fold_name(name.node)))
respan(folder.new_span(name.span), folder.fold_name(name.node)))
}
ExprTupField(el, index) => {
ExprTupField(folder.fold_expr(el),
respan(folder.new_span(index.span),
folder.fold_usize(index.node)))
respan(folder.new_span(index.span), folder.fold_usize(index.node)))
}
ExprIndex(el, er) => {
ExprIndex(folder.fold_expr(el), folder.fold_expr(er))
Expand All @@ -1154,12 +1149,10 @@ pub fn noop_fold_expr<T: Folder>(Expr { id, node, span }: Expr, folder: &mut T)
ExprPath(qself, folder.fold_path(path))
}
ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|label| {
respan(folder.new_span(label.span),
folder.fold_ident(label.node))
respan(folder.new_span(label.span), folder.fold_ident(label.node))
})),
ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|label| {
respan(folder.new_span(label.span),
folder.fold_ident(label.node))
respan(folder.new_span(label.span), folder.fold_ident(label.node))
})),
ExprRet(e) => ExprRet(e.map(|x| folder.fold_expr(x))),
ExprInlineAsm(InlineAsm {
Expand Down
39 changes: 27 additions & 12 deletions src/librustc_front/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,6 @@ pub type ViewPath = Spanned<ViewPath_>;

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum ViewPath_ {

/// `foo::bar::baz as quux`
///
/// or just
Expand Down Expand Up @@ -1185,32 +1184,48 @@ pub type FieldIter<'a> = iter::FlatMap<option::IntoIter<&'a Vec<StructField>>,

impl VariantData {
pub fn fields(&self) -> FieldIter {
fn vec_iter<T>(v: &Vec<T>) -> slice::Iter<T> { v.iter() }
fn vec_iter<T>(v: &Vec<T>) -> slice::Iter<T> {
v.iter()
}
match *self {
VariantData::Struct(ref fields, _) | VariantData::Tuple(ref fields, _) => Some(fields),
_ => None,
}.into_iter().flat_map(vec_iter)
}
.into_iter()
.flat_map(vec_iter)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure the above is worse than original.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustfmt fix is in the works...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed now

}
pub fn id(&self) -> NodeId {
match *self {
VariantData::Struct(_, id) | VariantData::Tuple(_, id) | VariantData::Unit(id) => id
VariantData::Struct(_, id) | VariantData::Tuple(_, id) | VariantData::Unit(id) => id,
}
}
pub fn is_struct(&self) -> bool {
if let VariantData::Struct(..) = *self { true } else { false }
if let VariantData::Struct(..) = *self {
true
} else {
false
}
}
pub fn is_tuple(&self) -> bool {
if let VariantData::Tuple(..) = *self { true } else { false }
if let VariantData::Tuple(..) = *self {
true
} else {
false
}
}
pub fn is_unit(&self) -> bool {
if let VariantData::Unit(..) = *self { true } else { false }
if let VariantData::Unit(..) = *self {
true
} else {
false
}
}
}

/*
FIXME (#3300): Should allow items to be anonymous. Right now
we just use dummy names for anon items.
*/
//
// FIXME (#3300): Should allow items to be anonymous. Right now
// we just use dummy names for anon items.
//
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/nrc/rustfmt/issues/539

Could you fix this manually for now please?

/// An item
///
/// The name might be a dummy name in case of anonymous items
Expand Down Expand Up @@ -1253,7 +1268,7 @@ pub enum Item_ {
ItemTrait(Unsafety, Generics, TyParamBounds, Vec<P<TraitItem>>),

// Default trait implementations
///
//
// `impl Trait for .. {}`
ItemDefaultImpl(Unsafety, TraitRef),
/// An implementation, eg `impl<A> Trait for Foo { .. }`
Expand Down
Loading