From f3c13bf2809f02d2e8f79496a27cf41c7a1fbd80 Mon Sep 17 00:00:00 2001 From: Maybe Lapkin Date: Sun, 7 Jul 2024 20:07:01 +0200 Subject: [PATCH] Allow casting `*mut dyn T`->`*mut (dyn T + Send)` if `T` has `Send` super trait --- compiler/rustc_hir_typeck/src/cast.rs | 11 +++++++++-- tests/ui/cast/ptr-to-trait-obj-add-super-auto.rs | 9 +++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 tests/ui/cast/ptr-to-trait-obj-add-super-auto.rs diff --git a/compiler/rustc_hir_typeck/src/cast.rs b/compiler/rustc_hir_typeck/src/cast.rs index 7b27a4b3ba7ed..57175f11baf33 100644 --- a/compiler/rustc_hir_typeck/src/cast.rs +++ b/compiler/rustc_hir_typeck/src/cast.rs @@ -869,9 +869,16 @@ impl<'a, 'tcx> CastCheck<'tcx> { // `dyn Src = dyn Dst`, this checks for matching traits/generics 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. - let src_auto = src_tty.auto_traits().collect::>(); + 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 .auto_traits() .filter(|trait_did| !src_auto.contains(trait_did)) diff --git a/tests/ui/cast/ptr-to-trait-obj-add-super-auto.rs b/tests/ui/cast/ptr-to-trait-obj-add-super-auto.rs new file mode 100644 index 0000000000000..ac8108d8ec41b --- /dev/null +++ b/tests/ui/cast/ptr-to-trait-obj-add-super-auto.rs @@ -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); +}