This commit is contained in:
Ali Bektas 2023-08-24 02:01:07 +02:00
parent 43edb51b21
commit 6dc7fa9423
2 changed files with 28 additions and 9 deletions

View File

@ -22,7 +22,7 @@
//! Our current behavior is ¯\_(ツ)_/¯. //! Our current behavior is ¯\_(ツ)_/¯.
use std::fmt; use std::fmt;
use base_db::{AnchoredPathBuf, CrateOrigin, FileId, FileRange}; use base_db::{AnchoredPathBuf, FileId, FileRange};
use either::Either; use either::Either;
use hir::{FieldSource, HasSource, InFile, ModuleSource, Semantics}; use hir::{FieldSource, HasSource, InFile, ModuleSource, Semantics};
use stdx::never; use stdx::never;
@ -71,21 +71,29 @@ pub fn rename(
sema: &Semantics<'_, RootDatabase>, sema: &Semantics<'_, RootDatabase>,
new_name: &str, new_name: &str,
) -> Result<SourceChange> { ) -> Result<SourceChange> {
// self.krate() returns None if
// self is a built-in attr, built-in type or tool module.
if let Some(krate) = self.krate(sema.db) { if let Some(krate) = self.krate(sema.db) {
if !matches!(krate.origin(sema.db), CrateOrigin::Local { .. }) { if !krate.origin(sema.db).is_local() {
bail!("Cannot rename a non-local definition.") 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::ToolModule(_) => {
bail!("Cannot rename a tool module")
}
Definition::BuiltinType(_) => { Definition::BuiltinType(_) => {
bail!("Cannot rename builtin type") bail!("Cannot rename builtin type")
} }
Definition::BuiltinAttr(_) => {
bail!("Cannot rename a builtin attr.")
}
Definition::SelfType(_) => bail!("Cannot rename `Self`"), Definition::SelfType(_) => bail!("Cannot rename `Self`"),
Definition::Macro(mac) => { Definition::Macro(mac) => {
if mac.is_builtin_derive(sema.db) { if mac.is_builtin_derive(sema.db) {
bail!("Cannot rename builtin macro") bail!("Cannot rename builtin derive")
} }
rename_reference(sema, Definition::Macro(mac), new_name) rename_reference(sema, Definition::Macro(mac), new_name)

View File

@ -2529,7 +2529,6 @@ fn main() {
", ",
) )
} }
<<<<<<< HEAD
#[test] #[test]
fn extern_crate() { fn extern_crate() {
@ -2635,8 +2634,6 @@ fn extern_crate_self_rename() {
// ", // ",
// ); // );
} }
||||||| parent of 948d9f274 (Disallow renaming of non-local structs)
=======
#[test] #[test]
fn disallow_renaming_for_non_local_definition() { fn disallow_renaming_for_non_local_definition() {
@ -2644,12 +2641,26 @@ fn disallow_renaming_for_non_local_definition() {
"Baz", "Baz",
r#" r#"
//- /lib.rs crate:lib new_source_root:library //- /lib.rs crate:lib new_source_root:library
pub struct S$0; pub struct S;
//- /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$0;
"#, "#,
"error: Cannot rename a non-local definition.", "error: Cannot rename a non-local definition.",
); );
} }
>>>>>>> 948d9f274 (Disallow renaming of non-local structs)
#[test]
fn disallow_renaming_for_builtin_macros() {
check(
"Baz",
r#"
//- minicore: derive, hash
//- /main.rs crate:main
use core::hash::Hash;
#[derive(H$0ash)]
struct A;
"#,
"error: Cannot rename a non-local definition.",
)
}
} }