Skip to content

Commit d6412b5

Browse files
committed
Auto merge of rust-lang#13034 - lowr:fix/regression-from-12993, r=lowr
fix: escape keywords used as names in earlier editions Fixes rust-lang#13030 There are keywords in Rust 2018+ that you can use as names without escaping when your crate is in Rust 2015 e.g. "try". We need to be consistent on how to keep track of the names regardless of how they are actually written in each crate. This patch attempts at it by taking such names into account and storing them uniformly in their escaped form.
2 parents dd9ead5 + a3409c3 commit d6412b5

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

crates/hir-expand/src/name.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,16 @@ impl Name {
9090

9191
/// Resolve a name from the text of token.
9292
fn resolve(raw_text: &str) -> Name {
93-
// When `raw_text` starts with "r#" but the name does not coincide with any
94-
// keyword, we never need the prefix so we strip it.
9593
match raw_text.strip_prefix("r#") {
94+
// When `raw_text` starts with "r#" but the name does not coincide with any
95+
// keyword, we never need the prefix so we strip it.
9696
Some(text) if !is_raw_identifier(text) => Name::new_text(SmolStr::new(text)),
97+
// Keywords (in the current edition) *can* be used as a name in earlier editions of
98+
// Rust, e.g. "try" in Rust 2015. Even in such cases, we keep track of them in their
99+
// escaped form.
100+
None if is_raw_identifier(raw_text) => {
101+
Name::new_text(SmolStr::from_iter(["r#", raw_text]))
102+
}
97103
_ => Name::new_text(raw_text.into()),
98104
}
99105
}

crates/ide-db/src/search.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,9 @@ impl<'a> FindUsages<'a> {
402402
.or_else(|| ty.as_builtin().map(|builtin| builtin.name()))
403403
})
404404
};
405-
self.def.name(sema.db).or_else(self_kw_refs).map(|it| it.to_smol_str())
405+
// We need to unescape the name in case it is written without "r#" in earlier
406+
// editions of Rust where it isn't a keyword.
407+
self.def.name(sema.db).or_else(self_kw_refs).map(|it| it.unescaped().to_smol_str())
406408
}
407409
};
408410
let name = match &name {

0 commit comments

Comments
 (0)