Skip to content

Commit b706b9d

Browse files
pre-cleanups
1 parent 41aa06e commit b706b9d

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

compiler/rustc_middle/src/ty/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ impl MainDefinition {
233233
#[derive(Clone, Debug, TypeFoldable, TypeVisitable)]
234234
pub struct ImplHeader<'tcx> {
235235
pub impl_def_id: DefId,
236+
pub impl_args: ty::GenericArgsRef<'tcx>,
236237
pub self_ty: Ty<'tcx>,
237238
pub trait_ref: Option<TraitRef<'tcx>>,
238239
pub predicates: Vec<Predicate<'tcx>>,

compiler/rustc_trait_selection/src/traits/coherence.rs

+32-24
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,13 @@ pub fn overlapping_impls(
142142
Some(overlap)
143143
}
144144

145-
fn with_fresh_ty_vars<'cx, 'tcx>(
146-
selcx: &mut SelectionContext<'cx, 'tcx>,
147-
param_env: ty::ParamEnv<'tcx>,
148-
impl_def_id: DefId,
149-
) -> ty::ImplHeader<'tcx> {
150-
let tcx = selcx.tcx();
151-
let impl_args = selcx.infcx.fresh_args_for_item(DUMMY_SP, impl_def_id);
145+
fn fresh_impl_header<'tcx>(infcx: &InferCtxt<'tcx>, impl_def_id: DefId) -> ty::ImplHeader<'tcx> {
146+
let tcx = infcx.tcx;
147+
let impl_args = infcx.fresh_args_for_item(DUMMY_SP, impl_def_id);
152148

153-
let header = ty::ImplHeader {
149+
ty::ImplHeader {
154150
impl_def_id,
151+
impl_args,
155152
self_ty: tcx.type_of(impl_def_id).instantiate(tcx, impl_args),
156153
trait_ref: tcx.impl_trait_ref(impl_def_id).map(|i| i.instantiate(tcx, impl_args)),
157154
predicates: tcx
@@ -160,10 +157,18 @@ fn with_fresh_ty_vars<'cx, 'tcx>(
160157
.iter()
161158
.map(|(c, _)| c.as_predicate())
162159
.collect(),
163-
};
160+
}
161+
}
162+
163+
fn fresh_impl_header_normalized<'tcx>(
164+
infcx: &InferCtxt<'tcx>,
165+
param_env: ty::ParamEnv<'tcx>,
166+
impl_def_id: DefId,
167+
) -> ty::ImplHeader<'tcx> {
168+
let header = fresh_impl_header(infcx, impl_def_id);
164169

165170
let InferOk { value: mut header, obligations } =
166-
selcx.infcx.at(&ObligationCause::dummy(), param_env).normalize(header);
171+
infcx.at(&ObligationCause::dummy(), param_env).normalize(header);
167172

168173
header.predicates.extend(obligations.into_iter().map(|o| o.predicate));
169174
header
@@ -206,12 +211,13 @@ fn overlap<'tcx>(
206211
// empty environment.
207212
let param_env = ty::ParamEnv::empty();
208213

209-
let impl1_header = with_fresh_ty_vars(selcx, param_env, impl1_def_id);
210-
let impl2_header = with_fresh_ty_vars(selcx, param_env, impl2_def_id);
214+
let impl1_header = fresh_impl_header_normalized(selcx.infcx, param_env, impl1_def_id);
215+
let impl2_header = fresh_impl_header_normalized(selcx.infcx, param_env, impl2_def_id);
211216

212217
// Equate the headers to find their intersection (the general type, with infer vars,
213218
// that may apply both impls).
214-
let mut obligations = equate_impl_headers(selcx.infcx, &impl1_header, &impl2_header)?;
219+
let mut obligations =
220+
equate_impl_headers(selcx.infcx, param_env, &impl1_header, &impl2_header)?;
215221
debug!("overlap: unification check succeeded");
216222

217223
obligations.extend(
@@ -312,20 +318,22 @@ fn overlap<'tcx>(
312318
#[instrument(level = "debug", skip(infcx), ret)]
313319
fn equate_impl_headers<'tcx>(
314320
infcx: &InferCtxt<'tcx>,
321+
param_env: ty::ParamEnv<'tcx>,
315322
impl1: &ty::ImplHeader<'tcx>,
316323
impl2: &ty::ImplHeader<'tcx>,
317324
) -> Option<PredicateObligations<'tcx>> {
318-
let result = match (impl1.trait_ref, impl2.trait_ref) {
319-
(Some(impl1_ref), Some(impl2_ref)) => infcx
320-
.at(&ObligationCause::dummy(), ty::ParamEnv::empty())
321-
.eq(DefineOpaqueTypes::Yes, impl1_ref, impl2_ref),
322-
(None, None) => infcx.at(&ObligationCause::dummy(), ty::ParamEnv::empty()).eq(
323-
DefineOpaqueTypes::Yes,
324-
impl1.self_ty,
325-
impl2.self_ty,
326-
),
327-
_ => bug!("mk_eq_impl_headers given mismatched impl kinds"),
328-
};
325+
let result =
326+
match (impl1.trait_ref, impl2.trait_ref) {
327+
(Some(impl1_ref), Some(impl2_ref)) => infcx
328+
.at(&ObligationCause::dummy(), param_env)
329+
.eq(DefineOpaqueTypes::Yes, impl1_ref, impl2_ref),
330+
(None, None) => infcx.at(&ObligationCause::dummy(), param_env).eq(
331+
DefineOpaqueTypes::Yes,
332+
impl1.self_ty,
333+
impl2.self_ty,
334+
),
335+
_ => bug!("mk_eq_impl_headers given mismatched impl kinds"),
336+
};
329337

330338
result.map(|infer_ok| infer_ok.obligations).ok()
331339
}

0 commit comments

Comments
 (0)