Skip to content

Commit ab1b47f

Browse files
committed
Migrate 'lossy int2ptr cast' diagnostic
1 parent 028fd12 commit ab1b47f

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

compiler/rustc_hir_typeck/messages.ftl

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ hir_typeck_lang_start_incorrect_param = parameter {$param_num} of the `start` la
6363
hir_typeck_lang_start_incorrect_ret_ty = the return type of the `start` lang item is incorrect
6464
.suggestion = change the type from `{$found_ty}` to `{$expected_ty}`
6565
66+
hir_typeck_lossy_provenance_int2ptr =
67+
strict provenance disallows casting integer `{$expr_ty}` to pointer `{$cast_ty}`
68+
.suggestion = use `.with_addr()` to adjust a valid pointer in the same allocation, to this address
69+
.help = if you can't comply with strict provenance and don't have a pointer with the correct provenance you can use `std::ptr::from_exposed_addr()` instead
70+
6671
hir_typeck_lossy_provenance_ptr2int =
6772
under strict provenance it is considered bad style to cast pointer `{$expr_ty}` to integer `{$cast_ty}`
6873
.suggestion = use `.addr()` to obtain the address of a pointer

compiler/rustc_hir_typeck/src/cast.rs

+8-20
Original file line numberDiff line numberDiff line change
@@ -1040,29 +1040,17 @@ impl<'a, 'tcx> CastCheck<'tcx> {
10401040
}
10411041

10421042
fn fuzzy_provenance_int2ptr_lint(&self, fcx: &FnCtxt<'a, 'tcx>) {
1043-
fcx.tcx.struct_span_lint_hir(
1043+
let sugg = errors::LossyProvenanceInt2PtrSuggestion {
1044+
lo: self.expr_span.shrink_to_lo(),
1045+
hi: self.expr_span.shrink_to_hi().to(self.cast_span),
1046+
};
1047+
let lint =
1048+
errors::LossyProvenanceInt2Ptr { expr_ty: self.expr_ty, cast_ty: self.cast_ty, sugg };
1049+
fcx.tcx.emit_spanned_lint(
10441050
lint::builtin::FUZZY_PROVENANCE_CASTS,
10451051
self.expr.hir_id,
10461052
self.span,
1047-
DelayDm(|| format!(
1048-
"strict provenance disallows casting integer `{}` to pointer `{}`",
1049-
self.expr_ty, self.cast_ty
1050-
)),
1051-
|lint| {
1052-
let msg = "use `.with_addr()` to adjust a valid pointer in the same allocation, to this address";
1053-
let suggestions = vec![
1054-
(self.expr_span.shrink_to_lo(), String::from("(...).with_addr(")),
1055-
(self.expr_span.shrink_to_hi().to(self.cast_span), String::from(")")),
1056-
];
1057-
1058-
lint.multipart_suggestion(msg, suggestions, Applicability::MaybeIncorrect);
1059-
lint.help(
1060-
"if you can't comply with strict provenance and don't have a pointer with \
1061-
the correct provenance you can use `std::ptr::from_exposed_addr()` instead"
1062-
);
1063-
1064-
lint
1065-
},
1053+
lint,
10661054
);
10671055
}
10681056

compiler/rustc_hir_typeck/src/errors.rs

+19
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,25 @@ pub struct LangStartIncorrectRetTy<'tcx> {
231231
pub found_ty: Ty<'tcx>,
232232
}
233233

234+
#[derive(LintDiagnostic)]
235+
#[diag(hir_typeck_lossy_provenance_int2ptr)]
236+
#[help]
237+
pub struct LossyProvenanceInt2Ptr<'tcx> {
238+
pub expr_ty: Ty<'tcx>,
239+
pub cast_ty: Ty<'tcx>,
240+
#[subdiagnostic]
241+
pub sugg: LossyProvenanceInt2PtrSuggestion,
242+
}
243+
244+
#[derive(Subdiagnostic)]
245+
#[multipart_suggestion(hir_typeck_suggestion, applicability = "has-placeholders")]
246+
pub struct LossyProvenanceInt2PtrSuggestion {
247+
#[suggestion_part(code = "(...).with_addr(")]
248+
pub lo: Span,
249+
#[suggestion_part(code = ")")]
250+
pub hi: Span,
251+
}
252+
234253
#[derive(LintDiagnostic)]
235254
#[diag(hir_typeck_lossy_provenance_ptr2int)]
236255
#[help]

0 commit comments

Comments
 (0)