Skip to content

Commit

Permalink
register infer-ok obligations properly
Browse files Browse the repository at this point in the history
Or at least, more properly. I think I left one or two FIXMEs still in
there.

cc #32730
  • Loading branch information
nikomatsakis committed Nov 15, 2016
1 parent 19c1a47 commit 48dc6e2
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 119 deletions.
3 changes: 2 additions & 1 deletion src/librustc/infer/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use super::combine::CombineFields;
use super::SubregionOrigin;
use super::combine::CombineFields;
use super::type_variable::{SubtypeOf, SupertypeOf};

use ty::{self, Ty, TyCtxt};
Expand Down Expand Up @@ -117,6 +117,7 @@ impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
// error messages.
let origin = SubregionOrigin::Subtype(self.fields.trace.clone());
self.fields.infcx.region_vars.make_subregion(origin, a, b);

Ok(a)
}

Expand Down
6 changes: 2 additions & 4 deletions src/librustc/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,8 @@ fn process_predicate<'a, 'gcx, 'tcx>(

ty::Predicate::Equate(ref binder) => {
match selcx.infcx().equality_predicate(&obligation.cause, binder) {
Ok(InferOk { obligations, .. }) => {
// FIXME(#32730) propagate obligations
assert!(obligations.is_empty());
Ok(Some(Vec::new()))
Ok(InferOk { obligations, value: () }) => {
Ok(Some(obligations))
},
Err(_) => Err(CodeSelectionError(Unimplemented)),
}
Expand Down
17 changes: 6 additions & 11 deletions src/librustc/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,7 @@ fn project_and_unify_type<'cx, 'gcx, 'tcx>(

let infcx = selcx.infcx();
match infcx.eq_types(true, &obligation.cause, normalized_ty, obligation.predicate.ty) {
Ok(InferOk { obligations: inferred_obligations, .. }) => {
// FIXME(#32730) once obligations are generated in inference, drop this assertion
assert!(inferred_obligations.is_empty());
Ok(InferOk { obligations: inferred_obligations, value: () }) => {
obligations.extend(inferred_obligations);
Ok(Some(obligations))
},
Expand Down Expand Up @@ -847,9 +845,10 @@ fn assemble_candidates_from_predicates<'cx, 'gcx, 'tcx, I>(
obligation.cause.clone(),
data_poly_trait_ref,
obligation_poly_trait_ref)
// FIXME(#32730) once obligations are propagated from unification in
// inference, drop this assertion
.map(|InferOk { obligations, .. }| assert!(obligations.is_empty()))
.map(|InferOk { obligations: _, value: () }| {
// FIXME(#32730) -- do we need to take obligations
// into account in any way? At the moment, no.
})
.is_ok()
});

Expand Down Expand Up @@ -1184,12 +1183,10 @@ fn confirm_fn_pointer_candidate<'cx, 'gcx, 'tcx>(
fn_pointer_vtable: VtableFnPointerData<'tcx, PredicateObligation<'tcx>>)
-> Progress<'tcx>
{
// FIXME(#32730) drop this assertion once obligations are propagated from inference (fn pointer
// vtable nested obligations ONLY come from unification in inference)
assert!(fn_pointer_vtable.nested.is_empty());
let fn_type = selcx.infcx().shallow_resolve(fn_pointer_vtable.fn_ty);
let sig = fn_type.fn_sig();
confirm_callable_candidate(selcx, obligation, sig, util::TupleArgumentsFlag::Yes)
.with_addl_obligations(fn_pointer_vtable.nested)
}

fn confirm_closure_candidate<'cx, 'gcx, 'tcx>(
Expand Down Expand Up @@ -1266,8 +1263,6 @@ fn confirm_param_env_candidate<'cx, 'gcx, 'tcx>(
let trait_ref = obligation.predicate.trait_ref;
match infcx.match_poly_projection_predicate(cause, poly_projection, trait_ref) {
Ok(InferOk { value: ty_match, obligations }) => {
// FIXME(#32730) once obligations are generated in inference, drop this assertion
assert!(obligations.is_empty());
Progress {
ty: ty_match.value,
obligations: obligations,
Expand Down
3 changes: 0 additions & 3 deletions src/librustc/traits/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,6 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
None => Ok(None),
Some(candidate) => {
let mut candidate = self.confirm_candidate(obligation, candidate)?;
// FIXME(#32730) remove this assertion once inferred obligations are propagated
// from inference
assert!(self.inferred_obligations.len() == 0);
let inferred_obligations = (*self.inferred_obligations).into_iter().cloned();
candidate.nested_obligations_mut().extend(inferred_obligations);
Ok(Some(candidate))
Expand Down
37 changes: 21 additions & 16 deletions src/librustc_mir/transform/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,20 +324,25 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
traits::ObligationCause::misc(span, self.body_id)
}

fn sub_types(&self, span: Span, sup: Ty<'tcx>, sub: Ty<'tcx>)
pub fn register_infer_ok_obligations<T>(&mut self, infer_ok: InferOk<'tcx, T>) -> T {
for obligation in infer_ok.obligations {
self.fulfillment_cx.register_predicate_obligation(self.infcx, obligation);
}
infer_ok.value
}

fn sub_types(&mut self, sup: Ty<'tcx>, sub: Ty<'tcx>)
-> infer::UnitResult<'tcx>
{
self.infcx.sub_types(false, &self.misc(span), sup, sub)
// FIXME(#32730) propagate obligations
.map(|InferOk { obligations, .. }| assert!(obligations.is_empty()))
self.infcx.sub_types(false, &self.misc(self.last_span), sup, sub)
.map(|ok| self.register_infer_ok_obligations(ok))
}

fn eq_types(&self, span: Span, a: Ty<'tcx>, b: Ty<'tcx>)
fn eq_types(&mut self, span: Span, a: Ty<'tcx>, b: Ty<'tcx>)
-> infer::UnitResult<'tcx>
{
self.infcx.eq_types(false, &self.misc(span), a, b)
// FIXME(#32730) propagate obligations
.map(|InferOk { obligations, .. }| assert!(obligations.is_empty()))
.map(|ok| self.register_infer_ok_obligations(ok))
}

fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> {
Expand All @@ -352,7 +357,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
let lv_ty = lv.ty(mir, tcx).to_ty(tcx);
let rv_ty = rv.ty(mir, tcx);
if let Some(rv_ty) = rv_ty {
if let Err(terr) = self.sub_types(self.last_span, rv_ty, lv_ty) {
if let Err(terr) = self.sub_types(rv_ty, lv_ty) {
span_mirbug!(self, stmt, "bad assignment ({:?} = {:?}): {:?}",
lv_ty, rv_ty, terr);
}
Expand Down Expand Up @@ -413,7 +418,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
} => {
let lv_ty = location.ty(mir, tcx).to_ty(tcx);
let rv_ty = value.ty(mir, tcx);
if let Err(terr) = self.sub_types(self.last_span, rv_ty, lv_ty) {
if let Err(terr) = self.sub_types(rv_ty, lv_ty) {
span_mirbug!(self, term, "bad DropAndReplace ({:?} = {:?}): {:?}",
lv_ty, rv_ty, terr);
}
Expand All @@ -430,7 +435,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
}
TerminatorKind::SwitchInt { ref discr, switch_ty, .. } => {
let discr_ty = discr.ty(mir, tcx).to_ty(tcx);
if let Err(terr) = self.sub_types(self.last_span, discr_ty, switch_ty) {
if let Err(terr) = self.sub_types(discr_ty, switch_ty) {
span_mirbug!(self, term, "bad SwitchInt ({:?} on {:?}): {:?}",
switch_ty, discr_ty, terr);
}
Expand Down Expand Up @@ -492,7 +497,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
}
}

fn check_call_dest(&self,
fn check_call_dest(&mut self,
mir: &Mir<'tcx>,
term: &Terminator<'tcx>,
sig: &ty::FnSig<'tcx>,
Expand All @@ -501,7 +506,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
match *destination {
Some((ref dest, _)) => {
let dest_ty = dest.ty(mir, tcx).to_ty(tcx);
if let Err(terr) = self.sub_types(self.last_span, sig.output, dest_ty) {
if let Err(terr) = self.sub_types(sig.output, dest_ty) {
span_mirbug!(self, term,
"call dest mismatch ({:?} <- {:?}): {:?}",
dest_ty, sig.output, terr);
Expand All @@ -516,7 +521,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
}
}

fn check_call_inputs(&self,
fn check_call_inputs(&mut self,
mir: &Mir<'tcx>,
term: &Terminator<'tcx>,
sig: &ty::FnSig<'tcx>,
Expand All @@ -529,7 +534,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
}
for (n, (fn_arg, op_arg)) in sig.inputs.iter().zip(args).enumerate() {
let op_arg_ty = op_arg.ty(mir, self.tcx());
if let Err(terr) = self.sub_types(self.last_span, op_arg_ty, fn_arg) {
if let Err(terr) = self.sub_types(op_arg_ty, fn_arg) {
span_mirbug!(self, term, "bad arg #{:?} ({:?} <- {:?}): {:?}",
n, fn_arg, op_arg_ty, terr);
}
Expand All @@ -547,7 +552,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
}
}

fn check_box_free_inputs(&self,
fn check_box_free_inputs(&mut self,
mir: &Mir<'tcx>,
term: &Terminator<'tcx>,
sig: &ty::FnSig<'tcx>,
Expand Down Expand Up @@ -584,7 +589,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
}
};

if let Err(terr) = self.sub_types(self.last_span, arg_ty, pointee_ty) {
if let Err(terr) = self.sub_types(arg_ty, pointee_ty) {
span_mirbug!(self, term, "bad box_free arg ({:?} <- {:?}): {:?}",
pointee_ty, arg_ty, terr);
}
Expand Down
7 changes: 3 additions & 4 deletions src/librustc_typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use rustc::hir::{self, PatKind};
use rustc::hir::def::{Def, CtorKind};
use rustc::hir::pat_util::EnumerateAndAdjustIterator;
use rustc::infer::{self, InferOk};
use rustc::infer;
use rustc::traits::ObligationCauseCode;
use rustc::ty::{self, Ty, TypeFoldable, LvaluePreference};
use check::{FnCtxt, Expectation, Diverges};
Expand Down Expand Up @@ -462,9 +462,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {

let result = if is_if_let_fallback {
self.eq_types(true, &cause, arm_ty, result_ty)
.map(|InferOk { obligations, .. }| {
// FIXME(#32730) propagate obligations
assert!(obligations.is_empty());
.map(|infer_ok| {
self.register_infer_ok_obligations(infer_ok);
arm_ty
})
} else if i == 0 {
Expand Down
33 changes: 6 additions & 27 deletions src/librustc_typeck/check/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,11 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
let trace = TypeTrace::types(&self.cause, false, a, b);
if self.use_lub {
self.lub(false, trace, &a, &b)
.map(|InferOk { value, obligations }| {
// FIXME(#32730) propagate obligations
assert!(obligations.is_empty());
value
})
.map(|ok| self.register_infer_ok_obligations(ok))
} else {
self.sub(false, trace, &a, &b)
.map(|InferOk { value, obligations }| {
// FIXME(#32730) propagate obligations
assert!(obligations.is_empty());
self.fcx.register_predicates(obligations);
value
})
}
Expand Down Expand Up @@ -678,21 +673,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
(&ty::TyFnDef(a_def_id, a_substs, a_fty), &ty::TyFnDef(b_def_id, b_substs, b_fty)) => {
// The signature must always match.
let fty = self.lub(true, trace.clone(), &a_fty, &b_fty)
.map(|InferOk { value, obligations }| {
// FIXME(#32730) propagate obligations
assert!(obligations.is_empty());
value
})?;
.map(|ok| self.register_infer_ok_obligations(ok))?;

if a_def_id == b_def_id {
// Same function, maybe the parameters match.
let substs = self.commit_if_ok(|_| {
self.lub(true, trace.clone(), &a_substs, &b_substs)
.map(|InferOk { value, obligations }| {
// FIXME(#32730) propagate obligations
assert!(obligations.is_empty());
value
})
.map(|ok| self.register_infer_ok_obligations(ok))
});

if let Ok(substs) = substs {
Expand Down Expand Up @@ -761,11 +748,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if !noop {
return self.commit_if_ok(|_| {
self.lub(true, trace.clone(), &prev_ty, &new_ty)
.map(|InferOk { value, obligations }| {
// FIXME(#32730) propagate obligations
assert!(obligations.is_empty());
value
})
.map(|ok| self.register_infer_ok_obligations(ok))
});
}
}
Expand All @@ -778,11 +761,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
} else {
self.commit_if_ok(|_| {
self.lub(true, trace, &prev_ty, &new_ty)
.map(|InferOk { value, obligations }| {
// FIXME(#32730) propagate obligations
assert!(obligations.is_empty());
value
})
.map(|ok| self.register_infer_ok_obligations(ok))
})
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/librustc_typeck/check/compare_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -810,10 +810,11 @@ pub fn compare_const_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
debug!("compare_const_impl: trait_ty={:?}", trait_ty);

infcx.sub_types(false, &cause, impl_ty, trait_ty)
.map(|InferOk { obligations, .. }| {
// FIXME(#32730) propagate obligations
assert!(obligations.is_empty())
})
.map(|InferOk { obligations, value: () }| {
for obligation in obligations {
fulfillment_cx.register_predicate_obligation(&infcx, obligation);
}
})
});

if let Err(terr) = err {
Expand Down
10 changes: 4 additions & 6 deletions src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
pub fn demand_suptype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
let cause = self.misc(sp);
match self.sub_types(false, &cause, actual, expected) {
Ok(InferOk { obligations, .. }) => {
// FIXME(#32730) propagate obligations
assert!(obligations.is_empty());
Ok(InferOk { obligations, value: () }) => {
self.register_predicates(obligations);
},
Err(e) => {
self.report_mismatched_types(&cause, expected, actual, e);
Expand All @@ -43,9 +42,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
actual: Ty<'tcx>)
{
match self.eq_types(false, cause, actual, expected) {
Ok(InferOk { obligations, .. }) => {
// FIXME(#32730) propagate obligations
assert!(obligations.is_empty());
Ok(InferOk { obligations, value: () }) => {
self.register_predicates(obligations);
},
Err(e) => {
self.report_mismatched_types(cause, expected, actual, e);
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_typeck/check/method/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,8 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {

fn unify_receivers(&mut self, self_ty: Ty<'tcx>, method_self_ty: Ty<'tcx>) {
match self.sub_types(false, &self.misc(self.span), self_ty, method_self_ty) {
Ok(InferOk { obligations, .. }) => {
// FIXME(#32730) propagate obligations
assert!(obligations.is_empty());
Ok(InferOk { obligations, value: () }) => {
self.register_predicates(obligations);
}
Err(_) => {
span_bug!(self.span,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ use super::suggest;
use check::FnCtxt;
use hir::def_id::DefId;
use hir::def::Def;
use rustc::infer::InferOk;
use rustc::ty::subst::{Subst, Substs};
use rustc::traits::{self, ObligationCause};
use rustc::ty::{self, Ty, ToPolyTraitRef, TraitRef, TypeFoldable};
use rustc::infer::InferOk;
use rustc::util::nodemap::FxHashSet;
use syntax::ast;
use syntax_pos::Span;
Expand Down Expand Up @@ -1035,7 +1035,7 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
&ObligationCause::dummy(),
self_ty,
probe.xform_self_ty) {
Ok(InferOk { obligations, .. }) => {
Ok(InferOk { obligations, value: () }) => {
// FIXME(#32730) propagate obligations
assert!(obligations.is_empty())
}
Expand Down
Loading

0 comments on commit 48dc6e2

Please sign in to comment.