Fix #[rustc_deprecated_safe_2024]

It should be considered by the edition of the caller, not the callee.

Technically we still don't do it correctly - we need the span of the method name (if it comes from a macro), but we don't keep it and this is good enough for now.
This commit is contained in:
Chayim Refael Friedman 2025-01-27 14:34:33 +02:00
parent 6862329068
commit 55c63abc59
16 changed files with 274 additions and 100 deletions

View file

@ -478,7 +478,15 @@ fn traverse(
{
continue;
}
highlight_format_string(hl, sema, krate, &string, &expanded_string, range);
highlight_format_string(
hl,
sema,
krate,
&string,
&expanded_string,
range,
file_id.edition(),
);
if !string.is_raw() {
highlight_escape_string(hl, &string, range.start());
@ -526,6 +534,7 @@ fn traverse(
&mut bindings_shadow_count,
config.syntactic_name_ref_highlighting,
name_like,
file_id.edition(),
),
NodeOrToken::Token(token) => {
highlight::token(sema, token, file_id.edition()).zip(Some(None))

View file

@ -4,6 +4,7 @@ use ide_db::{
syntax_helpers::format_string::{is_format_string, lex_format_specifiers, FormatSpecifier},
SymbolKind,
};
use span::Edition;
use syntax::{ast, TextRange};
use crate::{
@ -18,6 +19,7 @@ pub(super) fn highlight_format_string(
string: &ast::String,
expanded_string: &ast::String,
range: TextRange,
edition: Edition,
) {
if is_format_string(expanded_string) {
// FIXME: Replace this with the HIR info we have now.
@ -39,7 +41,7 @@ pub(super) fn highlight_format_string(
if let Some(res) = res {
stack.add(HlRange {
range,
highlight: highlight_def(sema, krate, Definition::from(res)),
highlight: highlight_def(sema, krate, Definition::from(res), edition),
binding_hash: None,
})
}

View file

@ -58,6 +58,7 @@ pub(super) fn name_like(
bindings_shadow_count: &mut FxHashMap<hir::Name, u32>,
syntactic_name_ref_highlighting: bool,
name_like: ast::NameLike,
edition: Edition,
) -> Option<(Highlight, Option<u64>)> {
let mut binding_hash = None;
let highlight = match name_like {
@ -68,16 +69,17 @@ pub(super) fn name_like(
&mut binding_hash,
syntactic_name_ref_highlighting,
name_ref,
edition,
),
ast::NameLike::Name(name) => {
highlight_name(sema, bindings_shadow_count, &mut binding_hash, krate, name)
highlight_name(sema, bindings_shadow_count, &mut binding_hash, krate, name, edition)
}
ast::NameLike::Lifetime(lifetime) => match IdentClass::classify_lifetime(sema, &lifetime) {
Some(IdentClass::NameClass(NameClass::Definition(def))) => {
highlight_def(sema, krate, def) | HlMod::Definition
highlight_def(sema, krate, def, edition) | HlMod::Definition
}
Some(IdentClass::NameRefClass(NameRefClass::Definition(def, _))) => {
highlight_def(sema, krate, def)
highlight_def(sema, krate, def, edition)
}
// FIXME: Fallback for 'static and '_, as we do not resolve these yet
_ => SymbolKind::LifetimeParam.into(),
@ -234,16 +236,17 @@ fn highlight_name_ref(
binding_hash: &mut Option<u64>,
syntactic_name_ref_highlighting: bool,
name_ref: ast::NameRef,
edition: Edition,
) -> Highlight {
let db = sema.db;
if let Some(res) = highlight_method_call_by_name_ref(sema, krate, &name_ref) {
if let Some(res) = highlight_method_call_by_name_ref(sema, krate, &name_ref, edition) {
return res;
}
let name_class = match NameRefClass::classify(sema, &name_ref) {
Some(name_kind) => name_kind,
None if syntactic_name_ref_highlighting => {
return highlight_name_ref_by_syntax(name_ref, sema, krate)
return highlight_name_ref_by_syntax(name_ref, sema, krate, edition)
}
// FIXME: This is required for helper attributes used by proc-macros, as those do not map down
// to anything when used.
@ -267,7 +270,7 @@ fn highlight_name_ref(
*binding_hash = Some(calc_binding_hash(&name, *shadow_count))
};
let mut h = highlight_def(sema, krate, def);
let mut h = highlight_def(sema, krate, def, edition);
match def {
Definition::Local(local) if is_consumed_lvalue(name_ref.syntax(), &local, db) => {
@ -305,7 +308,7 @@ fn highlight_name_ref(
h
}
NameRefClass::FieldShorthand { field_ref, .. } => {
highlight_def(sema, krate, field_ref.into())
highlight_def(sema, krate, field_ref.into(), edition)
}
NameRefClass::ExternCrateShorthand { decl, krate: resolved_krate } => {
let mut h = HlTag::Symbol(SymbolKind::Module).into();
@ -341,6 +344,7 @@ fn highlight_name(
binding_hash: &mut Option<u64>,
krate: hir::Crate,
name: ast::Name,
edition: Edition,
) -> Highlight {
let name_kind = NameClass::classify(sema, &name);
if let Some(NameClass::Definition(Definition::Local(local))) = &name_kind {
@ -351,7 +355,7 @@ fn highlight_name(
};
match name_kind {
Some(NameClass::Definition(def)) => {
let mut h = highlight_def(sema, krate, def) | HlMod::Definition;
let mut h = highlight_def(sema, krate, def, edition) | HlMod::Definition;
if let Definition::Trait(trait_) = &def {
if trait_.is_unsafe(sema.db) {
h |= HlMod::Unsafe;
@ -359,7 +363,7 @@ fn highlight_name(
}
h
}
Some(NameClass::ConstReference(def)) => highlight_def(sema, krate, def),
Some(NameClass::ConstReference(def)) => highlight_def(sema, krate, def, edition),
Some(NameClass::PatFieldShorthand { field_ref, .. }) => {
let mut h = HlTag::Symbol(SymbolKind::Field).into();
if let hir::VariantDef::Union(_) = field_ref.parent_def(sema.db) {
@ -379,6 +383,7 @@ pub(super) fn highlight_def(
sema: &Semantics<'_, RootDatabase>,
krate: hir::Crate,
def: Definition,
edition: Edition,
) -> Highlight {
let db = sema.db;
let mut h = match def {
@ -431,7 +436,8 @@ pub(super) fn highlight_def(
// highlighted as unsafe, even when the current target features set is a superset (RFC 2396).
// We probably should consider checking the current function, but I found no easy way to do
// that (also I'm worried about perf). There's also an instance below.
if func.is_unsafe_to_call(db, None) {
// FIXME: This should be the edition of the call.
if func.is_unsafe_to_call(db, None, edition) {
h |= HlMod::Unsafe;
}
if func.is_async(db) {
@ -579,21 +585,23 @@ fn highlight_method_call_by_name_ref(
sema: &Semantics<'_, RootDatabase>,
krate: hir::Crate,
name_ref: &ast::NameRef,
edition: Edition,
) -> Option<Highlight> {
let mc = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
highlight_method_call(sema, krate, &mc)
highlight_method_call(sema, krate, &mc, edition)
}
fn highlight_method_call(
sema: &Semantics<'_, RootDatabase>,
krate: hir::Crate,
method_call: &ast::MethodCallExpr,
edition: Edition,
) -> Option<Highlight> {
let func = sema.resolve_method_call(method_call)?;
let mut h = SymbolKind::Method.into();
if func.is_unsafe_to_call(sema.db, None) || sema.is_unsafe_method_call(method_call) {
if func.is_unsafe_to_call(sema.db, None, edition) || sema.is_unsafe_method_call(method_call) {
h |= HlMod::Unsafe;
}
if func.is_async(sema.db) {
@ -679,6 +687,7 @@ fn highlight_name_ref_by_syntax(
name: ast::NameRef,
sema: &Semantics<'_, RootDatabase>,
krate: hir::Crate,
edition: Edition,
) -> Highlight {
let default = HlTag::UnresolvedReference;
@ -689,7 +698,7 @@ fn highlight_name_ref_by_syntax(
match parent.kind() {
METHOD_CALL_EXPR => ast::MethodCallExpr::cast(parent)
.and_then(|it| highlight_method_call(sema, krate, &it))
.and_then(|it| highlight_method_call(sema, krate, &it, edition))
.unwrap_or_else(|| SymbolKind::Method.into()),
FIELD_EXPR => {
let h = HlTag::Symbol(SymbolKind::Field);