Skip to content

Commit 9cc3bfc

Browse files
committed
Introduce ImplHeader
This commit introduces the idea of an "impl header", which consists of everything outside the impl body: the Self type, the trait reference (when applicable), and predicates from `where` clauses. This type is usable with the type folding machinery, making it possible to work with impl headers at a higher and more generic level.
1 parent 40c85cd commit 9cc3bfc

File tree

6 files changed

+107
-75
lines changed

6 files changed

+107
-75
lines changed

src/librustc/middle/infer/mod.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -458,14 +458,13 @@ pub fn mk_eqty<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
458458
}
459459

460460
pub fn mk_eq_trait_refs<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
461-
a_is_expected: bool,
462-
origin: TypeOrigin,
463-
a: ty::TraitRef<'tcx>,
464-
b: ty::TraitRef<'tcx>)
465-
-> UnitResult<'tcx>
461+
a_is_expected: bool,
462+
origin: TypeOrigin,
463+
a: ty::TraitRef<'tcx>,
464+
b: ty::TraitRef<'tcx>)
465+
-> UnitResult<'tcx>
466466
{
467-
debug!("mk_eq_trait_refs({:?} <: {:?})",
468-
a, b);
467+
debug!("mk_eq_trait_refs({:?} = {:?})", a, b);
469468
cx.eq_trait_refs(a_is_expected, origin, a, b)
470469
}
471470

@@ -476,11 +475,25 @@ pub fn mk_sub_poly_trait_refs<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
476475
b: ty::PolyTraitRef<'tcx>)
477476
-> UnitResult<'tcx>
478477
{
479-
debug!("mk_sub_poly_trait_refs({:?} <: {:?})",
480-
a, b);
478+
debug!("mk_sub_poly_trait_refs({:?} <: {:?})", a, b);
481479
cx.sub_poly_trait_refs(a_is_expected, origin, a, b)
482480
}
483481

482+
pub fn mk_eq_impl_headers<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
483+
a_is_expected: bool,
484+
origin: TypeOrigin,
485+
a: ty::ImplHeader<'tcx>,
486+
b: ty::ImplHeader<'tcx>)
487+
-> UnitResult<'tcx>
488+
{
489+
debug!("mk_eq_impl_header({:?} = {:?})", a, b);
490+
match (a.trait_ref, b.trait_ref) {
491+
(Some(a_ref), Some(b_ref)) => mk_eq_trait_refs(cx, a_is_expected, a_ref, b_ref),
492+
(None, None) => mk_eqty(cx, a_is_expected, a.self_ty, b.self_ty),
493+
_ => cx.tcx.sess.bug("mk_eq_impl_headers given mismatched impl kinds"),
494+
}
495+
}
496+
484497
fn expected_found<T>(a_is_expected: bool,
485498
a: T,
486499
b: T)

src/librustc/middle/traits/coherence.rs

Lines changed: 24 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,28 @@
1010

1111
//! See `README.md` for high-level documentation
1212
13-
use super::Normalized;
14-
use super::SelectionContext;
15-
use super::ObligationCause;
16-
use super::PredicateObligation;
13+
use super::{Normalized, SelectionContext};
14+
use super::{Obligation, ObligationCause, PredicateObligation};
1715
use super::project;
1816
use super::util;
1917

2018
use middle::cstore::LOCAL_CRATE;
2119
use middle::def_id::DefId;
2220
use middle::subst::{Subst, Substs, TypeSpace};
2321
use middle::ty::{self, Ty, TyCtxt};
22+
use middle::ty::error::TypeError;
2423
use middle::infer::{self, InferCtxt, TypeOrigin};
2524
use syntax::codemap::{DUMMY_SP, Span};
2625

2726
#[derive(Copy, Clone)]
2827
struct InferIsLocal(bool);
2928

30-
/// If there are types that satisfy both impls, returns a `TraitRef`
29+
/// If there are types that satisfy both impls, returns an `ImplTy`
3130
/// with those types substituted (by updating the given `infcx`)
3231
pub fn overlapping_impls<'cx, 'tcx>(infcx: &InferCtxt<'cx, 'tcx>,
3332
impl1_def_id: DefId,
3433
impl2_def_id: DefId)
35-
-> Option<ty::TraitRef<'tcx>>
34+
-> Option<ImplTy<'tcx>>
3635
{
3736
debug!("impl_can_satisfy(\
3837
impl1_def_id={:?}, \
@@ -45,34 +44,28 @@ pub fn overlapping_impls<'cx, 'tcx>(infcx: &InferCtxt<'cx, 'tcx>,
4544
}
4645

4746
/// Can both impl `a` and impl `b` be satisfied by a common type (including
48-
/// `where` clauses)? If so, returns a `TraitRef` that unifies the two impls.
47+
/// `where` clauses)? If so, returns an `ImplHeader` that unifies the two impls.
4948
fn overlap<'cx, 'tcx>(selcx: &mut SelectionContext<'cx, 'tcx>,
5049
a_def_id: DefId,
5150
b_def_id: DefId)
52-
-> Option<ty::TraitRef<'tcx>>
51+
-> Option<ImplHeader<'tcx>>
5352
{
5453
debug!("overlap(a_def_id={:?}, b_def_id={:?})",
5554
a_def_id,
5655
b_def_id);
5756

58-
let (a_trait_ref, a_obligations) = impl_trait_ref_and_oblig(selcx,
59-
a_def_id,
60-
util::fresh_type_vars_for_impl);
57+
let a_impl_header = ty::ImplHeader::with_fresh_ty_vars(selcx, a_def_id);
58+
let b_impl_header = ty::ImplHeader::with_fresh_ty_vars(selcx, b_def_id);
6159

62-
let (b_trait_ref, b_obligations) = impl_trait_ref_and_oblig(selcx,
63-
b_def_id,
64-
util::fresh_type_vars_for_impl);
65-
66-
debug!("overlap: a_trait_ref={:?} a_obligations={:?}", a_trait_ref, a_obligations);
67-
68-
debug!("overlap: b_trait_ref={:?} b_obligations={:?}", b_trait_ref, b_obligations);
60+
debug!("overlap: a_impl_header={:?}", a_impl_header);
61+
debug!("overlap: b_impl_header={:?}", b_impl_header);
6962

7063
// Do `a` and `b` unify? If not, no overlap.
71-
if let Err(_) = infer::mk_eq_trait_refs(selcx.infcx(),
72-
true,
73-
TypeOrigin::Misc(DUMMY_SP),
74-
a_trait_ref,
75-
b_trait_ref) {
64+
if let Err(_) = infer::mk_eq_impl_headers(selcx.infcx(),
65+
true,
66+
TypeOrigin::Misc(DUMMY_SP),
67+
a_impl_header,
68+
b_impl_header) {
7669
return None;
7770
}
7871

@@ -81,17 +74,21 @@ fn overlap<'cx, 'tcx>(selcx: &mut SelectionContext<'cx, 'tcx>,
8174
// Are any of the obligations unsatisfiable? If so, no overlap.
8275
let infcx = selcx.infcx();
8376
let opt_failing_obligation =
84-
a_obligations.iter()
85-
.chain(&b_obligations)
86-
.map(|o| infcx.resolve_type_vars_if_possible(o))
77+
a_impl_header.prediates
78+
.iter()
79+
.chain(&b_impl_header.predicates)
80+
.map(|p| infcx.resolve_type_vars_if_possible(p))
81+
.map(|p| Obligation { cause: ObligationCause::dummy(),
82+
recursion_depth: 0,
83+
predicate: p })
8784
.find(|o| !selcx.evaluate_obligation(o));
8885

8986
if let Some(failing_obligation) = opt_failing_obligation {
9087
debug!("overlap: obligation unsatisfiable {:?}", failing_obligation);
9188
return None
9289
}
9390

94-
Some(selcx.infcx().resolve_type_vars_if_possible(&a_trait_ref))
91+
Some(selcx.infcx().resolve_type_vars_if_possible(&a_impl_header))
9592
}
9693

9794
pub fn trait_ref_is_knowable<'tcx>(tcx: &TyCtxt<'tcx>, trait_ref: &ty::TraitRef<'tcx>) -> bool
@@ -125,44 +122,6 @@ pub fn trait_ref_is_knowable<'tcx>(tcx: &TyCtxt<'tcx>, trait_ref: &ty::TraitRef<
125122
orphan_check_trait_ref(tcx, trait_ref, InferIsLocal(true)).is_err()
126123
}
127124

128-
type SubstsFn = for<'a,'tcx> fn(infcx: &InferCtxt<'a, 'tcx>,
129-
span: Span,
130-
impl_def_id: DefId)
131-
-> Substs<'tcx>;
132-
133-
/// Instantiate fresh variables for all bound parameters of the impl
134-
/// and return the impl trait ref with those variables substituted.
135-
fn impl_trait_ref_and_oblig<'a,'tcx>(selcx: &mut SelectionContext<'a,'tcx>,
136-
impl_def_id: DefId,
137-
substs_fn: SubstsFn)
138-
-> (ty::TraitRef<'tcx>,
139-
Vec<PredicateObligation<'tcx>>)
140-
{
141-
let impl_substs =
142-
&substs_fn(selcx.infcx(), DUMMY_SP, impl_def_id);
143-
let impl_trait_ref =
144-
selcx.tcx().impl_trait_ref(impl_def_id).unwrap();
145-
let impl_trait_ref =
146-
impl_trait_ref.subst(selcx.tcx(), impl_substs);
147-
let Normalized { value: impl_trait_ref, obligations: normalization_obligations1 } =
148-
project::normalize(selcx, ObligationCause::dummy(), &impl_trait_ref);
149-
150-
let predicates = selcx.tcx().lookup_predicates(impl_def_id);
151-
let predicates = predicates.instantiate(selcx.tcx(), impl_substs);
152-
let Normalized { value: predicates, obligations: normalization_obligations2 } =
153-
project::normalize(selcx, ObligationCause::dummy(), &predicates);
154-
let impl_obligations =
155-
util::predicates_for_generics(ObligationCause::dummy(), 0, &predicates);
156-
157-
let impl_obligations: Vec<_> =
158-
impl_obligations.into_iter()
159-
.chain(normalization_obligations1)
160-
.chain(normalization_obligations2)
161-
.collect();
162-
163-
(impl_trait_ref, impl_obligations)
164-
}
165-
166125
pub enum OrphanCheckErr<'tcx> {
167126
NoLocalInputType,
168127
UncoveredTy(Ty<'tcx>),

src/librustc/middle/traits/select.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
391391
// The result is "true" if the obligation *may* hold and "false" if
392392
// we can be sure it does not.
393393

394-
395394
/// Evaluates whether the obligation `obligation` can be satisfied (by any means).
396395
pub fn evaluate_obligation(&mut self,
397396
obligation: &PredicateObligation<'tcx>)

src/librustc/middle/ty/fold.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ pub trait TypeFolder<'tcx> : Sized {
146146
t.super_fold_with(self)
147147
}
148148

149+
fn fold_impl_header(&mut self, imp: &ty::ImplHeader<'tcx>) -> ty::ImplHeader<'tcx> {
150+
imp.super_fold_with(self)
151+
}
152+
149153
fn fold_substs(&mut self,
150154
substs: &subst::Substs<'tcx>)
151155
-> subst::Substs<'tcx> {

src/librustc/middle/ty/mod.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,41 @@ impl ImplOrTraitItemContainer {
152152
}
153153
}
154154

155+
/// The "header" of an impl is everything outside the body: a Self type, a trait
156+
/// ref (in the case of a trait impl), and a set of predicates (from the
157+
/// bounds/where clauses).
158+
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
159+
pub struct ImplHeader<'tcx> {
160+
pub impl_def_id: DefId,
161+
pub self_ty: Ty<'tcx>,
162+
pub trait_ref: Option<TraitRef<'tcx>>,
163+
pub predicates: Vec<Predicate<'tcx>>,
164+
}
165+
166+
impl<'tcx> ImplHeader<'tcx> {
167+
pub fn with_fresh_ty_vars<'a,'tcx>(selcx: &mut traits::SelectionContext<'a,'tcx>,
168+
impl_def_id: DefId)
169+
-> ImplHeader<'tcx>
170+
{
171+
let tcx = selcx.tcx();
172+
let impl_generics = tcx.lookup_item_type(impl_def_id).generics;
173+
let impl_substs = selcx.infcx().fresh_substs_for_generics(DUMMY_SP, &impl_generics);
174+
175+
let header = ImplHeader {
176+
impl_def_id: impl_def_id,
177+
self_ty: tcx.lookup_item_type(impl_def_id),
178+
trait_ref: tcx.impl_trait_ref(impl_def_id),
179+
predicates: tcx.lookup_predicates(impl_def_id),
180+
}.subst(tcx, impl_substs);
181+
182+
let Normalized { value: mut header, obligations: obligations } =
183+
proect::normalize(selcx, ObligationCause::dummy(), &header);
184+
185+
header.predicates.extend(obligations.into_iter().map(|o| o.predicate));
186+
header
187+
}
188+
}
189+
155190
#[derive(Clone)]
156191
pub enum ImplOrTraitItem<'tcx> {
157192
ConstTraitItem(Rc<AssociatedConst<'tcx>>),

src/librustc/middle/ty/structural_impls.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,28 @@ impl<'tcx> TypeFoldable<'tcx> for ty::TraitRef<'tcx> {
446446
}
447447
}
448448

449+
impl<'tcx> TypeFoldable<'tcx> for ty::ImplHeader<'tcx> {
450+
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
451+
ty::ImplHeader {
452+
impl_def_id: self.impl_def_id,
453+
self_ty: self.self_ty.fold_with(folder),
454+
trait_ref: self.trait_ref.map(|t| t.fold_with(folder)),
455+
predicates: self.predicates.into_iter().map(|p| p.fold_with(folder)).collect(),
456+
polarity: self.polarity,
457+
}
458+
}
459+
460+
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
461+
folder.fold_impl_header(self)
462+
}
463+
464+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
465+
self.self_ty.visit_with(visitor) ||
466+
self.trait_ref.map(|r| r.visit_with(visitor)).unwrap_or(false) ||
467+
self.predicates.iter().any(|p| p.visit_with(visitor))
468+
}
469+
}
470+
449471
impl<'tcx> TypeFoldable<'tcx> for ty::Region {
450472
fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
451473
*self

0 commit comments

Comments
 (0)