Skip to content

Commit 6d1844c

Browse files
committed
Record default implementations in a separate step
1 parent 3ebc2ab commit 6d1844c

8 files changed

+43
-12
lines changed

src/librustc/middle/traits/select.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
847847

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

1150+
Ok(())
1151+
}
1152+
1153+
fn assemble_candidates_from_default_impls(&mut self,
1154+
obligation: &TraitObligation<'tcx>,
1155+
candidates: &mut SelectionCandidateSet<'tcx>)
1156+
-> Result<(), SelectionError<'tcx>>
1157+
{
1158+
1159+
let self_ty = self.infcx.shallow_resolve(obligation.self_ty());
1160+
debug!("assemble_candidates_from_default_impls(self_ty={})", self_ty.repr(self.tcx()));
1161+
1162+
let def_id = obligation.predicate.def_id();
1163+
11451164
if ty::trait_has_default_impl(self.tcx(), def_id) {
11461165
match self_ty.sty {
11471166
ty::ty_trait(..) |
@@ -1323,7 +1342,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
13231342
}
13241343
(&DefaultImplCandidate(_), _) => {
13251344
// Prefer other candidates over default implementations.
1326-
true
1345+
self.tcx().sess.bug(
1346+
"default implementations shouldn't be recorded \
1347+
when there are other valid candidates");
13271348
}
13281349
(&ProjectionCandidate, &ParamCandidate(_)) => {
13291350
// FIXME(#20297) -- this gives where clauses precedent

src/libtest/stats.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ pub fn winsorize<T: Float + FromPrimitive>(samples: &mut [T], pct: T) {
332332

333333
/// Returns a HashMap with the number of occurrences of every element in the
334334
/// sequence that the iterator exposes.
335+
#[cfg(not(stage0))]
335336
pub fn freq_count<T, U>(iter: T) -> hash_map::HashMap<U, uint>
336337
where T: Iterator<Item=U>, U: Eq + Clone + Hash
337338
{

src/test/compile-fail/coherence-default-trait-impl.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
#![feature(optin_builtin_traits)]
1414

15-
trait MyTrait {}
15+
use std::marker::MarkerTrait;
16+
17+
trait MyTrait: MarkerTrait {}
1618

1719
impl MyTrait for .. {}
1820

src/test/compile-fail/typeck-default-trait-impl-constituent-types-2.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
#![feature(optin_builtin_traits)]
1212

13-
trait MyTrait {}
13+
use std::marker::MarkerTrait;
14+
15+
trait MyTrait: MarkerTrait {}
1416

1517
impl MyTrait for .. {}
1618

src/test/compile-fail/typeck-default-trait-impl-constituent-types.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
#![feature(optin_builtin_traits)]
1212

13-
trait MyTrait {}
13+
use std::marker::MarkerTrait;
14+
15+
trait MyTrait: MarkerTrait {}
1416

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

3133
is_mytrait::<MyS2>();
3234
//~^ ERROR the trait `MyTrait` is not implemented for the type `MyS2`
33-
34-
is_mytrait::<Vec<MyS3>>();
35-
//~^ ERROR the trait `MyTrait` is not implemented for the type `*mut MyS3`
3635
}

src/test/compile-fail/typeck-default-trait-impl-negation.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010

1111
#![feature(optin_builtin_traits)]
1212

13-
trait MyTrait {}
13+
use std::marker::MarkerTrait;
14+
15+
trait MyTrait: MarkerTrait {}
1416

1517
impl MyTrait for .. {}
1618

17-
unsafe trait MyUnsafeTrait {}
19+
unsafe trait MyUnsafeTrait: MarkerTrait {}
1820

1921
unsafe impl MyUnsafeTrait for .. {}
2022

src/test/compile-fail/typeck-default-trait-impl-supertrait.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
#![feature(optin_builtin_traits)]
1515

16-
trait NotImplemented { }
16+
use std::marker::MarkerTrait;
17+
18+
trait NotImplemented: MarkerTrait { }
1719

1820
trait MyTrait : NotImplemented {}
1921

src/test/compile-fail/typeck-default-trait-impl-trait-where-clause.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515

1616
#![feature(optin_builtin_traits)]
1717

18-
trait NotImplemented { }
18+
use std::marker::MarkerTrait;
1919

20-
trait MyTrait
20+
trait NotImplemented: MarkerTrait { }
21+
22+
trait MyTrait: MarkerTrait
2123
where Option<Self> : NotImplemented
2224
{}
2325

0 commit comments

Comments
 (0)