mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Properly account for editions in names
This PR touches a lot of parts. But the main changes are changing `hir_expand::Name` to be raw edition-dependently and only when necessary (unrelated to how the user originally wrote the identifier), and changing `is_keyword()` and `is_raw_identifier()` to be edition-aware (this was done in #17896, but the FIXMEs were fixed here). It is possible that I missed some cases, but most IDE parts should properly escape (or not escape) identifiers now. The rules of thumb are: - If we show the identifier to the user, its rawness should be determined by the edition of the edited crate. This is nice for IDE features, but really important for changes we insert to the source code. - For tests, I chose `Edition::CURRENT` (so we only have to (maybe) update tests when an edition becomes stable, to avoid churn). - For debugging tools (helper methods and logs), I used `Edition::LATEST`.
This commit is contained in:
parent
91aa3f46b3
commit
9d3368f2c2
179 changed files with 2485 additions and 1250 deletions
|
@ -5,6 +5,7 @@ use either::Either;
|
|||
use hir::{AsAssocItem, HirDisplay, ImportPathConfig, ModuleDef, SemanticsScope};
|
||||
use itertools::Itertools;
|
||||
use rustc_hash::FxHashMap;
|
||||
use span::Edition;
|
||||
use syntax::{
|
||||
ast::{self, make, AstNode, HasGenericArgs},
|
||||
ted, NodeOrToken, SyntaxNode,
|
||||
|
@ -146,6 +147,7 @@ impl<'a> PathTransform<'a> {
|
|||
let mut type_substs: FxHashMap<hir::TypeParam, ast::Type> = Default::default();
|
||||
let mut const_substs: FxHashMap<hir::ConstParam, SyntaxNode> = Default::default();
|
||||
let mut defaulted_params: Vec<DefaultedParam> = Default::default();
|
||||
let target_edition = target_module.krate().edition(self.source_scope.db);
|
||||
self.generic_def
|
||||
.into_iter()
|
||||
.flat_map(|it| it.type_or_const_params(db))
|
||||
|
@ -190,7 +192,7 @@ impl<'a> PathTransform<'a> {
|
|||
}
|
||||
}
|
||||
(Either::Left(k), None) => {
|
||||
if let Some(default) = k.default(db) {
|
||||
if let Some(default) = k.default(db, target_edition) {
|
||||
if let Some(default) = default.expr() {
|
||||
const_substs.insert(k, default.syntax().clone_for_update());
|
||||
defaulted_params.push(Either::Right(k));
|
||||
|
@ -204,7 +206,9 @@ impl<'a> PathTransform<'a> {
|
|||
.into_iter()
|
||||
.flat_map(|it| it.lifetime_params(db))
|
||||
.zip(self.substs.lifetimes.clone())
|
||||
.filter_map(|(k, v)| Some((k.name(db).display(db.upcast()).to_string(), v.lifetime()?)))
|
||||
.filter_map(|(k, v)| {
|
||||
Some((k.name(db).display(db.upcast(), target_edition).to_string(), v.lifetime()?))
|
||||
})
|
||||
.collect();
|
||||
let ctx = Ctx {
|
||||
type_substs,
|
||||
|
@ -213,6 +217,7 @@ impl<'a> PathTransform<'a> {
|
|||
target_module,
|
||||
source_scope: self.source_scope,
|
||||
same_self_type: self.target_scope.has_same_self_type(self.source_scope),
|
||||
target_edition,
|
||||
};
|
||||
ctx.transform_default_values(defaulted_params);
|
||||
ctx
|
||||
|
@ -226,6 +231,7 @@ struct Ctx<'a> {
|
|||
target_module: hir::Module,
|
||||
source_scope: &'a SemanticsScope<'a>,
|
||||
same_self_type: bool,
|
||||
target_edition: Edition,
|
||||
}
|
||||
|
||||
fn preorder_rev(item: &SyntaxNode) -> impl Iterator<Item = SyntaxNode> {
|
||||
|
@ -318,7 +324,7 @@ impl Ctx<'_> {
|
|||
hir::ModuleDef::Trait(trait_ref),
|
||||
cfg,
|
||||
)?;
|
||||
match make::ty_path(mod_path_to_ast(&found_path)) {
|
||||
match make::ty_path(mod_path_to_ast(&found_path, self.target_edition)) {
|
||||
ast::Type::PathType(path_ty) => Some(path_ty),
|
||||
_ => None,
|
||||
}
|
||||
|
@ -374,7 +380,7 @@ impl Ctx<'_> {
|
|||
};
|
||||
let found_path =
|
||||
self.target_module.find_path(self.source_scope.db.upcast(), def, cfg)?;
|
||||
let res = mod_path_to_ast(&found_path).clone_for_update();
|
||||
let res = mod_path_to_ast(&found_path, self.target_edition).clone_for_update();
|
||||
if let Some(args) = path.segment().and_then(|it| it.generic_arg_list()) {
|
||||
if let Some(segment) = res.segment() {
|
||||
let old = segment.get_or_create_generic_arg_list();
|
||||
|
@ -417,7 +423,9 @@ impl Ctx<'_> {
|
|||
cfg,
|
||||
)?;
|
||||
|
||||
if let Some(qual) = mod_path_to_ast(&found_path).qualifier() {
|
||||
if let Some(qual) =
|
||||
mod_path_to_ast(&found_path, self.target_edition).qualifier()
|
||||
{
|
||||
let res = make::path_concat(qual, path_ty.path()?).clone_for_update();
|
||||
ted::replace(path.syntax(), res.syntax());
|
||||
return Some(());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue