Skip to content

Commit

Permalink
Normalize super trait bounds when confirming object candidates
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper committed Oct 6, 2020
1 parent d08ab94 commit e674cf0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
29 changes: 20 additions & 9 deletions compiler/rustc_trait_selection/src/traits/select/confirmation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,26 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
let upcast_trait_ref = upcast_trait_ref.unwrap();

// Check supertraits hold
nested.extend(
tcx.super_predicates_of(trait_predicate.def_id())
.instantiate(tcx, trait_predicate.trait_ref.substs)
.predicates
.into_iter()
.map(|super_trait| {
Obligation::new(obligation.cause.clone(), obligation.param_env, super_trait)
}),
);
for super_trait in tcx
.super_predicates_of(trait_predicate.def_id())
.instantiate(tcx, trait_predicate.trait_ref.substs)
.predicates
.into_iter()
{
let normalized_super_trait = normalize_with_depth_to(
self,
obligation.param_env,
obligation.cause.clone(),
obligation.recursion_depth + 1,
&super_trait,
&mut nested,
);
nested.push(Obligation::new(
obligation.cause.clone(),
obligation.param_env.clone(),
normalized_super_trait,
));
}

let assoc_types: Vec<_> = tcx
.associated_items(trait_predicate.def_id())
Expand Down
26 changes: 26 additions & 0 deletions src/test/ui/associated-types/object-normalization.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// ignore-tidy-linelength

// Check that we normalize super predicates for object candidates.

// check-pass

use std::ops::Index;

fn next<'a, T>(s: &'a mut dyn SVec<Item = T, Output = T>) {
// To prove
// `dyn SVec<Item = T, Output = T>: SVec`
// we need to show
// `dyn SVec<Item = T, Output = T> as Index>::Output == <dyn SVec<Item = T, Output = T> as SVec>::Item`
// which, with the current normalization strategy, has to be eagerly
// normalized to:
// `dyn SVec<Item = T, Output = T> as Index>::Output == T`.
let _ = s.len();
}

trait SVec: Index<usize, Output = <Self as SVec>::Item> {
type Item;

fn len(&self) -> usize;
}

fn main() {}

0 comments on commit e674cf0

Please sign in to comment.