Skip to content

Commit

Permalink
#[deprecated_safe_2024]: Also use the // TODO: hint in the compil…
Browse files Browse the repository at this point in the history
…er error

This doesn't work for translated compiler error messages.
  • Loading branch information
tbu- committed Jul 29, 2024
1 parent a253f11 commit 445595b
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
through unstable paths"
),
rustc_attr!(
rustc_deprecated_safe_2024, Normal, template!(List: r#"todo = "...""#),
rustc_deprecated_safe_2024, Normal, template!(List: r#"audit_that = "...""#),
ErrorFollowing, EncodeCrossCrate::Yes,
"rustc_deprecated_safe_2024 is supposed to be used in libstd only",
),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mir_build_call_to_deprecated_safe_fn_requires_unsafe =
call to deprecated safe function `{$function}` is unsafe and requires unsafe block
.note = consult the function's documentation for information on how to avoid undefined behavior
.label = call to unsafe function
.suggestion = you can wrap the call in an `unsafe` block if you can guarantee its unsafe preconditions
.suggestion = you can wrap the call in an `unsafe` block if you can guarantee {$guarantee}
mir_build_call_to_fn_with_requires_unsafe =
call to function `{$function}` with `#[target_feature]` is unsafe and requires unsafe block
Expand Down
20 changes: 12 additions & 8 deletions compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,10 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
fn emit_deprecated_safe_fn_call(&self, span: Span, kind: &UnsafeOpKind) -> bool {
fn parse_rustc_deprecated_safe_2024_attr(attr: &Attribute) -> Option<Symbol> {
for item in attr.meta_item_list().unwrap_or_default() {
if item.has_name(sym::todo) {
return Some(
item.value_str().expect(
"`#[rustc_deprecated_safe_2024(todo)]` must have a string value",
),
);
if item.has_name(sym::audit_that) {
return Some(item.value_str().expect(
"`#[rustc_deprecated_safe_2024(audit_that)]` must have a string value",
));
}
}
None
Expand All @@ -115,10 +113,15 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
let suggestion = parse_rustc_deprecated_safe_2024_attr(attr);

let sm = self.tcx.sess.source_map();
let guarantee = suggestion
.as_ref()
.map(|suggestion| format!("that {}", suggestion))
.unwrap_or_else(|| String::from("its unsafe preconditions"));
let suggestion = suggestion
.and_then(|suggestion| {
sm.indentation_before(span)
.map(|indent| format!("{}// TODO: {}\n", indent, suggestion)) // ignore-tidy-todo
sm.indentation_before(span).map(|indent| {
format!("{}// TODO: Audit that {}.\n", indent, suggestion) // ignore-tidy-todo
})
})
.unwrap_or_default();

Expand All @@ -129,6 +132,7 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
CallToDeprecatedSafeFnRequiresUnsafe {
span,
function: with_no_trimmed_paths!(self.tcx.def_path_str(id)),
guarantee,
sub: CallToDeprecatedSafeFnRequiresUnsafeSub {
start_of_line_suggestion: suggestion,
start_of_line: sm.span_extend_to_line(span).shrink_to_lo(),
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_mir_build/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub(crate) struct CallToDeprecatedSafeFnRequiresUnsafe {
#[label]
pub(crate) span: Span,
pub(crate) function: String,
pub(crate) guarantee: String,
#[subdiagnostic]
pub(crate) sub: CallToDeprecatedSafeFnRequiresUnsafeSub,
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ symbols! {
attr,
attr_literals,
attributes,
audit_that,
augmented_assignments,
auto_traits,
automatically_derived,
Expand Down Expand Up @@ -1892,7 +1893,6 @@ symbols! {
to_string,
to_string_method,
to_vec,
todo,
todo_macro,
tool_attributes,
tool_lints,
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ impl Error for VarError {
#[cfg_attr(
not(bootstrap),
rustc_deprecated_safe_2024(
todo = "Audit that the environment access only happens in single-threaded code."
audit_that = "the environment access only happens in single-threaded code"
)
)]
#[stable(feature = "env", since = "1.0.0")]
Expand Down Expand Up @@ -429,7 +429,7 @@ pub unsafe fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) {
#[cfg_attr(
not(bootstrap),
rustc_deprecated_safe_2024(
todo = "Audit that the environment access only happens in single-threaded code."
audit_that = "the environment access only happens in single-threaded code"
)
)]
#[stable(feature = "env", since = "1.0.0")]
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/rust-2024/unsafe-env-suggestion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ note: the lint level is defined here
|
LL | #![deny(deprecated_safe_2024)]
| ^^^^^^^^^^^^^^^^^^^^
help: you can wrap the call in an `unsafe` block if you can guarantee its unsafe preconditions
help: you can wrap the call in an `unsafe` block if you can guarantee that the environment access only happens in single-threaded code
|
LL + // TODO: Audit that the environment access only happens in single-threaded code.
LL ~ unsafe { env::set_var("FOO", "BAR") };
Expand All @@ -25,7 +25,7 @@ LL | env::remove_var("FOO");
|
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!
= note: for more information, see issue #27970 <https://github.com/rust-lang/rust/issues/27970>
help: you can wrap the call in an `unsafe` block if you can guarantee its unsafe preconditions
help: you can wrap the call in an `unsafe` block if you can guarantee that the environment access only happens in single-threaded code
|
LL + // TODO: Audit that the environment access only happens in single-threaded code.
LL ~ unsafe { env::remove_var("FOO") };
Expand Down

0 comments on commit 445595b

Please sign in to comment.