Skip to content

Commit d87c77b

Browse files
committed
Preserve and format type aliases in extern blocks
Previously, non-trivial type aliases in extern blocks were dropped by rustfmt because only the type alias name would be passed to a rewritter. This commit fixes that by passing all type information (generics, bounds, and assignments) to a type alias rewritter, and consolidates `rewrite_type_alias` and `rewrite_associated_type` as one function. Closes rust-lang#4159
1 parent c83e729 commit d87c77b

File tree

3 files changed

+81
-48
lines changed

3 files changed

+81
-48
lines changed

rustfmt-core/rustfmt-lib/src/items.rs

+54-41
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,7 @@ fn rewrite_type_prefix(
15621562
prefix: &str,
15631563
ident: ast::Ident,
15641564
generics: &ast::Generics,
1565+
generic_bounds_opt: Option<&ast::GenericBounds>,
15651566
) -> Option<String> {
15661567
let mut result = String::with_capacity(128);
15671568
result.push_str(prefix);
@@ -1578,6 +1579,19 @@ fn rewrite_type_prefix(
15781579
result.push_str(&generics_str);
15791580
}
15801581

1582+
let type_bounds_str = if let Some(bounds) = generic_bounds_opt {
1583+
if bounds.is_empty() {
1584+
String::new()
1585+
} else {
1586+
// 2 = `: `
1587+
let shape = Shape::indented(indent, context.config).offset_left(result.len() + 2)?;
1588+
bounds.rewrite(context, shape).map(|s| format!(": {}", s))?
1589+
}
1590+
} else {
1591+
String::new()
1592+
};
1593+
result.push_str(&type_bounds_str);
1594+
15811595
let where_budget = context.budget(last_line_width(&result));
15821596
let option = WhereClauseOption::snuggled(&result);
15831597
let where_clause_str = rewrite_where_clause(
@@ -1604,6 +1618,7 @@ fn rewrite_type_item<R: Rewrite>(
16041618
ident: ast::Ident,
16051619
rhs: &R,
16061620
generics: &ast::Generics,
1621+
generic_bounds_opt: Option<&ast::GenericBounds>,
16071622
vis: &ast::Visibility,
16081623
) -> Option<String> {
16091624
let mut result = String::with_capacity(128);
@@ -1613,6 +1628,7 @@ fn rewrite_type_item<R: Rewrite>(
16131628
&format!("{}{} ", format_visibility(context, vis), prefix),
16141629
ident,
16151630
generics,
1631+
generic_bounds_opt,
16161632
)?);
16171633

16181634
if generics.where_clause.predicates.is_empty() {
@@ -1627,17 +1643,6 @@ fn rewrite_type_item<R: Rewrite>(
16271643
rewrite_assign_rhs(context, result, rhs, rhs_shape).map(|s| s + ";")
16281644
}
16291645

1630-
pub(crate) fn rewrite_type_alias(
1631-
context: &RewriteContext<'_>,
1632-
indent: Indent,
1633-
ident: ast::Ident,
1634-
ty: &ast::Ty,
1635-
generics: &ast::Generics,
1636-
vis: &ast::Visibility,
1637-
) -> Option<String> {
1638-
rewrite_type_item(context, indent, "type", " =", ident, ty, generics, vis)
1639-
}
1640-
16411646
pub(crate) fn rewrite_opaque_type(
16421647
context: &RewriteContext<'_>,
16431648
indent: Indent,
@@ -1655,6 +1660,7 @@ pub(crate) fn rewrite_opaque_type(
16551660
ident,
16561661
&opaque_type_bounds,
16571662
generics,
1663+
Some(generic_bounds),
16581664
vis,
16591665
)
16601666
}
@@ -1897,39 +1903,39 @@ fn rewrite_static(
18971903
}
18981904
}
18991905

1900-
pub(crate) fn rewrite_associated_type(
1906+
pub(crate) fn rewrite_type_alias(
19011907
ident: ast::Ident,
19021908
ty_opt: Option<&ptr::P<ast::Ty>>,
19031909
generics: &ast::Generics,
19041910
generic_bounds_opt: Option<&ast::GenericBounds>,
19051911
context: &RewriteContext<'_>,
19061912
indent: Indent,
1913+
vis: &ast::Visibility,
19071914
) -> Option<String> {
1908-
let ident_str = rewrite_ident(context, ident);
1909-
// 5 = "type "
1910-
let generics_shape = Shape::indented(indent, context.config).offset_left(5)?;
1911-
let generics_str = rewrite_generics(context, ident_str, generics, generics_shape)?;
1912-
let prefix = format!("type {}", generics_str);
1913-
1914-
let type_bounds_str = if let Some(bounds) = generic_bounds_opt {
1915-
if bounds.is_empty() {
1916-
String::new()
1917-
} else {
1918-
// 2 = ": ".len()
1919-
let shape = Shape::indented(indent, context.config).offset_left(prefix.len() + 2)?;
1920-
bounds.rewrite(context, shape).map(|s| format!(": {}", s))?
1921-
}
1922-
} else {
1923-
String::new()
1924-
};
1915+
let mut prefix = rewrite_type_prefix(
1916+
context,
1917+
indent,
1918+
&format!("{}type ", format_visibility(context, vis)),
1919+
ident,
1920+
generics,
1921+
generic_bounds_opt,
1922+
)?;
19251923

19261924
if let Some(ty) = ty_opt {
19271925
// 1 = `;`
19281926
let shape = Shape::indented(indent, context.config).sub_width(1)?;
1929-
let lhs = format!("{}{} =", prefix, type_bounds_str);
1927+
1928+
// If there's a where clause, add a newline before the assignment. Otherwise just add a
1929+
// space.
1930+
if !generics.where_clause.predicates.is_empty() {
1931+
prefix.push_str(&indent.to_string_with_newline(context.config));
1932+
} else {
1933+
prefix.push(' ');
1934+
}
1935+
let lhs = format!("{}=", prefix);
19301936
rewrite_assign_rhs(context, lhs, &**ty, shape).map(|s| s + ";")
19311937
} else {
1932-
Some(format!("{}{};", prefix, type_bounds_str))
1938+
Some(format!("{};", prefix))
19331939
}
19341940
}
19351941

@@ -1973,13 +1979,14 @@ pub(crate) fn rewrite_opaque_impl_type(
19731979

19741980
pub(crate) fn rewrite_associated_impl_type(
19751981
ident: ast::Ident,
1982+
vis: &ast::Visibility,
19761983
defaultness: ast::Defaultness,
19771984
ty_opt: Option<&ptr::P<ast::Ty>>,
19781985
generics: &ast::Generics,
19791986
context: &RewriteContext<'_>,
19801987
indent: Indent,
19811988
) -> Option<String> {
1982-
let result = rewrite_associated_type(ident, ty_opt, generics, None, context, indent)?;
1989+
let result = rewrite_type_alias(ident, ty_opt, generics, None, context, indent, vis)?;
19831990

19841991
match defaultness {
19851992
ast::Defaultness::Default(..) => Some(format!("default {}", result)),
@@ -3132,7 +3139,7 @@ impl Rewrite for ast::ForeignItem {
31323139
// FIXME: this may be a faulty span from libsyntax.
31333140
let span = mk_sp(self.span.lo(), self.span.hi() - BytePos(1));
31343141

3135-
let item_str = match self.kind {
3142+
let item_str: String = match self.kind {
31363143
ast::ForeignItemKind::Fn(_, ref fn_sig, ref generics, _) => rewrite_fn_base(
31373144
context,
31383145
shape.indent,
@@ -3156,14 +3163,20 @@ impl Rewrite for ast::ForeignItem {
31563163
// 1 = ;
31573164
rewrite_assign_rhs(context, prefix, &**ty, shape.sub_width(1)?).map(|s| s + ";")
31583165
}
3159-
ast::ForeignItemKind::TyAlias(..) => {
3160-
let vis = format_visibility(context, &self.vis);
3161-
Some(format!(
3162-
"{}type {};",
3163-
vis,
3164-
rewrite_ident(context, self.ident)
3165-
))
3166-
}
3166+
ast::ForeignItemKind::TyAlias(
3167+
_,
3168+
ref generics,
3169+
ref generic_bounds,
3170+
ref type_default,
3171+
) => rewrite_type_alias(
3172+
self.ident,
3173+
type_default.as_ref(),
3174+
generics,
3175+
Some(generic_bounds),
3176+
&context,
3177+
shape.indent,
3178+
&self.vis,
3179+
),
31673180
ast::ForeignItemKind::MacCall(ref mac) => {
31683181
rewrite_macro(mac, None, context, shape, MacroPosition::Item)
31693182
}

rustfmt-core/rustfmt-lib/src/visitor.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ use crate::comment::{rewrite_comment, CodeCharKind, CommentCodeSlices};
99
use crate::config::{BraceStyle, Config};
1010
use crate::items::{
1111
format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item,
12-
rewrite_associated_impl_type, rewrite_associated_type, rewrite_extern_crate,
13-
rewrite_opaque_impl_type, rewrite_opaque_type, rewrite_type_alias, FnBraceStyle, FnSig,
14-
StaticParts, StructParts,
12+
rewrite_associated_impl_type, rewrite_extern_crate, rewrite_opaque_impl_type,
13+
rewrite_opaque_type, rewrite_type_alias, FnBraceStyle, FnSig, StaticParts, StructParts,
1514
};
1615
use crate::macros::{macro_style, rewrite_macro, rewrite_macro_def, MacroPosition};
1716
use crate::rewrite::{Rewrite, RewriteContext};
@@ -538,11 +537,12 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
538537
ast::ItemKind::TyAlias(_, ref generics, ref generic_bounds, ref ty) => match ty {
539538
Some(ty) => {
540539
let rewrite = rewrite_type_alias(
541-
&self.get_context(),
542-
self.block_indent,
543540
item.ident,
544-
&*ty,
541+
Some(&*ty),
545542
generics,
543+
Some(generic_bounds),
544+
&self.get_context(),
545+
self.block_indent,
546546
&item.vis,
547547
);
548548
self.push_rewrite(item.span, rewrite);
@@ -609,13 +609,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
609609
);
610610
}
611611
ast::AssocItemKind::TyAlias(_, ref generics, ref generic_bounds, ref type_default) => {
612-
let rewrite = rewrite_associated_type(
612+
let rewrite = rewrite_type_alias(
613613
ti.ident,
614614
type_default.as_ref(),
615615
generics,
616616
Some(generic_bounds),
617617
&self.get_context(),
618618
self.block_indent,
619+
&ti.vis,
619620
);
620621
self.push_rewrite(ti.span, rewrite);
621622
}
@@ -656,6 +657,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
656657
let rewrite_associated = || {
657658
rewrite_associated_impl_type(
658659
ii.ident,
660+
&ii.vis,
659661
defaultness,
660662
ty.as_ref(),
661663
generics,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
extern "C" {
2+
type A: Ord;
3+
4+
type A<'a>
5+
where
6+
'a: 'static,;
7+
8+
type A<T: Ord>
9+
where
10+
T: 'static,;
11+
12+
type A = u8;
13+
14+
type A<'a: 'static, T: Ord + 'static>: Eq + PartialEq
15+
where
16+
T: 'static + Copy,
17+
= Vec<u8>;
18+
}

0 commit comments

Comments
 (0)