Skip to content

Commit aaa1db6

Browse files
committed
Remove AscribeUserTypeCx
1 parent e704e95 commit aaa1db6

File tree

1 file changed

+52
-107
lines changed

1 file changed

+52
-107
lines changed

compiler/rustc_traits/src/type_op.rs

+52-107
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use rustc_hir as hir;
2-
use rustc_hir::def_id::DefId;
3-
use rustc_infer::infer::at::ToTrace;
42
use rustc_infer::infer::canonical::{Canonical, QueryResponse};
53
use rustc_infer::infer::{DefiningAnchor, TyCtxtInferExt};
64
use rustc_infer::traits::ObligationCauseCode;
@@ -57,122 +55,69 @@ pub fn type_op_ascribe_user_type_with_span<'tcx>(
5755
"type_op_ascribe_user_type: mir_ty={:?} def_id={:?} user_substs={:?}",
5856
mir_ty, def_id, user_substs
5957
);
60-
let cx = AscribeUserTypeCx { ocx, param_env, span: span.unwrap_or(DUMMY_SP) };
61-
cx.relate_mir_and_user_ty(mir_ty, def_id, user_substs)?;
62-
Ok(())
63-
}
64-
65-
struct AscribeUserTypeCx<'me, 'tcx> {
66-
ocx: &'me ObligationCtxt<'me, 'tcx>,
67-
param_env: ty::ParamEnv<'tcx>,
68-
span: Span,
69-
}
58+
let span = span.unwrap_or(DUMMY_SP);
7059

71-
impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> {
72-
fn normalize<T>(&self, value: T) -> T
73-
where
74-
T: TypeFoldable<'tcx>,
75-
{
76-
self.normalize_with_cause(value, ObligationCause::misc(self.span, hir::CRATE_HIR_ID))
77-
}
60+
let UserSubsts { user_self_ty, substs } = user_substs;
61+
let tcx = ocx.infcx.tcx;
7862

79-
fn normalize_with_cause<T>(&self, value: T, cause: ObligationCause<'tcx>) -> T
80-
where
81-
T: TypeFoldable<'tcx>,
82-
{
83-
self.ocx.normalize(cause, self.param_env, value)
84-
}
63+
let ty = tcx.bound_type_of(def_id).subst(tcx, substs);
64+
let ty = ocx.normalize(ObligationCause::misc(span, hir::CRATE_HIR_ID), param_env, ty);
65+
debug!("relate_type_and_user_type: ty of def-id is {:?}", ty);
8566

86-
fn eq<T>(&self, a: T, b: T) -> Result<(), NoSolution>
87-
where
88-
T: ToTrace<'tcx>,
89-
{
90-
Ok(self.ocx.eq(&ObligationCause::dummy_with_span(self.span), self.param_env, a, b)?)
91-
}
67+
ocx.eq(&ObligationCause::dummy_with_span(span), param_env, mir_ty, ty)?;
9268

93-
fn prove_predicate(&self, predicate: Predicate<'tcx>, cause: ObligationCause<'tcx>) {
94-
self.ocx.register_obligation(Obligation::new(
95-
self.ocx.infcx.tcx,
96-
cause,
97-
self.param_env,
98-
predicate,
99-
));
100-
}
69+
// Prove the predicates coming along with `def_id`.
70+
//
71+
// Also, normalize the `instantiated_predicates`
72+
// because otherwise we wind up with duplicate "type
73+
// outlives" error messages.
74+
let instantiated_predicates = tcx.predicates_of(def_id).instantiate(tcx, substs);
10175

102-
fn tcx(&self) -> TyCtxt<'tcx> {
103-
self.ocx.infcx.tcx
104-
}
76+
let cause = ObligationCause::dummy_with_span(span);
10577

106-
#[instrument(level = "debug", skip(self))]
107-
fn relate_mir_and_user_ty(
108-
&self,
109-
mir_ty: Ty<'tcx>,
110-
def_id: DefId,
111-
user_substs: UserSubsts<'tcx>,
112-
) -> Result<(), NoSolution> {
113-
let UserSubsts { user_self_ty, substs } = user_substs;
114-
let tcx = self.tcx();
115-
116-
let ty = tcx.bound_type_of(def_id).subst(tcx, substs);
117-
let ty = self.normalize(ty);
118-
debug!("relate_type_and_user_type: ty of def-id is {:?}", ty);
119-
120-
self.eq(mir_ty, ty)?;
121-
122-
// Prove the predicates coming along with `def_id`.
123-
//
124-
// Also, normalize the `instantiated_predicates`
125-
// because otherwise we wind up with duplicate "type
126-
// outlives" error messages.
127-
let instantiated_predicates = tcx.predicates_of(def_id).instantiate(tcx, substs);
128-
129-
let cause = ObligationCause::dummy_with_span(self.span);
130-
131-
debug!(?instantiated_predicates);
132-
for (instantiated_predicate, predicate_span) in
133-
zip(instantiated_predicates.predicates, instantiated_predicates.spans)
134-
{
135-
let span = if self.span == DUMMY_SP { predicate_span } else { self.span };
136-
let cause = ObligationCause::new(
137-
span,
138-
hir::CRATE_HIR_ID,
139-
ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span),
140-
);
141-
let instantiated_predicate =
142-
self.normalize_with_cause(instantiated_predicate, cause.clone());
143-
self.prove_predicate(instantiated_predicate, cause);
144-
}
78+
debug!(?instantiated_predicates);
79+
for (instantiated_predicate, predicate_span) in
80+
zip(instantiated_predicates.predicates, instantiated_predicates.spans)
81+
{
82+
let span = if span == DUMMY_SP { predicate_span } else { span };
83+
let cause = ObligationCause::new(
84+
span,
85+
hir::CRATE_HIR_ID,
86+
ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span),
87+
);
88+
let instantiated_predicate =
89+
ocx.normalize(cause.clone(), param_env, instantiated_predicate);
14590

146-
if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty {
147-
let impl_self_ty = tcx.bound_type_of(impl_def_id).subst(tcx, substs);
148-
let impl_self_ty = self.normalize(impl_self_ty);
91+
ocx.register_obligation(Obligation::new(tcx, cause, param_env, instantiated_predicate));
92+
}
14993

150-
self.eq(self_ty, impl_self_ty)?;
94+
if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty {
95+
let impl_self_ty = tcx.bound_type_of(impl_def_id).subst(tcx, substs);
96+
let impl_self_ty =
97+
ocx.normalize(ObligationCause::misc(span, hir::CRATE_HIR_ID), param_env, impl_self_ty);
15198

152-
self.prove_predicate(
153-
ty::Binder::dummy(ty::PredicateKind::WellFormed(impl_self_ty.into()))
154-
.to_predicate(tcx),
155-
cause.clone(),
156-
);
157-
}
99+
ocx.eq(&ObligationCause::dummy_with_span(span), param_env, self_ty, impl_self_ty)?;
158100

159-
// In addition to proving the predicates, we have to
160-
// prove that `ty` is well-formed -- this is because
161-
// the WF of `ty` is predicated on the substs being
162-
// well-formed, and we haven't proven *that*. We don't
163-
// want to prove the WF of types from `substs` directly because they
164-
// haven't been normalized.
165-
//
166-
// FIXME(nmatsakis): Well, perhaps we should normalize
167-
// them? This would only be relevant if some input
168-
// type were ill-formed but did not appear in `ty`,
169-
// which...could happen with normalization...
170-
self.prove_predicate(
171-
ty::Binder::dummy(ty::PredicateKind::WellFormed(ty.into())).to_predicate(tcx),
172-
cause,
173-
);
174-
Ok(())
101+
let predicate: Predicate<'tcx> =
102+
ty::Binder::dummy(ty::PredicateKind::WellFormed(impl_self_ty.into())).to_predicate(tcx);
103+
ocx.register_obligation(Obligation::new(tcx, cause.clone(), param_env, predicate));
175104
}
105+
106+
// In addition to proving the predicates, we have to
107+
// prove that `ty` is well-formed -- this is because
108+
// the WF of `ty` is predicated on the substs being
109+
// well-formed, and we haven't proven *that*. We don't
110+
// want to prove the WF of types from `substs` directly because they
111+
// haven't been normalized.
112+
//
113+
// FIXME(nmatsakis): Well, perhaps we should normalize
114+
// them? This would only be relevant if some input
115+
// type were ill-formed but did not appear in `ty`,
116+
// which...could happen with normalization...
117+
let predicate: Predicate<'tcx> =
118+
ty::Binder::dummy(ty::PredicateKind::WellFormed(ty.into())).to_predicate(tcx);
119+
ocx.register_obligation(Obligation::new(tcx, cause, param_env, predicate));
120+
Ok(())
176121
}
177122

178123
fn type_op_eq<'tcx>(

0 commit comments

Comments
 (0)