Skip to content

Commit fefb542

Browse files
fix: handle GAT types in impls with self bounds
1 parent 8cbee56 commit fefb542

File tree

4 files changed

+59
-87
lines changed

4 files changed

+59
-87
lines changed

src/items.rs

Lines changed: 26 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,7 +1497,7 @@ fn format_tuple_struct(
14971497
Some(result)
14981498
}
14991499

1500-
fn rewrite_type<R: Rewrite>(
1500+
pub(crate) fn rewrite_type<R: Rewrite>(
15011501
context: &RewriteContext<'_>,
15021502
indent: Indent,
15031503
ident: symbol::Ident,
@@ -1841,29 +1841,6 @@ fn rewrite_static(
18411841
Some(format!("{}{};", prefix, ty_str))
18421842
}
18431843
}
1844-
1845-
pub(crate) fn rewrite_type_alias(
1846-
ident: symbol::Ident,
1847-
ty_opt: Option<&ptr::P<ast::Ty>>,
1848-
generics: &ast::Generics,
1849-
generic_bounds_opt: Option<&ast::GenericBounds>,
1850-
context: &RewriteContext<'_>,
1851-
indent: Indent,
1852-
vis: &ast::Visibility,
1853-
span: Span,
1854-
) -> Option<String> {
1855-
rewrite_type(
1856-
context,
1857-
indent,
1858-
ident,
1859-
vis,
1860-
generics,
1861-
generic_bounds_opt,
1862-
ty_opt,
1863-
span,
1864-
)
1865-
}
1866-
18671844
struct OpaqueType<'a> {
18681845
bounds: &'a ast::GenericBounds,
18691846
}
@@ -1877,32 +1854,7 @@ impl<'a> Rewrite for OpaqueType<'a> {
18771854
}
18781855
}
18791856

1880-
pub(crate) fn rewrite_opaque_impl_type(
1881-
context: &RewriteContext<'_>,
1882-
ident: symbol::Ident,
1883-
generics: &ast::Generics,
1884-
generic_bounds: &ast::GenericBounds,
1885-
indent: Indent,
1886-
) -> Option<String> {
1887-
let ident_str = rewrite_ident(context, ident);
1888-
// 5 = "type "
1889-
let generics_shape = Shape::indented(indent, context.config).offset_left(5)?;
1890-
let generics_str = rewrite_generics(context, ident_str, generics, generics_shape)?;
1891-
let prefix = format!("type {} =", generics_str);
1892-
let rhs = OpaqueType {
1893-
bounds: generic_bounds,
1894-
};
1895-
1896-
rewrite_assign_rhs(
1897-
context,
1898-
&prefix,
1899-
&rhs,
1900-
Shape::indented(indent, context.config).sub_width(1)?,
1901-
)
1902-
.map(|s| s + ";")
1903-
}
1904-
1905-
pub(crate) fn rewrite_associated_impl_type(
1857+
pub(crate) fn rewrite_impl_type(
19061858
ident: symbol::Ident,
19071859
vis: &ast::Visibility,
19081860
defaultness: ast::Defaultness,
@@ -1912,7 +1864,25 @@ pub(crate) fn rewrite_associated_impl_type(
19121864
indent: Indent,
19131865
span: Span,
19141866
) -> Option<String> {
1915-
let result = rewrite_type_alias(ident, ty_opt, generics, None, context, indent, vis, span)?;
1867+
// Opaque type
1868+
let result = if let Some(rustc_ast::ast::Ty {
1869+
kind: ast::TyKind::ImplTrait(_, ref bounds),
1870+
..
1871+
}) = ty_opt.map(|t| &**t)
1872+
{
1873+
rewrite_type(
1874+
context,
1875+
indent,
1876+
ident,
1877+
&DEFAULT_VISIBILITY,
1878+
generics,
1879+
None,
1880+
Some(&OpaqueType { bounds }),
1881+
span,
1882+
)
1883+
} else {
1884+
rewrite_type(context, indent, ident, vis, generics, None, ty_opt, span)
1885+
}?;
19161886

19171887
match defaultness {
19181888
ast::Defaultness::Default(..) => Some(format!("default {}", result)),
@@ -3164,14 +3134,14 @@ impl Rewrite for ast::ForeignItem {
31643134
ast::ForeignItemKind::TyAlias(ref ty_alias_kind) => {
31653135
let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref type_default) =
31663136
**ty_alias_kind;
3167-
rewrite_type_alias(
3168-
self.ident,
3169-
type_default.as_ref(),
3170-
generics,
3171-
Some(generic_bounds),
3137+
rewrite_type(
31723138
&context,
31733139
shape.indent,
3140+
self.ident,
31743141
&self.vis,
3142+
generics,
3143+
Some(generic_bounds),
3144+
type_default.as_ref(),
31753145
self.span,
31763146
)
31773147
}

src/visitor.rs

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use crate::config::Version;
1111
use crate::config::{BraceStyle, Config};
1212
use crate::coverage::transform_missing_snippet;
1313
use crate::items::{
14-
format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item,
15-
rewrite_associated_impl_type, rewrite_extern_crate, rewrite_opaque_impl_type,
16-
rewrite_opaque_type, rewrite_type_alias, FnBraceStyle, FnSig, StaticParts, StructParts,
14+
format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item, rewrite_extern_crate,
15+
rewrite_impl_type, rewrite_opaque_type, rewrite_type, FnBraceStyle, FnSig, StaticParts,
16+
StructParts,
1717
};
1818
use crate::macros::{macro_style, rewrite_macro, rewrite_macro_def, MacroPosition};
1919
use crate::modules::Module;
@@ -579,14 +579,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
579579
**alias_kind;
580580
match ty {
581581
Some(ty) => {
582-
let rewrite = rewrite_type_alias(
583-
item.ident,
584-
Some(&*ty),
585-
generics,
586-
Some(generic_bounds),
582+
let rewrite = rewrite_type(
587583
&self.get_context(),
588584
self.block_indent,
585+
item.ident,
589586
&item.vis,
587+
generics,
588+
Some(generic_bounds),
589+
Some(&*ty),
590590
item.span,
591591
);
592592
self.push_rewrite(item.span, rewrite);
@@ -665,14 +665,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
665665
ast::AssocItemKind::TyAlias(ref ty_alias_kind) => {
666666
let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref type_default) =
667667
**ty_alias_kind;
668-
let rewrite = rewrite_type_alias(
669-
ti.ident,
670-
type_default.as_ref(),
671-
generics,
672-
Some(generic_bounds),
668+
let rewrite = rewrite_type(
673669
&self.get_context(),
674670
self.block_indent,
671+
ti.ident,
675672
&ti.vis,
673+
generics,
674+
Some(generic_bounds),
675+
type_default.as_ref(),
676676
ti.span,
677677
);
678678
self.push_rewrite(ti.span, rewrite);
@@ -715,8 +715,9 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
715715
ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_impl_item(ii)),
716716
ast::AssocItemKind::TyAlias(ref ty_alias_kind) => {
717717
let ast::TyAliasKind(defaultness, ref generics, _, ref ty) = **ty_alias_kind;
718-
let rewrite_associated = || {
719-
rewrite_associated_impl_type(
718+
self.push_rewrite(
719+
ii.span,
720+
rewrite_impl_type(
720721
ii.ident,
721722
&ii.vis,
722723
defaultness,
@@ -725,22 +726,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
725726
&self.get_context(),
726727
self.block_indent,
727728
ii.span,
728-
)
729-
};
730-
let rewrite = match ty {
731-
None => rewrite_associated(),
732-
Some(ty) => match ty.kind {
733-
ast::TyKind::ImplTrait(_, ref bounds) => rewrite_opaque_impl_type(
734-
&self.get_context(),
735-
ii.ident,
736-
generics,
737-
bounds,
738-
self.block_indent,
739-
),
740-
_ => rewrite_associated(),
741-
},
742-
};
743-
self.push_rewrite(ii.span, rewrite);
729+
),
730+
);
744731
}
745732
ast::AssocItemKind::MacCall(ref mac) => {
746733
self.visit_mac(mac, Some(ii.ident), MacroPosition::Item);

tests/source/issue_4911.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![feature(generic_associated_types)]
2+
#![feature(min_type_alias_impl_trait)]
3+
4+
impl SomeTrait for SomeType {
5+
type SomeGAT<'a> where Self: 'a = impl SomeOtherTrait;
6+
}

tests/target/issue_4911.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(generic_associated_types)]
2+
#![feature(min_type_alias_impl_trait)]
3+
4+
impl SomeTrait for SomeType {
5+
type SomeGAT<'a>
6+
where
7+
Self: 'a,
8+
= impl SomeOtherTrait;
9+
}

0 commit comments

Comments
 (0)