mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Auto merge of #17905 - ChayimFriedman2:edition-dependent-raw-keyword, r=Veykril
fix: 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`. Reviewing notes: This is a really big PR but most of it is mechanical translation. I changed `Name` displayers to require an edition, and followed the compiler errors. Most methods just propagate the edition requirement. The interesting cases are mostly in `ide-assists`, as sometimes the correct crate to fetch the edition from requires awareness (there may be two). `ide-completions` and `ide-diagnostics` were solved pretty easily by introducing an edition field to their context. `ide` contains many features, for most of them it was propagated to the top level function and there the edition was fetched based on the file. I also fixed all FIXMEs from #17896. Some required introducing an edition parameter (usually not for many methods after the changes to `Name`), some were changed to a new method `is_any_identifier()` because they really want any possible keyword. Fixes #17895. Fixes #17774.
This commit is contained in:
commit
c9ee892263
179 changed files with 2485 additions and 1250 deletions
|
@ -91,7 +91,7 @@ use ide_db::{
|
|||
use stdx::never;
|
||||
use syntax::{
|
||||
ast::{self, AstNode},
|
||||
AstPtr, SyntaxNode, SyntaxNodePtr, TextRange,
|
||||
AstPtr, Edition, SyntaxNode, SyntaxNodePtr, TextRange,
|
||||
};
|
||||
|
||||
// FIXME: Make this an enum
|
||||
|
@ -280,6 +280,7 @@ struct DiagnosticsContext<'a> {
|
|||
config: &'a DiagnosticsConfig,
|
||||
sema: Semantics<'a, RootDatabase>,
|
||||
resolve: &'a AssistResolveStrategy,
|
||||
edition: Edition,
|
||||
}
|
||||
|
||||
impl DiagnosticsContext<'_> {
|
||||
|
@ -360,12 +361,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 {
|
||||
|
@ -491,6 +499,7 @@ pub fn semantic_diagnostics(
|
|||
&mut rustc_stack,
|
||||
&mut clippy_stack,
|
||||
&mut diagnostics_of_range,
|
||||
ctx.edition,
|
||||
);
|
||||
|
||||
res.retain(|d| d.severity != Severity::Allow);
|
||||
|
@ -545,6 +554,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);
|
||||
|
@ -553,9 +563,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() })
|
||||
|
@ -592,6 +608,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();
|
||||
|
@ -606,17 +623,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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -628,6 +652,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;
|
||||
|
@ -638,7 +663,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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue