Skip to content

Commit 1fe18a5

Browse files
authoredNov 26, 2022
Rollup merge of #104906 - spastorino:remove-ascribeusertypecx, r=compiler-errors
Remove AscribeUserTypeCx r? ``@compiler-errors`` This basically inlines `AscribeUserTypeCx::relate_mir_and_user_ty` into `type_op_ascribe_user_type_with_span` which is the only place where it's used and makes direct use of `ObligationCtxt` API.
2 parents 8586544 + 3c9b30e commit 1fe18a5

File tree

1 file changed

+50
-107
lines changed

1 file changed

+50
-107
lines changed
 

Diff for: ‎compiler/rustc_traits/src/type_op.rs

+50-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,67 @@ 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-
}
58+
let span = span.unwrap_or(DUMMY_SP);
6459

65-
struct AscribeUserTypeCx<'me, 'tcx> {
66-
ocx: &'me ObligationCtxt<'me, 'tcx>,
67-
param_env: ty::ParamEnv<'tcx>,
68-
span: Span,
69-
}
60+
let UserSubsts { user_self_ty, substs } = user_substs;
61+
let tcx = ocx.infcx.tcx;
62+
let cause = ObligationCause::dummy_with_span(span);
7063

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-
}
64+
let ty = tcx.bound_type_of(def_id).subst(tcx, substs);
65+
let ty = ocx.normalize(cause.clone(), param_env, ty);
66+
debug!("relate_type_and_user_type: ty of def-id is {:?}", ty);
7867

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-
}
68+
ocx.eq(&cause, param_env, mir_ty, ty)?;
8569

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-
}
70+
// Prove the predicates coming along with `def_id`.
71+
//
72+
// Also, normalize the `instantiated_predicates`
73+
// because otherwise we wind up with duplicate "type
74+
// outlives" error messages.
75+
let instantiated_predicates = tcx.predicates_of(def_id).instantiate(tcx, substs);
9276

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-
}
77+
debug!(?instantiated_predicates);
78+
for (instantiated_predicate, predicate_span) in
79+
zip(instantiated_predicates.predicates, instantiated_predicates.spans)
80+
{
81+
let span = if span == DUMMY_SP { predicate_span } else { span };
82+
let cause = ObligationCause::new(
83+
span,
84+
hir::CRATE_HIR_ID,
85+
ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span),
86+
);
87+
let instantiated_predicate =
88+
ocx.normalize(cause.clone(), param_env, instantiated_predicate);
10189

102-
fn tcx(&self) -> TyCtxt<'tcx> {
103-
self.ocx.infcx.tcx
90+
ocx.register_obligation(Obligation::new(tcx, cause, param_env, instantiated_predicate));
10491
}
10592

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-
}
93+
if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty {
94+
let impl_self_ty = tcx.bound_type_of(impl_def_id).subst(tcx, substs);
95+
let impl_self_ty = ocx.normalize(cause.clone(), param_env, impl_self_ty);
14596

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);
97+
ocx.eq(&cause, param_env, self_ty, impl_self_ty)?;
14998

150-
self.eq(self_ty, impl_self_ty)?;
151-
152-
self.prove_predicate(
153-
ty::Binder::dummy(ty::PredicateKind::WellFormed(impl_self_ty.into()))
154-
.to_predicate(tcx),
155-
cause.clone(),
156-
);
157-
}
158-
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(())
99+
let predicate: Predicate<'tcx> =
100+
ty::Binder::dummy(ty::PredicateKind::WellFormed(impl_self_ty.into())).to_predicate(tcx);
101+
ocx.register_obligation(Obligation::new(tcx, cause.clone(), param_env, predicate));
175102
}
103+
104+
// In addition to proving the predicates, we have to
105+
// prove that `ty` is well-formed -- this is because
106+
// the WF of `ty` is predicated on the substs being
107+
// well-formed, and we haven't proven *that*. We don't
108+
// want to prove the WF of types from `substs` directly because they
109+
// haven't been normalized.
110+
//
111+
// FIXME(nmatsakis): Well, perhaps we should normalize
112+
// them? This would only be relevant if some input
113+
// type were ill-formed but did not appear in `ty`,
114+
// which...could happen with normalization...
115+
let predicate: Predicate<'tcx> =
116+
ty::Binder::dummy(ty::PredicateKind::WellFormed(ty.into())).to_predicate(tcx);
117+
ocx.register_obligation(Obligation::new(tcx, cause, param_env, predicate));
118+
Ok(())
176119
}
177120

178121
fn type_op_eq<'tcx>(

0 commit comments

Comments
 (0)