Skip to content

Commit d1c7984

Browse files
committed
Auto merge of rust-lang#14407 - Veykril:rename-fix, r=Veykril
fix: Fix renames of locals being broken in macro calls Fixes rust-lang/rust-analyzer#14379
2 parents 90340b7 + 0daf069 commit d1c7984

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

crates/hir-expand/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,7 @@ impl<N: AstNode> InFile<N> {
977977
self.value.syntax().descendants().filter_map(T::cast).map(move |n| self.with_value(n))
978978
}
979979

980+
// FIXME: this should return `Option<InFileNotHirFile<N>>`
980981
pub fn original_ast_node(self, db: &dyn db::ExpandDatabase) -> Option<InFile<N>> {
981982
// This kind of upmapping can only be achieved in attribute expanded files,
982983
// as we don't have node inputs otherwise and therefore can't find an `N` node in the input

crates/ide-db/src/rename.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ fn rename_mod(
207207
}
208208
// The anchor is on the same level as target dir
209209
(false, true, Some(mod_name)) => {
210-
Some((mod_name.unescaped().to_string(), new_name.to_string()))
210+
Some((mod_name.unescaped().to_string(), new_name.to_owned()))
211211
}
212212
_ => None,
213213
};
@@ -232,7 +232,7 @@ fn rename_mod(
232232
{
233233
source_change.insert_source_edit(
234234
file_id,
235-
TextEdit::replace(file_range.range, new_name.to_string()),
235+
TextEdit::replace(file_range.range, new_name.to_owned()),
236236
)
237237
};
238238
}
@@ -442,7 +442,7 @@ fn source_edit_from_name_ref(
442442
let s = field_name.syntax().text_range().start();
443443
let e = pat.syntax().text_range().start();
444444
edit.delete(TextRange::new(s, e));
445-
edit.replace(name.syntax().text_range(), new_name.to_string());
445+
edit.replace(name.syntax().text_range(), new_name.to_owned());
446446
return true;
447447
}
448448
}
@@ -462,7 +462,19 @@ fn source_edit_from_def(
462462
if let Definition::Local(local) = def {
463463
let mut file_id = None;
464464
for source in local.sources(sema.db) {
465-
let source = source.source;
465+
let source = match source.source.clone().original_ast_node(sema.db) {
466+
Some(source) => source,
467+
None => match source.source.syntax().original_file_range_opt(sema.db) {
468+
Some(FileRange { file_id: file_id2, range }) => {
469+
file_id = Some(file_id2);
470+
edit.replace(range, new_name.to_owned());
471+
continue;
472+
}
473+
None => {
474+
bail!("Can't rename local that is defined in a macro declaration")
475+
}
476+
},
477+
};
466478
file_id = source.file_id.file_id();
467479
if let Either::Left(pat) = source.value {
468480
let name_range = pat.name().unwrap().syntax().text_range();
@@ -485,7 +497,7 @@ fn source_edit_from_def(
485497
// Foo { field: ref mut local @ local 2} -> Foo { field: ref mut new_name @ local2 }
486498
// Foo { field: ref mut local } -> Foo { field: ref mut new_name }
487499
// ^^^^^ replace this with `new_name`
488-
edit.replace(name_range, new_name.to_string());
500+
edit.replace(name_range, new_name.to_owned());
489501
}
490502
} else {
491503
// Foo { ref mut field } -> Foo { field: ref mut new_name }
@@ -495,10 +507,10 @@ fn source_edit_from_def(
495507
pat.syntax().text_range().start(),
496508
format!("{}: ", pat_field.field_name().unwrap()),
497509
);
498-
edit.replace(name_range, new_name.to_string());
510+
edit.replace(name_range, new_name.to_owned());
499511
}
500512
} else {
501-
edit.replace(name_range, new_name.to_string());
513+
edit.replace(name_range, new_name.to_owned());
502514
}
503515
}
504516
}

0 commit comments

Comments
 (0)