Unnamed repository; edit this file 'description' to name the repository.
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
bors 2024-06-07
parent 7c5d496 · parent 379f885 · commit 4735576
-rw-r--r--crates/ide/src/rename.rs41
1 files changed, 30 insertions, 11 deletions
diff --git a/crates/ide/src/rename.rs b/crates/ide/src/rename.rs
index 8c2ae327c7..a13758a9f4 100644
--- a/crates/ide/src/rename.rs
+++ b/crates/ide/src/rename.rs
@@ -119,9 +119,9 @@ pub(crate) fn rename(
};
let mut source_change = SourceChange::default();
- source_change.extend(usages.iter().map(|(&file_id, refs)| {
- (file_id, source_edit_from_references(refs, def, new_name))
- }));
+ source_change.extend(usages.references.get_mut(&position.file_id).iter().map(
+ |refs| (position.file_id, source_edit_from_references(refs, def, new_name)),
+ ));
Ok(source_change)
})
@@ -444,12 +444,8 @@ mod tests {
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]
- 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 (analysis, position) = fixture::position(ra_fixture_before);
if !ra_fixture_after.starts_with("error: ") {
@@ -466,7 +462,7 @@ mod tests {
let (&file_id, edit) = match source_change.source_file_edits.len() {
0 => return,
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() {
text_edit_builder.replace(indel.delete, indel.insert.clone());
@@ -2689,7 +2685,7 @@ use qux as frob;
#[test]
fn disallow_renaming_for_non_local_definition() {
- check_with_rename_config(
+ check(
"Baz",
r#"
//- /lib.rs crate:lib new_source_root:library
@@ -2704,7 +2700,7 @@ fn main() { let _: S$0; }
#[test]
fn disallow_renaming_for_builtin_macros() {
- check_with_rename_config(
+ check(
"Baz",
r#"
//- minicore: derive, hash
@@ -2762,14 +2758,19 @@ fn test() {
check(
"Baz",
r#"
+//- /main.rs crate:main
+mod module;
mod foo { pub struct Foo; }
mod bar { use super::Foo; }
use foo::Foo$0;
fn main() { let _: Foo; }
+//- /module.rs
+use crate::foo::Foo;
"#,
r#"
+mod module;
mod foo { pub struct Foo; }
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; }
+"#,
+ );
+ }
}