Skip to content

Commit

Permalink
Auto merge of rust-lang#16067 - roife:fix-introduce-named-generic-imp…
Browse files Browse the repository at this point in the history
…l-inside-types, r=Veykril

fix: no code action 'introduce_named_generic' for impl inside types

Fix rust-lang#15734.

### Changes Made
- Find params in `ancestors` instead of just `parent`
- Added tests (`replace_impl_with_mut` and `replace_impl_inside`)
  • Loading branch information
bors committed Jan 2, 2024
2 parents 9f46ff5 + 63d2f35 commit e53a115
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion crates/ide-assists/src/handlers/introduce_named_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists};
// ```
pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
let impl_trait_type = ctx.find_node_at_offset::<ast::ImplTraitType>()?;
let param = impl_trait_type.syntax().parent().and_then(ast::Param::cast)?;
let param = impl_trait_type.syntax().ancestors().find_map(|node| ast::Param::cast(node))?;
let fn_ = param.syntax().ancestors().find_map(ast::Fn::cast)?;

let type_bound_list = impl_trait_type.type_bound_list()?;
Expand Down Expand Up @@ -149,4 +149,22 @@ fn foo<
r#"fn foo<$0F: Foo + Bar>(bar: F) {}"#,
);
}

#[test]
fn replace_impl_with_mut() {
check_assist(
introduce_named_generic,
r#"fn f(iter: &mut $0impl Iterator<Item = i32>) {}"#,
r#"fn f<$0I: Iterator<Item = i32>>(iter: &mut I) {}"#,
);
}

#[test]
fn replace_impl_inside() {
check_assist(
introduce_named_generic,
r#"fn f(x: &mut Vec<$0impl Iterator<Item = i32>>) {}"#,
r#"fn f<$0I: Iterator<Item = i32>>(x: &mut Vec<I>) {}"#,
);
}
}

0 comments on commit e53a115

Please sign in to comment.