Generalize disallowing of definition renaming

This commit is contained in:
Ali Bektas 2023-07-08 12:22:07 +02:00
parent 7ae70a06ce
commit 43edb51b21
2 changed files with 13 additions and 9 deletions

View File

@ -71,20 +71,24 @@ pub fn rename(
sema: &Semantics<'_, RootDatabase>, sema: &Semantics<'_, RootDatabase>,
new_name: &str, new_name: &str,
) -> Result<SourceChange> { ) -> 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 { match *self {
Definition::Module(module) => rename_mod(sema, module, new_name), Definition::Module(module) => rename_mod(sema, module, new_name),
Definition::BuiltinType(_) => { Definition::BuiltinType(_) => {
bail!("Cannot rename builtin type") bail!("Cannot rename builtin type")
} }
Definition::SelfType(_) => bail!("Cannot rename `Self`"), Definition::SelfType(_) => bail!("Cannot rename `Self`"),
Definition::Adt(hir::Adt::Struct(strukt)) => { Definition::Macro(mac) => {
if !matches!( if mac.is_builtin_derive(sema.db) {
strukt.module(sema.db).krate().origin(sema.db), bail!("Cannot rename builtin macro")
CrateOrigin::Local { .. }
) {
bail!("Cannot rename a non-local struct.")
} }
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), def => rename_reference(sema, def, new_name),
} }

View File

@ -2639,7 +2639,7 @@ fn extern_crate_self_rename() {
======= =======
#[test] #[test]
fn disallow_renaming_for_non_local_struct() { fn disallow_renaming_for_non_local_definition() {
check( check(
"Baz", "Baz",
r#" r#"
@ -2648,7 +2648,7 @@ fn disallow_renaming_for_non_local_struct() {
//- /main.rs crate:main deps:lib new_source_root:local //- /main.rs crate:main deps:lib new_source_root:local
use lib::S; use lib::S;
"#, "#,
"error: Cannot rename a non-local struct.", "error: Cannot rename a non-local definition.",
); );
} }
>>>>>>> 948d9f274 (Disallow renaming of non-local structs) >>>>>>> 948d9f274 (Disallow renaming of non-local structs)