Skip to content

Commit 511adad

Browse files
committed
Generalize disallowing of definition renaming
1 parent 948d9f2 commit 511adad

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

crates/ide-db/src/rename.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::fmt;
2424

2525
use base_db::{AnchoredPathBuf, CrateOrigin, FileId, FileRange};
2626
use either::Either;
27-
use hir::{FieldSource, HasSource, InFile, ModuleSource, Semantics};
27+
use hir::{Crate, FieldSource, HasCrate, HasSource, InFile, ModuleSource, Semantics};
2828
use stdx::never;
2929
use syntax::{
3030
ast::{self, HasName},
@@ -71,20 +71,24 @@ impl Definition {
7171
sema: &Semantics<'_, RootDatabase>,
7272
new_name: &str,
7373
) -> Result<SourceChange> {
74+
if let Some(krate) = self.krate(sema.db) {
75+
if !matches!(krate.origin(sema.db), CrateOrigin::Local { .. }) {
76+
bail!("Cannot rename a non-local definition.")
77+
}
78+
}
79+
7480
match *self {
7581
Definition::Module(module) => rename_mod(sema, module, new_name),
7682
Definition::BuiltinType(_) => {
7783
bail!("Cannot rename builtin type")
7884
}
7985
Definition::SelfType(_) => bail!("Cannot rename `Self`"),
80-
Definition::Adt(hir::Adt::Struct(strukt)) => {
81-
if !matches!(
82-
strukt.module(sema.db).krate().origin(sema.db),
83-
CrateOrigin::Local { .. }
84-
) {
85-
bail!("Cannot rename a non-local struct.")
86+
Definition::Macro(mac) => {
87+
if mac.is_builtin_derive(sema.db) {
88+
bail!("Cannot rename builtin macro")
8689
}
87-
rename_reference(sema, Definition::Adt(hir::Adt::Struct(strukt)), new_name)
90+
91+
rename_reference(sema, Definition::Macro(mac), new_name)
8892
}
8993
def => rename_reference(sema, def, new_name),
9094
}

crates/ide/src/rename.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2489,7 +2489,7 @@ fn main() {
24892489
}
24902490

24912491
#[test]
2492-
fn disallow_renaming_for_non_local_struct() {
2492+
fn disallow_renaming_for_non_local_definition() {
24932493
check(
24942494
"Baz",
24952495
r#"
@@ -2498,7 +2498,7 @@ pub struct S$0;
24982498
//- /main.rs crate:main deps:lib new_source_root:local
24992499
use lib::S;
25002500
"#,
2501-
"error: Cannot rename a non-local struct.",
2501+
"error: Cannot rename a non-local definition.",
25022502
);
25032503
}
25042504
}

0 commit comments

Comments
 (0)