Skip to content

Commit 92ab76c

Browse files
ayazhafizcalebcartwright
authored andcommitted
fixup! Preserve and format type aliases in extern blocks
1 parent a9b0b05 commit 92ab76c

File tree

2 files changed

+36
-77
lines changed

2 files changed

+36
-77
lines changed

src/items.rs

Lines changed: 34 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,44 +1504,44 @@ fn format_tuple_struct(
15041504
Some(result)
15051505
}
15061506

1507-
fn rewrite_type_prefix(
1507+
fn rewrite_type<R: Rewrite>(
15081508
context: &RewriteContext<'_>,
15091509
indent: Indent,
1510-
prefix: &str,
15111510
ident: symbol::Ident,
1511+
vis: &ast::Visibility,
15121512
generics: &ast::Generics,
15131513
generic_bounds_opt: Option<&ast::GenericBounds>,
1514+
rhs: Option<&R>,
15141515
) -> Option<String> {
15151516
let mut result = String::with_capacity(128);
1516-
result.push_str(prefix);
1517+
result.push_str(&format!("{}type ", format_visibility(context, vis)));
15171518
let ident_str = rewrite_ident(context, ident);
15181519

1519-
// 2 = `= `
15201520
if generics.params.is_empty() {
15211521
result.push_str(ident_str)
15221522
} else {
1523+
// 2 = `= `
15231524
let g_shape = Shape::indented(indent, context.config)
15241525
.offset_left(result.len())?
15251526
.sub_width(2)?;
15261527
let generics_str = rewrite_generics(context, ident_str, generics, g_shape)?;
15271528
result.push_str(&generics_str);
15281529
}
15291530

1530-
let type_bounds_str = if let Some(bounds) = generic_bounds_opt {
1531-
if bounds.is_empty() {
1532-
String::new()
1533-
} else {
1531+
if let Some(bounds) = generic_bounds_opt {
1532+
if !bounds.is_empty() {
15341533
// 2 = `: `
15351534
let shape = Shape::indented(indent, context.config).offset_left(result.len() + 2)?;
1536-
bounds.rewrite(context, shape).map(|s| format!(": {}", s))?
1535+
let type_bounds = bounds.rewrite(context, shape).map(|s| format!(": {}", s))?;
1536+
result.push_str(&type_bounds);
15371537
}
1538-
} else {
1539-
String::new()
1540-
};
1541-
result.push_str(&type_bounds_str);
1538+
}
15421539

15431540
let where_budget = context.budget(last_line_width(&result));
1544-
let option = WhereClauseOption::snuggled(&result);
1541+
let mut option = WhereClauseOption::snuggled(&result);
1542+
if rhs.is_none() {
1543+
option.suppress_comma();
1544+
}
15451545
let where_clause_str = rewrite_where_clause(
15461546
context,
15471547
&generics.where_clause,
@@ -1555,40 +1555,22 @@ fn rewrite_type_prefix(
15551555
)?;
15561556
result.push_str(&where_clause_str);
15571557

1558-
Some(result)
1559-
}
1560-
1561-
fn rewrite_type_item<R: Rewrite>(
1562-
context: &RewriteContext<'_>,
1563-
indent: Indent,
1564-
prefix: &str,
1565-
suffix: &str,
1566-
ident: symbol::Ident,
1567-
rhs: &R,
1568-
generics: &ast::Generics,
1569-
generic_bounds_opt: Option<&ast::GenericBounds>,
1570-
vis: &ast::Visibility,
1571-
) -> Option<String> {
1572-
let mut result = String::with_capacity(128);
1573-
result.push_str(&rewrite_type_prefix(
1574-
context,
1575-
indent,
1576-
&format!("{}{} ", format_visibility(context, vis), prefix),
1577-
ident,
1578-
generics,
1579-
generic_bounds_opt,
1580-
)?);
1558+
if let Some(ty) = rhs {
1559+
// If there's a where clause, add a newline before the assignment. Otherwise just add a
1560+
// space.
1561+
if !generics.where_clause.predicates.is_empty() {
1562+
result.push_str(&indent.to_string_with_newline(context.config));
1563+
} else {
1564+
result.push(' ');
1565+
}
1566+
let lhs = format!("{}=", result);
15811567

1582-
if generics.where_clause.predicates.is_empty() {
1583-
result.push_str(suffix);
1568+
// 1 = `;`
1569+
let shape = Shape::indented(indent, context.config).sub_width(1)?;
1570+
rewrite_assign_rhs(context, lhs, &*ty, shape).map(|s| s + ";")
15841571
} else {
1585-
result.push_str(&indent.to_string_with_newline(context.config));
1586-
result.push_str(suffix.trim_start());
1572+
Some(format!("{};", result))
15871573
}
1588-
1589-
// 1 = ";"
1590-
let rhs_shape = Shape::indented(indent, context.config).sub_width(1)?;
1591-
rewrite_assign_rhs(context, result, rhs, rhs_shape).map(|s| s + ";")
15921574
}
15931575

15941576
pub(crate) fn rewrite_opaque_type(
@@ -1600,16 +1582,14 @@ pub(crate) fn rewrite_opaque_type(
16001582
vis: &ast::Visibility,
16011583
) -> Option<String> {
16021584
let opaque_type_bounds = OpaqueTypeBounds { generic_bounds };
1603-
rewrite_type_item(
1585+
rewrite_type(
16041586
context,
16051587
indent,
1606-
"type",
1607-
" =",
16081588
ident,
1609-
&opaque_type_bounds,
1589+
vis,
16101590
generics,
16111591
Some(generic_bounds),
1612-
vis,
1592+
Some(&opaque_type_bounds),
16131593
)
16141594
}
16151595

@@ -1839,34 +1819,15 @@ pub(crate) fn rewrite_type_alias(
18391819
indent: Indent,
18401820
vis: &ast::Visibility,
18411821
) -> Option<String> {
1842-
let mut prefix = rewrite_type_prefix(
1822+
rewrite_type(
18431823
context,
18441824
indent,
1845-
&format!("{}type ", format_visibility(context, vis)),
18461825
ident,
1826+
vis,
18471827
generics,
18481828
generic_bounds_opt,
1849-
)?;
1850-
1851-
if let Some(ty) = ty_opt {
1852-
// 1 = `;`
1853-
let shape = Shape::indented(indent, context.config).sub_width(1)?;
1854-
1855-
// If there's a where clause, add a newline before the assignment. Otherwise just add a
1856-
// space.
1857-
if !generics.where_clause.predicates.is_empty() {
1858-
prefix.push_str(&indent.to_string_with_newline(context.config));
1859-
} else {
1860-
prefix.push(' ');
1861-
}
1862-
let lhs = format!("{}=", prefix);
1863-
rewrite_assign_rhs(context, lhs, &**ty, shape).map(|s| s + ";")
1864-
} else {
1865-
if !generics.where_clause.predicates.is_empty() {
1866-
prefix.push_str(&indent.to_string_with_newline(context.config));
1867-
}
1868-
Some(format!("{};", prefix))
1869-
}
1829+
ty_opt,
1830+
)
18701831
}
18711832

18721833
struct OpaqueType<'a> {

tests/target/issue-4159.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ extern "C" {
33

44
type A<'a>
55
where
6-
'a: 'static,
7-
;
6+
'a: 'static;
87

98
type A<T: Ord>
109
where
11-
T: 'static,
12-
;
10+
T: 'static;
1311

1412
type A = u8;
1513

0 commit comments

Comments
 (0)