Auto merge of #17859 - Veykril:rustc_deprecated_safe_2024, r=Veykril

fix: Correctly support `#[rustc_deprecated_safe_2024]`

Fixes https://github.com/rust-lang/rust-analyzer/issues/17852
This commit is contained in:
bors 2024-08-12 08:59:08 +00:00
commit e2fd1db609
8 changed files with 44 additions and 19 deletions

View file

@ -158,7 +158,7 @@ impl Body {
}),
)
});
is_async_fn = data.has_async_kw();
is_async_fn = data.is_async();
src.map(|it| it.body().map(ast::Expr::from))
}
DefWithBodyId::ConstId(c) => {

View file

@ -94,6 +94,12 @@ impl FunctionData {
.filter(|it| !it.is_empty())
.map(Box::new);
let rustc_allow_incoherent_impl = attrs.by_key(&sym::rustc_allow_incoherent_impl).exists();
if flags.contains(FnFlags::HAS_UNSAFE_KW)
&& !crate_graph[krate].edition.at_least_2024()
&& attrs.by_key(&sym::rustc_deprecated_safe_2024).exists()
{
flags.remove(FnFlags::HAS_UNSAFE_KW);
}
Arc::new(FunctionData {
name: func.name.clone(),
@ -126,19 +132,19 @@ impl FunctionData {
self.flags.contains(FnFlags::HAS_SELF_PARAM)
}
pub fn has_default_kw(&self) -> bool {
pub fn is_default(&self) -> bool {
self.flags.contains(FnFlags::HAS_DEFAULT_KW)
}
pub fn has_const_kw(&self) -> bool {
pub fn is_const(&self) -> bool {
self.flags.contains(FnFlags::HAS_CONST_KW)
}
pub fn has_async_kw(&self) -> bool {
pub fn is_async(&self) -> bool {
self.flags.contains(FnFlags::HAS_ASYNC_KW)
}
pub fn has_unsafe_kw(&self) -> bool {
pub fn is_unsafe(&self) -> bool {
self.flags.contains(FnFlags::HAS_UNSAFE_KW)
}

View file

@ -17,7 +17,7 @@ pub fn missing_unsafe(db: &dyn HirDatabase, def: DefWithBodyId) -> Vec<ExprId> {
let mut res = Vec::new();
let is_unsafe = match def {
DefWithBodyId::FunctionId(it) => db.function_data(it).has_unsafe_kw(),
DefWithBodyId::FunctionId(it) => db.function_data(it).is_unsafe(),
DefWithBodyId::StaticId(_)
| DefWithBodyId::ConstId(_)
| DefWithBodyId::VariantId(_)

View file

@ -1857,7 +1857,7 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
params,
ret,
data.is_varargs(),
if data.has_unsafe_kw() { Safety::Unsafe } else { Safety::Safe },
if data.is_unsafe() { Safety::Unsafe } else { Safety::Safe },
data.abi.as_ref().map_or(FnAbi::Rust, FnAbi::from_symbol),
);
make_binders(db, &generics, sig)

View file

@ -253,12 +253,7 @@ impl<'a> ClosureSubst<'a> {
pub fn is_fn_unsafe_to_call(db: &dyn HirDatabase, func: FunctionId) -> bool {
let data = db.function_data(func);
if data.has_unsafe_kw() {
// Functions that are `#[rustc_deprecated_safe_2024]` are safe to call before 2024.
if db.attrs(func.into()).by_key(&sym::rustc_deprecated_safe_2024).exists() {
// FIXME: Properly check the caller span and mark it as unsafe after 2024.
return false;
}
if data.is_unsafe() {
return true;
}

View file

@ -69,13 +69,13 @@ impl HirDisplay for Function {
write_visibility(module_id, self.visibility(db), f)?;
if data.has_default_kw() {
if data.is_default() {
f.write_str("default ")?;
}
if data.has_const_kw() {
if data.is_const() {
f.write_str("const ")?;
}
if data.has_async_kw() {
if data.is_async() {
f.write_str("async ")?;
}
if self.is_unsafe_to_call(db) {
@ -125,7 +125,7 @@ impl HirDisplay for Function {
// `FunctionData::ret_type` will be `::core::future::Future<Output = ...>` for async fns.
// Use ugly pattern match to strip the Future trait.
// Better way?
let ret_type = if !data.has_async_kw() {
let ret_type = if !data.is_async() {
&data.ret_type
} else {
match &*data.ret_type {

View file

@ -2189,11 +2189,11 @@ impl Function {
}
pub fn is_const(self, db: &dyn HirDatabase) -> bool {
db.function_data(self.id).has_const_kw()
db.function_data(self.id).is_const()
}
pub fn is_async(self, db: &dyn HirDatabase) -> bool {
db.function_data(self.id).has_async_kw()
db.function_data(self.id).is_async()
}
/// Does this function have `#[test]` attribute?

View file

@ -483,6 +483,30 @@ unsafe fn foo() -> u8 {
fn main() {
let x = format!("foo: {}", foo$0());
}
"#,
)
}
#[test]
fn rustc_deprecated_safe_2024() {
check_diagnostics(
r#"
//- /ed2021.rs crate:ed2021 edition:2021
#[rustc_deprecated_safe_2024]
unsafe fn safe() -> u8 {
0
}
//- /ed2024.rs crate:ed2024 edition:2024
#[rustc_deprecated_safe_2024]
unsafe fn not_safe() -> u8 {
0
}
//- /main.rs crate:main deps:ed2021,ed2024
fn main() {
ed2021::safe();
ed2024::not_safe();
//^^^^^^^^^^^^^^^^^^💡 error: this operation is unsafe and requires an unsafe function or block
}
"#,
)