fix: escape keywords used as names in earlier editions

This commit is contained in:
Ryo Yoshida 2022-08-16 10:08:45 +09:00
parent 010f68cacf
commit a3409c3a83
No known key found for this signature in database
GPG Key ID: E25698A930586171
2 changed files with 11 additions and 3 deletions

View File

@ -90,10 +90,16 @@ const fn new_inline(text: &str) -> Name {
/// Resolve a name from the text of token.
fn resolve(raw_text: &str) -> Name {
// When `raw_text` starts with "r#" but the name does not coincide with any
// keyword, we never need the prefix so we strip it.
match raw_text.strip_prefix("r#") {
// When `raw_text` starts with "r#" but the name does not coincide with any
// keyword, we never need the prefix so we strip it.
Some(text) if !is_raw_identifier(text) => Name::new_text(SmolStr::new(text)),
// Keywords (in the current edition) *can* be used as a name in earlier editions of
// Rust, e.g. "try" in Rust 2015. Even in such cases, we keep track of them in their
// escaped form.
None if is_raw_identifier(raw_text) => {
Name::new_text(SmolStr::from_iter(["r#", raw_text]))
}
_ => Name::new_text(raw_text.into()),
}
}

View File

@ -402,7 +402,9 @@ fn search(&self, sink: &mut dyn FnMut(FileId, FileReference) -> bool) {
.or_else(|| ty.as_builtin().map(|builtin| builtin.name()))
})
};
self.def.name(sema.db).or_else(self_kw_refs).map(|it| it.to_smol_str())
// We need to unescape the name in case it is written without "r#" in earlier
// editions of Rust where it isn't a keyword.
self.def.name(sema.db).or_else(self_kw_refs).map(|it| it.unescaped().to_smol_str())
}
};
let name = match &name {