Skip to content

Commit 0c07a3c

Browse files
committed
Auto merge of #32567 - soltanmm:clean-up, r=nikomatsakis
Refactor s.t. TypeRelation implementors in `infer` don't escape InferCtxt Some clean-up so that we can go back to the future of #31867 as opposed to #32542. r? @nikomatsakis
2 parents 641204a + 8cd0f0c commit 0c07a3c

File tree

5 files changed

+56
-72
lines changed

5 files changed

+56
-72
lines changed

src/librustc/infer/mod.rs

+22-49
Original file line numberDiff line numberDiff line change
@@ -386,33 +386,6 @@ pub fn normalizing_infer_ctxt<'a, 'tcx>(tcx: &'a TyCtxt<'tcx>,
386386
infcx
387387
}
388388

389-
/// Computes the least upper-bound of `a` and `b`. If this is not possible, reports an error and
390-
/// returns ty::err.
391-
pub fn common_supertype<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
392-
origin: TypeOrigin,
393-
a_is_expected: bool,
394-
a: Ty<'tcx>,
395-
b: Ty<'tcx>)
396-
-> Ty<'tcx>
397-
{
398-
debug!("common_supertype({:?}, {:?})",
399-
a, b);
400-
401-
let trace = TypeTrace {
402-
origin: origin,
403-
values: Types(expected_found(a_is_expected, a, b))
404-
};
405-
406-
let result = cx.commit_if_ok(|_| cx.lub(a_is_expected, trace.clone()).relate(&a, &b));
407-
match result {
408-
Ok(t) => t,
409-
Err(ref err) => {
410-
cx.report_and_explain_type_error(trace, err).emit();
411-
cx.tcx.types.err
412-
}
413-
}
414-
}
415-
416389
pub fn mk_subty<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
417390
a_is_expected: bool,
418391
origin: TypeOrigin,
@@ -434,7 +407,7 @@ pub fn can_mk_subty<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
434407
origin: TypeOrigin::Misc(codemap::DUMMY_SP),
435408
values: Types(expected_found(true, a, b))
436409
};
437-
cx.sub(true, trace).relate(&a, &b).map(|_| ())
410+
cx.sub(true, trace, &a, &b).map(|_| ())
438411
})
439412
}
440413

@@ -695,32 +668,32 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
695668
cause: None}
696669
}
697670

698-
// public so that it can be used from the rustc_driver unit tests
699-
pub fn equate(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>)
700-
-> 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>
701674
{
702-
self.combine_fields(a_is_expected, trace).equate()
675+
self.combine_fields(a_is_expected, trace).equate().relate(a, b)
703676
}
704677

705-
// public so that it can be used from the rustc_driver unit tests
706-
pub fn sub(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>)
707-
-> 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>
708681
{
709-
self.combine_fields(a_is_expected, trace).sub()
682+
self.combine_fields(a_is_expected, trace).sub().relate(a, b)
710683
}
711684

712-
// public so that it can be used from the rustc_driver unit tests
713-
pub fn lub(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>)
714-
-> 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>
715688
{
716-
self.combine_fields(a_is_expected, trace).lub()
689+
self.combine_fields(a_is_expected, trace).lub().relate(a, b)
717690
}
718691

719-
// public so that it can be used from the rustc_driver unit tests
720-
pub fn glb(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>)
721-
-> 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>
722695
{
723-
self.combine_fields(a_is_expected, trace).glb()
696+
self.combine_fields(a_is_expected, trace).glb().relate(a, b)
724697
}
725698

726699
fn start_snapshot(&self) -> CombinedSnapshot {
@@ -861,7 +834,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
861834
debug!("sub_types({:?} <: {:?})", a, b);
862835
self.commit_if_ok(|_| {
863836
let trace = TypeTrace::types(origin, a_is_expected, a, b);
864-
self.sub(a_is_expected, trace).relate(&a, &b).map(|_| ())
837+
self.sub(a_is_expected, trace, &a, &b).map(|_| ())
865838
})
866839
}
867840

@@ -874,7 +847,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
874847
{
875848
self.commit_if_ok(|_| {
876849
let trace = TypeTrace::types(origin, a_is_expected, a, b);
877-
self.equate(a_is_expected, trace).relate(&a, &b).map(|_| ())
850+
self.equate(a_is_expected, trace, &a, &b).map(|_| ())
878851
})
879852
}
880853

@@ -893,7 +866,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
893866
origin: origin,
894867
values: TraitRefs(expected_found(a_is_expected, a.clone(), b.clone()))
895868
};
896-
self.equate(a_is_expected, trace).relate(&a, &b).map(|_| ())
869+
self.equate(a_is_expected, trace, &a, &b).map(|_| ())
897870
})
898871
}
899872

@@ -912,7 +885,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
912885
origin: origin,
913886
values: PolyTraitRefs(expected_found(a_is_expected, a.clone(), b.clone()))
914887
};
915-
self.sub(a_is_expected, trace).relate(&a, &b).map(|_| ())
888+
self.sub(a_is_expected, trace, &a, &b).map(|_| ())
916889
})
917890
}
918891

@@ -1461,7 +1434,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14611434
origin: TypeOrigin::Misc(codemap::DUMMY_SP),
14621435
values: Types(expected_found(true, e, e))
14631436
};
1464-
self.equate(true, trace).relate(a, b)
1437+
self.equate(true, trace, a, b)
14651438
}).map(|_| ())
14661439
}
14671440

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)