Skip to content

Commit 6e341fe

Browse files
committed
fix: escape keywords used as names in earlier editions
1 parent 010f68c commit 6e341fe

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(["r#", raw_text].into_iter().collect())
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)