Skip to content

Commit 8cd0f0c

Browse files
Refactor s.t. TypeRelation implementors don't escape InferCtxt
1 parent f10a12c commit 8cd0f0c

File tree

5 files changed

+56
-45
lines changed

5 files changed

+56
-45
lines changed

src/librustc/infer/mod.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ pub fn can_mk_subty<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
407407
origin: TypeOrigin::Misc(codemap::DUMMY_SP),
408408
values: Types(expected_found(true, a, b))
409409
};
410-
cx.sub(true, trace).relate(&a, &b).map(|_| ())
410+
cx.sub(true, trace, &a, &b).map(|_| ())
411411
})
412412
}
413413

@@ -668,32 +668,32 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
668668
cause: None}
669669
}
670670

671-
// public so that it can be used from the rustc_driver unit tests
672-
pub fn equate(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>)
673-
-> equate::Equate<'a, 'tcx>
671+
pub fn equate<T>(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>, a: &T, b: &T)
672+
-> RelateResult<'tcx, T>
673+
where T: Relate<'a, 'tcx>
674674
{
675-
self.combine_fields(a_is_expected, trace).equate()
675+
self.combine_fields(a_is_expected, trace).equate().relate(a, b)
676676
}
677677

678-
// public so that it can be used from the rustc_driver unit tests
679-
pub fn sub(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>)
680-
-> sub::Sub<'a, 'tcx>
678+
pub fn sub<T>(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>, a: &T, b: &T)
679+
-> RelateResult<'tcx, T>
680+
where T: Relate<'a, 'tcx>
681681
{
682-
self.combine_fields(a_is_expected, trace).sub()
682+
self.combine_fields(a_is_expected, trace).sub().relate(a, b)
683683
}
684684

685-
// public so that it can be used from the rustc_driver unit tests
686-
pub fn lub(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>)
687-
-> lub::Lub<'a, 'tcx>
685+
pub fn lub<T>(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>, a: &T, b: &T)
686+
-> RelateResult<'tcx, T>
687+
where T: Relate<'a, 'tcx>
688688
{
689-
self.combine_fields(a_is_expected, trace).lub()
689+
self.combine_fields(a_is_expected, trace).lub().relate(a, b)
690690
}
691691

692-
// public so that it can be used from the rustc_driver unit tests
693-
pub fn glb(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>)
694-
-> glb::Glb<'a, 'tcx>
692+
pub fn glb<T>(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>, a: &T, b: &T)
693+
-> RelateResult<'tcx, T>
694+
where T: Relate<'a, 'tcx>
695695
{
696-
self.combine_fields(a_is_expected, trace).glb()
696+
self.combine_fields(a_is_expected, trace).glb().relate(a, b)
697697
}
698698

699699
fn start_snapshot(&self) -> CombinedSnapshot {
@@ -834,7 +834,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
834834
debug!("sub_types({:?} <: {:?})", a, b);
835835
self.commit_if_ok(|_| {
836836
let trace = TypeTrace::types(origin, a_is_expected, a, b);
837-
self.sub(a_is_expected, trace).relate(&a, &b).map(|_| ())
837+
self.sub(a_is_expected, trace, &a, &b).map(|_| ())
838838
})
839839
}
840840

@@ -847,7 +847,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
847847
{
848848
self.commit_if_ok(|_| {
849849
let trace = TypeTrace::types(origin, a_is_expected, a, b);
850-
self.equate(a_is_expected, trace).relate(&a, &b).map(|_| ())
850+
self.equate(a_is_expected, trace, &a, &b).map(|_| ())
851851
})
852852
}
853853

@@ -866,7 +866,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
866866
origin: origin,
867867
values: TraitRefs(expected_found(a_is_expected, a.clone(), b.clone()))
868868
};
869-
self.equate(a_is_expected, trace).relate(&a, &b).map(|_| ())
869+
self.equate(a_is_expected, trace, &a, &b).map(|_| ())
870870
})
871871
}
872872

@@ -885,7 +885,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
885885
origin: origin,
886886
values: PolyTraitRefs(expected_found(a_is_expected, a.clone(), b.clone()))
887887
};
888-
self.sub(a_is_expected, trace).relate(&a, &b).map(|_| ())
888+
self.sub(a_is_expected, trace, &a, &b).map(|_| ())
889889
})
890890
}
891891

@@ -1434,7 +1434,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14341434
origin: TypeOrigin::Misc(codemap::DUMMY_SP),
14351435
values: Types(expected_found(true, e, e))
14361436
};
1437-
self.equate(true, trace).relate(a, b)
1437+
self.equate(true, trace, a, b)
14381438
}).map(|_| ())
14391439
}
14401440

src/librustc/ty/relate.rs

+11
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,17 @@ impl<'a,'tcx:'a> Relate<'a,'tcx> for ty::ClosureSubsts<'tcx> {
602602
}
603603
}
604604

605+
impl<'a,'tcx:'a> Relate<'a,'tcx> for Substs<'tcx> {
606+
fn relate<R>(relation: &mut R,
607+
a: &Substs<'tcx>,
608+
b: &Substs<'tcx>)
609+
-> RelateResult<'tcx, Substs<'tcx>>
610+
where R: TypeRelation<'a,'tcx>
611+
{
612+
relate_substs(relation, None, a, b)
613+
}
614+
}
615+
605616
impl<'a,'tcx:'a> Relate<'a,'tcx> for ty::Region {
606617
fn relate<R>(relation: &mut R,
607618
a: &ty::Region,

src/librustc_driver/test.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ use rustc::ty::subst;
2424
use rustc::ty::subst::Subst;
2525
use rustc::traits::ProjectionMode;
2626
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
27-
use rustc::ty::relate::TypeRelation;
27+
use rustc::ty::relate::{TypeRelation, RelateResult};
2828
use rustc::infer::{self, TypeOrigin};
29-
use rustc::infer::lub::Lub;
30-
use rustc::infer::glb::Glb;
31-
use rustc::infer::sub::Sub;
3229
use rustc_metadata::cstore::CStore;
3330
use rustc::front::map as hir_map;
3431
use rustc::session::{self, config};
@@ -358,25 +355,25 @@ impl<'a, 'tcx> Env<'a, 'tcx> {
358355
infer::TypeTrace::dummy(self.tcx())
359356
}
360357

361-
pub fn sub(&self) -> Sub<'a, 'tcx> {
358+
pub fn sub(&self, t1: &Ty<'tcx>, t2: &Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
362359
let trace = self.dummy_type_trace();
363-
self.infcx.sub(true, trace)
360+
self.infcx.sub(true, trace, t1, t2)
364361
}
365362

366-
pub fn lub(&self) -> Lub<'a, 'tcx> {
363+
pub fn lub(&self, t1: &Ty<'tcx>, t2: &Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
367364
let trace = self.dummy_type_trace();
368-
self.infcx.lub(true, trace)
365+
self.infcx.lub(true, trace, t1, t2)
369366
}
370367

371-
pub fn glb(&self) -> Glb<'a, 'tcx> {
368+
pub fn glb(&self, t1: &Ty<'tcx>, t2: &Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
372369
let trace = self.dummy_type_trace();
373-
self.infcx.glb(true, trace)
370+
self.infcx.glb(true, trace, t1, t2)
374371
}
375372

376373
/// Checks that `t1 <: t2` is true (this may register additional
377374
/// region checks).
378375
pub fn check_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
379-
match self.sub().relate(&t1, &t2) {
376+
match self.sub(&t1, &t2) {
380377
Ok(_) => {}
381378
Err(ref e) => {
382379
panic!("unexpected error computing sub({:?},{:?}): {}", t1, t2, e);
@@ -387,7 +384,7 @@ impl<'a, 'tcx> Env<'a, 'tcx> {
387384
/// Checks that `t1 <: t2` is false (this may register additional
388385
/// region checks).
389386
pub fn check_not_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
390-
match self.sub().relate(&t1, &t2) {
387+
match self.sub(&t1, &t2) {
391388
Err(_) => {}
392389
Ok(_) => {
393390
panic!("unexpected success computing sub({:?},{:?})", t1, t2);
@@ -397,7 +394,7 @@ impl<'a, 'tcx> Env<'a, 'tcx> {
397394

398395
/// Checks that `LUB(t1,t2) == t_lub`
399396
pub fn check_lub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_lub: Ty<'tcx>) {
400-
match self.lub().relate(&t1, &t2) {
397+
match self.lub(&t1, &t2) {
401398
Ok(t) => {
402399
self.assert_eq(t, t_lub);
403400
}
@@ -410,7 +407,7 @@ impl<'a, 'tcx> Env<'a, 'tcx> {
410407
/// Checks that `GLB(t1,t2) == t_glb`
411408
pub fn check_glb(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_glb: Ty<'tcx>) {
412409
debug!("check_glb(t1={}, t2={}, t_glb={})", t1, t2, t_glb);
413-
match self.glb().relate(&t1, &t2) {
410+
match self.glb(&t1, &t2) {
414411
Err(e) => {
415412
panic!("unexpected error computing LUB: {:?}", e)
416413
}

src/librustc_typeck/check/coercion.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ use rustc::ty::adjustment::{AdjustUnsafeFnPointer, AdjustMutToConstPointer};
7171
use rustc::ty::{self, LvaluePreference, TypeAndMut, Ty, TyCtxt};
7272
use rustc::ty::fold::TypeFoldable;
7373
use rustc::ty::error::TypeError;
74-
use rustc::ty::relate::{relate_substs, Relate, RelateResult, TypeRelation};
74+
use rustc::ty::relate::{RelateResult, TypeRelation};
7575
use util::common::indent;
7676

7777
use std::cell::RefCell;
@@ -117,9 +117,9 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
117117
infcx.commit_if_ok(|_| {
118118
let trace = TypeTrace::types(self.origin, false, a, b);
119119
if self.use_lub {
120-
infcx.lub(false, trace).relate(&a, &b)
120+
infcx.lub(false, trace, &a, &b)
121121
} else {
122-
infcx.sub(false, trace).relate(&a, &b)
122+
infcx.sub(false, trace, &a, &b)
123123
}
124124
})
125125
}
@@ -649,20 +649,19 @@ pub fn try_find_lub<'a, 'b, 'tcx, E, I>(fcx: &FnCtxt<'a, 'tcx>,
649649
debug!("coercion::try_find_lub({:?}, {:?})", prev_ty, new_ty);
650650

651651
let trace = TypeTrace::types(origin, true, prev_ty, new_ty);
652-
let mut lub = fcx.infcx().lub(true, trace);
653652

654653
// Special-case that coercion alone cannot handle:
655654
// Two function item types of differing IDs or Substs.
656655
match (&prev_ty.sty, &new_ty.sty) {
657656
(&ty::TyFnDef(a_def_id, a_substs, a_fty),
658657
&ty::TyFnDef(b_def_id, b_substs, b_fty)) => {
659658
// The signature must always match.
660-
let fty = lub.relate(a_fty, b_fty)?;
659+
let fty = fcx.infcx().lub(true, trace.clone(), a_fty, b_fty)?;
661660

662661
if a_def_id == b_def_id {
663662
// Same function, maybe the parameters match.
664663
let substs = fcx.infcx().commit_if_ok(|_| {
665-
relate_substs(&mut lub, None, a_substs, b_substs)
664+
fcx.infcx().lub(true, trace.clone(), a_substs, b_substs)
666665
}).map(|s| fcx.tcx().mk_substs(s));
667666

668667
if let Ok(substs) = substs {
@@ -724,7 +723,9 @@ pub fn try_find_lub<'a, 'b, 'tcx, E, I>(fcx: &FnCtxt<'a, 'tcx>,
724723
};
725724

726725
if !noop {
727-
return fcx.infcx().commit_if_ok(|_| lub.relate(&prev_ty, &new_ty));
726+
return fcx.infcx().commit_if_ok(|_| {
727+
fcx.infcx().lub(true, trace.clone(), &prev_ty, &new_ty)
728+
});
728729
}
729730
}
730731

@@ -734,7 +735,9 @@ pub fn try_find_lub<'a, 'b, 'tcx, E, I>(fcx: &FnCtxt<'a, 'tcx>,
734735
if let Some(e) = first_error {
735736
Err(e)
736737
} else {
737-
fcx.infcx().commit_if_ok(|_| lub.relate(&prev_ty, &new_ty))
738+
fcx.infcx().commit_if_ok(|_| {
739+
fcx.infcx().lub(true, trace, &prev_ty, &new_ty)
740+
})
738741
}
739742
}
740743
Ok((ty, adjustment)) => {

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2904,7 +2904,7 @@ fn check_expr_with_expectation_and_lvalue_pref<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
29042904
} else {
29052905
fcx.infcx().commit_if_ok(|_| {
29062906
let trace = TypeTrace::types(origin, true, then_ty, else_ty);
2907-
fcx.infcx().lub(true, trace).relate(&then_ty, &else_ty)
2907+
fcx.infcx().lub(true, trace, &then_ty, &else_ty)
29082908
})
29092909
};
29102910
(origin, then_ty, else_ty, result)

0 commit comments

Comments
 (0)