Skip to content

Commit e2fd1db

Browse files
committed
Auto merge of #17859 - Veykril:rustc_deprecated_safe_2024, r=Veykril
fix: Correctly support `#[rustc_deprecated_safe_2024]` Fixes #17852
2 parents c7cbbb9 + ded3e21 commit e2fd1db

File tree

8 files changed

+44
-19
lines changed

8 files changed

+44
-19
lines changed

crates/hir-def/src/body.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ impl Body {
158158
}),
159159
)
160160
});
161-
is_async_fn = data.has_async_kw();
161+
is_async_fn = data.is_async();
162162
src.map(|it| it.body().map(ast::Expr::from))
163163
}
164164
DefWithBodyId::ConstId(c) => {

crates/hir-def/src/data.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ impl FunctionData {
9494
.filter(|it| !it.is_empty())
9595
.map(Box::new);
9696
let rustc_allow_incoherent_impl = attrs.by_key(&sym::rustc_allow_incoherent_impl).exists();
97+
if flags.contains(FnFlags::HAS_UNSAFE_KW)
98+
&& !crate_graph[krate].edition.at_least_2024()
99+
&& attrs.by_key(&sym::rustc_deprecated_safe_2024).exists()
100+
{
101+
flags.remove(FnFlags::HAS_UNSAFE_KW);
102+
}
97103

98104
Arc::new(FunctionData {
99105
name: func.name.clone(),
@@ -126,19 +132,19 @@ impl FunctionData {
126132
self.flags.contains(FnFlags::HAS_SELF_PARAM)
127133
}
128134

129-
pub fn has_default_kw(&self) -> bool {
135+
pub fn is_default(&self) -> bool {
130136
self.flags.contains(FnFlags::HAS_DEFAULT_KW)
131137
}
132138

133-
pub fn has_const_kw(&self) -> bool {
139+
pub fn is_const(&self) -> bool {
134140
self.flags.contains(FnFlags::HAS_CONST_KW)
135141
}
136142

137-
pub fn has_async_kw(&self) -> bool {
143+
pub fn is_async(&self) -> bool {
138144
self.flags.contains(FnFlags::HAS_ASYNC_KW)
139145
}
140146

141-
pub fn has_unsafe_kw(&self) -> bool {
147+
pub fn is_unsafe(&self) -> bool {
142148
self.flags.contains(FnFlags::HAS_UNSAFE_KW)
143149
}
144150

crates/hir-ty/src/diagnostics/unsafe_check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn missing_unsafe(db: &dyn HirDatabase, def: DefWithBodyId) -> Vec<ExprId> {
1717

1818
let mut res = Vec::new();
1919
let is_unsafe = match def {
20-
DefWithBodyId::FunctionId(it) => db.function_data(it).has_unsafe_kw(),
20+
DefWithBodyId::FunctionId(it) => db.function_data(it).is_unsafe(),
2121
DefWithBodyId::StaticId(_)
2222
| DefWithBodyId::ConstId(_)
2323
| DefWithBodyId::VariantId(_)

crates/hir-ty/src/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1857,7 +1857,7 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
18571857
params,
18581858
ret,
18591859
data.is_varargs(),
1860-
if data.has_unsafe_kw() { Safety::Unsafe } else { Safety::Safe },
1860+
if data.is_unsafe() { Safety::Unsafe } else { Safety::Safe },
18611861
data.abi.as_ref().map_or(FnAbi::Rust, FnAbi::from_symbol),
18621862
);
18631863
make_binders(db, &generics, sig)

crates/hir-ty/src/utils.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,7 @@ impl<'a> ClosureSubst<'a> {
253253

254254
pub fn is_fn_unsafe_to_call(db: &dyn HirDatabase, func: FunctionId) -> bool {
255255
let data = db.function_data(func);
256-
if data.has_unsafe_kw() {
257-
// Functions that are `#[rustc_deprecated_safe_2024]` are safe to call before 2024.
258-
if db.attrs(func.into()).by_key(&sym::rustc_deprecated_safe_2024).exists() {
259-
// FIXME: Properly check the caller span and mark it as unsafe after 2024.
260-
return false;
261-
}
256+
if data.is_unsafe() {
262257
return true;
263258
}
264259

crates/hir/src/display.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ impl HirDisplay for Function {
6969

7070
write_visibility(module_id, self.visibility(db), f)?;
7171

72-
if data.has_default_kw() {
72+
if data.is_default() {
7373
f.write_str("default ")?;
7474
}
75-
if data.has_const_kw() {
75+
if data.is_const() {
7676
f.write_str("const ")?;
7777
}
78-
if data.has_async_kw() {
78+
if data.is_async() {
7979
f.write_str("async ")?;
8080
}
8181
if self.is_unsafe_to_call(db) {
@@ -125,7 +125,7 @@ impl HirDisplay for Function {
125125
// `FunctionData::ret_type` will be `::core::future::Future<Output = ...>` for async fns.
126126
// Use ugly pattern match to strip the Future trait.
127127
// Better way?
128-
let ret_type = if !data.has_async_kw() {
128+
let ret_type = if !data.is_async() {
129129
&data.ret_type
130130
} else {
131131
match &*data.ret_type {

crates/hir/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,11 +2189,11 @@ impl Function {
21892189
}
21902190

21912191
pub fn is_const(self, db: &dyn HirDatabase) -> bool {
2192-
db.function_data(self.id).has_const_kw()
2192+
db.function_data(self.id).is_const()
21932193
}
21942194

21952195
pub fn is_async(self, db: &dyn HirDatabase) -> bool {
2196-
db.function_data(self.id).has_async_kw()
2196+
db.function_data(self.id).is_async()
21972197
}
21982198

21992199
/// Does this function have `#[test]` attribute?

crates/ide-diagnostics/src/handlers/missing_unsafe.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,30 @@ unsafe fn foo() -> u8 {
483483
484484
fn main() {
485485
let x = format!("foo: {}", foo$0());
486+
}
487+
"#,
488+
)
489+
}
490+
491+
#[test]
492+
fn rustc_deprecated_safe_2024() {
493+
check_diagnostics(
494+
r#"
495+
//- /ed2021.rs crate:ed2021 edition:2021
496+
#[rustc_deprecated_safe_2024]
497+
unsafe fn safe() -> u8 {
498+
0
499+
}
500+
//- /ed2024.rs crate:ed2024 edition:2024
501+
#[rustc_deprecated_safe_2024]
502+
unsafe fn not_safe() -> u8 {
503+
0
504+
}
505+
//- /main.rs crate:main deps:ed2021,ed2024
506+
fn main() {
507+
ed2021::safe();
508+
ed2024::not_safe();
509+
//^^^^^^^^^^^^^^^^^^💡 error: this operation is unsafe and requires an unsafe function or block
486510
}
487511
"#,
488512
)

0 commit comments

Comments
 (0)