Skip to content

Commit f68a28d

Browse files
authored
Rollup merge of rust-lang#127857 - tbu-:pr_deprecated_safe_todo, r=petrochenkov
Allow to customize `// TODO:` comment for deprecated safe autofix Relevant for the deprecation of `CommandExt::before_exit` in rust-lang#125970. Tracking: - rust-lang#124866
2 parents bc9c31d + 811d7dd commit f68a28d

File tree

7 files changed

+49
-13
lines changed

7 files changed

+49
-13
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

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

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 the code is only ever called from single-threaded code
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

+26-2
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,42 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
9696
// from an edition before 2024.
9797
&UnsafeOpKind::CallToUnsafeFunction(Some(id))
9898
if !span.at_least_rust_2024()
99-
&& self.tcx.has_attr(id, sym::rustc_deprecated_safe_2024) =>
99+
&& let Some(attr) = self.tcx.get_attr(id, sym::rustc_deprecated_safe_2024) =>
100100
{
101+
let suggestion = attr
102+
.meta_item_list()
103+
.unwrap_or_default()
104+
.into_iter()
105+
.find(|item| item.has_name(sym::audit_that))
106+
.map(|item| {
107+
item.value_str().expect(
108+
"`#[rustc_deprecated_safe_2024(audit_that)]` must have a string value",
109+
)
110+
});
111+
101112
let sm = self.tcx.sess.source_map();
113+
let guarantee = suggestion
114+
.as_ref()
115+
.map(|suggestion| format!("that {}", suggestion))
116+
.unwrap_or_else(|| String::from("its unsafe preconditions"));
117+
let suggestion = suggestion
118+
.and_then(|suggestion| {
119+
sm.indentation_before(span).map(|indent| {
120+
format!("{}// TODO: Audit that {}.\n", indent, suggestion) // ignore-tidy-todo
121+
})
122+
})
123+
.unwrap_or_default();
124+
102125
self.tcx.emit_node_span_lint(
103126
DEPRECATED_SAFE_2024,
104127
self.hir_context,
105128
span,
106129
CallToDeprecatedSafeFnRequiresUnsafe {
107130
span,
108131
function: with_no_trimmed_paths!(self.tcx.def_path_str(id)),
132+
guarantee,
109133
sub: CallToDeprecatedSafeFnRequiresUnsafeSub {
110-
indent: sm.indentation_before(span).unwrap_or_default(),
134+
start_of_line_suggestion: suggestion,
111135
start_of_line: sm.span_extend_to_line(span).shrink_to_lo(),
112136
left: span.shrink_to_lo(),
113137
right: span.shrink_to_hi(),

compiler/rustc_mir_build/src/errors.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,16 @@ 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
}
3435

3536
#[derive(Subdiagnostic)]
3637
#[multipart_suggestion(mir_build_suggestion, applicability = "machine-applicable")]
3738
pub(crate) struct CallToDeprecatedSafeFnRequiresUnsafeSub {
38-
pub(crate) indent: String,
39-
#[suggestion_part(
40-
code = "{indent}// TODO: Audit that the environment access only happens in single-threaded code.\n" // ignore-tidy-todo
41-
)]
39+
pub(crate) start_of_line_suggestion: String,
40+
#[suggestion_part(code = "{start_of_line_suggestion}")]
4241
pub(crate) start_of_line: Span,
4342
#[suggestion_part(code = "unsafe {{ ")]
4443
pub(crate) left: Span,

compiler/rustc_span/src/symbol.rs

+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,

library/std/src/env.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,13 @@ impl Error for VarError {
355355
/// }
356356
/// assert_eq!(env::var(key), Ok("VALUE".to_string()));
357357
/// ```
358-
#[rustc_deprecated_safe_2024]
358+
#[cfg_attr(bootstrap, rustc_deprecated_safe_2024)]
359+
#[cfg_attr(
360+
not(bootstrap),
361+
rustc_deprecated_safe_2024(
362+
audit_that = "the environment access only happens in single-threaded code"
363+
)
364+
)]
359365
#[stable(feature = "env", since = "1.0.0")]
360366
pub unsafe fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) {
361367
let (key, value) = (key.as_ref(), value.as_ref());
@@ -419,7 +425,13 @@ pub unsafe fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) {
419425
/// }
420426
/// assert!(env::var(key).is_err());
421427
/// ```
422-
#[rustc_deprecated_safe_2024]
428+
#[cfg_attr(bootstrap, rustc_deprecated_safe_2024)]
429+
#[cfg_attr(
430+
not(bootstrap),
431+
rustc_deprecated_safe_2024(
432+
audit_that = "the environment access only happens in single-threaded code"
433+
)
434+
)]
423435
#[stable(feature = "env", since = "1.0.0")]
424436
pub unsafe fn remove_var<K: AsRef<OsStr>>(key: K) {
425437
let key = key.as_ref();

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 the code is only ever called from single-threaded code
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 the code is only ever called from single-threaded code
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)