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

@ -90,7 +90,7 @@ use once_cell::sync::Lazy;
use stdx::never;
use syntax::{
ast::{self, AstNode},
AstPtr, SyntaxNode, SyntaxNodePtr, TextRange,
AstPtr, Edition, SyntaxNode, SyntaxNodePtr, TextRange,
};
// FIXME: Make this an enum
@ -279,6 +279,7 @@ struct DiagnosticsContext<'a> {
config: &'a DiagnosticsConfig,
sema: Semantics<'a, RootDatabase>,
resolve: &'a AssistResolveStrategy,
edition: Edition,
}
impl DiagnosticsContext<'_> {
@ -359,12 +360,19 @@ pub fn semantic_diagnostics(
for node in parse.syntax().descendants() {
handlers::useless_braces::useless_braces(&mut res, file_id, &node);
handlers::field_shorthand::field_shorthand(&mut res, file_id, &node);
handlers::json_is_not_rust::json_in_items(&sema, &mut res, file_id, &node, config);
handlers::json_is_not_rust::json_in_items(
&sema,
&mut res,
file_id,
&node,
config,
file_id.edition(),
);
}
let module = sema.file_to_module_def(file_id);
let ctx = DiagnosticsContext { config, sema, resolve };
let ctx = DiagnosticsContext { config, sema, resolve, edition: file_id.edition() };
let mut diags = Vec::new();
match module {
@ -490,6 +498,7 @@ pub fn semantic_diagnostics(
&mut rustc_stack,
&mut clippy_stack,
&mut diagnostics_of_range,
ctx.edition,
);
res.retain(|d| d.severity != Severity::Allow);
@ -544,6 +553,7 @@ fn handle_lint_attributes(
rustc_stack: &mut FxHashMap<String, Vec<Severity>>,
clippy_stack: &mut FxHashMap<String, Vec<Severity>>,
diagnostics_of_range: &mut FxHashMap<InFile<SyntaxNode>, &mut Diagnostic>,
edition: Edition,
) {
let _g = tracing::info_span!("handle_lint_attributes").entered();
let file_id = sema.hir_file_for(root);
@ -552,9 +562,15 @@ fn handle_lint_attributes(
match ev {
syntax::WalkEvent::Enter(node) => {
for attr in node.children().filter_map(ast::Attr::cast) {
parse_lint_attribute(attr, rustc_stack, clippy_stack, |stack, severity| {
stack.push(severity);
});
parse_lint_attribute(
attr,
rustc_stack,
clippy_stack,
|stack, severity| {
stack.push(severity);
},
edition,
);
}
if let Some(it) =
diagnostics_of_range.get_mut(&InFile { file_id, value: node.clone() })
@ -591,6 +607,7 @@ fn handle_lint_attributes(
rustc_stack,
clippy_stack,
diagnostics_of_range,
edition,
);
for stack in [&mut *rustc_stack, &mut *clippy_stack] {
stack.entry("__RA_EVERY_LINT".to_owned()).or_default().pop();
@ -605,17 +622,24 @@ fn handle_lint_attributes(
rustc_stack,
clippy_stack,
diagnostics_of_range,
edition,
);
}
}
}
syntax::WalkEvent::Leave(node) => {
for attr in node.children().filter_map(ast::Attr::cast) {
parse_lint_attribute(attr, rustc_stack, clippy_stack, |stack, severity| {
if stack.pop() != Some(severity) {
never!("Mismatched serevity in walking lint attributes");
}
});
parse_lint_attribute(
attr,
rustc_stack,
clippy_stack,
|stack, severity| {
if stack.pop() != Some(severity) {
never!("Mismatched serevity in walking lint attributes");
}
},
edition,
);
}
}
}
@ -627,6 +651,7 @@ fn parse_lint_attribute(
rustc_stack: &mut FxHashMap<String, Vec<Severity>>,
clippy_stack: &mut FxHashMap<String, Vec<Severity>>,
job: impl Fn(&mut Vec<Severity>, Severity),
edition: Edition,
) {
let Some((tag, args_tt)) = attr.as_simple_call() else {
return;
@ -637,7 +662,7 @@ fn parse_lint_attribute(
"forbid" | "deny" => Severity::Error,
_ => return,
};
for lint in parse_tt_as_comma_sep_paths(args_tt).into_iter().flatten() {
for lint in parse_tt_as_comma_sep_paths(args_tt, edition).into_iter().flatten() {
if let Some(lint) = lint.as_single_name_ref() {
job(rustc_stack.entry(lint.to_string()).or_default(), severity);
}