Skip to content

Commit 77c78f1

Browse files
committed
Migrate 'lossy int2ptr cast' diagnostic
1 parent 910077d commit 77c78f1

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

compiler/rustc_hir_typeck/messages.ftl

Lines changed: 5 additions & 0 deletions
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

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,29 +1041,18 @@ impl<'a, 'tcx> CastCheck<'tcx> {
10411041
}
10421042

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

compiler/rustc_hir_typeck/src/errors.rs

Lines changed: 19 additions & 0 deletions
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)