Skip to content

Commit 24bdce4

Browse files
nikomatsakisflaper87
authored andcommitted
some comments and nits
1 parent 64d33d8 commit 24bdce4

File tree

2 files changed

+45
-35
lines changed

2 files changed

+45
-35
lines changed

src/librustc/middle/traits/select.rs

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ use self::SelectionCandidate::*;
1717
use self::BuiltinBoundConditions::*;
1818
use self::EvaluationResult::*;
1919

20-
use super::{DerivedObligationCause};
21-
use super::{project};
22-
use super::project::Normalized;
20+
use super::DerivedObligationCause;
21+
use super::project;
22+
use super::project::{normalize_with_depth, Normalized};
2323
use super::{PredicateObligation, TraitObligation, ObligationCause};
2424
use super::{ObligationCauseCode, BuiltinDerivedObligation, ImplDerivedObligation};
2525
use super::{SelectionError, Unimplemented, Overflow, OutputTypeParameterMismatch};
@@ -1620,7 +1620,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
16201620
}
16211621
}
16221622

1623-
fn constituent_types(&self, t: Ty<'tcx>) -> Vec<Ty<'tcx>> {
1623+
fn constituent_ty_obligations(&self, t: Ty<'tcx>) -> Vec<Ty<'tcx>> {
16241624
match t.sty {
16251625
ty::ty_uint(_) |
16261626
ty::ty_int(_) |
@@ -1870,6 +1870,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
18701870
VtableBuiltinData { nested: obligations }
18711871
}
18721872

1873+
/// This handles the case where a `impl Foo for ..` impl is being used.
1874+
/// The idea is that the impl applies to `X : Foo` if the following conditions are met:
1875+
///
1876+
/// 1. For each constituent type `Y` in `X`, `Y : Foo` holds
1877+
/// 2. For each where-clause `C` declared on `Foo`, `[Self => X] C` holds.
18731878
fn confirm_default_impl_candidate(&mut self,
18741879
obligation: &TraitObligation<'tcx>,
18751880
impl_def_id: ast::DefId)
@@ -1881,10 +1886,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
18811886
impl_def_id.repr(self.tcx()));
18821887

18831888
let self_ty = self.infcx.shallow_resolve(obligation.predicate.0.self_ty());
1884-
let types = self.constituent_types(self_ty);
1889+
let types = self.constituent_ty_obligations(self_ty);
18851890
Ok(self.vtable_default_impl(obligation, impl_def_id, types))
18861891
}
18871892

1893+
/// See `confirm_default_impl_candidate`
18881894
fn vtable_default_impl(&mut self,
18891895
obligation: &TraitObligation<'tcx>,
18901896
trait_def_id: ast::DefId,
@@ -1930,12 +1936,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
19301936
self.infcx().skolemize_late_bound_regions(&obligation.predicate, snapshot);
19311937

19321938
let substs = obligation.predicate.to_poly_trait_ref().substs();
1933-
let trait_obligations = self.impl_obligations(obligation.cause.clone(),
1934-
obligation.recursion_depth + 1,
1935-
trait_def_id,
1936-
substs,
1937-
skol_map,
1938-
snapshot);
1939+
let trait_obligations = self.impl_or_trait_obligations(obligation.cause.clone(),
1940+
obligation.recursion_depth + 1,
1941+
trait_def_id,
1942+
substs,
1943+
skol_map,
1944+
snapshot);
19391945
obligations.push_all(trait_obligations.as_slice());
19401946
Ok(())
19411947
});
@@ -1988,12 +1994,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
19881994
skol_map.repr(self.tcx()));
19891995

19901996
let mut impl_obligations =
1991-
self.impl_obligations(cause,
1992-
recursion_depth,
1993-
impl_def_id,
1994-
&substs.value,
1995-
skol_map,
1996-
snapshot);
1997+
self.impl_or_trait_obligations(cause,
1998+
recursion_depth,
1999+
impl_def_id,
2000+
&substs.value,
2001+
skol_map,
2002+
snapshot);
19972003

19982004
debug!("vtable_impl: impl_def_id={} impl_obligations={}",
19992005
impl_def_id.repr(self.tcx()),
@@ -2413,28 +2419,30 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
24132419
ty::Binder(trait_ref)
24142420
}
24152421

2416-
fn impl_obligations(&mut self,
2417-
cause: ObligationCause<'tcx>,
2418-
recursion_depth: uint,
2419-
impl_def_id: ast::DefId,
2420-
impl_substs: &Substs<'tcx>,
2421-
skol_map: infer::SkolemizationMap,
2422-
snapshot: &infer::CombinedSnapshot)
2423-
-> VecPerParamSpace<PredicateObligation<'tcx>>
2422+
/// Returns the obligations that are implied by instantiating an
2423+
/// impl or trait. The obligations are substituted and fully
2424+
/// normalized. This is used when confirming an impl or default
2425+
/// impl.
2426+
fn impl_or_trait_obligations(&mut self,
2427+
cause: ObligationCause<'tcx>,
2428+
recursion_depth: uint,
2429+
def_id: ast::DefId, // of impl or trait
2430+
substs: &Substs<'tcx>, // for impl or trait
2431+
skol_map: infer::SkolemizationMap,
2432+
snapshot: &infer::CombinedSnapshot)
2433+
-> VecPerParamSpace<PredicateObligation<'tcx>>
24242434
{
2425-
let impl_bounds = ty::lookup_predicates(self.tcx(), impl_def_id);
2426-
let bounds = impl_bounds.instantiate(self.tcx(), impl_substs);
2427-
let normalized_bounds =
2428-
project::normalize_with_depth(self, cause.clone(), recursion_depth, &bounds);
2429-
let normalized_bounds =
2430-
self.infcx().plug_leaks(skol_map, snapshot, &normalized_bounds);
2431-
let mut impl_obligations =
2435+
let predicates = ty::lookup_predicates(self.tcx(), def_id);
2436+
let predicates = predicates.instantiate(self.tcx(), substs);
2437+
let predicates = normalize_with_depth(self, cause.clone(), recursion_depth, &predicates);
2438+
let predicates = self.infcx().plug_leaks(skol_map, snapshot, &predicates);
2439+
let mut obligations =
24322440
util::predicates_for_generics(self.tcx(),
24332441
cause,
24342442
recursion_depth,
2435-
&normalized_bounds.value);
2436-
impl_obligations.extend(TypeSpace, normalized_bounds.obligations.into_iter());
2437-
impl_obligations
2443+
&predicates.value);
2444+
obligations.extend(TypeSpace, predicates.obligations.into_iter());
2445+
obligations
24382446
}
24392447

24402448
#[allow(unused_comparisons)]

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// ignore-tidy-linelength
12+
1113
// Test that when a `..` impl applies, we also check that any
1214
// supertrait conditions are met.
1315

0 commit comments

Comments
 (0)