1 //! Contains the logic to lower rustc types into Chalk types
3 //! In many cases there is a 1:1 relationship between a rustc type and a Chalk type.
4 //! For example, a `SubstsRef` maps almost directly to a `Substitution`. In some
5 //! other cases, such as `Param`s, there is no Chalk type, so we have to handle
9 //! Much of the `Ty` lowering is 1:1 with Chalk. (Or will be eventually). A
10 //! helpful table for what types lower to what can be found in the
11 //! [Chalk book](https://rust-lang.github.io/chalk/book/types/rust_types.html).
12 //! The most notable difference lies with `Param`s. To convert from rustc to
13 //! Chalk, we eagerly and deeply convert `Param`s to placeholders (in goals) or
14 //! bound variables (for clause generation through functions in `db`).
16 //! ## `Region` lowering
17 //! Regions are handled in rustc and Chalk is quite differently. In rustc, there
18 //! is a difference between "early bound" and "late bound" regions, where only
19 //! the late bound regions have a `DebruijnIndex`. Moreover, in Chalk all
20 //! regions (Lifetimes) have an associated index. In rustc, only `BrAnon`s have
21 //! an index, whereas `BrNamed` don't. In order to lower regions to Chalk, we
22 //! convert all regions into `BrAnon` late-bound regions.
24 //! ## `Const` lowering
25 //! Chalk doesn't handle consts currently, so consts are currently lowered to
28 //! ## Bound variable collection
29 //! Another difference between rustc and Chalk lies in the handling of binders.
30 //! Chalk requires that we store the bound parameter kinds, whereas rustc does
31 //! not. To lower anything wrapped in a `Binder`, we first deeply find any bound
32 //! variables from the current `Binder`.
35 use rustc_middle::traits::{ChalkEnvironmentAndGoal, ChalkRustInterner as RustInterner};
36 use rustc_middle::ty::subst::{GenericArg, GenericArgKind, SubstsRef};
37 use rustc_middle::ty::{
38 self, Binder, Region, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
39 TypeSuperVisitable, TypeVisitable, TypeVisitor,
41 use rustc_span::def_id::DefId;
43 use chalk_ir::{FnSig, ForeignDefId};
44 use rustc_hir::Unsafety;
45 use std::collections::btree_map::{BTreeMap, Entry};
46 use std::ops::ControlFlow;
48 /// Essentially an `Into` with a `&RustInterner` parameter
49 pub(crate) trait LowerInto<'tcx, T> {
50 /// Lower a rustc construct (e.g., `ty::TraitPredicate`) to a chalk type, consuming `self`.
51 fn lower_into(self, interner: RustInterner<'tcx>) -> T;
54 impl<'tcx> LowerInto<'tcx, chalk_ir::Substitution<RustInterner<'tcx>>> for SubstsRef<'tcx> {
57 interner: RustInterner<'tcx>,
58 ) -> chalk_ir::Substitution<RustInterner<'tcx>> {
59 chalk_ir::Substitution::from_iter(interner, self.iter().map(|s| s.lower_into(interner)))
63 impl<'tcx> LowerInto<'tcx, SubstsRef<'tcx>> for &chalk_ir::Substitution<RustInterner<'tcx>> {
64 fn lower_into(self, interner: RustInterner<'tcx>) -> SubstsRef<'tcx> {
65 interner.tcx.mk_substs(self.iter(interner).map(|subst| subst.lower_into(interner)))
69 impl<'tcx> LowerInto<'tcx, chalk_ir::AliasTy<RustInterner<'tcx>>> for ty::ProjectionTy<'tcx> {
70 fn lower_into(self, interner: RustInterner<'tcx>) -> chalk_ir::AliasTy<RustInterner<'tcx>> {
71 chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy {
72 associated_ty_id: chalk_ir::AssocTypeId(self.item_def_id),
73 substitution: self.substs.lower_into(interner),
78 impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment<chalk_ir::Goal<RustInterner<'tcx>>>>
79 for ChalkEnvironmentAndGoal<'tcx>
83 interner: RustInterner<'tcx>,
84 ) -> chalk_ir::InEnvironment<chalk_ir::Goal<RustInterner<'tcx>>> {
85 let clauses = self.environment.into_iter().map(|predicate| {
86 let (predicate, binders, _named_regions) =
87 collect_bound_vars(interner, interner.tcx, predicate.kind());
88 let consequence = match predicate {
89 ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
90 chalk_ir::DomainGoal::FromEnv(chalk_ir::FromEnv::Ty(ty.lower_into(interner)))
92 ty::PredicateKind::Trait(predicate) => chalk_ir::DomainGoal::FromEnv(
93 chalk_ir::FromEnv::Trait(predicate.trait_ref.lower_into(interner)),
95 ty::PredicateKind::RegionOutlives(predicate) => chalk_ir::DomainGoal::Holds(
96 chalk_ir::WhereClause::LifetimeOutlives(chalk_ir::LifetimeOutlives {
97 a: predicate.0.lower_into(interner),
98 b: predicate.1.lower_into(interner),
101 ty::PredicateKind::TypeOutlives(predicate) => chalk_ir::DomainGoal::Holds(
102 chalk_ir::WhereClause::TypeOutlives(chalk_ir::TypeOutlives {
103 ty: predicate.0.lower_into(interner),
104 lifetime: predicate.1.lower_into(interner),
107 ty::PredicateKind::Projection(predicate) => chalk_ir::DomainGoal::Holds(
108 chalk_ir::WhereClause::AliasEq(predicate.lower_into(interner)),
110 ty::PredicateKind::WellFormed(arg) => match arg.unpack() {
111 ty::GenericArgKind::Type(ty) => chalk_ir::DomainGoal::WellFormed(
112 chalk_ir::WellFormed::Ty(ty.lower_into(interner)),
114 // FIXME(chalk): we need to change `WellFormed` in Chalk to take a `GenericArg`
115 _ => chalk_ir::DomainGoal::WellFormed(chalk_ir::WellFormed::Ty(
116 interner.tcx.types.unit.lower_into(interner),
119 ty::PredicateKind::ObjectSafe(..)
120 | ty::PredicateKind::ClosureKind(..)
121 | ty::PredicateKind::Subtype(..)
122 | ty::PredicateKind::Coerce(..)
123 | ty::PredicateKind::ConstEvaluatable(..)
124 | ty::PredicateKind::ConstEquate(..) => bug!("unexpected predicate {}", predicate),
126 let value = chalk_ir::ProgramClauseImplication {
128 conditions: chalk_ir::Goals::empty(interner),
129 priority: chalk_ir::ClausePriority::High,
130 constraints: chalk_ir::Constraints::empty(interner),
132 chalk_ir::ProgramClauseData(chalk_ir::Binders::new(binders, value)).intern(interner)
135 let goal: chalk_ir::GoalData<RustInterner<'tcx>> = self.goal.lower_into(interner);
136 chalk_ir::InEnvironment {
137 environment: chalk_ir::Environment {
138 clauses: chalk_ir::ProgramClauses::from_iter(interner, clauses),
140 goal: goal.intern(interner),
145 impl<'tcx> LowerInto<'tcx, chalk_ir::GoalData<RustInterner<'tcx>>> for ty::Predicate<'tcx> {
146 fn lower_into(self, interner: RustInterner<'tcx>) -> chalk_ir::GoalData<RustInterner<'tcx>> {
147 let (predicate, binders, _named_regions) =
148 collect_bound_vars(interner, interner.tcx, self.kind());
150 let value = match predicate {
151 ty::PredicateKind::Trait(predicate) => {
152 chalk_ir::GoalData::DomainGoal(chalk_ir::DomainGoal::Holds(
153 chalk_ir::WhereClause::Implemented(predicate.trait_ref.lower_into(interner)),
156 ty::PredicateKind::RegionOutlives(predicate) => {
157 chalk_ir::GoalData::DomainGoal(chalk_ir::DomainGoal::Holds(
158 chalk_ir::WhereClause::LifetimeOutlives(chalk_ir::LifetimeOutlives {
159 a: predicate.0.lower_into(interner),
160 b: predicate.1.lower_into(interner),
164 ty::PredicateKind::TypeOutlives(predicate) => {
165 chalk_ir::GoalData::DomainGoal(chalk_ir::DomainGoal::Holds(
166 chalk_ir::WhereClause::TypeOutlives(chalk_ir::TypeOutlives {
167 ty: predicate.0.lower_into(interner),
168 lifetime: predicate.1.lower_into(interner),
172 ty::PredicateKind::Projection(predicate) => {
173 chalk_ir::GoalData::DomainGoal(chalk_ir::DomainGoal::Holds(
174 chalk_ir::WhereClause::AliasEq(predicate.lower_into(interner)),
177 ty::PredicateKind::WellFormed(arg) => match arg.unpack() {
178 GenericArgKind::Type(ty) => match ty.kind() {
179 // FIXME(chalk): In Chalk, a placeholder is WellFormed if it
180 // `FromEnv`. However, when we "lower" Params, we don't update
182 ty::Placeholder(..) => {
183 chalk_ir::GoalData::All(chalk_ir::Goals::empty(interner))
186 _ => chalk_ir::GoalData::DomainGoal(chalk_ir::DomainGoal::WellFormed(
187 chalk_ir::WellFormed::Ty(ty.lower_into(interner)),
190 // FIXME(chalk): handle well formed consts
191 GenericArgKind::Const(..) => {
192 chalk_ir::GoalData::All(chalk_ir::Goals::empty(interner))
194 GenericArgKind::Lifetime(lt) => bug!("unexpect well formed predicate: {:?}", lt),
197 ty::PredicateKind::ObjectSafe(t) => chalk_ir::GoalData::DomainGoal(
198 chalk_ir::DomainGoal::ObjectSafe(chalk_ir::TraitId(t)),
201 ty::PredicateKind::Subtype(ty::SubtypePredicate { a, b, a_is_expected: _ }) => {
202 chalk_ir::GoalData::SubtypeGoal(chalk_ir::SubtypeGoal {
203 a: a.lower_into(interner),
204 b: b.lower_into(interner),
208 // FIXME(chalk): other predicates
210 // We can defer this, but ultimately we'll want to express
211 // some of these in terms of chalk operations.
212 ty::PredicateKind::ClosureKind(..)
213 | ty::PredicateKind::Coerce(..)
214 | ty::PredicateKind::ConstEvaluatable(..)
215 | ty::PredicateKind::ConstEquate(..) => {
216 chalk_ir::GoalData::All(chalk_ir::Goals::empty(interner))
218 ty::PredicateKind::TypeWellFormedFromEnv(ty) => chalk_ir::GoalData::DomainGoal(
219 chalk_ir::DomainGoal::FromEnv(chalk_ir::FromEnv::Ty(ty.lower_into(interner))),
223 chalk_ir::GoalData::Quantified(
224 chalk_ir::QuantifierKind::ForAll,
225 chalk_ir::Binders::new(binders, value.intern(interner)),
230 impl<'tcx> LowerInto<'tcx, chalk_ir::TraitRef<RustInterner<'tcx>>>
231 for rustc_middle::ty::TraitRef<'tcx>
233 fn lower_into(self, interner: RustInterner<'tcx>) -> chalk_ir::TraitRef<RustInterner<'tcx>> {
235 trait_id: chalk_ir::TraitId(self.def_id),
236 substitution: self.substs.lower_into(interner),
241 impl<'tcx> LowerInto<'tcx, chalk_ir::AliasEq<RustInterner<'tcx>>>
242 for rustc_middle::ty::ProjectionPredicate<'tcx>
244 fn lower_into(self, interner: RustInterner<'tcx>) -> chalk_ir::AliasEq<RustInterner<'tcx>> {
245 // FIXME(associated_const_equality): teach chalk about terms for alias eq.
247 ty: self.term.ty().unwrap().lower_into(interner),
248 alias: self.projection_ty.lower_into(interner),
254 // FIXME(...): Where do I add this to Chalk? I can't find it in the rustc repo anywhere.
255 impl<'tcx> LowerInto<'tcx, chalk_ir::Term<RustInterner<'tcx>>> for rustc_middle::ty::Term<'tcx> {
256 fn lower_into(self, interner: RustInterner<'tcx>) -> chalk_ir::Term<RustInterner<'tcx>> {
258 ty::Term::Ty(ty) => ty.lower_into(interner).into(),
259 ty::Term::Const(c) => c.lower_into(interner).into(),
265 impl<'tcx> LowerInto<'tcx, chalk_ir::Ty<RustInterner<'tcx>>> for Ty<'tcx> {
266 fn lower_into(self, interner: RustInterner<'tcx>) -> chalk_ir::Ty<RustInterner<'tcx>> {
267 let int = |i| chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Int(i));
268 let uint = |i| chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Uint(i));
269 let float = |f| chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Float(f));
272 ty::Bool => chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Bool),
273 ty::Char => chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Char),
274 ty::Int(ty) => match ty {
275 ty::IntTy::Isize => int(chalk_ir::IntTy::Isize),
276 ty::IntTy::I8 => int(chalk_ir::IntTy::I8),
277 ty::IntTy::I16 => int(chalk_ir::IntTy::I16),
278 ty::IntTy::I32 => int(chalk_ir::IntTy::I32),
279 ty::IntTy::I64 => int(chalk_ir::IntTy::I64),
280 ty::IntTy::I128 => int(chalk_ir::IntTy::I128),
282 ty::Uint(ty) => match ty {
283 ty::UintTy::Usize => uint(chalk_ir::UintTy::Usize),
284 ty::UintTy::U8 => uint(chalk_ir::UintTy::U8),
285 ty::UintTy::U16 => uint(chalk_ir::UintTy::U16),
286 ty::UintTy::U32 => uint(chalk_ir::UintTy::U32),
287 ty::UintTy::U64 => uint(chalk_ir::UintTy::U64),
288 ty::UintTy::U128 => uint(chalk_ir::UintTy::U128),
290 ty::Float(ty) => match ty {
291 ty::FloatTy::F32 => float(chalk_ir::FloatTy::F32),
292 ty::FloatTy::F64 => float(chalk_ir::FloatTy::F64),
294 ty::Adt(def, substs) => {
295 chalk_ir::TyKind::Adt(chalk_ir::AdtId(def), substs.lower_into(interner))
297 ty::Foreign(def_id) => chalk_ir::TyKind::Foreign(ForeignDefId(def_id)),
298 ty::Str => chalk_ir::TyKind::Str,
299 ty::Array(ty, len) => {
300 chalk_ir::TyKind::Array(ty.lower_into(interner), len.lower_into(interner))
302 ty::Slice(ty) => chalk_ir::TyKind::Slice(ty.lower_into(interner)),
305 chalk_ir::TyKind::Raw(ptr.mutbl.lower_into(interner), ptr.ty.lower_into(interner))
307 ty::Ref(region, ty, mutability) => chalk_ir::TyKind::Ref(
308 mutability.lower_into(interner),
309 region.lower_into(interner),
310 ty.lower_into(interner),
312 ty::FnDef(def_id, substs) => {
313 chalk_ir::TyKind::FnDef(chalk_ir::FnDefId(def_id), substs.lower_into(interner))
316 let (inputs_and_outputs, binders, _named_regions) =
317 collect_bound_vars(interner, interner.tcx, sig.inputs_and_output());
318 chalk_ir::TyKind::Function(chalk_ir::FnPointer {
319 num_binders: binders.len(interner),
320 sig: sig.lower_into(interner),
321 substitution: chalk_ir::FnSubst(chalk_ir::Substitution::from_iter(
323 inputs_and_outputs.iter().map(|ty| {
324 chalk_ir::GenericArgData::Ty(ty.lower_into(interner)).intern(interner)
329 ty::Dynamic(predicates, region) => chalk_ir::TyKind::Dyn(chalk_ir::DynTy {
330 bounds: predicates.lower_into(interner),
331 lifetime: region.lower_into(interner),
333 ty::Closure(def_id, substs) => {
334 chalk_ir::TyKind::Closure(chalk_ir::ClosureId(def_id), substs.lower_into(interner))
336 ty::Generator(def_id, substs, _) => chalk_ir::TyKind::Generator(
337 chalk_ir::GeneratorId(def_id),
338 substs.lower_into(interner),
340 ty::GeneratorWitness(_) => unimplemented!(),
341 ty::Never => chalk_ir::TyKind::Never,
342 ty::Tuple(types) => {
343 chalk_ir::TyKind::Tuple(types.len(), types.as_substs().lower_into(interner))
345 ty::Projection(proj) => chalk_ir::TyKind::Alias(proj.lower_into(interner)),
346 ty::Opaque(def_id, substs) => {
347 chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy {
348 opaque_ty_id: chalk_ir::OpaqueTyId(def_id),
349 substitution: substs.lower_into(interner),
352 // This should have been done eagerly prior to this, and all Params
353 // should have been substituted to placeholders
354 ty::Param(_) => panic!("Lowering Param when not expected."),
355 ty::Bound(db, bound) => chalk_ir::TyKind::BoundVar(chalk_ir::BoundVar::new(
356 chalk_ir::DebruijnIndex::new(db.as_u32()),
359 ty::Placeholder(_placeholder) => {
360 chalk_ir::TyKind::Placeholder(chalk_ir::PlaceholderIndex {
361 ui: chalk_ir::UniverseIndex { counter: _placeholder.universe.as_usize() },
362 idx: _placeholder.name.as_usize(),
365 ty::Infer(_infer) => unimplemented!(),
366 ty::Error(_) => chalk_ir::TyKind::Error,
372 impl<'tcx> LowerInto<'tcx, Ty<'tcx>> for &chalk_ir::Ty<RustInterner<'tcx>> {
373 fn lower_into(self, interner: RustInterner<'tcx>) -> Ty<'tcx> {
374 use chalk_ir::TyKind;
376 let kind = match self.kind(interner) {
377 TyKind::Adt(struct_id, substitution) => {
378 ty::Adt(struct_id.0, substitution.lower_into(interner))
380 TyKind::Scalar(scalar) => match scalar {
381 chalk_ir::Scalar::Bool => ty::Bool,
382 chalk_ir::Scalar::Char => ty::Char,
383 chalk_ir::Scalar::Int(int_ty) => match int_ty {
384 chalk_ir::IntTy::Isize => ty::Int(ty::IntTy::Isize),
385 chalk_ir::IntTy::I8 => ty::Int(ty::IntTy::I8),
386 chalk_ir::IntTy::I16 => ty::Int(ty::IntTy::I16),
387 chalk_ir::IntTy::I32 => ty::Int(ty::IntTy::I32),
388 chalk_ir::IntTy::I64 => ty::Int(ty::IntTy::I64),
389 chalk_ir::IntTy::I128 => ty::Int(ty::IntTy::I128),
391 chalk_ir::Scalar::Uint(int_ty) => match int_ty {
392 chalk_ir::UintTy::Usize => ty::Uint(ty::UintTy::Usize),
393 chalk_ir::UintTy::U8 => ty::Uint(ty::UintTy::U8),
394 chalk_ir::UintTy::U16 => ty::Uint(ty::UintTy::U16),
395 chalk_ir::UintTy::U32 => ty::Uint(ty::UintTy::U32),
396 chalk_ir::UintTy::U64 => ty::Uint(ty::UintTy::U64),
397 chalk_ir::UintTy::U128 => ty::Uint(ty::UintTy::U128),
399 chalk_ir::Scalar::Float(float_ty) => match float_ty {
400 chalk_ir::FloatTy::F32 => ty::Float(ty::FloatTy::F32),
401 chalk_ir::FloatTy::F64 => ty::Float(ty::FloatTy::F64),
404 TyKind::Array(ty, c) => {
405 let ty = ty.lower_into(interner);
406 let c = c.lower_into(interner);
409 TyKind::FnDef(id, substitution) => ty::FnDef(id.0, substitution.lower_into(interner)),
410 TyKind::Closure(closure, substitution) => {
411 ty::Closure(closure.0, substitution.lower_into(interner))
413 TyKind::Generator(..) => unimplemented!(),
414 TyKind::GeneratorWitness(..) => unimplemented!(),
415 TyKind::Never => ty::Never,
416 TyKind::Tuple(_len, substitution) => {
417 ty::Tuple(substitution.lower_into(interner).try_as_type_list().unwrap())
419 TyKind::Slice(ty) => ty::Slice(ty.lower_into(interner)),
420 TyKind::Raw(mutbl, ty) => ty::RawPtr(ty::TypeAndMut {
421 ty: ty.lower_into(interner),
422 mutbl: mutbl.lower_into(interner),
424 TyKind::Ref(mutbl, lifetime, ty) => ty::Ref(
425 lifetime.lower_into(interner),
426 ty.lower_into(interner),
427 mutbl.lower_into(interner),
429 TyKind::Str => ty::Str,
430 TyKind::OpaqueType(opaque_ty, substitution) => {
431 ty::Opaque(opaque_ty.0, substitution.lower_into(interner))
433 TyKind::AssociatedType(assoc_ty, substitution) => ty::Projection(ty::ProjectionTy {
434 substs: substitution.lower_into(interner),
435 item_def_id: assoc_ty.0,
437 TyKind::Foreign(def_id) => ty::Foreign(def_id.0),
438 TyKind::Error => return interner.tcx.ty_error(),
439 TyKind::Placeholder(placeholder) => ty::Placeholder(ty::Placeholder {
440 universe: ty::UniverseIndex::from_usize(placeholder.ui.counter),
441 name: ty::BoundVar::from_usize(placeholder.idx),
443 TyKind::Alias(alias_ty) => match alias_ty {
444 chalk_ir::AliasTy::Projection(projection) => ty::Projection(ty::ProjectionTy {
445 item_def_id: projection.associated_ty_id.0,
446 substs: projection.substitution.lower_into(interner),
448 chalk_ir::AliasTy::Opaque(opaque) => {
449 ty::Opaque(opaque.opaque_ty_id.0, opaque.substitution.lower_into(interner))
452 TyKind::Function(_quantified_ty) => unimplemented!(),
453 TyKind::BoundVar(_bound) => ty::Bound(
454 ty::DebruijnIndex::from_usize(_bound.debruijn.depth() as usize),
456 var: ty::BoundVar::from_usize(_bound.index),
457 kind: ty::BoundTyKind::Anon,
460 TyKind::InferenceVar(_, _) => unimplemented!(),
461 TyKind::Dyn(_) => unimplemented!(),
463 interner.tcx.mk_ty(kind)
467 impl<'tcx> LowerInto<'tcx, chalk_ir::Lifetime<RustInterner<'tcx>>> for Region<'tcx> {
468 fn lower_into(self, interner: RustInterner<'tcx>) -> chalk_ir::Lifetime<RustInterner<'tcx>> {
470 ty::ReEarlyBound(_) => {
471 panic!("Should have already been substituted.");
473 ty::ReLateBound(db, br) => chalk_ir::LifetimeData::BoundVar(chalk_ir::BoundVar::new(
474 chalk_ir::DebruijnIndex::new(db.as_u32()),
478 ty::ReFree(_) => unimplemented!(),
479 ty::ReStatic => chalk_ir::LifetimeData::Static.intern(interner),
480 ty::ReVar(_) => unimplemented!(),
481 ty::RePlaceholder(placeholder_region) => {
482 chalk_ir::LifetimeData::Placeholder(chalk_ir::PlaceholderIndex {
483 ui: chalk_ir::UniverseIndex { counter: placeholder_region.universe.index() },
489 chalk_ir::LifetimeData::Empty(chalk_ir::UniverseIndex { counter: ui.index() })
492 ty::ReErased => chalk_ir::LifetimeData::Erased.intern(interner),
497 impl<'tcx> LowerInto<'tcx, Region<'tcx>> for &chalk_ir::Lifetime<RustInterner<'tcx>> {
498 fn lower_into(self, interner: RustInterner<'tcx>) -> Region<'tcx> {
499 let kind = match self.data(interner) {
500 chalk_ir::LifetimeData::BoundVar(var) => ty::ReLateBound(
501 ty::DebruijnIndex::from_u32(var.debruijn.depth()),
503 var: ty::BoundVar::from_usize(var.index),
504 kind: ty::BrAnon(var.index as u32),
507 chalk_ir::LifetimeData::InferenceVar(_var) => unimplemented!(),
508 chalk_ir::LifetimeData::Placeholder(p) => ty::RePlaceholder(ty::Placeholder {
509 universe: ty::UniverseIndex::from_usize(p.ui.counter),
510 name: ty::BoundRegionKind::BrAnon(p.idx as u32),
512 chalk_ir::LifetimeData::Static => return interner.tcx.lifetimes.re_static,
513 chalk_ir::LifetimeData::Empty(ui) => {
514 ty::ReEmpty(ty::UniverseIndex::from_usize(ui.counter))
516 chalk_ir::LifetimeData::Erased => return interner.tcx.lifetimes.re_erased,
517 chalk_ir::LifetimeData::Phantom(void, _) => match *void {},
519 interner.tcx.mk_region(kind)
523 impl<'tcx> LowerInto<'tcx, chalk_ir::Const<RustInterner<'tcx>>> for ty::Const<'tcx> {
524 fn lower_into(self, interner: RustInterner<'tcx>) -> chalk_ir::Const<RustInterner<'tcx>> {
525 let ty = self.ty().lower_into(interner);
526 let value = match self.kind() {
527 ty::ConstKind::Value(val) => {
528 chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: val })
530 ty::ConstKind::Bound(db, bound) => chalk_ir::ConstValue::BoundVar(
531 chalk_ir::BoundVar::new(chalk_ir::DebruijnIndex::new(db.as_u32()), bound.index()),
533 _ => unimplemented!("Const not implemented. {:?}", self),
535 chalk_ir::ConstData { ty, value }.intern(interner)
539 impl<'tcx> LowerInto<'tcx, ty::Const<'tcx>> for &chalk_ir::Const<RustInterner<'tcx>> {
540 fn lower_into(self, interner: RustInterner<'tcx>) -> ty::Const<'tcx> {
541 let data = self.data(interner);
542 let ty = data.ty.lower_into(interner);
543 let kind = match data.value {
544 chalk_ir::ConstValue::BoundVar(var) => ty::ConstKind::Bound(
545 ty::DebruijnIndex::from_u32(var.debruijn.depth()),
546 ty::BoundVar::from_u32(var.index as u32),
548 chalk_ir::ConstValue::InferenceVar(_var) => unimplemented!(),
549 chalk_ir::ConstValue::Placeholder(_p) => unimplemented!(),
550 chalk_ir::ConstValue::Concrete(c) => ty::ConstKind::Value(c.interned),
552 interner.tcx.mk_const(ty::ConstS { ty, kind })
556 impl<'tcx> LowerInto<'tcx, chalk_ir::GenericArg<RustInterner<'tcx>>> for GenericArg<'tcx> {
557 fn lower_into(self, interner: RustInterner<'tcx>) -> chalk_ir::GenericArg<RustInterner<'tcx>> {
558 match self.unpack() {
559 ty::subst::GenericArgKind::Type(ty) => {
560 chalk_ir::GenericArgData::Ty(ty.lower_into(interner))
562 ty::subst::GenericArgKind::Lifetime(lifetime) => {
563 chalk_ir::GenericArgData::Lifetime(lifetime.lower_into(interner))
565 ty::subst::GenericArgKind::Const(c) => {
566 chalk_ir::GenericArgData::Const(c.lower_into(interner))
573 impl<'tcx> LowerInto<'tcx, ty::subst::GenericArg<'tcx>>
574 for &chalk_ir::GenericArg<RustInterner<'tcx>>
576 fn lower_into(self, interner: RustInterner<'tcx>) -> ty::subst::GenericArg<'tcx> {
577 match self.data(interner) {
578 chalk_ir::GenericArgData::Ty(ty) => {
579 let t: Ty<'tcx> = ty.lower_into(interner);
582 chalk_ir::GenericArgData::Lifetime(lifetime) => {
583 let r: Region<'tcx> = lifetime.lower_into(interner);
586 chalk_ir::GenericArgData::Const(c) => {
587 let c: ty::Const<'tcx> = c.lower_into(interner);
594 // We lower into an Option here since there are some predicates which Chalk
595 // doesn't have a representation for yet (as a `WhereClause`), but are so common
596 // that we just are accepting the unsoundness for now. The `Option` will
597 // eventually be removed.
598 impl<'tcx> LowerInto<'tcx, Option<chalk_ir::QuantifiedWhereClause<RustInterner<'tcx>>>>
599 for ty::Predicate<'tcx>
603 interner: RustInterner<'tcx>,
604 ) -> Option<chalk_ir::QuantifiedWhereClause<RustInterner<'tcx>>> {
605 let (predicate, binders, _named_regions) =
606 collect_bound_vars(interner, interner.tcx, self.kind());
607 let value = match predicate {
608 ty::PredicateKind::Trait(predicate) => {
609 Some(chalk_ir::WhereClause::Implemented(predicate.trait_ref.lower_into(interner)))
611 ty::PredicateKind::RegionOutlives(predicate) => {
612 Some(chalk_ir::WhereClause::LifetimeOutlives(chalk_ir::LifetimeOutlives {
613 a: predicate.0.lower_into(interner),
614 b: predicate.1.lower_into(interner),
617 ty::PredicateKind::TypeOutlives(predicate) => {
618 Some(chalk_ir::WhereClause::TypeOutlives(chalk_ir::TypeOutlives {
619 ty: predicate.0.lower_into(interner),
620 lifetime: predicate.1.lower_into(interner),
623 ty::PredicateKind::Projection(predicate) => {
624 Some(chalk_ir::WhereClause::AliasEq(predicate.lower_into(interner)))
626 ty::PredicateKind::WellFormed(_ty) => None,
628 ty::PredicateKind::ObjectSafe(..)
629 | ty::PredicateKind::ClosureKind(..)
630 | ty::PredicateKind::Subtype(..)
631 | ty::PredicateKind::Coerce(..)
632 | ty::PredicateKind::ConstEvaluatable(..)
633 | ty::PredicateKind::ConstEquate(..)
634 | ty::PredicateKind::TypeWellFormedFromEnv(..) => {
635 bug!("unexpected predicate {}", &self)
638 value.map(|value| chalk_ir::Binders::new(binders, value))
642 impl<'tcx> LowerInto<'tcx, chalk_ir::Binders<chalk_ir::QuantifiedWhereClauses<RustInterner<'tcx>>>>
643 for &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>
647 interner: RustInterner<'tcx>,
648 ) -> chalk_ir::Binders<chalk_ir::QuantifiedWhereClauses<RustInterner<'tcx>>> {
649 // `Self` has one binder:
650 // Binder<&'tcx ty::List<ty::ExistentialPredicate<'tcx>>>
651 // The return type has two:
652 // Binders<&[Binders<WhereClause<I>>]>
653 // This means that any variables that are escaping `self` need to be
654 // shifted in by one so that they are still escaping.
655 let predicates = ty::fold::shift_vars(interner.tcx, self, 1);
657 let self_ty = interner.tcx.mk_ty(ty::Bound(
658 // This is going to be wrapped in a binder
659 ty::DebruijnIndex::from_usize(1),
660 ty::BoundTy { var: ty::BoundVar::from_usize(0), kind: ty::BoundTyKind::Anon },
662 let where_clauses = predicates.into_iter().map(|predicate| {
663 let (predicate, binders, _named_regions) =
664 collect_bound_vars(interner, interner.tcx, predicate);
666 ty::ExistentialPredicate::Trait(ty::ExistentialTraitRef { def_id, substs }) => {
667 chalk_ir::Binders::new(
669 chalk_ir::WhereClause::Implemented(chalk_ir::TraitRef {
670 trait_id: chalk_ir::TraitId(def_id),
671 substitution: interner
673 .mk_substs_trait(self_ty, substs)
674 .lower_into(interner),
678 ty::ExistentialPredicate::Projection(predicate) => chalk_ir::Binders::new(
680 chalk_ir::WhereClause::AliasEq(chalk_ir::AliasEq {
681 alias: chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy {
682 associated_ty_id: chalk_ir::AssocTypeId(predicate.item_def_id),
683 substitution: interner
685 .mk_substs_trait(self_ty, predicate.substs)
686 .lower_into(interner),
688 // FIXME(associated_const_equality): teach chalk about terms for alias eq.
689 ty: predicate.term.ty().unwrap().lower_into(interner),
692 ty::ExistentialPredicate::AutoTrait(def_id) => chalk_ir::Binders::new(
694 chalk_ir::WhereClause::Implemented(chalk_ir::TraitRef {
695 trait_id: chalk_ir::TraitId(def_id),
696 substitution: interner
698 .mk_substs_trait(self_ty, &[])
699 .lower_into(interner),
705 // Binder for the bound variable representing the concrete underlying type.
706 let existential_binder = chalk_ir::VariableKinds::from1(
708 chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General),
710 let value = chalk_ir::QuantifiedWhereClauses::from_iter(interner, where_clauses);
711 chalk_ir::Binders::new(existential_binder, value)
715 impl<'tcx> LowerInto<'tcx, chalk_ir::FnSig<RustInterner<'tcx>>>
716 for ty::Binder<'tcx, ty::FnSig<'tcx>>
718 fn lower_into(self, _interner: RustInterner<'_>) -> FnSig<RustInterner<'tcx>> {
721 safety: match self.unsafety() {
722 Unsafety::Normal => chalk_ir::Safety::Safe,
723 Unsafety::Unsafe => chalk_ir::Safety::Unsafe,
725 variadic: self.c_variadic(),
730 // We lower into an Option here since there are some predicates which Chalk
731 // doesn't have a representation for yet (as an `InlineBound`). The `Option` will
732 // eventually be removed.
733 impl<'tcx> LowerInto<'tcx, Option<chalk_solve::rust_ir::QuantifiedInlineBound<RustInterner<'tcx>>>>
734 for ty::Predicate<'tcx>
738 interner: RustInterner<'tcx>,
739 ) -> Option<chalk_solve::rust_ir::QuantifiedInlineBound<RustInterner<'tcx>>> {
740 let (predicate, binders, _named_regions) =
741 collect_bound_vars(interner, interner.tcx, self.kind());
743 ty::PredicateKind::Trait(predicate) => Some(chalk_ir::Binders::new(
745 chalk_solve::rust_ir::InlineBound::TraitBound(
746 predicate.trait_ref.lower_into(interner),
749 ty::PredicateKind::Projection(predicate) => Some(chalk_ir::Binders::new(
751 chalk_solve::rust_ir::InlineBound::AliasEqBound(predicate.lower_into(interner)),
753 ty::PredicateKind::TypeOutlives(_predicate) => None,
754 ty::PredicateKind::WellFormed(_ty) => None,
756 ty::PredicateKind::RegionOutlives(..)
757 | ty::PredicateKind::ObjectSafe(..)
758 | ty::PredicateKind::ClosureKind(..)
759 | ty::PredicateKind::Subtype(..)
760 | ty::PredicateKind::Coerce(..)
761 | ty::PredicateKind::ConstEvaluatable(..)
762 | ty::PredicateKind::ConstEquate(..)
763 | ty::PredicateKind::TypeWellFormedFromEnv(..) => {
764 bug!("unexpected predicate {}", &self)
770 impl<'tcx> LowerInto<'tcx, chalk_solve::rust_ir::TraitBound<RustInterner<'tcx>>>
771 for ty::TraitRef<'tcx>
775 interner: RustInterner<'tcx>,
776 ) -> chalk_solve::rust_ir::TraitBound<RustInterner<'tcx>> {
777 chalk_solve::rust_ir::TraitBound {
778 trait_id: chalk_ir::TraitId(self.def_id),
779 args_no_self: self.substs[1..].iter().map(|arg| arg.lower_into(interner)).collect(),
784 impl<'tcx> LowerInto<'tcx, chalk_ir::Mutability> for ast::Mutability {
785 fn lower_into(self, _interner: RustInterner<'tcx>) -> chalk_ir::Mutability {
787 rustc_ast::Mutability::Mut => chalk_ir::Mutability::Mut,
788 rustc_ast::Mutability::Not => chalk_ir::Mutability::Not,
793 impl<'tcx> LowerInto<'tcx, ast::Mutability> for chalk_ir::Mutability {
794 fn lower_into(self, _interner: RustInterner<'tcx>) -> ast::Mutability {
796 chalk_ir::Mutability::Mut => ast::Mutability::Mut,
797 chalk_ir::Mutability::Not => ast::Mutability::Not,
802 impl<'tcx> LowerInto<'tcx, chalk_solve::rust_ir::Polarity> for ty::ImplPolarity {
803 fn lower_into(self, _interner: RustInterner<'tcx>) -> chalk_solve::rust_ir::Polarity {
805 ty::ImplPolarity::Positive => chalk_solve::rust_ir::Polarity::Positive,
806 ty::ImplPolarity::Negative => chalk_solve::rust_ir::Polarity::Negative,
807 // FIXME(chalk) reservation impls
808 ty::ImplPolarity::Reservation => chalk_solve::rust_ir::Polarity::Negative,
812 impl<'tcx> LowerInto<'tcx, chalk_ir::Variance> for ty::Variance {
813 fn lower_into(self, _interner: RustInterner<'tcx>) -> chalk_ir::Variance {
815 ty::Variance::Covariant => chalk_ir::Variance::Covariant,
816 ty::Variance::Invariant => chalk_ir::Variance::Invariant,
817 ty::Variance::Contravariant => chalk_ir::Variance::Contravariant,
818 ty::Variance::Bivariant => unimplemented!(),
823 impl<'tcx> LowerInto<'tcx, chalk_solve::rust_ir::AliasEqBound<RustInterner<'tcx>>>
824 for ty::ProjectionPredicate<'tcx>
828 interner: RustInterner<'tcx>,
829 ) -> chalk_solve::rust_ir::AliasEqBound<RustInterner<'tcx>> {
830 let (trait_ref, own_substs) = self.projection_ty.trait_ref_and_own_substs(interner.tcx);
831 chalk_solve::rust_ir::AliasEqBound {
832 trait_bound: trait_ref.lower_into(interner),
833 associated_ty_id: chalk_ir::AssocTypeId(self.projection_ty.item_def_id),
834 parameters: own_substs.iter().map(|arg| arg.lower_into(interner)).collect(),
835 value: self.term.ty().unwrap().lower_into(interner),
840 /// To collect bound vars, we have to do two passes. In the first pass, we
841 /// collect all `BoundRegionKind`s and `ty::Bound`s. In the second pass, we then
842 /// replace `BrNamed` into `BrAnon`. The two separate passes are important,
843 /// since we can only replace `BrNamed` with `BrAnon`s with indices *after* all
844 /// "real" `BrAnon`s.
846 /// It's important to note that because of prior substitution, we may have
847 /// late-bound regions, even outside of fn contexts, since this is the best way
848 /// to prep types for chalk lowering.
849 pub(crate) fn collect_bound_vars<'tcx, T: TypeFoldable<'tcx>>(
850 interner: RustInterner<'tcx>,
853 ) -> (T, chalk_ir::VariableKinds<RustInterner<'tcx>>, BTreeMap<DefId, u32>) {
854 let mut bound_vars_collector = BoundVarsCollector::new();
855 ty.as_ref().skip_binder().visit_with(&mut bound_vars_collector);
856 let mut parameters = bound_vars_collector.parameters;
857 let named_parameters: BTreeMap<DefId, u32> = bound_vars_collector
861 .map(|(i, def_id)| (def_id, (i + parameters.len()) as u32))
864 let mut bound_var_substitutor = NamedBoundVarSubstitutor::new(tcx, &named_parameters);
865 let new_ty = ty.skip_binder().fold_with(&mut bound_var_substitutor);
867 for var in named_parameters.values() {
868 parameters.insert(*var, chalk_ir::VariableKind::Lifetime);
871 (0..parameters.len()).for_each(|i| {
874 .or_else(|| bug!("Skipped bound var index: parameters={:?}", parameters));
878 chalk_ir::VariableKinds::from_iter(interner, parameters.into_iter().map(|(_, v)| v));
880 (new_ty, binders, named_parameters)
883 pub(crate) struct BoundVarsCollector<'tcx> {
884 binder_index: ty::DebruijnIndex,
885 pub(crate) parameters: BTreeMap<u32, chalk_ir::VariableKind<RustInterner<'tcx>>>,
886 pub(crate) named_parameters: Vec<DefId>,
889 impl<'tcx> BoundVarsCollector<'tcx> {
890 pub(crate) fn new() -> Self {
892 binder_index: ty::INNERMOST,
893 parameters: BTreeMap::new(),
894 named_parameters: vec![],
899 impl<'tcx> TypeVisitor<'tcx> for BoundVarsCollector<'tcx> {
900 fn visit_binder<T: TypeVisitable<'tcx>>(
903 ) -> ControlFlow<Self::BreakTy> {
904 self.binder_index.shift_in(1);
905 let result = t.super_visit_with(self);
906 self.binder_index.shift_out(1);
910 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
912 ty::Bound(debruijn, bound_ty) if debruijn == self.binder_index => {
913 match self.parameters.entry(bound_ty.var.as_u32()) {
914 Entry::Vacant(entry) => {
915 entry.insert(chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General));
917 Entry::Occupied(entry) => match entry.get() {
918 chalk_ir::VariableKind::Ty(_) => {}
927 t.super_visit_with(self)
930 fn visit_region(&mut self, r: Region<'tcx>) -> ControlFlow<Self::BreakTy> {
932 ty::ReLateBound(index, br) if index == self.binder_index => match br.kind {
933 ty::BoundRegionKind::BrNamed(def_id, _name) => {
934 if !self.named_parameters.iter().any(|d| *d == def_id) {
935 self.named_parameters.push(def_id);
939 ty::BoundRegionKind::BrAnon(var) => match self.parameters.entry(var) {
940 Entry::Vacant(entry) => {
941 entry.insert(chalk_ir::VariableKind::Lifetime);
943 Entry::Occupied(entry) => match entry.get() {
944 chalk_ir::VariableKind::Lifetime => {}
949 ty::BoundRegionKind::BrEnv => unimplemented!(),
952 ty::ReEarlyBound(_re) => {
953 // FIXME(chalk): jackh726 - I think we should always have already
954 // substituted away `ReEarlyBound`s for `ReLateBound`s, but need to confirm.
961 r.super_visit_with(self)
965 /// This is used to replace `BoundRegionKind::BrNamed` with `BoundRegionKind::BrAnon`.
966 /// Note: we assume that we will always have room for more bound vars. (i.e. we
967 /// won't ever hit the `u32` limit in `BrAnon`s).
968 struct NamedBoundVarSubstitutor<'a, 'tcx> {
970 binder_index: ty::DebruijnIndex,
971 named_parameters: &'a BTreeMap<DefId, u32>,
974 impl<'a, 'tcx> NamedBoundVarSubstitutor<'a, 'tcx> {
975 fn new(tcx: TyCtxt<'tcx>, named_parameters: &'a BTreeMap<DefId, u32>) -> Self {
976 NamedBoundVarSubstitutor { tcx, binder_index: ty::INNERMOST, named_parameters }
980 impl<'a, 'tcx> TypeFolder<'tcx> for NamedBoundVarSubstitutor<'a, 'tcx> {
981 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
985 fn fold_binder<T: TypeFoldable<'tcx>>(&mut self, t: Binder<'tcx, T>) -> Binder<'tcx, T> {
986 self.binder_index.shift_in(1);
987 let result = t.super_fold_with(self);
988 self.binder_index.shift_out(1);
992 fn fold_region(&mut self, r: Region<'tcx>) -> Region<'tcx> {
994 ty::ReLateBound(index, br) if index == self.binder_index => match br.kind {
995 ty::BrNamed(def_id, _name) => match self.named_parameters.get(&def_id) {
997 let new_br = ty::BoundRegion { var: br.var, kind: ty::BrAnon(*idx) };
998 return self.tcx.mk_region(ty::ReLateBound(index, new_br));
1000 None => panic!("Missing `BrNamed`."),
1002 ty::BrEnv => unimplemented!(),
1008 r.super_fold_with(self)
1012 /// Used to substitute `Param`s with placeholders. We do this since Chalk
1013 /// have a notion of `Param`s.
1014 pub(crate) struct ParamsSubstitutor<'tcx> {
1016 binder_index: ty::DebruijnIndex,
1017 list: Vec<rustc_middle::ty::ParamTy>,
1018 next_ty_placeholder: usize,
1019 pub(crate) params: rustc_data_structures::fx::FxHashMap<usize, rustc_middle::ty::ParamTy>,
1020 pub(crate) named_regions: BTreeMap<DefId, u32>,
1023 impl<'tcx> ParamsSubstitutor<'tcx> {
1024 pub(crate) fn new(tcx: TyCtxt<'tcx>, next_ty_placeholder: usize) -> Self {
1027 binder_index: ty::INNERMOST,
1029 next_ty_placeholder,
1030 params: rustc_data_structures::fx::FxHashMap::default(),
1031 named_regions: BTreeMap::default(),
1036 impl<'tcx> TypeFolder<'tcx> for ParamsSubstitutor<'tcx> {
1037 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
1041 fn fold_binder<T: TypeFoldable<'tcx>>(&mut self, t: Binder<'tcx, T>) -> Binder<'tcx, T> {
1042 self.binder_index.shift_in(1);
1043 let result = t.super_fold_with(self);
1044 self.binder_index.shift_out(1);
1048 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
1050 ty::Param(param) => match self.list.iter().position(|r| r == ¶m) {
1051 Some(idx) => self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
1052 universe: ty::UniverseIndex::from_usize(0),
1053 name: ty::BoundVar::from_usize(idx),
1056 self.list.push(param);
1057 let idx = self.list.len() - 1 + self.next_ty_placeholder;
1058 self.params.insert(idx, param);
1059 self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
1060 universe: ty::UniverseIndex::from_usize(0),
1061 name: ty::BoundVar::from_usize(idx),
1065 _ => t.super_fold_with(self),
1069 fn fold_region(&mut self, r: Region<'tcx>) -> Region<'tcx> {
1071 // FIXME(chalk) - jackh726 - this currently isn't hit in any tests,
1072 // since canonicalization will already change these to canonical
1073 // variables (ty::ReLateBound).
1074 ty::ReEarlyBound(_re) => match self.named_regions.get(&_re.def_id) {
1076 let br = ty::BoundRegion {
1077 var: ty::BoundVar::from_u32(*idx),
1078 kind: ty::BrAnon(*idx),
1080 self.tcx.mk_region(ty::ReLateBound(self.binder_index, br))
1083 let idx = self.named_regions.len() as u32;
1085 ty::BoundRegion { var: ty::BoundVar::from_u32(idx), kind: ty::BrAnon(idx) };
1086 self.named_regions.insert(_re.def_id, idx);
1087 self.tcx.mk_region(ty::ReLateBound(self.binder_index, br))
1091 _ => r.super_fold_with(self),
1096 pub(crate) struct ReverseParamsSubstitutor<'tcx> {
1098 params: rustc_data_structures::fx::FxHashMap<usize, rustc_middle::ty::ParamTy>,
1101 impl<'tcx> ReverseParamsSubstitutor<'tcx> {
1104 params: rustc_data_structures::fx::FxHashMap<usize, rustc_middle::ty::ParamTy>,
1106 Self { tcx, params }
1110 impl<'tcx> TypeFolder<'tcx> for ReverseParamsSubstitutor<'tcx> {
1111 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
1115 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
1117 ty::Placeholder(ty::PlaceholderType { universe: ty::UniverseIndex::ROOT, name }) => {
1118 match self.params.get(&name.as_usize()) {
1119 Some(param) => self.tcx.mk_ty(ty::Param(*param)),
1124 _ => t.super_fold_with(self),
1129 /// Used to collect `Placeholder`s.
1130 pub(crate) struct PlaceholdersCollector {
1131 universe_index: ty::UniverseIndex,
1132 pub(crate) next_ty_placeholder: usize,
1133 pub(crate) next_anon_region_placeholder: u32,
1136 impl PlaceholdersCollector {
1137 pub(crate) fn new() -> Self {
1138 PlaceholdersCollector {
1139 universe_index: ty::UniverseIndex::ROOT,
1140 next_ty_placeholder: 0,
1141 next_anon_region_placeholder: 0,
1146 impl<'tcx> TypeVisitor<'tcx> for PlaceholdersCollector {
1147 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
1149 ty::Placeholder(p) if p.universe == self.universe_index => {
1150 self.next_ty_placeholder = self.next_ty_placeholder.max(p.name.as_usize() + 1);
1156 t.super_visit_with(self)
1159 fn visit_region(&mut self, r: Region<'tcx>) -> ControlFlow<Self::BreakTy> {
1161 ty::RePlaceholder(p) if p.universe == self.universe_index => {
1162 if let ty::BoundRegionKind::BrAnon(anon) = p.name {
1163 self.next_anon_region_placeholder = self.next_anon_region_placeholder.max(anon);
1170 r.super_visit_with(self)