Skip to content

Commit 1625b1e

Browse files
authored
Merge pull request #20957 from A4-Tacks/blanket-assoc-items
Fix missing other assoc items for generate_blanket_trait_impl
2 parents 103f094 + 365798d commit 1625b1e

File tree

1 file changed

+97
-16
lines changed

1 file changed

+97
-16
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_blanket_trait_impl.rs

Lines changed: 97 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use syntax::{
1313
AstNode,
1414
ast::{
1515
self, AssocItem, BlockExpr, GenericParam, HasAttrs, HasGenericParams, HasName,
16-
HasTypeBounds, HasVisibility, edit_in_place::Indent, make,
16+
HasTypeBounds, HasVisibility, edit::AstNodeEdit, make,
1717
},
1818
syntax_editor::Position,
1919
};
@@ -75,7 +75,7 @@ pub(crate) fn generate_blanket_trait_impl(
7575
|builder| {
7676
let mut edit = builder.make_editor(traitd.syntax());
7777
let namety = make::ty_path(make::path_from_text(&name.text()));
78-
let trait_where_clause = traitd.where_clause().map(|it| it.clone_for_update());
78+
let trait_where_clause = traitd.where_clause().map(|it| it.reset_indent());
7979
let bounds = traitd.type_bound_list().and_then(exlucde_sized);
8080
let is_unsafe = traitd.unsafe_token().is_some();
8181
let thisname = this_name(&traitd);
@@ -90,10 +90,6 @@ pub(crate) fn generate_blanket_trait_impl(
9090
let trait_gen_args =
9191
traitd.generic_param_list().map(|param_list| param_list.to_generic_args());
9292

93-
if let Some(ref where_clause) = trait_where_clause {
94-
where_clause.reindent_to(0.into());
95-
}
96-
9793
let impl_ = make::impl_trait(
9894
cfg_attrs(&traitd),
9995
is_unsafe,
@@ -112,20 +108,19 @@ pub(crate) fn generate_blanket_trait_impl(
112108

113109
if let Some(trait_assoc_list) = traitd.assoc_item_list() {
114110
let assoc_item_list = impl_.get_or_create_assoc_item_list();
115-
for method in trait_assoc_list.assoc_items() {
116-
let AssocItem::Fn(method) = method else {
117-
continue;
111+
for item in trait_assoc_list.assoc_items() {
112+
let item = match item {
113+
ast::AssocItem::Fn(method) if method.body().is_none() => {
114+
todo_fn(&method, ctx.config).into()
115+
}
116+
ast::AssocItem::Const(_) | ast::AssocItem::TypeAlias(_) => item,
117+
_ => continue,
118118
};
119-
if method.body().is_some() {
120-
continue;
121-
}
122-
let f = todo_fn(&method, ctx.config).clone_for_update();
123-
f.indent(1.into());
124-
assoc_item_list.add_item(AssocItem::Fn(f));
119+
assoc_item_list.add_item(item.reset_indent().indent(1.into()));
125120
}
126121
}
127122

128-
impl_.indent(indent);
123+
let impl_ = impl_.indent(indent);
129124

130125
edit.insert_all(
131126
Position::after(traitd.syntax()),
@@ -506,6 +501,41 @@ impl<I: Iterator + ?Sized> Foo for $0I {
506501
);
507502
}
508503

504+
#[test]
505+
fn test_gen_blanket_other_assoc_items() {
506+
check_assist(
507+
generate_blanket_trait_impl,
508+
r#"
509+
trait $0Foo {
510+
type Item;
511+
512+
const N: usize;
513+
514+
fn foo(&self);
515+
}
516+
"#,
517+
r#"
518+
trait Foo {
519+
type Item;
520+
521+
const N: usize;
522+
523+
fn foo(&self);
524+
}
525+
526+
impl<T: ?Sized> Foo for $0T {
527+
type Item;
528+
529+
const N: usize;
530+
531+
fn foo(&self) {
532+
todo!()
533+
}
534+
}
535+
"#,
536+
);
537+
}
538+
509539
#[test]
510540
fn test_gen_blanket_indent() {
511541
check_assist(
@@ -739,6 +769,49 @@ mod foo {
739769
}
740770
}
741771
}
772+
}
773+
"#,
774+
);
775+
check_assist(
776+
generate_blanket_trait_impl,
777+
r#"
778+
mod foo {
779+
mod bar {
780+
trait $0Foo {
781+
type Item: Bar<
782+
Self,
783+
>;
784+
785+
const N: Baz<
786+
Self,
787+
>;
788+
}
789+
}
790+
}
791+
"#,
792+
r#"
793+
mod foo {
794+
mod bar {
795+
trait Foo {
796+
type Item: Bar<
797+
Self,
798+
>;
799+
800+
const N: Baz<
801+
Self,
802+
>;
803+
}
804+
805+
impl<T: ?Sized> Foo for $0T {
806+
type Item: Bar<
807+
Self,
808+
>;
809+
810+
const N: Baz<
811+
Self,
812+
>;
813+
}
814+
}
742815
}
743816
"#,
744817
);
@@ -824,6 +897,8 @@ impl<T: Send, T1: ToOwned + ?Sized> Foo<T> for $0T1
824897
where
825898
Self::Owned: Default,
826899
{
900+
type X: Sync;
901+
827902
fn foo(&self, x: Self::X) -> T {
828903
todo!()
829904
}
@@ -871,6 +946,8 @@ where
871946
Self: ToOwned,
872947
Self::Owned: Default,
873948
{
949+
type X: Sync;
950+
874951
fn foo(&self, x: Self::X) -> T {
875952
todo!()
876953
}
@@ -906,6 +983,8 @@ trait Foo<T: Send> {
906983
}
907984
908985
impl<T: Send, T1: ?Sized> Foo<T> for $0T1 {
986+
type X: Sync;
987+
909988
fn foo(&self, x: Self::X) -> T {
910989
todo!()
911990
}
@@ -941,6 +1020,8 @@ trait Foo {
9411020
}
9421021
9431022
impl<T: ?Sized> Foo for $0T {
1023+
type X: Sync;
1024+
9441025
fn foo(&self, x: Self::X) -> i32 {
9451026
todo!()
9461027
}

0 commit comments

Comments
 (0)