1 //! Provides the `RustIrDatabase` implementation for `chalk-solve`
3 //! The purpose of the `chalk_solve::RustIrDatabase` is to get data about
4 //! specific types, such as bounds, where clauses, or fields. This file contains
5 //! the minimal logic to assemble the types for `chalk-solve` by calling out to
6 //! either the `TyCtxt` (for information about types) or
7 //! `crate::chalk::lowering` (to lower rustc types into Chalk types).
9 use rustc_middle::traits::ChalkRustInterner as RustInterner;
10 use rustc_middle::ty::subst::{InternalSubsts, Subst, SubstsRef};
11 use rustc_middle::ty::{self, AssocItemContainer, AssocKind, Ty, TyCtxt, TypeFoldable};
14 use rustc_attr as attr;
16 use rustc_hir::def_id::DefId;
18 use rustc_span::symbol::sym;
23 use crate::chalk::lowering::LowerInto;
25 pub struct RustIrDatabase<'tcx> {
26 pub(crate) interner: RustInterner<'tcx>,
29 impl fmt::Debug for RustIrDatabase<'_> {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 write!(f, "RustIrDatabase")
35 impl<'tcx> RustIrDatabase<'tcx> {
39 bound_vars: SubstsRef<'tcx>,
40 ) -> Vec<chalk_ir::QuantifiedWhereClause<RustInterner<'tcx>>> {
41 let predicates = self.interner.tcx.predicates_defined_on(def_id).predicates;
44 .map(|(wc, _)| wc.subst(self.interner.tcx, bound_vars))
45 .filter_map(|wc| LowerInto::<
46 Option<chalk_ir::QuantifiedWhereClause<RustInterner<'tcx>>>
47 >::lower_into(wc, self.interner)).collect()
50 fn bounds_for<T>(&self, def_id: DefId, bound_vars: SubstsRef<'tcx>) -> Vec<T>
52 ty::Predicate<'tcx>: LowerInto<'tcx, std::option::Option<T>>,
56 .explicit_item_bounds(def_id)
58 .map(|(bound, _)| bound.subst(self.interner.tcx, &bound_vars))
59 .filter_map(|bound| LowerInto::<Option<_>>::lower_into(bound, self.interner))
64 impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'tcx> {
65 fn interner(&self) -> RustInterner<'tcx> {
69 fn associated_ty_data(
71 assoc_type_id: chalk_ir::AssocTypeId<RustInterner<'tcx>>,
72 ) -> Arc<chalk_solve::rust_ir::AssociatedTyDatum<RustInterner<'tcx>>> {
73 let def_id = assoc_type_id.0;
74 let assoc_item = self.interner.tcx.associated_item(def_id);
75 let AssocItemContainer::TraitContainer(trait_def_id) = assoc_item.container else {
76 unimplemented!("Not possible??");
78 match assoc_item.kind {
80 _ => unimplemented!("Not possible??"),
82 let bound_vars = bound_vars_for_item(self.interner.tcx, def_id);
83 let binders = binders_for(self.interner, bound_vars);
85 let where_clauses = self.where_clauses_for(def_id, bound_vars);
86 let bounds = self.bounds_for(def_id, bound_vars);
88 Arc::new(chalk_solve::rust_ir::AssociatedTyDatum {
89 trait_id: chalk_ir::TraitId(trait_def_id),
92 binders: chalk_ir::Binders::new(
94 chalk_solve::rust_ir::AssociatedTyDatumBound { bounds, where_clauses },
101 trait_id: chalk_ir::TraitId<RustInterner<'tcx>>,
102 ) -> Arc<chalk_solve::rust_ir::TraitDatum<RustInterner<'tcx>>> {
103 use chalk_solve::rust_ir::WellKnownTrait::*;
105 let def_id = trait_id.0;
106 let trait_def = self.interner.tcx.trait_def(def_id);
108 let bound_vars = bound_vars_for_item(self.interner.tcx, def_id);
109 let binders = binders_for(self.interner, bound_vars);
111 let where_clauses = self.where_clauses_for(def_id, bound_vars);
113 let associated_ty_ids: Vec<_> = self
116 .associated_items(def_id)
117 .in_definition_order()
118 .filter(|i| i.kind == AssocKind::Type)
119 .map(|i| chalk_ir::AssocTypeId(i.def_id))
122 let lang_items = self.interner.tcx.lang_items();
123 let well_known = if lang_items.sized_trait() == Some(def_id) {
125 } else if lang_items.copy_trait() == Some(def_id) {
127 } else if lang_items.clone_trait() == Some(def_id) {
129 } else if lang_items.drop_trait() == Some(def_id) {
131 } else if lang_items.fn_trait() == Some(def_id) {
133 } else if lang_items.fn_once_trait() == Some(def_id) {
135 } else if lang_items.fn_mut_trait() == Some(def_id) {
137 } else if lang_items.unsize_trait() == Some(def_id) {
139 } else if lang_items.unpin_trait() == Some(def_id) {
141 } else if lang_items.coerce_unsized_trait() == Some(def_id) {
143 } else if lang_items.dispatch_from_dyn_trait() == Some(def_id) {
144 Some(DispatchFromDyn)
148 Arc::new(chalk_solve::rust_ir::TraitDatum {
150 binders: chalk_ir::Binders::new(
152 chalk_solve::rust_ir::TraitDatumBound { where_clauses },
154 flags: chalk_solve::rust_ir::TraitFlags {
155 auto: trait_def.has_auto_impl,
156 marker: trait_def.is_marker,
157 upstream: !def_id.is_local(),
158 fundamental: self.interner.tcx.has_attr(def_id, sym::fundamental),
159 non_enumerable: true,
169 adt_id: chalk_ir::AdtId<RustInterner<'tcx>>,
170 ) -> Arc<chalk_solve::rust_ir::AdtDatum<RustInterner<'tcx>>> {
171 let adt_def = adt_id.0;
173 let bound_vars = bound_vars_for_item(self.interner.tcx, adt_def.did());
174 let binders = binders_for(self.interner, bound_vars);
176 let where_clauses = self.where_clauses_for(adt_def.did(), bound_vars);
178 let variants: Vec<_> = adt_def
181 .map(|variant| chalk_solve::rust_ir::AdtVariantDatum {
185 .map(|field| field.ty(self.interner.tcx, bound_vars).lower_into(self.interner))
189 Arc::new(chalk_solve::rust_ir::AdtDatum {
191 binders: chalk_ir::Binders::new(
193 chalk_solve::rust_ir::AdtDatumBound { variants, where_clauses },
195 flags: chalk_solve::rust_ir::AdtFlags {
196 upstream: !adt_def.did().is_local(),
197 fundamental: adt_def.is_fundamental(),
198 phantom_data: adt_def.is_phantom_data(),
200 kind: match adt_def.adt_kind() {
201 ty::AdtKind::Struct => chalk_solve::rust_ir::AdtKind::Struct,
202 ty::AdtKind::Union => chalk_solve::rust_ir::AdtKind::Union,
203 ty::AdtKind::Enum => chalk_solve::rust_ir::AdtKind::Enum,
210 adt_id: chalk_ir::AdtId<RustInterner<'tcx>>,
211 ) -> Arc<chalk_solve::rust_ir::AdtRepr<RustInterner<'tcx>>> {
212 let adt_def = adt_id.0;
213 let int = |i| chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Int(i)).intern(self.interner);
214 let uint = |i| chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Uint(i)).intern(self.interner);
215 Arc::new(chalk_solve::rust_ir::AdtRepr {
216 c: adt_def.repr().c(),
217 packed: adt_def.repr().packed(),
218 int: adt_def.repr().int.map(|i| match i {
219 attr::IntType::SignedInt(ty) => match ty {
220 ast::IntTy::Isize => int(chalk_ir::IntTy::Isize),
221 ast::IntTy::I8 => int(chalk_ir::IntTy::I8),
222 ast::IntTy::I16 => int(chalk_ir::IntTy::I16),
223 ast::IntTy::I32 => int(chalk_ir::IntTy::I32),
224 ast::IntTy::I64 => int(chalk_ir::IntTy::I64),
225 ast::IntTy::I128 => int(chalk_ir::IntTy::I128),
227 attr::IntType::UnsignedInt(ty) => match ty {
228 ast::UintTy::Usize => uint(chalk_ir::UintTy::Usize),
229 ast::UintTy::U8 => uint(chalk_ir::UintTy::U8),
230 ast::UintTy::U16 => uint(chalk_ir::UintTy::U16),
231 ast::UintTy::U32 => uint(chalk_ir::UintTy::U32),
232 ast::UintTy::U64 => uint(chalk_ir::UintTy::U64),
233 ast::UintTy::U128 => uint(chalk_ir::UintTy::U128),
241 adt_id: chalk_ir::AdtId<RustInterner<'tcx>>,
242 ) -> Arc<chalk_solve::rust_ir::AdtSizeAlign> {
243 let tcx = self.interner.tcx;
244 let did = adt_id.0.did();
246 // Grab the ADT and the param we might need to calculate its layout
247 let param_env = tcx.param_env(did);
248 let adt_ty = tcx.type_of(did);
250 // The ADT is a 1-zst if it's a ZST and its alignment is 1.
251 // Mark the ADT as _not_ a 1-zst if there was a layout error.
252 let one_zst = if let Ok(layout) = tcx.layout_of(param_env.and(adt_ty)) {
253 layout.is_zst() && layout.align.abi.bytes() == 1
258 Arc::new(chalk_solve::rust_ir::AdtSizeAlign::from_one_zst(one_zst))
263 fn_def_id: chalk_ir::FnDefId<RustInterner<'tcx>>,
264 ) -> Arc<chalk_solve::rust_ir::FnDefDatum<RustInterner<'tcx>>> {
265 let def_id = fn_def_id.0;
266 let bound_vars = bound_vars_for_item(self.interner.tcx, def_id);
267 let binders = binders_for(self.interner, bound_vars);
269 let where_clauses = self.where_clauses_for(def_id, bound_vars);
271 let sig = self.interner.tcx.fn_sig(def_id);
272 let (inputs_and_output, iobinders, _) = crate::chalk::lowering::collect_bound_vars(
275 sig.inputs_and_output().subst(self.interner.tcx, bound_vars),
278 let argument_types = inputs_and_output[..inputs_and_output.len() - 1]
280 .map(|t| t.subst(self.interner.tcx, &bound_vars).lower_into(self.interner))
283 let return_type = inputs_and_output[inputs_and_output.len() - 1]
284 .subst(self.interner.tcx, &bound_vars)
285 .lower_into(self.interner);
287 let bound = chalk_solve::rust_ir::FnDefDatumBound {
288 inputs_and_output: chalk_ir::Binders::new(
290 chalk_solve::rust_ir::FnDefInputsAndOutputDatum { argument_types, return_type },
294 Arc::new(chalk_solve::rust_ir::FnDefDatum {
296 sig: sig.lower_into(self.interner),
297 binders: chalk_ir::Binders::new(binders, bound),
303 impl_id: chalk_ir::ImplId<RustInterner<'tcx>>,
304 ) -> Arc<chalk_solve::rust_ir::ImplDatum<RustInterner<'tcx>>> {
305 let def_id = impl_id.0;
306 let bound_vars = bound_vars_for_item(self.interner.tcx, def_id);
307 let binders = binders_for(self.interner, bound_vars);
309 let trait_ref = self.interner.tcx.impl_trait_ref(def_id).expect("not an impl");
310 let trait_ref = trait_ref.subst(self.interner.tcx, bound_vars);
312 let where_clauses = self.where_clauses_for(def_id, bound_vars);
314 let value = chalk_solve::rust_ir::ImplDatumBound {
315 trait_ref: trait_ref.lower_into(self.interner),
319 let associated_ty_value_ids: Vec<_> = self
322 .associated_items(def_id)
323 .in_definition_order()
324 .filter(|i| i.kind == AssocKind::Type)
325 .map(|i| chalk_solve::rust_ir::AssociatedTyValueId(i.def_id))
328 Arc::new(chalk_solve::rust_ir::ImplDatum {
329 polarity: self.interner.tcx.impl_polarity(def_id).lower_into(self.interner),
330 binders: chalk_ir::Binders::new(binders, value),
331 impl_type: chalk_solve::rust_ir::ImplType::Local,
332 associated_ty_value_ids,
338 trait_id: chalk_ir::TraitId<RustInterner<'tcx>>,
339 parameters: &[chalk_ir::GenericArg<RustInterner<'tcx>>],
340 _binders: &chalk_ir::CanonicalVarKinds<RustInterner<'tcx>>,
341 ) -> Vec<chalk_ir::ImplId<RustInterner<'tcx>>> {
342 let def_id = trait_id.0;
344 // FIXME(chalk): use TraitDef::for_each_relevant_impl, but that will
345 // require us to be able to interconvert `Ty<'tcx>`, and we're
348 let all_impls = self.interner.tcx.all_impls(def_id);
349 let matched_impls = all_impls.filter(|impl_def_id| {
350 use chalk_ir::could_match::CouldMatch;
351 let trait_ref = self.interner.tcx.impl_trait_ref(*impl_def_id).unwrap();
352 let bound_vars = bound_vars_for_item(self.interner.tcx, *impl_def_id);
354 let self_ty = trait_ref.self_ty();
355 let self_ty = self_ty.subst(self.interner.tcx, bound_vars);
356 let lowered_ty = self_ty.lower_into(self.interner);
358 parameters[0].assert_ty_ref(self.interner).could_match(
360 self.unification_database(),
365 let impls = matched_impls.map(chalk_ir::ImplId).collect();
369 fn impl_provided_for(
371 auto_trait_id: chalk_ir::TraitId<RustInterner<'tcx>>,
372 chalk_ty: &chalk_ir::TyKind<RustInterner<'tcx>>,
374 use chalk_ir::Scalar::*;
375 use chalk_ir::TyKind::*;
377 let trait_def_id = auto_trait_id.0;
378 let all_impls = self.interner.tcx.all_impls(trait_def_id);
379 for impl_def_id in all_impls {
380 let trait_ref = self.interner.tcx.impl_trait_ref(impl_def_id).unwrap();
381 let self_ty = trait_ref.self_ty();
382 let provides = match (self_ty.kind(), chalk_ty) {
383 (&ty::Adt(impl_adt_def, ..), Adt(id, ..)) => impl_adt_def.did() == id.0.did(),
384 (_, AssociatedType(_ty_id, ..)) => {
385 // FIXME(chalk): See https://github.com/rust-lang/rust/pull/77152#discussion_r494484774
388 (ty::Bool, Scalar(Bool)) => true,
389 (ty::Char, Scalar(Char)) => true,
390 (ty::Int(ty1), Scalar(Int(ty2))) => matches!(
392 (ty::IntTy::Isize, chalk_ir::IntTy::Isize)
393 | (ty::IntTy::I8, chalk_ir::IntTy::I8)
394 | (ty::IntTy::I16, chalk_ir::IntTy::I16)
395 | (ty::IntTy::I32, chalk_ir::IntTy::I32)
396 | (ty::IntTy::I64, chalk_ir::IntTy::I64)
397 | (ty::IntTy::I128, chalk_ir::IntTy::I128)
399 (ty::Uint(ty1), Scalar(Uint(ty2))) => matches!(
401 (ty::UintTy::Usize, chalk_ir::UintTy::Usize)
402 | (ty::UintTy::U8, chalk_ir::UintTy::U8)
403 | (ty::UintTy::U16, chalk_ir::UintTy::U16)
404 | (ty::UintTy::U32, chalk_ir::UintTy::U32)
405 | (ty::UintTy::U64, chalk_ir::UintTy::U64)
406 | (ty::UintTy::U128, chalk_ir::UintTy::U128)
408 (ty::Float(ty1), Scalar(Float(ty2))) => matches!(
410 (ty::FloatTy::F32, chalk_ir::FloatTy::F32)
411 | (ty::FloatTy::F64, chalk_ir::FloatTy::F64)
413 (&ty::Tuple(substs), Tuple(len, _)) => substs.len() == *len,
414 (&ty::Array(..), Array(..)) => true,
415 (&ty::Slice(..), Slice(..)) => true,
416 (&ty::RawPtr(type_and_mut), Raw(mutability, _)) => {
417 match (type_and_mut.mutbl, mutability) {
418 (ast::Mutability::Mut, chalk_ir::Mutability::Mut) => true,
419 (ast::Mutability::Mut, chalk_ir::Mutability::Not) => false,
420 (ast::Mutability::Not, chalk_ir::Mutability::Mut) => false,
421 (ast::Mutability::Not, chalk_ir::Mutability::Not) => true,
424 (&ty::Ref(.., mutability1), Ref(mutability2, ..)) => {
425 match (mutability1, mutability2) {
426 (ast::Mutability::Mut, chalk_ir::Mutability::Mut) => true,
427 (ast::Mutability::Mut, chalk_ir::Mutability::Not) => false,
428 (ast::Mutability::Not, chalk_ir::Mutability::Mut) => false,
429 (ast::Mutability::Not, chalk_ir::Mutability::Not) => true,
432 (&ty::Opaque(def_id, ..), OpaqueType(opaque_ty_id, ..)) => def_id == opaque_ty_id.0,
433 (&ty::FnDef(def_id, ..), FnDef(fn_def_id, ..)) => def_id == fn_def_id.0,
434 (&ty::Str, Str) => true,
435 (&ty::Never, Never) => true,
436 (&ty::Closure(def_id, ..), Closure(closure_id, _)) => def_id == closure_id.0,
437 (&ty::Foreign(def_id), Foreign(foreign_def_id)) => def_id == foreign_def_id.0,
438 (&ty::Error(..), Error) => false,
448 fn associated_ty_value(
450 associated_ty_id: chalk_solve::rust_ir::AssociatedTyValueId<RustInterner<'tcx>>,
451 ) -> Arc<chalk_solve::rust_ir::AssociatedTyValue<RustInterner<'tcx>>> {
452 let def_id = associated_ty_id.0;
453 let assoc_item = self.interner.tcx.associated_item(def_id);
454 let impl_id = assoc_item.container.id();
455 match assoc_item.kind {
456 AssocKind::Type => {}
457 _ => unimplemented!("Not possible??"),
460 let trait_item_id = assoc_item.trait_item_def_id.expect("assoc_ty with no trait version");
461 let bound_vars = bound_vars_for_item(self.interner.tcx, def_id);
462 let binders = binders_for(self.interner, bound_vars);
467 .subst(self.interner.tcx, bound_vars)
468 .lower_into(self.interner);
470 Arc::new(chalk_solve::rust_ir::AssociatedTyValue {
471 impl_id: chalk_ir::ImplId(impl_id),
472 associated_ty_id: chalk_ir::AssocTypeId(trait_item_id),
473 value: chalk_ir::Binders::new(
475 chalk_solve::rust_ir::AssociatedTyValueBound { ty },
480 fn custom_clauses(&self) -> Vec<chalk_ir::ProgramClause<RustInterner<'tcx>>> {
484 fn local_impls_to_coherence_check(
486 _trait_id: chalk_ir::TraitId<RustInterner<'tcx>>,
487 ) -> Vec<chalk_ir::ImplId<RustInterner<'tcx>>> {
493 opaque_ty_id: chalk_ir::OpaqueTyId<RustInterner<'tcx>>,
494 ) -> Arc<chalk_solve::rust_ir::OpaqueTyDatum<RustInterner<'tcx>>> {
495 let bound_vars = ty::fold::shift_vars(
497 bound_vars_for_item(self.interner.tcx, opaque_ty_id.0),
500 let where_clauses = self.where_clauses_for(opaque_ty_id.0, bound_vars);
502 let identity_substs = InternalSubsts::identity_for_item(self.interner.tcx, opaque_ty_id.0);
507 .explicit_item_bounds(opaque_ty_id.0)
509 .map(|(bound, _)| bound.subst(self.interner.tcx, &bound_vars))
511 bound.fold_with(&mut ReplaceOpaqueTyFolder {
512 tcx: self.interner.tcx,
515 binder_index: ty::INNERMOST,
518 .filter_map(|bound| {
520 Option<chalk_ir::QuantifiedWhereClause<RustInterner<'tcx>>>
521 >::lower_into(bound, self.interner)
525 // Binder for the bound variable representing the concrete impl Trait type.
526 let existential_binder = chalk_ir::VariableKinds::from1(
528 chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General),
531 let value = chalk_solve::rust_ir::OpaqueTyDatumBound {
532 bounds: chalk_ir::Binders::new(existential_binder.clone(), bounds),
533 where_clauses: chalk_ir::Binders::new(existential_binder, where_clauses),
536 let binders = binders_for(self.interner, bound_vars);
537 Arc::new(chalk_solve::rust_ir::OpaqueTyDatum {
539 bound: chalk_ir::Binders::new(binders, value),
543 fn program_clauses_for_env(
545 environment: &chalk_ir::Environment<RustInterner<'tcx>>,
546 ) -> chalk_ir::ProgramClauses<RustInterner<'tcx>> {
547 chalk_solve::program_clauses_for_env(self, environment)
550 fn well_known_trait_id(
552 well_known_trait: chalk_solve::rust_ir::WellKnownTrait,
553 ) -> Option<chalk_ir::TraitId<RustInterner<'tcx>>> {
554 use chalk_solve::rust_ir::WellKnownTrait::*;
555 let lang_items = self.interner.tcx.lang_items();
556 let def_id = match well_known_trait {
557 Sized => lang_items.sized_trait(),
558 Copy => lang_items.copy_trait(),
559 Clone => lang_items.clone_trait(),
560 Drop => lang_items.drop_trait(),
561 Fn => lang_items.fn_trait(),
562 FnMut => lang_items.fn_mut_trait(),
563 FnOnce => lang_items.fn_once_trait(),
564 Generator => lang_items.gen_trait(),
565 Unsize => lang_items.unsize_trait(),
566 Unpin => lang_items.unpin_trait(),
567 CoerceUnsized => lang_items.coerce_unsized_trait(),
568 DiscriminantKind => lang_items.discriminant_kind_trait(),
569 DispatchFromDyn => lang_items.dispatch_from_dyn_trait(),
571 def_id.map(chalk_ir::TraitId)
574 fn is_object_safe(&self, trait_id: chalk_ir::TraitId<RustInterner<'tcx>>) -> bool {
575 self.interner.tcx.is_object_safe(trait_id.0)
578 fn hidden_opaque_type(
580 _id: chalk_ir::OpaqueTyId<RustInterner<'tcx>>,
581 ) -> chalk_ir::Ty<RustInterner<'tcx>> {
582 // FIXME(chalk): actually get hidden ty
585 .mk_ty(ty::Tuple(self.interner.tcx.intern_type_list(&[])))
586 .lower_into(self.interner)
591 _closure_id: chalk_ir::ClosureId<RustInterner<'tcx>>,
592 substs: &chalk_ir::Substitution<RustInterner<'tcx>>,
593 ) -> chalk_solve::rust_ir::ClosureKind {
594 let kind = &substs.as_slice(self.interner)[substs.len(self.interner) - 3];
595 match kind.assert_ty_ref(self.interner).kind(self.interner) {
596 chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Int(int_ty)) => match int_ty {
597 chalk_ir::IntTy::I8 => chalk_solve::rust_ir::ClosureKind::Fn,
598 chalk_ir::IntTy::I16 => chalk_solve::rust_ir::ClosureKind::FnMut,
599 chalk_ir::IntTy::I32 => chalk_solve::rust_ir::ClosureKind::FnOnce,
600 _ => bug!("bad closure kind"),
602 _ => bug!("bad closure kind"),
606 fn closure_inputs_and_output(
608 _closure_id: chalk_ir::ClosureId<RustInterner<'tcx>>,
609 substs: &chalk_ir::Substitution<RustInterner<'tcx>>,
610 ) -> chalk_ir::Binders<chalk_solve::rust_ir::FnDefInputsAndOutputDatum<RustInterner<'tcx>>>
612 let sig = &substs.as_slice(self.interner)[substs.len(self.interner) - 2];
613 match sig.assert_ty_ref(self.interner).kind(self.interner) {
614 chalk_ir::TyKind::Function(f) => {
615 let substitution = f.substitution.0.as_slice(self.interner);
616 let return_type = substitution.last().unwrap().assert_ty_ref(self.interner).clone();
617 // Closure arguments are tupled
618 let argument_tuple = substitution[0].assert_ty_ref(self.interner);
619 let argument_types = match argument_tuple.kind(self.interner) {
620 chalk_ir::TyKind::Tuple(_len, substitution) => substitution
622 .map(|arg| arg.assert_ty_ref(self.interner))
625 _ => bug!("Expecting closure FnSig args to be tupled."),
628 chalk_ir::Binders::new(
629 chalk_ir::VariableKinds::from_iter(
631 (0..f.num_binders).map(|_| chalk_ir::VariableKind::Lifetime),
633 chalk_solve::rust_ir::FnDefInputsAndOutputDatum { argument_types, return_type },
636 _ => panic!("Invalid sig."),
642 _closure_id: chalk_ir::ClosureId<RustInterner<'tcx>>,
643 substs: &chalk_ir::Substitution<RustInterner<'tcx>>,
644 ) -> chalk_ir::Binders<chalk_ir::Ty<RustInterner<'tcx>>> {
645 let inputs_and_output = self.closure_inputs_and_output(_closure_id, substs);
646 let tuple = substs.as_slice(self.interner).last().unwrap().assert_ty_ref(self.interner);
647 inputs_and_output.map_ref(|_| tuple.clone())
650 fn closure_fn_substitution(
652 _closure_id: chalk_ir::ClosureId<RustInterner<'tcx>>,
653 substs: &chalk_ir::Substitution<RustInterner<'tcx>>,
654 ) -> chalk_ir::Substitution<RustInterner<'tcx>> {
655 let substitution = &substs.as_slice(self.interner)[0..substs.len(self.interner) - 3];
656 chalk_ir::Substitution::from_iter(self.interner, substitution)
661 _generator_id: chalk_ir::GeneratorId<RustInterner<'tcx>>,
662 ) -> Arc<chalk_solve::rust_ir::GeneratorDatum<RustInterner<'tcx>>> {
666 fn generator_witness_datum(
668 _generator_id: chalk_ir::GeneratorId<RustInterner<'tcx>>,
669 ) -> Arc<chalk_solve::rust_ir::GeneratorWitnessDatum<RustInterner<'tcx>>> {
673 fn unification_database(&self) -> &dyn chalk_ir::UnificationDatabase<RustInterner<'tcx>> {
677 fn discriminant_type(
679 _: chalk_ir::Ty<RustInterner<'tcx>>,
680 ) -> chalk_ir::Ty<RustInterner<'tcx>> {
685 impl<'tcx> chalk_ir::UnificationDatabase<RustInterner<'tcx>> for RustIrDatabase<'tcx> {
688 def_id: chalk_ir::FnDefId<RustInterner<'tcx>>,
689 ) -> chalk_ir::Variances<RustInterner<'tcx>> {
690 let variances = self.interner.tcx.variances_of(def_id.0);
691 chalk_ir::Variances::from_iter(
693 variances.iter().map(|v| v.lower_into(self.interner)),
699 adt_id: chalk_ir::AdtId<RustInterner<'tcx>>,
700 ) -> chalk_ir::Variances<RustInterner<'tcx>> {
701 let variances = self.interner.tcx.variances_of(adt_id.0.did());
702 chalk_ir::Variances::from_iter(
704 variances.iter().map(|v| v.lower_into(self.interner)),
709 /// Creates an `InternalSubsts` that maps each generic parameter to a higher-ranked
710 /// var bound at index `0`. For types, we use a `BoundVar` index equal to
711 /// the type parameter index. For regions, we use the `BoundRegionKind::BrNamed`
712 /// variant (which has a `DefId`).
713 fn bound_vars_for_item<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> SubstsRef<'tcx> {
714 InternalSubsts::for_item(tcx, def_id, |param, substs| match param.kind {
715 ty::GenericParamDefKind::Type { .. } => tcx
719 var: ty::BoundVar::from(param.index),
720 kind: ty::BoundTyKind::Param(param.name),
725 ty::GenericParamDefKind::Lifetime => {
726 let br = ty::BoundRegion {
727 var: ty::BoundVar::from_usize(substs.len()),
728 kind: ty::BrAnon(substs.len() as u32),
730 tcx.mk_region(ty::ReLateBound(ty::INNERMOST, br)).into()
733 ty::GenericParamDefKind::Const { .. } => tcx
734 .mk_const(ty::ConstS {
735 val: ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from(param.index)),
736 ty: tcx.type_of(param.def_id),
742 fn binders_for<'tcx>(
743 interner: RustInterner<'tcx>,
744 bound_vars: SubstsRef<'tcx>,
745 ) -> chalk_ir::VariableKinds<RustInterner<'tcx>> {
746 chalk_ir::VariableKinds::from_iter(
748 bound_vars.iter().map(|arg| match arg.unpack() {
749 ty::subst::GenericArgKind::Lifetime(_re) => chalk_ir::VariableKind::Lifetime,
750 ty::subst::GenericArgKind::Type(_ty) => {
751 chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General)
753 ty::subst::GenericArgKind::Const(c) => {
754 chalk_ir::VariableKind::Const(c.ty().lower_into(interner))
760 struct ReplaceOpaqueTyFolder<'tcx> {
762 opaque_ty_id: chalk_ir::OpaqueTyId<RustInterner<'tcx>>,
763 identity_substs: SubstsRef<'tcx>,
764 binder_index: ty::DebruijnIndex,
767 impl<'tcx> ty::TypeFolder<'tcx> for ReplaceOpaqueTyFolder<'tcx> {
768 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
772 fn fold_binder<T: TypeFoldable<'tcx>>(
774 t: ty::Binder<'tcx, T>,
775 ) -> ty::Binder<'tcx, T> {
776 self.binder_index.shift_in(1);
777 let t = t.super_fold_with(self);
778 self.binder_index.shift_out(1);
782 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
783 if let ty::Opaque(def_id, substs) = *ty.kind() {
784 if def_id == self.opaque_ty_id.0 && substs == self.identity_substs {
785 return self.tcx.mk_ty(ty::Bound(
787 ty::BoundTy::from(ty::BoundVar::from_u32(0)),