Rollup merge of #152159 - JohnTitor:issue-74756, r=estebank

Add note for `?Sized` params in int-ptr casts diag

Close rust-lang/rust#74756
This commit is contained in:
Jacob Pratt 2026-02-12 00:41:07 -05:00 committed by GitHub
commit 3ad8e9ce04
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 0 deletions

View file

@ -572,6 +572,17 @@ impl<'a, 'tcx> CastCheck<'tcx> {
let metadata = known_metadata.unwrap_or("type-specific metadata");
let known_wide = known_metadata.is_some();
let span = self.cast_span;
let param_note = (!known_wide)
.then(|| match cast_ty.kind() {
ty::RawPtr(pointee, _) => match pointee.kind() {
ty::Param(param) => {
Some(errors::IntToWideParamNote { param: param.name })
}
_ => None,
},
_ => None,
})
.flatten();
fcx.dcx().emit_err(errors::IntToWide {
span,
metadata,
@ -579,6 +590,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
cast_ty,
expr_if_nightly,
known_wide,
param_note,
});
}
CastError::UnknownCastPtrKind | CastError::UnknownExprPtrKind => {

View file

@ -590,6 +590,14 @@ pub(crate) struct IntToWide<'tcx> {
)]
pub expr_if_nightly: Option<Span>,
pub known_wide: bool,
#[subdiagnostic]
pub param_note: Option<IntToWideParamNote>,
}
#[derive(Subdiagnostic)]
#[note("the type parameter `{$param}` is not known to be `Sized`, so this pointer may be wide")]
pub(crate) struct IntToWideParamNote {
pub param: Symbol,
}
#[derive(Subdiagnostic)]

View file

@ -81,6 +81,8 @@ LL | let s = 0 as *const T;
| - ^^^^^^^^ creating a `*const T` requires both an address and type-specific metadata
| |
| consider casting this expression to `*const ()`, then using `core::ptr::from_raw_parts`
|
= note: the type parameter `T` is not known to be `Sized`, so this pointer may be wide
error: aborting due to 11 previous errors