Skip to content

Commit 516570f

Browse files
committed
rustc_typeck: move some obligation methods to Inherited.
1 parent 2ad1964 commit 516570f

File tree

3 files changed

+68
-113
lines changed

3 files changed

+68
-113
lines changed

src/librustc_typeck/check/assoc.rs

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/librustc_typeck/check/compare_method.rs

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use rustc::util::common::ErrorReported;
2020
use syntax::ast;
2121
use syntax_pos::Span;
2222

23-
use super::assoc;
2423
use super::{Inherited, FnCtxt};
2524
use astconv::ExplicitSelf;
2625

@@ -227,7 +226,6 @@ fn compare_predicate_entailment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
227226
tcx.infer_ctxt(trait_param_env, Reveal::UserFacing).enter(|infcx| {
228227
let inh = Inherited::new(infcx);
229228
let infcx = &inh.infcx;
230-
let fulfillment_cx = &inh.fulfillment_cx;
231229

232230
debug!("compare_impl_method: caller_bounds={:?}",
233231
infcx.parameter_environment.caller_bounds);
@@ -239,12 +237,11 @@ fn compare_predicate_entailment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
239237
infer::HigherRankedType,
240238
&ty::Binder(impl_m_own_bounds.predicates));
241239
for predicate in impl_m_own_bounds {
242-
let traits::Normalized { value: predicate, .. } =
240+
let traits::Normalized { value: predicate, obligations } =
243241
traits::normalize(&mut selcx, normalize_cause.clone(), &predicate);
244242

245-
fulfillment_cx.borrow_mut().register_predicate_obligation(
246-
&infcx,
247-
traits::Obligation::new(cause.clone(), predicate));
243+
inh.register_predicates(obligations);
244+
inh.register_predicate(traits::Obligation::new(cause.clone(), predicate));
248245
}
249246

250247
// We now need to check that the signature of the impl method is
@@ -277,11 +274,9 @@ fn compare_predicate_entailment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
277274
let impl_sig =
278275
impl_sig.subst(tcx, impl_to_skol_substs);
279276
let impl_sig =
280-
assoc::normalize_associated_types_in(&infcx,
281-
&mut fulfillment_cx.borrow_mut(),
282-
impl_m_span,
283-
impl_m_body_id,
284-
&impl_sig);
277+
inh.normalize_associated_types_in(impl_m_span,
278+
impl_m_body_id,
279+
&impl_sig);
285280
let impl_fty = tcx.mk_fn_ptr(ty::Binder(impl_sig));
286281
debug!("compare_impl_method: impl_fty={:?}", impl_fty);
287282

@@ -291,11 +286,9 @@ fn compare_predicate_entailment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
291286
let trait_sig =
292287
trait_sig.subst(tcx, trait_to_skol_substs);
293288
let trait_sig =
294-
assoc::normalize_associated_types_in(&infcx,
295-
&mut fulfillment_cx.borrow_mut(),
296-
impl_m_span,
297-
impl_m_body_id,
298-
&trait_sig);
289+
inh.normalize_associated_types_in(impl_m_span,
290+
impl_m_body_id,
291+
&trait_sig);
299292
let trait_fty = tcx.mk_fn_ptr(ty::Binder(trait_sig));
300293

301294
debug!("compare_impl_method: trait_fty={:?}", trait_fty);
@@ -344,7 +337,7 @@ fn compare_predicate_entailment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
344337

345338
// Check that all obligations are satisfied by the implementation's
346339
// version.
347-
if let Err(ref errors) = fulfillment_cx.borrow_mut().select_all_or_error(&infcx) {
340+
if let Err(ref errors) = inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx) {
348341
infcx.report_fulfillment_errors(errors);
349342
return Err(ErrorReported);
350343
}
@@ -731,7 +724,8 @@ pub fn compare_const_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
731724
debug!("compare_const_impl(impl_trait_ref={:?})", impl_trait_ref);
732725

733726
tcx.infer_ctxt((), Reveal::UserFacing).enter(|infcx| {
734-
let mut fulfillment_cx = traits::FulfillmentContext::new();
727+
let inh = Inherited::new(infcx);
728+
let infcx = &inh.infcx;
735729

736730
// The below is for the most part highly similar to the procedure
737731
// for methods above. It is simpler in many respects, especially
@@ -761,31 +755,21 @@ pub fn compare_const_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
761755
let trait_ty = tcx.item_type(trait_c.def_id).subst(tcx, trait_to_skol_substs);
762756
let mut cause = ObligationCause::misc(impl_c_span, impl_c_node_id);
763757

764-
let err = infcx.commit_if_ok(|_| {
765-
// There is no "body" here, so just pass dummy id.
766-
let impl_ty = assoc::normalize_associated_types_in(&infcx,
767-
&mut fulfillment_cx,
768-
impl_c_span,
769-
ast::CRATE_NODE_ID,
770-
&impl_ty);
758+
// There is no "body" here, so just pass dummy id.
759+
let impl_ty = inh.normalize_associated_types_in(impl_c_span,
760+
impl_c_node_id,
761+
&impl_ty);
771762

772-
debug!("compare_const_impl: impl_ty={:?}", impl_ty);
763+
debug!("compare_const_impl: impl_ty={:?}", impl_ty);
773764

774-
let trait_ty = assoc::normalize_associated_types_in(&infcx,
775-
&mut fulfillment_cx,
776-
impl_c_span,
777-
ast::CRATE_NODE_ID,
778-
&trait_ty);
765+
let trait_ty = inh.normalize_associated_types_in(impl_c_span,
766+
impl_c_node_id,
767+
&trait_ty);
779768

780-
debug!("compare_const_impl: trait_ty={:?}", trait_ty);
769+
debug!("compare_const_impl: trait_ty={:?}", trait_ty);
781770

782-
infcx.sub_types(false, &cause, impl_ty, trait_ty)
783-
.map(|InferOk { obligations, value: () }| {
784-
for obligation in obligations {
785-
fulfillment_cx.register_predicate_obligation(&infcx, obligation);
786-
}
787-
})
788-
});
771+
let err = infcx.sub_types(false, &cause, impl_ty, trait_ty)
772+
.map(|ok| inh.register_infer_ok_obligations(ok));
789773

790774
if let Err(terr) = err {
791775
debug!("checking associated const for compatibility: impl ty {:?}, trait ty {:?}",
@@ -822,5 +806,7 @@ pub fn compare_const_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
822806
&terr);
823807
diag.emit();
824808
}
809+
810+
// FIXME(#41323) Check the obligations in the fulfillment context.
825811
});
826812
}

src/librustc_typeck/check/mod.rs

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ use rustc_back::slice;
129129
use rustc_const_eval::eval_length;
130130
use rustc_const_math::ConstInt;
131131

132-
mod assoc;
133132
mod autoderef;
134133
pub mod dropck;
135134
pub mod _match;
@@ -537,7 +536,7 @@ impl<'a, 'gcx, 'tcx> InheritedBuilder<'a, 'gcx, 'tcx> {
537536
}
538537

539538
impl<'a, 'gcx, 'tcx> Inherited<'a, 'gcx, 'tcx> {
540-
pub fn new(infcx: InferCtxt<'a, 'gcx, 'tcx>) -> Self {
539+
fn new(infcx: InferCtxt<'a, 'gcx, 'tcx>) -> Self {
541540
Inherited {
542541
infcx: infcx,
543542
fulfillment_cx: RefCell::new(traits::FulfillmentContext::new()),
@@ -548,20 +547,55 @@ impl<'a, 'gcx, 'tcx> Inherited<'a, 'gcx, 'tcx> {
548547
}
549548
}
550549

550+
fn register_predicate(&self, obligation: traits::PredicateObligation<'tcx>) {
551+
debug!("register_predicate({:?})", obligation);
552+
if obligation.has_escaping_regions() {
553+
span_bug!(obligation.cause.span, "escaping regions in predicate {:?}",
554+
obligation);
555+
}
556+
self.fulfillment_cx
557+
.borrow_mut()
558+
.register_predicate_obligation(self, obligation);
559+
}
560+
561+
fn register_predicates(&self, obligations: Vec<traits::PredicateObligation<'tcx>>) {
562+
for obligation in obligations {
563+
self.register_predicate(obligation);
564+
}
565+
}
566+
567+
fn register_infer_ok_obligations<T>(&self, infer_ok: InferOk<'tcx, T>) -> T {
568+
self.register_predicates(infer_ok.obligations);
569+
infer_ok.value
570+
}
571+
551572
fn normalize_associated_types_in<T>(&self,
552573
span: Span,
553574
body_id: ast::NodeId,
554-
value: &T)
555-
-> T
575+
value: &T) -> T
556576
where T : TypeFoldable<'tcx>
557577
{
558-
assoc::normalize_associated_types_in(self,
559-
&mut self.fulfillment_cx.borrow_mut(),
560-
span,
561-
body_id,
562-
value)
578+
let ok = self.normalize_associated_types_in_as_infer_ok(span, body_id, value);
579+
self.register_infer_ok_obligations(ok)
563580
}
564581

582+
fn normalize_associated_types_in_as_infer_ok<T>(&self,
583+
span: Span,
584+
body_id: ast::NodeId,
585+
value: &T)
586+
-> InferOk<'tcx, T>
587+
where T : TypeFoldable<'tcx>
588+
{
589+
debug!("normalize_associated_types_in(value={:?})", value);
590+
let mut selcx = traits::SelectionContext::new(self);
591+
let cause = ObligationCause::misc(span, body_id);
592+
let traits::Normalized { value, obligations } =
593+
traits::normalize(&mut selcx, cause, value);
594+
debug!("normalize_associated_types_in: result={:?} predicates={:?}",
595+
value,
596+
obligations);
597+
InferOk { value, obligations }
598+
}
565599
}
566600

567601
struct CheckItemTypesVisitor<'a, 'tcx: 'a> { tcx: TyCtxt<'a, 'tcx, 'tcx> }
@@ -1806,32 +1840,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
18061840
.register_bound(self, ty, def_id, cause);
18071841
}
18081842

1809-
pub fn register_predicate(&self,
1810-
obligation: traits::PredicateObligation<'tcx>)
1811-
{
1812-
debug!("register_predicate({:?})", obligation);
1813-
if obligation.has_escaping_regions() {
1814-
span_bug!(obligation.cause.span, "escaping regions in predicate {:?}",
1815-
obligation);
1816-
}
1817-
self.fulfillment_cx
1818-
.borrow_mut()
1819-
.register_predicate_obligation(self, obligation);
1820-
}
1821-
1822-
pub fn register_predicates(&self,
1823-
obligations: Vec<traits::PredicateObligation<'tcx>>)
1824-
{
1825-
for obligation in obligations {
1826-
self.register_predicate(obligation);
1827-
}
1828-
}
1829-
1830-
pub fn register_infer_ok_obligations<T>(&self, infer_ok: InferOk<'tcx, T>) -> T {
1831-
self.register_predicates(infer_ok.obligations);
1832-
infer_ok.value
1833-
}
1834-
18351843
pub fn to_ty(&self, ast_t: &hir::Ty) -> Ty<'tcx> {
18361844
let t = AstConv::ast_ty_to_ty(self, ast_t);
18371845
self.register_wf_obligation(t, ast_t.span, traits::MiscObligation);

0 commit comments

Comments
 (0)