Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trait upcasting to dyn type with no principal when there are projections #139421

Merged
Merged
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
38 changes: 24 additions & 14 deletions compiler/rustc_trait_selection/src/traits/select/confirmation.rs
Original file line number Diff line number Diff line change
@@ -1090,26 +1090,36 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
{
// See `assemble_candidates_for_unsizing` for more info.
// We already checked the compatibility of auto traits within `assemble_candidates_for_unsizing`.
let iter = data_a
.principal()
.filter(|_| {
// optionally drop the principal, if we're unsizing to no principal
data_b.principal().is_some()
})
.map(|b| b.map_bound(ty::ExistentialPredicate::Trait))
.into_iter()
.chain(
let existential_predicates = if data_b.principal().is_some() {
tcx.mk_poly_existential_predicates_from_iter(
data_a
.projection_bounds()
.map(|b| b.map_bound(ty::ExistentialPredicate::Projection)),
.principal()
.map(|b| b.map_bound(ty::ExistentialPredicate::Trait))
.into_iter()
.chain(
data_a
.projection_bounds()
.map(|b| b.map_bound(ty::ExistentialPredicate::Projection)),
)
.chain(
data_b
.auto_traits()
.map(ty::ExistentialPredicate::AutoTrait)
.map(ty::Binder::dummy),
),
)
.chain(
} else {
// If we're unsizing to a dyn type that has no principal, then drop
// the principal and projections from the type. We use the auto traits
// from the RHS type since as we noted that we've checked for auto
// trait compatibility during unsizing.
tcx.mk_poly_existential_predicates_from_iter(
data_b
.auto_traits()
.map(ty::ExistentialPredicate::AutoTrait)
.map(ty::Binder::dummy),
);
let existential_predicates = tcx.mk_poly_existential_predicates_from_iter(iter);
)
};
let source_trait = Ty::new_dynamic(tcx, existential_predicates, r_b, dyn_a);

// Require that the traits involved in this upcast are **equal**;
13 changes: 13 additions & 0 deletions tests/ui/traits/dyn-drop-principal-with-projections.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@ check-pass

trait Tr {
type Assoc;
}

impl Tr for () {
type Assoc = ();
}

fn main() {
let x = &() as &(dyn Tr<Assoc = ()> + Send) as &dyn Send;
}