Skip to content

Commit ce67440

Browse files
committed
Replace PlaceholderReplacer
1 parent fd760f2 commit ce67440

4 files changed

Lines changed: 9 additions & 158 deletions

File tree

compiler/rustc_trait_selection/src/solve/normalize.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ where
201201
let result =
202202
ensure_sufficient_stack(|| self.normalize_alias_term(ty.into()))?.expect_type();
203203
Ok(PlaceholderReplacer::replace_placeholders(
204-
infcx,
204+
infcx.tcx,
205205
mapped_regions,
206206
mapped_types,
207207
mapped_consts,
@@ -229,7 +229,7 @@ where
229229
let result =
230230
ensure_sufficient_stack(|| self.normalize_alias_term(ct.into()))?.expect_const();
231231
Ok(PlaceholderReplacer::replace_placeholders(
232-
infcx,
232+
infcx.tcx,
233233
mapped_regions,
234234
mapped_types,
235235
mapped_consts,

compiler/rustc_trait_selection/src/traits/normalize.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> {
228228
.unwrap_or_else(|| proj.to_term(infcx.tcx));
229229

230230
PlaceholderReplacer::replace_placeholders(
231-
infcx,
231+
infcx.tcx,
232232
mapped_regions,
233233
mapped_types,
234234
mapped_consts,
@@ -275,7 +275,7 @@ impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> {
275275
);
276276

277277
PlaceholderReplacer::replace_placeholders(
278-
infcx,
278+
infcx.tcx,
279279
mapped_regions,
280280
mapped_types,
281281
mapped_consts,

compiler/rustc_trait_selection/src/traits/query/normalize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ impl<'a, 'tcx> QueryNormalizer<'a, 'tcx> {
363363
self.obligations.extend(obligations);
364364
let res = if let Some((mapped_regions, mapped_types, mapped_consts)) = maps {
365365
PlaceholderReplacer::replace_placeholders(
366-
infcx,
366+
infcx.tcx,
367367
mapped_regions,
368368
mapped_types,
369369
mapped_consts,

compiler/rustc_trait_selection/src/traits/util.rs

Lines changed: 4 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
use std::collections::VecDeque;
22

3-
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
3+
use rustc_data_structures::fx::FxHashSet;
44
use rustc_hir::LangItem;
55
use rustc_hir::def_id::DefId;
66
use rustc_infer::infer::InferCtxt;
77
use rustc_infer::traits::PolyTraitObligation;
88
pub use rustc_infer::traits::util::*;
9-
use rustc_middle::bug;
109
use rustc_middle::ty::fast_reject::DeepRejectCtxt;
1110
use rustc_middle::ty::{
1211
self, PolyTraitPredicate, PredicatePolarity, SizedTraitKind, TraitPredicate, TraitRef, Ty,
13-
TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
12+
TyCtxt, TypeFoldable, TypeVisitableExt,
1413
};
15-
pub use rustc_next_trait_solver::placeholder::BoundVarReplacer;
14+
pub use rustc_next_trait_solver::placeholder::{BoundVarReplacer, PlaceholderReplacer};
1615
use rustc_span::Span;
1716
use smallvec::{SmallVec, smallvec};
1817
use tracing::debug;
@@ -205,7 +204,7 @@ pub fn with_replaced_escaping_bound_vars<
205204
BoundVarReplacer::replace_bound_vars(infcx, universe_indices, value);
206205
let result = f(value);
207206
PlaceholderReplacer::replace_placeholders(
208-
infcx,
207+
infcx.tcx,
209208
mapped_regions,
210209
mapped_types,
211210
mapped_consts,
@@ -217,154 +216,6 @@ pub fn with_replaced_escaping_bound_vars<
217216
}
218217
}
219218

220-
/// The inverse of [`BoundVarReplacer`]: replaces placeholders with the bound vars from which they came.
221-
pub struct PlaceholderReplacer<'a, 'tcx> {
222-
infcx: &'a InferCtxt<'tcx>,
223-
mapped_regions: FxIndexMap<ty::PlaceholderRegion<'tcx>, ty::BoundRegion<'tcx>>,
224-
mapped_types: FxIndexMap<ty::PlaceholderType<'tcx>, ty::BoundTy<'tcx>>,
225-
mapped_consts: FxIndexMap<ty::PlaceholderConst<'tcx>, ty::BoundConst<'tcx>>,
226-
universe_indices: &'a [Option<ty::UniverseIndex>],
227-
current_index: ty::DebruijnIndex,
228-
}
229-
230-
impl<'a, 'tcx> PlaceholderReplacer<'a, 'tcx> {
231-
pub fn replace_placeholders<T: TypeFoldable<TyCtxt<'tcx>>>(
232-
infcx: &'a InferCtxt<'tcx>,
233-
mapped_regions: FxIndexMap<ty::PlaceholderRegion<'tcx>, ty::BoundRegion<'tcx>>,
234-
mapped_types: FxIndexMap<ty::PlaceholderType<'tcx>, ty::BoundTy<'tcx>>,
235-
mapped_consts: FxIndexMap<ty::PlaceholderConst<'tcx>, ty::BoundConst<'tcx>>,
236-
universe_indices: &'a [Option<ty::UniverseIndex>],
237-
value: T,
238-
) -> T {
239-
let mut replacer = PlaceholderReplacer {
240-
infcx,
241-
mapped_regions,
242-
mapped_types,
243-
mapped_consts,
244-
universe_indices,
245-
current_index: ty::INNERMOST,
246-
};
247-
value.fold_with(&mut replacer)
248-
}
249-
}
250-
251-
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for PlaceholderReplacer<'_, 'tcx> {
252-
fn cx(&self) -> TyCtxt<'tcx> {
253-
self.infcx.tcx
254-
}
255-
256-
fn fold_binder<T: TypeFoldable<TyCtxt<'tcx>>>(
257-
&mut self,
258-
t: ty::Binder<'tcx, T>,
259-
) -> ty::Binder<'tcx, T> {
260-
if !t.has_placeholders() && !t.has_infer() {
261-
return t;
262-
}
263-
self.current_index.shift_in(1);
264-
let t = t.super_fold_with(self);
265-
self.current_index.shift_out(1);
266-
t
267-
}
268-
269-
fn fold_region(&mut self, r0: ty::Region<'tcx>) -> ty::Region<'tcx> {
270-
let r1 = match r0.kind() {
271-
ty::ReVar(vid) => self
272-
.infcx
273-
.inner
274-
.borrow_mut()
275-
.unwrap_region_constraints()
276-
.opportunistic_resolve_var(self.infcx.tcx, vid),
277-
_ => r0,
278-
};
279-
280-
let r2 = match r1.kind() {
281-
ty::RePlaceholder(p) => {
282-
let replace_var = self.mapped_regions.get(&p);
283-
match replace_var {
284-
Some(replace_var) => {
285-
let index = self
286-
.universe_indices
287-
.iter()
288-
.position(|u| matches!(u, Some(pu) if *pu == p.universe))
289-
.unwrap_or_else(|| bug!("Unexpected placeholder universe."));
290-
let db = ty::DebruijnIndex::from_usize(
291-
self.universe_indices.len() - index + self.current_index.as_usize() - 1,
292-
);
293-
ty::Region::new_bound(self.cx(), db, *replace_var)
294-
}
295-
None => r1,
296-
}
297-
}
298-
_ => r1,
299-
};
300-
301-
debug!(?r0, ?r1, ?r2, "fold_region");
302-
303-
r2
304-
}
305-
306-
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
307-
let ty = self.infcx.shallow_resolve(ty);
308-
match *ty.kind() {
309-
ty::Placeholder(p) => {
310-
let replace_var = self.mapped_types.get(&p);
311-
match replace_var {
312-
Some(replace_var) => {
313-
let index = self
314-
.universe_indices
315-
.iter()
316-
.position(|u| matches!(u, Some(pu) if *pu == p.universe))
317-
.unwrap_or_else(|| bug!("Unexpected placeholder universe."));
318-
let db = ty::DebruijnIndex::from_usize(
319-
self.universe_indices.len() - index + self.current_index.as_usize() - 1,
320-
);
321-
Ty::new_bound(self.infcx.tcx, db, *replace_var)
322-
}
323-
None => {
324-
if ty.has_infer() {
325-
ty.super_fold_with(self)
326-
} else {
327-
ty
328-
}
329-
}
330-
}
331-
}
332-
333-
_ if ty.has_placeholders() || ty.has_infer() => ty.super_fold_with(self),
334-
_ => ty,
335-
}
336-
}
337-
338-
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
339-
let ct = self.infcx.shallow_resolve_const(ct);
340-
if let ty::ConstKind::Placeholder(p) = ct.kind() {
341-
let replace_var = self.mapped_consts.get(&p);
342-
match replace_var {
343-
Some(replace_var) => {
344-
let index = self
345-
.universe_indices
346-
.iter()
347-
.position(|u| matches!(u, Some(pu) if *pu == p.universe))
348-
.unwrap_or_else(|| bug!("Unexpected placeholder universe."));
349-
let db = ty::DebruijnIndex::from_usize(
350-
self.universe_indices.len() - index + self.current_index.as_usize() - 1,
351-
);
352-
ty::Const::new_bound(self.infcx.tcx, db, *replace_var)
353-
}
354-
None => {
355-
if ct.has_infer() {
356-
ct.super_fold_with(self)
357-
} else {
358-
ct
359-
}
360-
}
361-
}
362-
} else {
363-
ct.super_fold_with(self)
364-
}
365-
}
366-
}
367-
368219
pub fn sizedness_fast_path<'tcx>(
369220
tcx: TyCtxt<'tcx>,
370221
predicate: ty::Predicate<'tcx>,

0 commit comments

Comments
 (0)