Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions compiler/rustc_hir_typeck/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,13 +572,25 @@ 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,
expr_ty,
cast_ty,
expr_if_nightly,
known_wide,
param_note,
});
}
CastError::UnknownCastPtrKind | CastError::UnknownExprPtrKind => {
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/cast/fat-ptr-cast.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the long time until the review.

I'm not sure if this message is helpful, but I don't have a better idea.

r? @estebank who had commented on that issue


error: aborting due to 11 previous errors

Expand Down
Loading