Skip to content

Commit

Permalink
add Self: Trait<..> inside the param_env of a default impl
Browse files Browse the repository at this point in the history
  • Loading branch information
giannicic committed Feb 15, 2018
1 parent 2f22a92 commit 220bb22
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 62 deletions.
6 changes: 3 additions & 3 deletions src/libcore/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,15 +579,15 @@ impl<'a, I, T: 'a> FusedIterator for Cloned<I>
{}

#[doc(hidden)]
default unsafe impl<'a, I, T: 'a> TrustedRandomAccess for Cloned<I>
unsafe impl<'a, I, T: 'a> TrustedRandomAccess for Cloned<I>
where I: TrustedRandomAccess<Item=&'a T>, T: Clone
{
unsafe fn get_unchecked(&mut self, i: usize) -> Self::Item {
default unsafe fn get_unchecked(&mut self, i: usize) -> Self::Item {
self.it.get_unchecked(i).clone()
}

#[inline]
fn may_have_side_effect() -> bool { true }
default fn may_have_side_effect() -> bool { true }
}

#[doc(hidden)]
Expand Down
44 changes: 11 additions & 33 deletions src/librustc/traits/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1710,44 +1710,22 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
{
debug!("assemble_candidates_from_impls(obligation={:?})", obligation);

// Check if default impls should be emitted.
// default impls are emitted if the param_env is refered to a default impl.
// The param_env should contain a Self: Trait<..> predicate in those cases
let self_trait_is_present:Vec<&ty::Predicate<'tcx>> =
obligation.param_env
.caller_bounds
.iter()
.filter(|predicate| {
match **predicate {
ty::Predicate::Trait(ref trait_predicate) => {
trait_predicate.def_id() ==
obligation.predicate.def_id() &&
obligation.predicate.0.trait_ref.self_ty() ==
trait_predicate.skip_binder().self_ty()
}
_ => false
}
}).collect::<Vec<&ty::Predicate<'tcx>>>();

self.tcx().for_each_relevant_impl(
obligation.predicate.def_id(),
obligation.predicate.0.trait_ref.self_ty(),
|impl_def_id| {
if self_trait_is_present.len() > 0 ||
!self.tcx().impl_is_default(impl_def_id) {
self.probe(|this, snapshot| { /* [1] */
match this.match_impl(impl_def_id, obligation, snapshot) {
Ok(skol_map) => {
candidates.vec.push(ImplCandidate(impl_def_id));

// NB: we can safely drop the skol map
// since we are in a probe [1]
mem::drop(skol_map);
}
Err(_) => { }
self.probe(|this, snapshot| { /* [1] */
match this.match_impl(impl_def_id, obligation, snapshot) {
Ok(skol_map) => {
candidates.vec.push(ImplCandidate(impl_def_id));

// NB: we can safely drop the skol map
// since we are in a probe [1]
mem::drop(skol_map);
}
});
}
Err(_) => { }
}
});
}
);

Expand Down
25 changes: 1 addition & 24 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2606,31 +2606,8 @@ fn param_env<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId)
-> ParamEnv<'tcx> {
// Compute the bounds on Self and the type parameters.
let mut predicates = tcx.predicates_of(def_id);
match tcx.hir.as_local_node_id(def_id)
.and_then(|node_id| tcx.hir.find(node_id))
.and_then(|item| {
match item {
hir::map::NodeItem(..) => {
if tcx.impl_is_default(def_id) {
tcx.impl_trait_ref(def_id)
} else {
None
}
}
_ => None
}
}) {
Some(trait_ref) =>
predicates.predicates
.push(
trait_ref.to_poly_trait_ref()
.to_predicate()
),
None => {}
}

let bounds = predicates.instantiate_identity(tcx);
let bounds = tcx.predicates_of(def_id).instantiate_identity(tcx);
let predicates = bounds.predicates;

// Finally, we have to normalize the bounds in the environment, in
Expand Down
20 changes: 19 additions & 1 deletion src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,7 @@ fn explicit_predicates_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let node = tcx.hir.get(node_id);

let mut is_trait = None;
let mut is_default_impl_trait = None;

let icx = ItemCtxt::new(tcx, def_id);
let no_generics = hir::Generics::empty();
Expand All @@ -1373,8 +1374,13 @@ fn explicit_predicates_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

NodeItem(item) => {
match item.node {
ItemImpl(_, _, defaultness, ref generics, ..) => {
if defaultness.is_default() {
is_default_impl_trait = tcx.impl_trait_ref(def_id);
}
generics
}
ItemFn(.., ref generics, _) |
ItemImpl(_, _, _, ref generics, ..) |
ItemTy(_, ref generics) |
ItemEnum(_, ref generics) |
ItemStruct(_, ref generics) |
Expand Down Expand Up @@ -1446,6 +1452,18 @@ fn explicit_predicates_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
predicates.push(trait_ref.to_poly_trait_ref().to_predicate());
}

// In default impls, we can assume that the self type implements
// the trait. So in:
//
// default impl Foo for Bar { .. }
//
// we add a default where clause `Foo: Bar`. We do a similar thing for traits
// (see below). Recall that a default impl is not itself an impl, but rather a
// set of defaults that can be incorporated into another impl.
if let Some(trait_ref) = is_default_impl_trait {
predicates.push(trait_ref.to_poly_trait_ref().to_predicate());
}

// Collect the region predicates that were declared inline as
// well. In the case of parameters declared on a fn or method, we
// have to be careful to only iterate over early-bound regions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Tests that default impls do not have to supply all items but regular impls do.

#![feature(specialization)]

trait Foo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Tests that:
// - default impls do not have to supply all items and
// - a default impl does not count as an impl (in this case, an incomplete default impl).

#![feature(specialization)]

trait Foo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Tests that a default impl still has to have a WF trait ref.

#![feature(specialization)]

trait Foo<'a, T: Eq + 'a> { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,33 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Tests that we can combine a default impl that supplies one method with a
// full impl that supplies the other, and they can invoke one another.

#![feature(specialization)]

trait Foo {
fn foo_one(&self) -> &'static str;
fn foo_two(&self) -> &'static str;
fn foo_three(&self) -> &'static str;
}

struct MyStruct;

default impl<T> Foo for T {
fn foo_one(&self) -> &'static str {
"generic"
self.foo_three()
}
}

impl Foo for MyStruct {
fn foo_two(&self) -> &'static str {
self.foo_one()
}

fn foo_three(&self) -> &'static str {
"generic"
}
}

fn main() {
Expand Down

0 comments on commit 220bb22

Please sign in to comment.