Skip to content

Commit 635efdf

Browse files
authored
Merge pull request rust-lang#3035 from topecongiro/issue-3006
Format generics on associated types
2 parents 829dbfa + 7eca33f commit 635efdf

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

src/items.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1715,11 +1715,16 @@ fn rewrite_static(
17151715
pub fn rewrite_associated_type(
17161716
ident: ast::Ident,
17171717
ty_opt: Option<&ptr::P<ast::Ty>>,
1718+
generics: &ast::Generics,
17181719
generic_bounds_opt: Option<&ast::GenericBounds>,
17191720
context: &RewriteContext,
17201721
indent: Indent,
17211722
) -> Option<String> {
1722-
let prefix = format!("type {}", rewrite_ident(context, ident));
1723+
let ident_str = rewrite_ident(context, ident);
1724+
// 5 = "type "
1725+
let generics_shape = Shape::indented(indent, context.config).offset_left(5)?;
1726+
let generics_str = rewrite_generics(context, ident_str, generics, generics_shape)?;
1727+
let prefix = format!("type {}", generics_str);
17231728

17241729
let type_bounds_str = if let Some(bounds) = generic_bounds_opt {
17251730
if bounds.is_empty() {
@@ -1746,21 +1751,23 @@ pub fn rewrite_associated_type(
17461751
pub fn rewrite_existential_impl_type(
17471752
context: &RewriteContext,
17481753
ident: ast::Ident,
1754+
generics: &ast::Generics,
17491755
generic_bounds: &ast::GenericBounds,
17501756
indent: Indent,
17511757
) -> Option<String> {
1752-
rewrite_associated_type(ident, None, Some(generic_bounds), context, indent)
1758+
rewrite_associated_type(ident, None, generics, Some(generic_bounds), context, indent)
17531759
.map(|s| format!("existential {}", s))
17541760
}
17551761

17561762
pub fn rewrite_associated_impl_type(
17571763
ident: ast::Ident,
17581764
defaultness: ast::Defaultness,
17591765
ty_opt: Option<&ptr::P<ast::Ty>>,
1766+
generics: &ast::Generics,
17601767
context: &RewriteContext,
17611768
indent: Indent,
17621769
) -> Option<String> {
1763-
let result = rewrite_associated_type(ident, ty_opt, None, context, indent)?;
1770+
let result = rewrite_associated_type(ident, ty_opt, generics, None, context, indent)?;
17641771

17651772
match defaultness {
17661773
ast::Defaultness::Default => Some(format!("default {}", result)),

src/visitor.rs

+3
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
497497
let rewrite = rewrite_associated_type(
498498
ti.ident,
499499
type_default.as_ref(),
500+
&ti.generics,
500501
Some(generic_bounds),
501502
&self.get_context(),
502503
self.block_indent,
@@ -535,6 +536,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
535536
ii.ident,
536537
ii.defaultness,
537538
Some(ty),
539+
&ii.generics,
538540
&self.get_context(),
539541
self.block_indent,
540542
);
@@ -544,6 +546,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
544546
let rewrite = rewrite_existential_impl_type(
545547
&self.get_context(),
546548
ii.ident,
549+
&ii.generics,
547550
generic_bounds,
548551
self.block_indent,
549552
);

tests/source/trait.rs

+9
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,12 @@ trait FooBar = Foo
9797
auto trait Example {}
9898
pub auto trait PubExample {}
9999
pub unsafe auto trait PubUnsafeExample {}
100+
101+
// #3006
102+
trait Foo<'a> {
103+
type Bar< 'a >;
104+
}
105+
106+
impl<'a> Foo<'a> for i32 {
107+
type Bar< 'a > = i32;
108+
}

tests/target/trait.rs

+9
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,12 @@ trait FooBar = Foo
135135
auto trait Example {}
136136
pub auto trait PubExample {}
137137
pub unsafe auto trait PubUnsafeExample {}
138+
139+
// #3006
140+
trait Foo<'a> {
141+
type Bar<'a>;
142+
}
143+
144+
impl<'a> Foo<'a> for i32 {
145+
type Bar<'a> = i32;
146+
}

0 commit comments

Comments
 (0)