Skip to content

Commit 9ad9819

Browse files
committed
#[deprecated_safe_2024]: Also use the // TODO: hint in the compiler error
This doesn't work for translated compiler error messages.
1 parent 9e51884 commit 9ad9819

File tree

7 files changed

+20
-15
lines changed

7 files changed

+20
-15
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
643643
through unstable paths"
644644
),
645645
rustc_attr!(
646-
rustc_deprecated_safe_2024, Normal, template!(List: r#"todo = "...""#),
646+
rustc_deprecated_safe_2024, Normal, template!(List: r#"audit_that = "...""#),
647647
ErrorFollowing, EncodeCrossCrate::Yes,
648648
"rustc_deprecated_safe_2024 is supposed to be used in libstd only",
649649
),

compiler/rustc_mir_build/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ mir_build_call_to_deprecated_safe_fn_requires_unsafe =
3030
call to deprecated safe function `{$function}` is unsafe and requires unsafe block
3131
.note = consult the function's documentation for information on how to avoid undefined behavior
3232
.label = call to unsafe function
33-
.suggestion = you can wrap the call in an `unsafe` block if you can guarantee its unsafe preconditions
33+
.suggestion = you can wrap the call in an `unsafe` block if you can guarantee {$guarantee}
3434
3535
mir_build_call_to_fn_with_requires_unsafe =
3636
call to function `{$function}` with `#[target_feature]` is unsafe and requires unsafe block

compiler/rustc_mir_build/src/check_unsafety.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,10 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
9494
fn emit_deprecated_safe_fn_call(&self, span: Span, kind: &UnsafeOpKind) -> bool {
9595
fn parse_rustc_deprecated_safe_2024_attr(attr: &Attribute) -> Option<Symbol> {
9696
for item in attr.meta_item_list().unwrap_or_default() {
97-
if item.has_name(sym::todo) {
98-
return Some(
99-
item.value_str().expect(
100-
"`#[rustc_deprecated_safe_2024(todo)]` must have a string value",
101-
),
102-
);
97+
if item.has_name(sym::audit_that) {
98+
return Some(item.value_str().expect(
99+
"`#[rustc_deprecated_safe_2024(audit_that)]` must have a string value",
100+
));
103101
}
104102
}
105103
None
@@ -115,10 +113,15 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
115113
let suggestion = parse_rustc_deprecated_safe_2024_attr(attr);
116114

117115
let sm = self.tcx.sess.source_map();
116+
let guarantee = suggestion
117+
.as_ref()
118+
.map(|suggestion| format!("that {}", suggestion))
119+
.unwrap_or_else(|| String::from("its unsafe preconditions"));
118120
let suggestion = suggestion
119121
.and_then(|suggestion| {
120-
sm.indentation_before(span)
121-
.map(|indent| format!("{}// TODO: {}\n", indent, suggestion)) // ignore-tidy-todo
122+
sm.indentation_before(span).map(|indent| {
123+
format!("{}// TODO: Audit that {}.\n", indent, suggestion) // ignore-tidy-todo
124+
})
122125
})
123126
.unwrap_or_default();
124127

@@ -129,6 +132,7 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
129132
CallToDeprecatedSafeFnRequiresUnsafe {
130133
span,
131134
function: with_no_trimmed_paths!(self.tcx.def_path_str(id)),
135+
guarantee,
132136
sub: CallToDeprecatedSafeFnRequiresUnsafeSub {
133137
start_of_line_suggestion: suggestion,
134138
start_of_line: sm.span_extend_to_line(span).shrink_to_lo(),

compiler/rustc_mir_build/src/errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub(crate) struct CallToDeprecatedSafeFnRequiresUnsafe {
2828
#[label]
2929
pub(crate) span: Span,
3030
pub(crate) function: String,
31+
pub(crate) guarantee: String,
3132
#[subdiagnostic]
3233
pub(crate) sub: CallToDeprecatedSafeFnRequiresUnsafeSub,
3334
}

compiler/rustc_span/src/symbol.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ symbols! {
472472
attr,
473473
attr_literals,
474474
attributes,
475+
audit_that,
475476
augmented_assignments,
476477
auto_traits,
477478
automatically_derived,
@@ -1896,7 +1897,6 @@ symbols! {
18961897
to_string,
18971898
to_string_method,
18981899
to_vec,
1899-
todo,
19001900
todo_macro,
19011901
tool_attributes,
19021902
tool_lints,

library/std/src/env.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ impl Error for VarError {
359359
#[cfg_attr(
360360
not(bootstrap),
361361
rustc_deprecated_safe_2024(
362-
todo = "Audit that the environment access only happens in single-threaded code."
362+
audit_that = "the environment access only happens in single-threaded code"
363363
)
364364
)]
365365
#[stable(feature = "env", since = "1.0.0")]
@@ -429,7 +429,7 @@ pub unsafe fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) {
429429
#[cfg_attr(
430430
not(bootstrap),
431431
rustc_deprecated_safe_2024(
432-
todo = "Audit that the environment access only happens in single-threaded code."
432+
audit_that = "the environment access only happens in single-threaded code"
433433
)
434434
)]
435435
#[stable(feature = "env", since = "1.0.0")]

tests/ui/rust-2024/unsafe-env-suggestion.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ note: the lint level is defined here
1111
|
1212
LL | #![deny(deprecated_safe_2024)]
1313
| ^^^^^^^^^^^^^^^^^^^^
14-
help: you can wrap the call in an `unsafe` block if you can guarantee its unsafe preconditions
14+
help: you can wrap the call in an `unsafe` block if you can guarantee that the environment access only happens in single-threaded code
1515
|
1616
LL + // TODO: Audit that the environment access only happens in single-threaded code.
1717
LL ~ unsafe { env::set_var("FOO", "BAR") };
@@ -25,7 +25,7 @@ LL | env::remove_var("FOO");
2525
|
2626
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!
2727
= note: for more information, see issue #27970 <https://github.com/rust-lang/rust/issues/27970>
28-
help: you can wrap the call in an `unsafe` block if you can guarantee its unsafe preconditions
28+
help: you can wrap the call in an `unsafe` block if you can guarantee that the environment access only happens in single-threaded code
2929
|
3030
LL + // TODO: Audit that the environment access only happens in single-threaded code.
3131
LL ~ unsafe { env::remove_var("FOO") };

0 commit comments

Comments
 (0)