Skip to content

fix: Correctly support #[rustc_deprecated_safe_2024] #17859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/hir-def/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
14 changes: 10 additions & 4 deletions crates/hir-def/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/diagnostics/unsafe_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_)
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions crates/hir-ty/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
8 changes: 4 additions & 4 deletions crates/hir/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
24 changes: 24 additions & 0 deletions crates/ide-diagnostics/src/handlers/missing_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
"#,
)
Expand Down