Skip to content

Commit 16835b8

Browse files
committed
v3
1 parent 9ad7613 commit 16835b8

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

crates/ide-db/src/rename.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
//! Our current behavior is ¯\_(ツ)_/¯.
2323
use std::fmt;
2424

25-
use base_db::{AnchoredPathBuf, CrateOrigin, FileId, FileRange};
25+
use base_db::{AnchoredPathBuf, FileId, FileRange};
2626
use either::Either;
2727
use hir::{FieldSource, HasSource, InFile, ModuleSource, Semantics};
2828
use stdx::never;
@@ -71,21 +71,29 @@ impl Definition {
7171
sema: &Semantics<'_, RootDatabase>,
7272
new_name: &str,
7373
) -> Result<SourceChange> {
74+
// self.krate() returns None if
75+
// self is a built-in attr, built-in type or tool module.
7476
if let Some(krate) = self.krate(sema.db) {
75-
if !matches!(krate.origin(sema.db), CrateOrigin::Local { .. }) {
77+
if !krate.origin(sema.db).is_local() {
7678
bail!("Cannot rename a non-local definition.")
7779
}
7880
}
7981

8082
match *self {
8183
Definition::Module(module) => rename_mod(sema, module, new_name),
84+
Definition::ToolModule(_) => {
85+
bail!("Cannot rename a tool module")
86+
}
8287
Definition::BuiltinType(_) => {
8388
bail!("Cannot rename builtin type")
8489
}
90+
Definition::BuiltinAttr(_) => {
91+
bail!("Cannot rename a builtin attr.")
92+
}
8593
Definition::SelfType(_) => bail!("Cannot rename `Self`"),
8694
Definition::Macro(mac) => {
8795
if mac.is_builtin_derive(sema.db) {
88-
bail!("Cannot rename builtin macro")
96+
bail!("Cannot rename builtin derive")
8997
}
9098

9199
rename_reference(sema, Definition::Macro(mac), new_name)

crates/ide/src/rename.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -2529,7 +2529,6 @@ fn main() {
25292529
",
25302530
)
25312531
}
2532-
<<<<<<< HEAD
25332532

25342533
#[test]
25352534
fn extern_crate() {
@@ -2635,21 +2634,33 @@ use qux as frob;
26352634
// ",
26362635
// );
26372636
}
2638-
||||||| parent of 948d9f274 (Disallow renaming of non-local structs)
2639-
=======
26402637

26412638
#[test]
26422639
fn disallow_renaming_for_non_local_definition() {
26432640
check(
26442641
"Baz",
26452642
r#"
26462643
//- /lib.rs crate:lib new_source_root:library
2647-
pub struct S$0;
2644+
pub struct S;
26482645
//- /main.rs crate:main deps:lib new_source_root:local
2649-
use lib::S;
2646+
use lib::S$0;
26502647
"#,
26512648
"error: Cannot rename a non-local definition.",
26522649
);
26532650
}
2654-
>>>>>>> 948d9f274 (Disallow renaming of non-local structs)
2651+
2652+
#[test]
2653+
fn disallow_renaming_for_builtin_macros() {
2654+
check(
2655+
"Baz",
2656+
r#"
2657+
//- minicore: derive, hash
2658+
//- /main.rs crate:main
2659+
use core::hash::Hash;
2660+
#[derive(H$0ash)]
2661+
struct A;
2662+
"#,
2663+
"error: Cannot rename a non-local definition.",
2664+
)
2665+
}
26552666
}

0 commit comments

Comments
 (0)