Skip to content

Commit 569ca2b

Browse files
committed
Deduplicate fresh_item_substs
1 parent 6eb6455 commit 569ca2b

File tree

2 files changed

+35
-60
lines changed
  • compiler
    • rustc_hir_analysis/src/astconv
    • rustc_hir_typeck/src/method

2 files changed

+35
-60
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

+34-31
Original file line numberDiff line numberDiff line change
@@ -2239,7 +2239,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
22392239
let ocx = ObligationCtxt::new_in_snapshot(&infcx);
22402240

22412241
let impl_ty = tcx.type_of(impl_);
2242-
let impl_substs = self.fresh_item_substs(impl_, &infcx);
2242+
let impl_substs = infcx.fresh_item_substs(impl_);
22432243
let impl_ty = impl_ty.subst(tcx, impl_substs);
22442244
let impl_ty = ocx.normalize(&cause, param_env, impl_ty);
22452245

@@ -2306,36 +2306,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
23062306
))
23072307
}
23082308

2309-
// FIXME(fmease): Copied from `rustc_hir_typeck::method::probe`. Deduplicate.
2310-
fn fresh_item_substs(&self, def_id: DefId, infcx: &InferCtxt<'tcx>) -> SubstsRef<'tcx> {
2311-
let tcx = self.tcx();
2312-
2313-
InternalSubsts::for_item(tcx, def_id, |param, _| match param.kind {
2314-
GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(),
2315-
GenericParamDefKind::Type { .. } => infcx
2316-
.next_ty_var(TypeVariableOrigin {
2317-
kind: TypeVariableOriginKind::SubstitutionPlaceholder,
2318-
span: tcx.def_span(def_id),
2319-
})
2320-
.into(),
2321-
GenericParamDefKind::Const { .. } => {
2322-
let span = tcx.def_span(def_id);
2323-
let origin = ConstVariableOrigin {
2324-
kind: ConstVariableOriginKind::SubstitutionPlaceholder,
2325-
span,
2326-
};
2327-
infcx
2328-
.next_const_var(
2329-
tcx.type_of(param.def_id)
2330-
.no_bound_vars()
2331-
.expect("const parameter types cannot be generic"),
2332-
origin,
2333-
)
2334-
.into()
2335-
}
2336-
})
2337-
}
2338-
23392309
fn lookup_assoc_ty(
23402310
&self,
23412311
name: Ident,
@@ -3531,3 +3501,36 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
35313501
}
35323502
}
35333503
}
3504+
3505+
pub trait InferCtxtExt<'tcx> {
3506+
fn fresh_item_substs(&self, def_id: DefId) -> SubstsRef<'tcx>;
3507+
}
3508+
3509+
impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
3510+
fn fresh_item_substs(&self, def_id: DefId) -> SubstsRef<'tcx> {
3511+
InternalSubsts::for_item(self.tcx, def_id, |param, _| match param.kind {
3512+
GenericParamDefKind::Lifetime => self.tcx.lifetimes.re_erased.into(),
3513+
GenericParamDefKind::Type { .. } => self
3514+
.next_ty_var(TypeVariableOrigin {
3515+
kind: TypeVariableOriginKind::SubstitutionPlaceholder,
3516+
span: self.tcx.def_span(def_id),
3517+
})
3518+
.into(),
3519+
GenericParamDefKind::Const { .. } => {
3520+
let span = self.tcx.def_span(def_id);
3521+
let origin = ConstVariableOrigin {
3522+
kind: ConstVariableOriginKind::SubstitutionPlaceholder,
3523+
span,
3524+
};
3525+
self.next_const_var(
3526+
self.tcx
3527+
.type_of(param.def_id)
3528+
.no_bound_vars()
3529+
.expect("const parameter types cannot be generic"),
3530+
origin,
3531+
)
3532+
.into()
3533+
}
3534+
})
3535+
}
3536+
}

compiler/rustc_hir_typeck/src/method/probe.rs

+1-29
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ use rustc_data_structures::fx::FxHashSet;
99
use rustc_errors::Applicability;
1010
use rustc_hir as hir;
1111
use rustc_hir::def::DefKind;
12+
use rustc_hir_analysis::astconv::InferCtxtExt as _;
1213
use rustc_hir_analysis::autoderef::{self, Autoderef};
1314
use rustc_infer::infer::canonical::OriginalQueryValues;
1415
use rustc_infer::infer::canonical::{Canonical, QueryResponse};
15-
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1616
use rustc_infer::infer::{self, InferOk, TyCtxtInferExt};
17-
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
1817
use rustc_middle::middle::stability;
1918
use rustc_middle::ty::fast_reject::{simplify_type, TreatParams};
2019
use rustc_middle::ty::AssocItem;
@@ -1941,33 +1940,6 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
19411940
(self.tcx.type_of(impl_def_id), self.fresh_item_substs(impl_def_id))
19421941
}
19431942

1944-
fn fresh_item_substs(&self, def_id: DefId) -> SubstsRef<'tcx> {
1945-
InternalSubsts::for_item(self.tcx, def_id, |param, _| match param.kind {
1946-
GenericParamDefKind::Lifetime => self.tcx.lifetimes.re_erased.into(),
1947-
GenericParamDefKind::Type { .. } => self
1948-
.next_ty_var(TypeVariableOrigin {
1949-
kind: TypeVariableOriginKind::SubstitutionPlaceholder,
1950-
span: self.tcx.def_span(def_id),
1951-
})
1952-
.into(),
1953-
GenericParamDefKind::Const { .. } => {
1954-
let span = self.tcx.def_span(def_id);
1955-
let origin = ConstVariableOrigin {
1956-
kind: ConstVariableOriginKind::SubstitutionPlaceholder,
1957-
span,
1958-
};
1959-
self.next_const_var(
1960-
self.tcx
1961-
.type_of(param.def_id)
1962-
.no_bound_vars()
1963-
.expect("const parameter types cannot be generic"),
1964-
origin,
1965-
)
1966-
.into()
1967-
}
1968-
})
1969-
}
1970-
19711943
/// Replaces late-bound-regions bound by `value` with `'static` using
19721944
/// `ty::erase_late_bound_regions`.
19731945
///

0 commit comments

Comments
 (0)