Skip to content

Commit 88b3d9f

Browse files
committed
Auto merge of rust-lang#14144 - lowr:fix/find-occurrence-of-raw-ident, r=Veykril
fix: Search raw identifiers without prefix When we find references/usages of a raw identifier, we should disregard `r#` prefix because there are keywords one can use without the prefix in earlier editions (see rust-lang#13034; this bug is actually fallout from the PR). `name`, the text we're searching for, has already been stripped of the prefix, but the text of nodes we compare it to hasn't been. The second commit is strictly refactoring, I can remove it if it's not much of value.
2 parents 31486a6 + 60fa8fe commit 88b3d9f

File tree

3 files changed

+59
-43
lines changed

3 files changed

+59
-43
lines changed

crates/ide-db/src/search.rs

+40-42
Original file line numberDiff line numberDiff line change
@@ -455,46 +455,45 @@ impl<'a> FindUsages<'a> {
455455
}
456456

457457
let find_nodes = move |name: &str, node: &syntax::SyntaxNode, offset: TextSize| {
458-
node.token_at_offset(offset).find(|it| it.text() == name).map(|token| {
459-
// FIXME: There should be optimization potential here
460-
// Currently we try to descend everything we find which
461-
// means we call `Semantics::descend_into_macros` on
462-
// every textual hit. That function is notoriously
463-
// expensive even for things that do not get down mapped
464-
// into macros.
465-
sema.descend_into_macros(token).into_iter().filter_map(|it| it.parent())
466-
})
458+
node.token_at_offset(offset)
459+
.find(|it| {
460+
// `name` is stripped of raw ident prefix. See the comment on name retrieval above.
461+
it.text().trim_start_matches("r#") == name
462+
})
463+
.into_iter()
464+
.flat_map(|token| {
465+
// FIXME: There should be optimization potential here
466+
// Currently we try to descend everything we find which
467+
// means we call `Semantics::descend_into_macros` on
468+
// every textual hit. That function is notoriously
469+
// expensive even for things that do not get down mapped
470+
// into macros.
471+
sema.descend_into_macros(token).into_iter().filter_map(|it| it.parent())
472+
})
467473
};
468474

469475
for (text, file_id, search_range) in scope_files(sema, &search_scope) {
470476
let tree = Lazy::new(move || sema.parse(file_id).syntax().clone());
471477

472478
// Search for occurrences of the items name
473479
for offset in match_indices(&text, finder, search_range) {
474-
if let Some(iter) = find_nodes(name, &tree, offset) {
475-
for name in iter.filter_map(ast::NameLike::cast) {
476-
if match name {
477-
ast::NameLike::NameRef(name_ref) => {
478-
self.found_name_ref(&name_ref, sink)
479-
}
480-
ast::NameLike::Name(name) => self.found_name(&name, sink),
481-
ast::NameLike::Lifetime(lifetime) => {
482-
self.found_lifetime(&lifetime, sink)
483-
}
484-
} {
485-
return;
486-
}
480+
for name in find_nodes(name, &tree, offset).filter_map(ast::NameLike::cast) {
481+
if match name {
482+
ast::NameLike::NameRef(name_ref) => self.found_name_ref(&name_ref, sink),
483+
ast::NameLike::Name(name) => self.found_name(&name, sink),
484+
ast::NameLike::Lifetime(lifetime) => self.found_lifetime(&lifetime, sink),
485+
} {
486+
return;
487487
}
488488
}
489489
}
490490
// Search for occurrences of the `Self` referring to our type
491491
if let Some((self_ty, finder)) = &include_self_kw_refs {
492492
for offset in match_indices(&text, finder, search_range) {
493-
if let Some(iter) = find_nodes("Self", &tree, offset) {
494-
for name_ref in iter.filter_map(ast::NameRef::cast) {
495-
if self.found_self_ty_name_ref(self_ty, &name_ref, sink) {
496-
return;
497-
}
493+
for name_ref in find_nodes("Self", &tree, offset).filter_map(ast::NameRef::cast)
494+
{
495+
if self.found_self_ty_name_ref(self_ty, &name_ref, sink) {
496+
return;
498497
}
499498
}
500499
}
@@ -513,21 +512,21 @@ impl<'a> FindUsages<'a> {
513512
let tree = Lazy::new(move || sema.parse(file_id).syntax().clone());
514513

515514
for offset in match_indices(&text, finder, search_range) {
516-
if let Some(iter) = find_nodes("super", &tree, offset) {
517-
for name_ref in iter.filter_map(ast::NameRef::cast) {
518-
if self.found_name_ref(&name_ref, sink) {
519-
return;
520-
}
515+
for name_ref in
516+
find_nodes("super", &tree, offset).filter_map(ast::NameRef::cast)
517+
{
518+
if self.found_name_ref(&name_ref, sink) {
519+
return;
521520
}
522521
}
523522
}
524523
if let Some(finder) = &is_crate_root {
525524
for offset in match_indices(&text, finder, search_range) {
526-
if let Some(iter) = find_nodes("crate", &tree, offset) {
527-
for name_ref in iter.filter_map(ast::NameRef::cast) {
528-
if self.found_name_ref(&name_ref, sink) {
529-
return;
530-
}
525+
for name_ref in
526+
find_nodes("crate", &tree, offset).filter_map(ast::NameRef::cast)
527+
{
528+
if self.found_name_ref(&name_ref, sink) {
529+
return;
531530
}
532531
}
533532
}
@@ -566,11 +565,10 @@ impl<'a> FindUsages<'a> {
566565
let finder = &Finder::new("self");
567566

568567
for offset in match_indices(&text, finder, search_range) {
569-
if let Some(iter) = find_nodes("self", &tree, offset) {
570-
for name_ref in iter.filter_map(ast::NameRef::cast) {
571-
if self.found_self_module_name_ref(&name_ref, sink) {
572-
return;
573-
}
568+
for name_ref in find_nodes("self", &tree, offset).filter_map(ast::NameRef::cast)
569+
{
570+
if self.found_self_module_name_ref(&name_ref, sink) {
571+
return;
574572
}
575573
}
576574
}

crates/ide/src/references.rs

+15
Original file line numberDiff line numberDiff line change
@@ -2016,4 +2016,19 @@ fn method$0() {}
20162016
"#]],
20172017
);
20182018
}
2019+
2020+
#[test]
2021+
fn raw_identifier() {
2022+
check(
2023+
r#"
2024+
fn r#fn$0() {}
2025+
fn main() { r#fn(); }
2026+
"#,
2027+
expect![[r#"
2028+
r#fn Function FileId(0) 0..12 3..7
2029+
2030+
FileId(0) 25..29
2031+
"#]],
2032+
);
2033+
}
20192034
}

crates/ide/src/rename.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,6 @@ pub fn baz() {}
13711371

13721372
#[test]
13731373
fn test_rename_mod_from_raw_ident() {
1374-
// FIXME: `r#fn` in path expression is not renamed.
13751374
check_expect(
13761375
"foo",
13771376
r#"
@@ -1397,6 +1396,10 @@ pub fn baz() {}
13971396
insert: "foo",
13981397
delete: 4..8,
13991398
},
1399+
Indel {
1400+
insert: "foo",
1401+
delete: 23..27,
1402+
},
14001403
],
14011404
},
14021405
},

0 commit comments

Comments
 (0)