1
Fork 0

Don't allow DispatchFromDyn impls that transmute ZST to non-ZST

This commit is contained in:
Michael Goulet 2025-01-08 00:29:22 +00:00
parent 3c3186148e
commit 11bc805369
3 changed files with 54 additions and 13 deletions

View file

@ -267,20 +267,20 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<()
let ty_a = field.ty(tcx, args_a);
let ty_b = field.ty(tcx, args_b);
// Allow 1-ZSTs that don't mention type params.
//
// Allowing type params here would allow us to possibly transmute
// between ZSTs, which may be used to create library unsoundness.
if let Ok(layout) =
tcx.layout_of(infcx.typing_env(param_env).as_query_input(ty_a))
&& layout.is_1zst()
&& !ty_a.has_non_region_param()
{
// ignore 1-ZST fields
return false;
}
if ty_a == ty_b {
// Allow 1-ZSTs that don't mention type params.
//
// Allowing type params here would allow us to possibly transmute
// between ZSTs, which may be used to create library unsoundness.
if let Ok(layout) =
tcx.layout_of(infcx.typing_env(param_env).as_query_input(ty_a))
&& layout.is_1zst()
&& !ty_a.has_non_region_param()
{
// ignore 1-ZST fields
return false;
}
res = Err(tcx.dcx().emit_err(errors::DispatchFromDynZST {
span,
name: field.name,