Skip to content

Commit

Permalink
Allow casting *mut dyn T->*mut (dyn T + Send) if T has Send s…
Browse files Browse the repository at this point in the history
…uper trait
  • Loading branch information
WaffleLapkin committed Jul 7, 2024
1 parent 073f3a2 commit f3c13bf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 9 additions & 2 deletions compiler/rustc_hir_typeck/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<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
.auto_traits()
.filter(|trait_did| !src_auto.contains(trait_did))
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/cast/ptr-to-trait-obj-add-super-auto.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit f3c13bf

Please sign in to comment.