Skip to content

Commit

Permalink
stop to strip 'impl' from impl trait type alias (rust-lang#3816)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchaser53 authored and topecongiro committed Oct 4, 2019
1 parent e28ae8b commit 7926851
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,18 @@ pub(crate) fn format_trait(
}
}

struct OpaqueTypeBounds<'a> {
generic_bounds: &'a ast::GenericBounds,
}

impl<'a> Rewrite for OpaqueTypeBounds<'a> {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
self.generic_bounds
.rewrite(context, shape)
.map(|s| format!("impl {}", s))
}
}

pub(crate) struct TraitAliasBounds<'a> {
generic_bounds: &'a ast::GenericBounds,
generics: &'a ast::Generics,
Expand Down Expand Up @@ -1524,13 +1536,14 @@ pub(crate) fn rewrite_opaque_type(
generics: &ast::Generics,
vis: &ast::Visibility,
) -> Option<String> {
let opaque_type_bounds = OpaqueTypeBounds { generic_bounds };
rewrite_type_item(
context,
indent,
"type",
" =",
ident,
generic_bounds,
&opaque_type_bounds,
generics,
vis,
)
Expand Down
8 changes: 8 additions & 0 deletions tests/source/existential_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ pub type Adderrr<T> = impl Fn( T ) -> T;
impl Foo for Bar {
type E = impl Trait;
}

pub type Adder_without_impl<F, T>
where
T: Clone,
F: Copy
= Fn(T) -> T;

pub type Adderrr_without_impl<T> = Fn( T ) -> T;
12 changes: 10 additions & 2 deletions tests/target/existential_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ pub type Adder<F, T>
where
T: Clone,
F: Copy,
= Fn(T) -> T;
= impl Fn(T) -> T;

pub type Adderrr<T> = Fn(T) -> T;
pub type Adderrr<T> = impl Fn(T) -> T;

impl Foo for Bar {
type E = impl Trait;
}

pub type Adder_without_impl<F, T>
where
T: Clone,
F: Copy,
= Fn(T) -> T;

pub type Adderrr_without_impl<T> = Fn(T) -> T;
4 changes: 4 additions & 0 deletions tests/target/issue-3815.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub type Type = impl Deref<Target = i8>;

pub type Type =
impl VeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeryLongType<Target = i8>;

0 comments on commit 7926851

Please sign in to comment.