Allow casting *mut dyn T->*mut (dyn T + Send) if T has Send super trait

This commit is contained in:
Maybe Lapkin 2024-07-07 20:07:01 +02:00
parent 073f3a263b
commit f3c13bf280
2 changed files with 18 additions and 2 deletions

View File

@ -869,9 +869,16 @@ fn check_ptr_ptr_cast(
// `dyn Src = dyn Dst`, this checks for matching traits/generics // `dyn Src = dyn Dst`, this checks for matching traits/generics
fcx.demand_eqtype(self.span, src_obj, dst_obj); fcx.demand_eqtype(self.span, src_obj, dst_obj);
// Check that `SrcAuto` is a superset of `DstAuto`. // Check that `SrcAuto` (+auto traits implied by `Src`) is a superset of `DstAuto`.
// Emit an FCW otherwise. // Emit an FCW otherwise.
let src_auto = src_tty.auto_traits().collect::<FxHashSet<_>>(); let src_auto: FxHashSet<_> = src_tty
.auto_traits()
.chain(
tcx.supertrait_def_ids(src_principal.def_id())
.filter(|def_id| tcx.trait_is_auto(*def_id)),
)
.collect();
let added = dst_tty let added = dst_tty
.auto_traits() .auto_traits()
.filter(|trait_did| !src_auto.contains(trait_did)) .filter(|trait_did| !src_auto.contains(trait_did))

View File

@ -0,0 +1,9 @@
//@ check-pass
trait Trait: Send {}
impl Trait for () {}
fn main() {
// This is OK: `Trait` has `Send` super trait.
&() as *const dyn Trait as *const (dyn Trait + Send);
}