From 492b26cf04b62ff372d5af4ba99e5d5f49079fba Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Tue, 12 Apr 2016 07:05:54 +1200 Subject: [PATCH] Empty structs and struct lits (#920) * Handle empty struct lits Closes #835 * Don't crash on empty struct defs. Not a great fix, but better than crashing. --- src/expr.rs | 5 ++++- src/items.rs | 10 +++++++--- tests/source/struct_lits.rs | 9 +++++++++ tests/source/structs.rs | 3 +++ tests/target/struct_lits.rs | 9 +++++++++ tests/target/structs.rs | 3 +++ 6 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index a0274e4db8a5..696a626bee8c 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1390,7 +1390,6 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext, offset: Indent) -> Option { debug!("rewrite_struct_lit: width {}, offset {:?}", width, offset); - assert!(!fields.is_empty() || base.is_some()); enum StructLitField<'a> { Regular(&'a ast::Field), @@ -1502,6 +1501,10 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext, }; let fields_str = try_opt!(write_list(&item_vec, &fmt)); + if fields_str.is_empty() { + return Some(format!("{} {{}}", path_str)); + } + let format_on_newline = || { let inner_indent = context.block_indent .block_indent(context.config) diff --git a/src/items.rs b/src/items.rs index 2e0cac0df079..82937e5a2ade 100644 --- a/src/items.rs +++ b/src/items.rs @@ -786,7 +786,7 @@ fn format_struct_struct(context: &RewriteContext, }; result.push_str(&generics_str); - // FIXME: properly format empty structs and their comments. + // FIXME(#919): properly format empty structs and their comments. if fields.is_empty() { result.push_str(&context.snippet(mk_sp(body_lo, span.hi))); return Some(result); @@ -838,13 +838,17 @@ fn format_tuple_struct(context: &RewriteContext, span: Span, offset: Indent) -> Option { - assert!(!fields.is_empty(), "Tuple struct with no fields?"); let mut result = String::with_capacity(1024); let header_str = format_header(item_name, ident, vis); result.push_str(&header_str); - let body_lo = fields[0].span.lo; + // FIXME(#919): don't lose comments on empty tuple structs. + let body_lo = if fields.is_empty() { + span.hi + } else { + fields[0].span.lo + }; let where_clause_str = match generics { Some(ref generics) => { diff --git a/tests/source/struct_lits.rs b/tests/source/struct_lits.rs index c62a355c5ddf..1341983ed0fb 100644 --- a/tests/source/struct_lits.rs +++ b/tests/source/struct_lits.rs @@ -123,3 +123,12 @@ fn issue698() { ffffffffffffffffffffffffffields: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, } } + +fn issue835() { + MyStruct {}; + MyStruct { /* a comment */ }; + MyStruct { + // Another comment + }; + MyStruct {} +} diff --git a/tests/source/structs.rs b/tests/source/structs.rs index 38469efa2d95..ff297655713f 100644 --- a/tests/source/structs.rs +++ b/tests/source/structs.rs @@ -151,3 +151,6 @@ struct Issue677 { pub trace: fn( obj: *const libc::c_void, tracer : *mut JSTracer ), } + +struct Foo {} +struct Foo(); diff --git a/tests/target/struct_lits.rs b/tests/target/struct_lits.rs index 299afb422ec9..c8b475df24fb 100644 --- a/tests/target/struct_lits.rs +++ b/tests/target/struct_lits.rs @@ -159,3 +159,12 @@ fn issue698() { aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, } } + +fn issue835() { + MyStruct {}; + MyStruct { /* a comment */ }; + MyStruct { + // Another comment + }; + MyStruct {} +} diff --git a/tests/target/structs.rs b/tests/target/structs.rs index adaa626c096a..428258c4f760 100644 --- a/tests/target/structs.rs +++ b/tests/target/structs.rs @@ -158,3 +158,6 @@ struct Issue677 { pub ptr: *const libc::c_void, pub trace: fn(obj: *const libc::c_void, tracer: *mut JSTracer), } + +struct Foo {} +struct Foo();