Skip to content

Commit

Permalink
Generalize disallowing of definition renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
alibektas committed Jul 8, 2023
1 parent 948d9f2 commit 853c5d0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
18 changes: 11 additions & 7 deletions crates/ide-db/src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,24 @@ impl Definition {
sema: &Semantics<'_, RootDatabase>,
new_name: &str,
) -> Result<SourceChange> {
if let Some(krate) = self.krate(sema.db) {
if !matches!(krate.origin(sema.db), CrateOrigin::Local { .. }) {
bail!("Cannot rename a non-local definition.")
}
}

match *self {
Definition::Module(module) => rename_mod(sema, module, new_name),
Definition::BuiltinType(_) => {
bail!("Cannot rename builtin type")
}
Definition::SelfType(_) => bail!("Cannot rename `Self`"),
Definition::Adt(hir::Adt::Struct(strukt)) => {
if !matches!(
strukt.module(sema.db).krate().origin(sema.db),
CrateOrigin::Local { .. }
) {
bail!("Cannot rename a non-local struct.")
Definition::Macro(mac) => {
if mac.is_builtin_derive(sema.db) {
bail!("Cannot rename builtin macro")
}
rename_reference(sema, Definition::Adt(hir::Adt::Struct(strukt)), new_name)

rename_reference(sema, Definition::Macro(mac), new_name)
}
def => rename_reference(sema, def, new_name),
}
Expand Down
4 changes: 2 additions & 2 deletions crates/ide/src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2489,7 +2489,7 @@ fn main() {
}

#[test]
fn disallow_renaming_for_non_local_struct() {
fn disallow_renaming_for_non_local_definition() {
check(
"Baz",
r#"
Expand All @@ -2498,7 +2498,7 @@ pub struct S$0;
//- /main.rs crate:main deps:lib new_source_root:local
use lib::S;
"#,
"error: Cannot rename a non-local struct.",
"error: Cannot rename a non-local definition.",
);
}
}

0 comments on commit 853c5d0

Please sign in to comment.