mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +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
|
@ -7,6 +7,7 @@ use hir_ty::{
|
|||
display::{DisplaySourceCodeError, HirDisplay},
|
||||
};
|
||||
use itertools::Itertools;
|
||||
use span::Edition;
|
||||
|
||||
use crate::{
|
||||
Adt, AsAssocItem, AssocItemContainer, Const, ConstParam, Field, Function, Local, ModuleDef,
|
||||
|
@ -29,9 +30,10 @@ fn mod_item_path_str(
|
|||
sema_scope: &SemanticsScope<'_>,
|
||||
def: &ModuleDef,
|
||||
cfg: ImportPathConfig,
|
||||
edition: Edition,
|
||||
) -> Result<String, DisplaySourceCodeError> {
|
||||
let path = mod_item_path(sema_scope, def, cfg);
|
||||
path.map(|it| it.display(sema_scope.db.upcast()).to_string())
|
||||
path.map(|it| it.display(sema_scope.db.upcast(), edition).to_string())
|
||||
.ok_or(DisplaySourceCodeError::PathNotFound)
|
||||
}
|
||||
|
||||
|
@ -97,37 +99,38 @@ impl Expr {
|
|||
sema_scope: &SemanticsScope<'_>,
|
||||
many_formatter: &mut dyn FnMut(&Type) -> String,
|
||||
cfg: ImportPathConfig,
|
||||
edition: Edition,
|
||||
) -> Result<String, DisplaySourceCodeError> {
|
||||
let db = sema_scope.db;
|
||||
let mod_item_path_str = |s, def| mod_item_path_str(s, def, cfg);
|
||||
let mod_item_path_str = |s, def| mod_item_path_str(s, def, cfg, edition);
|
||||
match self {
|
||||
Expr::Const(it) => match it.as_assoc_item(db).map(|it| it.container(db)) {
|
||||
Some(container) => {
|
||||
let container_name = container_name(container, sema_scope, cfg)?;
|
||||
let container_name = container_name(container, sema_scope, cfg, edition)?;
|
||||
let const_name = it
|
||||
.name(db)
|
||||
.map(|c| c.display(db.upcast()).to_string())
|
||||
.map(|c| c.display(db.upcast(), edition).to_string())
|
||||
.unwrap_or(String::new());
|
||||
Ok(format!("{container_name}::{const_name}"))
|
||||
}
|
||||
None => mod_item_path_str(sema_scope, &ModuleDef::Const(*it)),
|
||||
},
|
||||
Expr::Static(it) => mod_item_path_str(sema_scope, &ModuleDef::Static(*it)),
|
||||
Expr::Local(it) => Ok(it.name(db).display(db.upcast()).to_string()),
|
||||
Expr::ConstParam(it) => Ok(it.name(db).display(db.upcast()).to_string()),
|
||||
Expr::Local(it) => Ok(it.name(db).display(db.upcast(), edition).to_string()),
|
||||
Expr::ConstParam(it) => Ok(it.name(db).display(db.upcast(), edition).to_string()),
|
||||
Expr::FamousType { value, .. } => Ok(value.to_string()),
|
||||
Expr::Function { func, params, .. } => {
|
||||
let args = params
|
||||
.iter()
|
||||
.map(|f| f.gen_source_code(sema_scope, many_formatter, cfg))
|
||||
.map(|f| f.gen_source_code(sema_scope, many_formatter, cfg, edition))
|
||||
.collect::<Result<Vec<String>, DisplaySourceCodeError>>()?
|
||||
.into_iter()
|
||||
.join(", ");
|
||||
|
||||
match func.as_assoc_item(db).map(|it| it.container(db)) {
|
||||
Some(container) => {
|
||||
let container_name = container_name(container, sema_scope, cfg)?;
|
||||
let fn_name = func.name(db).display(db.upcast()).to_string();
|
||||
let container_name = container_name(container, sema_scope, cfg, edition)?;
|
||||
let fn_name = func.name(db).display(db.upcast(), edition).to_string();
|
||||
Ok(format!("{container_name}::{fn_name}({args})"))
|
||||
}
|
||||
None => {
|
||||
|
@ -141,12 +144,13 @@ impl Expr {
|
|||
return Ok(many_formatter(&target.ty(db)));
|
||||
}
|
||||
|
||||
let func_name = func.name(db).display(db.upcast()).to_string();
|
||||
let func_name = func.name(db).display(db.upcast(), edition).to_string();
|
||||
let self_param = func.self_param(db).unwrap();
|
||||
let target_str = target.gen_source_code(sema_scope, many_formatter, cfg)?;
|
||||
let target_str =
|
||||
target.gen_source_code(sema_scope, many_formatter, cfg, edition)?;
|
||||
let args = params
|
||||
.iter()
|
||||
.map(|f| f.gen_source_code(sema_scope, many_formatter, cfg))
|
||||
.map(|f| f.gen_source_code(sema_scope, many_formatter, cfg, edition))
|
||||
.collect::<Result<Vec<String>, DisplaySourceCodeError>>()?
|
||||
.into_iter()
|
||||
.join(", ");
|
||||
|
@ -176,7 +180,7 @@ impl Expr {
|
|||
StructKind::Tuple => {
|
||||
let args = params
|
||||
.iter()
|
||||
.map(|f| f.gen_source_code(sema_scope, many_formatter, cfg))
|
||||
.map(|f| f.gen_source_code(sema_scope, many_formatter, cfg, edition))
|
||||
.collect::<Result<Vec<String>, DisplaySourceCodeError>>()?
|
||||
.into_iter()
|
||||
.join(", ");
|
||||
|
@ -190,8 +194,8 @@ impl Expr {
|
|||
.map(|(a, f)| {
|
||||
let tmp = format!(
|
||||
"{}: {}",
|
||||
f.name(db).display(db.upcast()),
|
||||
a.gen_source_code(sema_scope, many_formatter, cfg)?
|
||||
f.name(db).display(db.upcast(), edition),
|
||||
a.gen_source_code(sema_scope, many_formatter, cfg, edition)?
|
||||
);
|
||||
Ok(tmp)
|
||||
})
|
||||
|
@ -211,7 +215,7 @@ impl Expr {
|
|||
StructKind::Tuple => {
|
||||
let args = params
|
||||
.iter()
|
||||
.map(|a| a.gen_source_code(sema_scope, many_formatter, cfg))
|
||||
.map(|a| a.gen_source_code(sema_scope, many_formatter, cfg, edition))
|
||||
.collect::<Result<Vec<String>, DisplaySourceCodeError>>()?
|
||||
.into_iter()
|
||||
.join(", ");
|
||||
|
@ -225,8 +229,8 @@ impl Expr {
|
|||
.map(|(a, f)| {
|
||||
let tmp = format!(
|
||||
"{}: {}",
|
||||
f.name(db).display(db.upcast()),
|
||||
a.gen_source_code(sema_scope, many_formatter, cfg)?
|
||||
f.name(db).display(db.upcast(), edition),
|
||||
a.gen_source_code(sema_scope, many_formatter, cfg, edition)?
|
||||
);
|
||||
Ok(tmp)
|
||||
})
|
||||
|
@ -244,7 +248,7 @@ impl Expr {
|
|||
Expr::Tuple { params, .. } => {
|
||||
let args = params
|
||||
.iter()
|
||||
.map(|a| a.gen_source_code(sema_scope, many_formatter, cfg))
|
||||
.map(|a| a.gen_source_code(sema_scope, many_formatter, cfg, edition))
|
||||
.collect::<Result<Vec<String>, DisplaySourceCodeError>>()?
|
||||
.into_iter()
|
||||
.join(", ");
|
||||
|
@ -256,8 +260,8 @@ impl Expr {
|
|||
return Ok(many_formatter(&expr.ty(db)));
|
||||
}
|
||||
|
||||
let strukt = expr.gen_source_code(sema_scope, many_formatter, cfg)?;
|
||||
let field = field.name(db).display(db.upcast()).to_string();
|
||||
let strukt = expr.gen_source_code(sema_scope, many_formatter, cfg, edition)?;
|
||||
let field = field.name(db).display(db.upcast(), edition).to_string();
|
||||
Ok(format!("{strukt}.{field}"))
|
||||
}
|
||||
Expr::Reference(expr) => {
|
||||
|
@ -265,7 +269,7 @@ impl Expr {
|
|||
return Ok(many_formatter(&expr.ty(db)));
|
||||
}
|
||||
|
||||
let inner = expr.gen_source_code(sema_scope, many_formatter, cfg)?;
|
||||
let inner = expr.gen_source_code(sema_scope, many_formatter, cfg, edition)?;
|
||||
Ok(format!("&{inner}"))
|
||||
}
|
||||
Expr::Many(ty) => Ok(many_formatter(ty)),
|
||||
|
@ -353,17 +357,18 @@ fn container_name(
|
|||
container: AssocItemContainer,
|
||||
sema_scope: &SemanticsScope<'_>,
|
||||
cfg: ImportPathConfig,
|
||||
edition: Edition,
|
||||
) -> Result<String, DisplaySourceCodeError> {
|
||||
let container_name = match container {
|
||||
crate::AssocItemContainer::Trait(trait_) => {
|
||||
mod_item_path_str(sema_scope, &ModuleDef::Trait(trait_), cfg)?
|
||||
mod_item_path_str(sema_scope, &ModuleDef::Trait(trait_), cfg, edition)?
|
||||
}
|
||||
crate::AssocItemContainer::Impl(imp) => {
|
||||
let self_ty = imp.self_ty(sema_scope.db);
|
||||
// Should it be guaranteed that `mod_item_path` always exists?
|
||||
match self_ty.as_adt().and_then(|adt| mod_item_path(sema_scope, &adt.into(), cfg)) {
|
||||
Some(path) => path.display(sema_scope.db.upcast()).to_string(),
|
||||
None => self_ty.display(sema_scope.db).to_string(),
|
||||
Some(path) => path.display(sema_scope.db.upcast(), edition).to_string(),
|
||||
None => self_ty.display(sema_scope.db, edition).to_string(),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue