Auto merge of #17360 - Veykril:rename-alias-foreign, r=Veykril

fix: Fix renaming imports of foreign items touching foreign sources

Fixes https://github.com/rust-lang/rust-analyzer/issues/17318
This commit is contained in:
bors 2024-06-07 12:38:20 +00:00
commit 47355766ca

View file

@ -119,9 +119,9 @@ pub(crate) fn rename(
}; };
let mut source_change = SourceChange::default(); let mut source_change = SourceChange::default();
source_change.extend(usages.iter().map(|(&file_id, refs)| { source_change.extend(usages.references.get_mut(&position.file_id).iter().map(
(file_id, source_edit_from_references(refs, def, new_name)) |refs| (position.file_id, source_edit_from_references(refs, def, new_name)),
})); ));
Ok(source_change) Ok(source_change)
}) })
@ -444,12 +444,8 @@ mod tests {
use super::{RangeInfo, RenameError}; use super::{RangeInfo, RenameError};
fn check(new_name: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
check_with_rename_config(new_name, ra_fixture_before, ra_fixture_after);
}
#[track_caller] #[track_caller]
fn check_with_rename_config(new_name: &str, ra_fixture_before: &str, ra_fixture_after: &str) { fn check(new_name: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
let ra_fixture_after = &trim_indent(ra_fixture_after); let ra_fixture_after = &trim_indent(ra_fixture_after);
let (analysis, position) = fixture::position(ra_fixture_before); let (analysis, position) = fixture::position(ra_fixture_before);
if !ra_fixture_after.starts_with("error: ") { if !ra_fixture_after.starts_with("error: ") {
@ -466,7 +462,7 @@ mod tests {
let (&file_id, edit) = match source_change.source_file_edits.len() { let (&file_id, edit) = match source_change.source_file_edits.len() {
0 => return, 0 => return,
1 => source_change.source_file_edits.iter().next().unwrap(), 1 => source_change.source_file_edits.iter().next().unwrap(),
_ => (&position.file_id, &source_change.source_file_edits[&position.file_id]), _ => panic!(),
}; };
for indel in edit.0.iter() { for indel in edit.0.iter() {
text_edit_builder.replace(indel.delete, indel.insert.clone()); text_edit_builder.replace(indel.delete, indel.insert.clone());
@ -2689,7 +2685,7 @@ use qux as frob;
#[test] #[test]
fn disallow_renaming_for_non_local_definition() { fn disallow_renaming_for_non_local_definition() {
check_with_rename_config( check(
"Baz", "Baz",
r#" r#"
//- /lib.rs crate:lib new_source_root:library //- /lib.rs crate:lib new_source_root:library
@ -2704,7 +2700,7 @@ fn main() { let _: S$0; }
#[test] #[test]
fn disallow_renaming_for_builtin_macros() { fn disallow_renaming_for_builtin_macros() {
check_with_rename_config( check(
"Baz", "Baz",
r#" r#"
//- minicore: derive, hash //- minicore: derive, hash
@ -2762,14 +2758,19 @@ fn test() {
check( check(
"Baz", "Baz",
r#" r#"
//- /main.rs crate:main
mod module;
mod foo { pub struct Foo; } mod foo { pub struct Foo; }
mod bar { use super::Foo; } mod bar { use super::Foo; }
use foo::Foo$0; use foo::Foo$0;
fn main() { let _: Foo; } fn main() { let _: Foo; }
//- /module.rs
use crate::foo::Foo;
"#, "#,
r#" r#"
mod module;
mod foo { pub struct Foo; } mod foo { pub struct Foo; }
mod bar { use super::Baz; } mod bar { use super::Baz; }
@ -2779,4 +2780,22 @@ fn main() { let _: Baz; }
"#, "#,
) )
} }
#[test]
fn rename_path_inside_use_tree_foreign() {
check(
"Baz",
r#"
//- /lib.rs crate:lib new_source_root:library
pub struct S;
//- /main.rs crate:main deps:lib new_source_root:local
use lib::S$0;
fn main() { let _: S; }
"#,
r#"
use lib::S as Baz;
fn main() { let _: Baz; }
"#,
);
}
} }