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:
Chayim Refael Friedman 2024-08-16 02:36:24 +03:00
parent 91aa3f46b3
commit 9d3368f2c2
179 changed files with 2485 additions and 1250 deletions

View file

@ -17,7 +17,7 @@ use syntax::{
self, edit::IndentLevel, edit_in_place::Indent, make, AstNode, BlockExpr, CallExpr,
HasArgList, HasGenericParams, HasModuleItem, HasTypeBounds,
},
ted, SyntaxKind, SyntaxNode, TextRange, T,
ted, Edition, SyntaxKind, SyntaxNode, TextRange, T,
};
use crate::{
@ -175,6 +175,7 @@ fn add_func_to_accumulator(
edit.edit_file(file);
let target = function_builder.target.clone();
let edition = function_builder.target_edition;
let func = function_builder.render(ctx.config.snippet_cap, edit);
if let Some(adt) =
@ -183,7 +184,7 @@ fn add_func_to_accumulator(
{
let name = make::ty_path(make::ext::ident_path(&format!(
"{}",
adt.name(ctx.db()).display(ctx.db())
adt.name(ctx.db()).display(ctx.db(), edition)
)));
// FIXME: adt may have generic params.
@ -222,6 +223,7 @@ struct FunctionBuilder {
should_focus_return_type: bool,
visibility: Visibility,
is_async: bool,
target_edition: Edition,
}
impl FunctionBuilder {
@ -237,6 +239,7 @@ impl FunctionBuilder {
) -> Option<Self> {
let target_module =
target_module.or_else(|| ctx.sema.scope(target.syntax()).map(|it| it.module()))?;
let target_edition = target_module.krate().edition(ctx.db());
let current_module = ctx.sema.scope(call.syntax())?.module();
let visibility = calculate_necessary_visibility(current_module, target_module, ctx);
@ -258,7 +261,9 @@ impl FunctionBuilder {
// If generated function has the name "new" and is an associated function, we generate fn body
// as a constructor and assume a "Self" return type.
if let Some(body) = make_fn_body_as_new_function(ctx, &fn_name.text(), adt_info) {
if let Some(body) =
make_fn_body_as_new_function(ctx, &fn_name.text(), adt_info, target_edition)
{
ret_type = Some(make::ret_type(make::ty_path(make::ext::ident_path("Self"))));
should_focus_return_type = false;
fn_body = body;
@ -288,6 +293,7 @@ impl FunctionBuilder {
should_focus_return_type,
visibility,
is_async,
target_edition,
})
}
@ -299,6 +305,8 @@ impl FunctionBuilder {
target_module: Module,
target: GeneratedFunctionTarget,
) -> Option<Self> {
let target_edition = target_module.krate().edition(ctx.db());
let current_module = ctx.sema.scope(call.syntax())?.module();
let visibility = calculate_necessary_visibility(current_module, target_module, ctx);
@ -336,6 +344,7 @@ impl FunctionBuilder {
should_focus_return_type,
visibility,
is_async,
target_edition,
})
}
@ -425,6 +434,7 @@ fn make_fn_body_as_new_function(
ctx: &AssistContext<'_>,
fn_name: &str,
adt_info: &Option<AdtInfo>,
edition: Edition,
) -> Option<ast::BlockExpr> {
if fn_name != "new" {
return None;
@ -441,7 +451,10 @@ fn make_fn_body_as_new_function(
.iter()
.map(|field| {
make::record_expr_field(
make::name_ref(&format!("{}", field.name(ctx.db()).display(ctx.db()))),
make::name_ref(&format!(
"{}",
field.name(ctx.db()).display(ctx.db(), edition)
)),
Some(placeholder_expr.clone()),
)
})
@ -1102,8 +1115,9 @@ fn fn_arg_type(
if ty.is_reference() || ty.is_mutable_reference() {
let famous_defs = &FamousDefs(&ctx.sema, ctx.sema.scope(fn_arg.syntax())?.krate());
let target_edition = target_module.krate().edition(ctx.db());
convert_reference_type(ty.strip_references(), ctx.db(), famous_defs)
.map(|conversion| conversion.convert_type(ctx.db()).to_string())
.map(|conversion| conversion.convert_type(ctx.db(), target_edition).to_string())
.or_else(|| ty.display_source_code(ctx.db(), target_module.into(), true).ok())
} else {
ty.display_source_code(ctx.db(), target_module.into(), true).ok()