Skip to content

Handle case where there is a where clause but no generic parameters. #2117

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

Merged
merged 1 commit into from
Nov 4, 2017
Merged
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
12 changes: 10 additions & 2 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ pub fn format_struct_struct(
let header_str = format_header(item_name, ident, vis);
result.push_str(&header_str);

let header_hi = span.lo() + BytePos(header_str.len() as u32);
let body_lo = context.codemap.span_after(span, "{");

let generics_str = match generics {
Expand All @@ -1081,7 +1082,7 @@ pub fn format_struct_struct(
context.config.item_brace_style(),
fields.is_empty(),
offset,
mk_sp(span.lo(), body_lo),
mk_sp(header_hi, body_lo),
last_line_width(&result),
)?,
None => {
Expand Down Expand Up @@ -2663,6 +2664,13 @@ fn format_generics(
let same_line_brace = if !generics.where_clause.predicates.is_empty() || result.contains('\n') {
let budget = context.budget(last_line_used_width(&result, offset.width()));
let option = WhereClauseOption::snuggled(&result);
// If the generics are not parameterized then generics.span.hi() == 0,
// so we use span.lo(), which is the position after `struct Foo`.
let span_end_before_where = if generics.is_parameterized() {
generics.span.hi()
} else {
span.lo()
};
let where_clause_str = rewrite_where_clause(
context,
&generics.where_clause,
Expand All @@ -2671,7 +2679,7 @@ fn format_generics(
Density::Tall,
terminator,
Some(span.hi()),
generics.span.hi(),
span_end_before_where,
option,
)?;
result.push_str(&where_clause_str);
Expand Down
14 changes: 14 additions & 0 deletions tests/target/issue-2103.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
struct X
where
i32: Sized,
{
x: i32,
}

struct X
// with comment
where
i32: Sized,
{
x: i32,
}