1
Fork 0

#[deprecated_safe_2024]: Also use the // TODO: hint in the compiler error

This doesn't work for translated compiler error messages.
This commit is contained in:
Tobias Bucher 2024-07-29 13:31:59 +02:00
parent 399ef23d2b
commit 811d7dd113
7 changed files with 18 additions and 11 deletions

View file

@ -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

View file

@ -102,18 +102,23 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
.meta_item_list()
.unwrap_or_default()
.into_iter()
.find(|item| item.has_name(sym::todo))
.find(|item| item.has_name(sym::audit_that))
.map(|item| {
item.value_str().expect(
"`#[rustc_deprecated_safe_2024(todo)]` must have a string value",
"`#[rustc_deprecated_safe_2024(audit_that)]` must have a string value",
)
});
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();
@ -124,6 +129,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(),

View file

@ -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,
}