Skip to content

Commit

Permalink
Record default implementations in a separate step
Browse files Browse the repository at this point in the history
  • Loading branch information
flaper87 committed Feb 22, 2015
1 parent 3ebc2ab commit 6d1844c
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 12 deletions.
23 changes: 22 additions & 1 deletion src/librustc/middle/traits/select.rs
Expand Up @@ -847,6 +847,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {

self.assemble_candidates_from_projected_tys(obligation, &mut candidates);
try!(self.assemble_candidates_from_caller_bounds(stack, &mut candidates));
// Default implementations have lower priority, so we only
// consider triggering a default if there is no other impl that can apply.
if candidates.vec.len() == 0 {
try!(self.assemble_candidates_from_default_impls(obligation, &mut candidates));
}
debug!("candidate list size: {}", candidates.vec.len());
Ok(candidates)
}
Expand Down Expand Up @@ -1142,6 +1147,20 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
});
}

Ok(())
}

fn assemble_candidates_from_default_impls(&mut self,
obligation: &TraitObligation<'tcx>,
candidates: &mut SelectionCandidateSet<'tcx>)
-> Result<(), SelectionError<'tcx>>
{

let self_ty = self.infcx.shallow_resolve(obligation.self_ty());
debug!("assemble_candidates_from_default_impls(self_ty={})", self_ty.repr(self.tcx()));

let def_id = obligation.predicate.def_id();

if ty::trait_has_default_impl(self.tcx(), def_id) {
match self_ty.sty {
ty::ty_trait(..) |
Expand Down Expand Up @@ -1323,7 +1342,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}
(&DefaultImplCandidate(_), _) => {
// Prefer other candidates over default implementations.
true
self.tcx().sess.bug(
"default implementations shouldn't be recorded \
when there are other valid candidates");
}
(&ProjectionCandidate, &ParamCandidate(_)) => {
// FIXME(#20297) -- this gives where clauses precedent
Expand Down
1 change: 1 addition & 0 deletions src/libtest/stats.rs
Expand Up @@ -332,6 +332,7 @@ pub fn winsorize<T: Float + FromPrimitive>(samples: &mut [T], pct: T) {

/// Returns a HashMap with the number of occurrences of every element in the
/// sequence that the iterator exposes.
#[cfg(not(stage0))]
pub fn freq_count<T, U>(iter: T) -> hash_map::HashMap<U, uint>
where T: Iterator<Item=U>, U: Eq + Clone + Hash
{
Expand Down
4 changes: 3 additions & 1 deletion src/test/compile-fail/coherence-default-trait-impl.rs
Expand Up @@ -12,7 +12,9 @@

#![feature(optin_builtin_traits)]

trait MyTrait {}
use std::marker::MarkerTrait;

trait MyTrait: MarkerTrait {}

impl MyTrait for .. {}

Expand Down
Expand Up @@ -10,7 +10,9 @@

#![feature(optin_builtin_traits)]

trait MyTrait {}
use std::marker::MarkerTrait;

trait MyTrait: MarkerTrait {}

impl MyTrait for .. {}

Expand Down
Expand Up @@ -10,7 +10,9 @@

#![feature(optin_builtin_traits)]

trait MyTrait {}
use std::marker::MarkerTrait;

trait MyTrait: MarkerTrait {}

impl MyTrait for .. {}
impl<T> !MyTrait for *mut T {}
Expand All @@ -30,7 +32,4 @@ fn main() {

is_mytrait::<MyS2>();
//~^ ERROR the trait `MyTrait` is not implemented for the type `MyS2`

is_mytrait::<Vec<MyS3>>();
//~^ ERROR the trait `MyTrait` is not implemented for the type `*mut MyS3`
}
6 changes: 4 additions & 2 deletions src/test/compile-fail/typeck-default-trait-impl-negation.rs
Expand Up @@ -10,11 +10,13 @@

#![feature(optin_builtin_traits)]

trait MyTrait {}
use std::marker::MarkerTrait;

trait MyTrait: MarkerTrait {}

impl MyTrait for .. {}

unsafe trait MyUnsafeTrait {}
unsafe trait MyUnsafeTrait: MarkerTrait {}

unsafe impl MyUnsafeTrait for .. {}

Expand Down
Expand Up @@ -13,7 +13,9 @@

#![feature(optin_builtin_traits)]

trait NotImplemented { }
use std::marker::MarkerTrait;

trait NotImplemented: MarkerTrait { }

trait MyTrait : NotImplemented {}

Expand Down
Expand Up @@ -15,9 +15,11 @@

#![feature(optin_builtin_traits)]

trait NotImplemented { }
use std::marker::MarkerTrait;

trait MyTrait
trait NotImplemented: MarkerTrait { }

trait MyTrait: MarkerTrait
where Option<Self> : NotImplemented
{}

Expand Down

0 comments on commit 6d1844c

Please sign in to comment.