Skip to content

Commit

Permalink
Auto merge of rust-lang#16199 - Young-Flash:extract_struct_with_Self,…
Browse files Browse the repository at this point in the history
… r=Veykril

fix: `extract_struct_from_enum_variant` assist should resolve Self generic arg

close rust-lang/rust-analyzer#16197
  • Loading branch information
bors committed Jan 5, 2024
2 parents 2980d54 + 1a9b1b8 commit 5945709
Showing 1 changed file with 66 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ide_db::{
defs::Definition,
helpers::mod_path_to_ast,
imports::insert_use::{insert_use, ImportScope, InsertUseConfig},
path_transform::PathTransform,
search::FileReference,
FxHashSet, RootDatabase,
};
Expand Down Expand Up @@ -105,6 +106,16 @@ pub(crate) fn extract_struct_from_enum_variant(
.generic_param_list()
.and_then(|known_generics| extract_generic_params(&known_generics, &field_list));
let generics = generic_params.as_ref().map(|generics| generics.clone_for_update());

// resolve GenericArg in field_list to actual type
let field_list = field_list.clone_for_update();
if let Some((target_scope, source_scope)) =
ctx.sema.scope(enum_ast.syntax()).zip(ctx.sema.scope(field_list.syntax()))
{
PathTransform::generic_transformation(&target_scope, &source_scope)
.apply(field_list.syntax());
}

let def =
create_struct_def(variant_name.clone(), &variant, &field_list, generics, &enum_ast);

Expand Down Expand Up @@ -244,8 +255,6 @@ fn create_struct_def(
// for fields without any existing visibility, use visibility of enum
let field_list: ast::FieldList = match field_list {
Either::Left(field_list) => {
let field_list = field_list.clone_for_update();

if let Some(vis) = &enum_vis {
field_list
.fields()
Expand All @@ -254,11 +263,9 @@ fn create_struct_def(
.for_each(|it| insert_vis(it.syntax(), vis.syntax()));
}

field_list.into()
field_list.clone().into()
}
Either::Right(field_list) => {
let field_list = field_list.clone_for_update();

if let Some(vis) = &enum_vis {
field_list
.fields()
Expand All @@ -267,7 +274,7 @@ fn create_struct_def(
.for_each(|it| insert_vis(it.syntax(), vis.syntax()));
}

field_list.into()
field_list.clone().into()
}
};
field_list.reindent_to(IndentLevel::single());
Expand Down Expand Up @@ -425,6 +432,59 @@ mod tests {

use super::*;

#[test]
fn issue_16197() {
check_assist(
extract_struct_from_enum_variant,
r#"
enum Foo {
Bar $0{ node: Box<Self> },
Nil,
}
"#,
r#"
struct Bar{ node: Box<Foo> }
enum Foo {
Bar(Bar),
Nil,
}
"#,
);
check_assist(
extract_struct_from_enum_variant,
r#"
enum Foo {
Bar $0{ node: Box<Self>, a: Arc<Box<Self>> },
Nil,
}
"#,
r#"
struct Bar{ node: Box<Foo>, a: Arc<Box<Foo>> }
enum Foo {
Bar(Bar),
Nil,
}
"#,
);
check_assist(
extract_struct_from_enum_variant,
r#"
enum Foo {
Nil(Box$0<Self>, Arc<Box<Self>>),
}
"#,
r#"
struct Nil(Box<Foo>, Arc<Box<Foo>>);
enum Foo {
Nil(Nil),
}
"#,
);
}

#[test]
fn test_extract_struct_several_fields_tuple() {
check_assist(
Expand Down

0 comments on commit 5945709

Please sign in to comment.