Skip to content

fix: rename and add import #19799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Hmikihiro
Copy link
Contributor

@Hmikihiro Hmikihiro commented May 15, 2025

fix: #18892

2025-05-16.4.31.58.mov

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 15, 2025
Comment on lines 1196 to +1200
if self.def == def
// is our def a trait assoc item? then we want to find all assoc items from trait impls of our trait
|| matches!(self.assoc_item_container, Some(hir::AssocItemContainer::Trait(_)))
&& convert_to_def_in_trait(self.sema.db, def) == self.def =>
&& convert_to_def_in_trait(self.sema.db, def) == self.def
|| self.is_related_import(name_ref) =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I find some definition item, we use NameRefClass::classify but it only return single definition. So, I check use block have some definition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change give affect to this test

#[test]
fn attr() {
check(
r#"
//- proc_macros: identity
use proc_macros::identity;
#[proc_macros::$0identity]
fn func() {}
"#,
expect![[r#"
identity Attribute FileId(1) 1..107 32..40
FileId(0) 17..25 import
FileId(0) 43..51
"#]],

Signed-off-by: Hayashi Mikihiro <[email protected]>
@Hmikihiro Hmikihiro force-pushed the rename_conflict_ns branch from 4b1488f to d2a05af Compare May 15, 2025 19:00
Comment on lines +531 to +555
} else if let Some(res) = ast::UseTree::find_tail_use_tree_for_name_ref(name_ref)
.and_then(|u| u.path())
.and_then(|p| sema.resolve_path_per_ns(&p))
{
let res = res
.to_small_vec()
.into_iter()
.flatten()
.filter_map(|res| match res {
hir::PathResolution::Def(def) => Some(Definition::from(def)),
_ => None,
})
.unique()
.collect::<Vec<_>>();

let range = name_ref.syntax().text_range();
if res.iter().any(|res| res == &def) {
if res.len() == 1 {
edit.replace(range, new_name.to_owned());
} else {
edit.replace(range, format!("{{{}, {}}}", new_name, name_ref.text()));
}
}

return true;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we rename import item, We check that the path is ambiguous or not. if It is ambiguous, we add old import name and new name.

Comment on lines +254 to +269
if let Some(path) = ast::UseTree::find_tail_use_tree_for_name_ref(name_ref).and_then(|u| u.path()).and_then(|p| sema.resolve_path_per_ns(&p)){
let defs = path.to_small_vec().into_iter().filter_map(|res| match res{
Some(PathResolution::Def(def)) => Some(Definition::from(def)),
_ => None,
}).unique().collect::<Vec<_>>();
match defs.len() {
0 => Err(format_err!("No references found at position")),
1 => {
if defs[0].name(sema.db).is_some_and(|it| it.as_str() != name_ref.text().trim_start_matches("r#")) {
Err(format_err!("Renaming aliases is currently unsupported"))
} else {
Ok(defs[0])
}
}
})
2.. => Err(format_err!("Ambiguous import is currently unsupported")),
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use bar::foo;
       //^^^ It import `fn foo()` and `struct foo{}`
mod bar {
    pub fn foo() {}
    pub struct foo {}
}

I think we shouldn't rename the ambiguous import name_ref.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Renaming a struct that has a function with the same name will break code:
2 participants