1use std::borrow::Borrow;
2use std::fmt::Debug;
3use std::hash::Hash;
4use std::ops::Deref;
5
6use rustc_ast_ir::Movability;
7use rustc_index::bit_set::DenseBitSet;
8
9use crate::fold::TypeFoldable;
10use crate::inherent::*;
11use crate::ir_print::IrPrint;
12use crate::lang_items::{SolverLangItem, SolverTraitLangItem};
13use crate::relate::Relate;
14use crate::solve::{
15 CanonicalInput, ExternalConstraintsData, PredefinedOpaquesData, QueryResult, inspect,
16};
17use crate::visit::{Flags, TypeVisitable};
18use crate::{self as ty, CanonicalParamEnvCacheEntry, search_graph};
19
20#[cfg_attr(feature = "nightly", rustc_diagnostic_item = "type_ir_interner")]
21pub trait Interner:
22 Sized
23 + Copy
24 + IrPrint<ty::AliasTy<Self>>
25 + IrPrint<ty::AliasTerm<Self>>
26 + IrPrint<ty::TraitRef<Self>>
27 + IrPrint<ty::TraitPredicate<Self>>
28 + IrPrint<ty::HostEffectPredicate<Self>>
29 + IrPrint<ty::ExistentialTraitRef<Self>>
30 + IrPrint<ty::ExistentialProjection<Self>>
31 + IrPrint<ty::ProjectionPredicate<Self>>
32 + IrPrint<ty::NormalizesTo<Self>>
33 + IrPrint<ty::SubtypePredicate<Self>>
34 + IrPrint<ty::CoercePredicate<Self>>
35 + IrPrint<ty::FnSig<Self>>
36 + IrPrint<ty::PatternKind<Self>>
37{
38 fn next_trait_solver_globally(self) -> bool {
39 true
40 }
41
42 type DefId: DefId<Self>;
43 type LocalDefId: Copy + Debug + Hash + Eq + Into<Self::DefId> + TypeFoldable<Self>;
44 type TraitId: DefId<Self> + Into<Self::DefId> + TryFrom<Self::DefId, Error: std::fmt::Debug>;
51 type Span: Span<Self>;
52
53 type GenericArgs: GenericArgs<Self>;
54 type GenericArgsSlice: Copy + Debug + Hash + Eq + SliceLike<Item = Self::GenericArg>;
55 type GenericArg: GenericArg<Self>;
56 type Term: Term<Self>;
57
58 type BoundVarKinds: Copy + Debug + Hash + Eq + SliceLike<Item = Self::BoundVarKind> + Default;
59 type BoundVarKind: Copy + Debug + Hash + Eq;
60
61 type PredefinedOpaques: Copy
62 + Debug
63 + Hash
64 + Eq
65 + TypeFoldable<Self>
66 + Deref<Target = PredefinedOpaquesData<Self>>;
67 fn mk_predefined_opaques_in_body(
68 self,
69 data: PredefinedOpaquesData<Self>,
70 ) -> Self::PredefinedOpaques;
71
72 type LocalDefIds: Copy
73 + Debug
74 + Hash
75 + Default
76 + Eq
77 + TypeVisitable<Self>
78 + SliceLike<Item = Self::LocalDefId>;
79
80 type CanonicalVarKinds: Copy
81 + Debug
82 + Hash
83 + Eq
84 + SliceLike<Item = ty::CanonicalVarKind<Self>>
85 + Default;
86 fn mk_canonical_var_kinds(
87 self,
88 kinds: &[ty::CanonicalVarKind<Self>],
89 ) -> Self::CanonicalVarKinds;
90
91 type ExternalConstraints: Copy
92 + Debug
93 + Hash
94 + Eq
95 + TypeFoldable<Self>
96 + Deref<Target = ExternalConstraintsData<Self>>;
97 fn mk_external_constraints(
98 self,
99 data: ExternalConstraintsData<Self>,
100 ) -> Self::ExternalConstraints;
101
102 type DepNodeIndex;
103 type Tracked<T: Debug + Clone>: Debug;
104 fn mk_tracked<T: Debug + Clone>(
105 self,
106 data: T,
107 dep_node: Self::DepNodeIndex,
108 ) -> Self::Tracked<T>;
109 fn get_tracked<T: Debug + Clone>(self, tracked: &Self::Tracked<T>) -> T;
110 fn with_cached_task<T>(self, task: impl FnOnce() -> T) -> (T, Self::DepNodeIndex);
111
112 type Ty: Ty<Self>;
114 type Tys: Tys<Self>;
115 type FnInputTys: Copy + Debug + Hash + Eq + SliceLike<Item = Self::Ty> + TypeVisitable<Self>;
116 type ParamTy: ParamLike;
117 type BoundTy: BoundVarLike<Self>;
118 type PlaceholderTy: PlaceholderLike<Self, Bound = Self::BoundTy>;
119 type Symbol: Copy + Hash + PartialEq + Eq + Debug;
120
121 type ErrorGuaranteed: Copy + Debug + Hash + Eq;
123 type BoundExistentialPredicates: BoundExistentialPredicates<Self>;
124 type AllocId: Copy + Debug + Hash + Eq;
125 type Pat: Copy
126 + Debug
127 + Hash
128 + Eq
129 + Debug
130 + Relate<Self>
131 + Flags
132 + IntoKind<Kind = ty::PatternKind<Self>>;
133 type PatList: Copy
134 + Debug
135 + Hash
136 + Default
137 + Eq
138 + TypeVisitable<Self>
139 + SliceLike<Item = Self::Pat>;
140 type Safety: Safety<Self>;
141 type Abi: Abi<Self>;
142
143 type Const: Const<Self>;
145 type ParamConst: Copy + Debug + Hash + Eq + ParamLike;
146 type BoundConst: BoundVarLike<Self>;
147 type PlaceholderConst: PlaceholderConst<Self>;
148 type ValueConst: ValueConst<Self>;
149 type ExprConst: ExprConst<Self>;
150 type ValTree: Copy + Debug + Hash + Eq;
151
152 type Region: Region<Self>;
154 type EarlyParamRegion: ParamLike;
155 type LateParamRegion: Copy + Debug + Hash + Eq;
156 type BoundRegion: BoundVarLike<Self>;
157 type PlaceholderRegion: PlaceholderLike<Self, Bound = Self::BoundRegion>;
158
159 type RegionAssumptions: Copy
160 + Debug
161 + Hash
162 + Eq
163 + SliceLike<Item = ty::OutlivesPredicate<Self, Self::GenericArg>>
164 + TypeFoldable<Self>;
165
166 type ParamEnv: ParamEnv<Self>;
168 type Predicate: Predicate<Self>;
169 type Clause: Clause<Self>;
170 type Clauses: Clauses<Self>;
171
172 fn with_global_cache<R>(self, f: impl FnOnce(&mut search_graph::GlobalCache<Self>) -> R) -> R;
173
174 fn canonical_param_env_cache_get_or_insert<R>(
175 self,
176 param_env: Self::ParamEnv,
177 f: impl FnOnce() -> CanonicalParamEnvCacheEntry<Self>,
178 from_entry: impl FnOnce(&CanonicalParamEnvCacheEntry<Self>) -> R,
179 ) -> R;
180
181 fn evaluation_is_concurrent(&self) -> bool;
182
183 fn expand_abstract_consts<T: TypeFoldable<Self>>(self, t: T) -> T;
184
185 type GenericsOf: GenericsOf<Self>;
186 fn generics_of(self, def_id: Self::DefId) -> Self::GenericsOf;
187
188 type VariancesOf: Copy + Debug + SliceLike<Item = ty::Variance>;
189 fn variances_of(self, def_id: Self::DefId) -> Self::VariancesOf;
190
191 fn opt_alias_variances(
192 self,
193 kind: impl Into<ty::AliasTermKind>,
194 def_id: Self::DefId,
195 ) -> Option<Self::VariancesOf>;
196
197 fn type_of(self, def_id: Self::DefId) -> ty::EarlyBinder<Self, Self::Ty>;
198 fn type_of_opaque_hir_typeck(self, def_id: Self::LocalDefId)
199 -> ty::EarlyBinder<Self, Self::Ty>;
200
201 type AdtDef: AdtDef<Self>;
202 fn adt_def(self, adt_def_id: Self::DefId) -> Self::AdtDef;
203
204 fn alias_ty_kind(self, alias: ty::AliasTy<Self>) -> ty::AliasTyKind;
205
206 fn alias_term_kind(self, alias: ty::AliasTerm<Self>) -> ty::AliasTermKind;
207
208 fn trait_ref_and_own_args_for_alias(
209 self,
210 def_id: Self::DefId,
211 args: Self::GenericArgs,
212 ) -> (ty::TraitRef<Self>, Self::GenericArgsSlice);
213
214 fn mk_args(self, args: &[Self::GenericArg]) -> Self::GenericArgs;
215
216 fn mk_args_from_iter<I, T>(self, args: I) -> T::Output
217 where
218 I: Iterator<Item = T>,
219 T: CollectAndApply<Self::GenericArg, Self::GenericArgs>;
220
221 fn check_args_compatible(self, def_id: Self::DefId, args: Self::GenericArgs) -> bool;
222
223 fn debug_assert_args_compatible(self, def_id: Self::DefId, args: Self::GenericArgs);
224
225 fn debug_assert_existential_args_compatible(self, def_id: Self::DefId, args: Self::GenericArgs);
228
229 fn mk_type_list_from_iter<I, T>(self, args: I) -> T::Output
230 where
231 I: Iterator<Item = T>,
232 T: CollectAndApply<Self::Ty, Self::Tys>;
233
234 fn parent(self, def_id: Self::DefId) -> Self::DefId;
235
236 fn recursion_limit(self) -> usize;
237
238 type Features: Features<Self>;
239 fn features(self) -> Self::Features;
240
241 fn coroutine_hidden_types(
242 self,
243 def_id: Self::DefId,
244 ) -> ty::EarlyBinder<Self, ty::Binder<Self, ty::CoroutineWitnessTypes<Self>>>;
245
246 fn fn_sig(
247 self,
248 def_id: Self::DefId,
249 ) -> ty::EarlyBinder<Self, ty::Binder<Self, ty::FnSig<Self>>>;
250
251 fn coroutine_movability(self, def_id: Self::DefId) -> Movability;
252
253 fn coroutine_for_closure(self, def_id: Self::DefId) -> Self::DefId;
254
255 fn generics_require_sized_self(self, def_id: Self::DefId) -> bool;
256
257 fn item_bounds(
258 self,
259 def_id: Self::DefId,
260 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
261
262 fn item_self_bounds(
263 self,
264 def_id: Self::DefId,
265 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
266
267 fn item_non_self_bounds(
268 self,
269 def_id: Self::DefId,
270 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
271
272 fn predicates_of(
273 self,
274 def_id: Self::DefId,
275 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
276
277 fn own_predicates_of(
278 self,
279 def_id: Self::DefId,
280 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
281
282 fn explicit_super_predicates_of(
283 self,
284 def_id: Self::TraitId,
285 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = (Self::Clause, Self::Span)>>;
286
287 fn explicit_implied_predicates_of(
288 self,
289 def_id: Self::DefId,
290 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = (Self::Clause, Self::Span)>>;
291
292 fn impl_super_outlives(
295 self,
296 impl_def_id: Self::DefId,
297 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
298
299 fn impl_is_const(self, def_id: Self::DefId) -> bool;
300 fn fn_is_const(self, def_id: Self::DefId) -> bool;
301 fn alias_has_const_conditions(self, def_id: Self::DefId) -> bool;
302 fn const_conditions(
303 self,
304 def_id: Self::DefId,
305 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = ty::Binder<Self, ty::TraitRef<Self>>>>;
306 fn explicit_implied_const_bounds(
307 self,
308 def_id: Self::DefId,
309 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = ty::Binder<Self, ty::TraitRef<Self>>>>;
310
311 fn impl_self_is_guaranteed_unsized(self, def_id: Self::DefId) -> bool;
312
313 fn has_target_features(self, def_id: Self::DefId) -> bool;
314
315 fn require_lang_item(self, lang_item: SolverLangItem) -> Self::DefId;
316
317 fn require_trait_lang_item(self, lang_item: SolverTraitLangItem) -> Self::TraitId;
318
319 fn is_lang_item(self, def_id: Self::DefId, lang_item: SolverLangItem) -> bool;
320
321 fn is_trait_lang_item(self, def_id: Self::TraitId, lang_item: SolverTraitLangItem) -> bool;
322
323 fn is_default_trait(self, def_id: Self::TraitId) -> bool;
324
325 fn as_lang_item(self, def_id: Self::DefId) -> Option<SolverLangItem>;
326
327 fn as_trait_lang_item(self, def_id: Self::TraitId) -> Option<SolverTraitLangItem>;
328
329 fn associated_type_def_ids(self, def_id: Self::DefId) -> impl IntoIterator<Item = Self::DefId>;
330
331 fn for_each_relevant_impl(
332 self,
333 trait_def_id: Self::TraitId,
334 self_ty: Self::Ty,
335 f: impl FnMut(Self::DefId),
336 );
337
338 fn has_item_definition(self, def_id: Self::DefId) -> bool;
339
340 fn impl_specializes(self, impl_def_id: Self::DefId, victim_def_id: Self::DefId) -> bool;
341
342 fn impl_is_default(self, impl_def_id: Self::DefId) -> bool;
343
344 fn impl_trait_ref(self, impl_def_id: Self::DefId) -> ty::EarlyBinder<Self, ty::TraitRef<Self>>;
345
346 fn impl_polarity(self, impl_def_id: Self::DefId) -> ty::ImplPolarity;
347
348 fn trait_is_auto(self, trait_def_id: Self::TraitId) -> bool;
349
350 fn trait_is_coinductive(self, trait_def_id: Self::TraitId) -> bool;
351
352 fn trait_is_alias(self, trait_def_id: Self::TraitId) -> bool;
353
354 fn trait_is_dyn_compatible(self, trait_def_id: Self::TraitId) -> bool;
355
356 fn trait_is_fundamental(self, def_id: Self::TraitId) -> bool;
357
358 fn trait_may_be_implemented_via_object(self, trait_def_id: Self::TraitId) -> bool;
359
360 fn trait_is_unsafe(self, trait_def_id: Self::TraitId) -> bool;
362
363 fn is_impl_trait_in_trait(self, def_id: Self::DefId) -> bool;
364
365 fn delay_bug(self, msg: impl ToString) -> Self::ErrorGuaranteed;
366
367 fn is_general_coroutine(self, coroutine_def_id: Self::DefId) -> bool;
368 fn coroutine_is_async(self, coroutine_def_id: Self::DefId) -> bool;
369 fn coroutine_is_gen(self, coroutine_def_id: Self::DefId) -> bool;
370 fn coroutine_is_async_gen(self, coroutine_def_id: Self::DefId) -> bool;
371
372 type UnsizingParams: Deref<Target = DenseBitSet<u32>>;
373 fn unsizing_params_for_adt(self, adt_def_id: Self::DefId) -> Self::UnsizingParams;
374
375 fn anonymize_bound_vars<T: TypeFoldable<Self>>(
376 self,
377 binder: ty::Binder<Self, T>,
378 ) -> ty::Binder<Self, T>;
379
380 fn opaque_types_defined_by(self, defining_anchor: Self::LocalDefId) -> Self::LocalDefIds;
381
382 fn opaque_types_and_coroutines_defined_by(
383 self,
384 defining_anchor: Self::LocalDefId,
385 ) -> Self::LocalDefIds;
386
387 type Probe: Debug + Hash + Eq + Borrow<inspect::Probe<Self>>;
388 fn mk_probe(self, probe: inspect::Probe<Self>) -> Self::Probe;
389 fn evaluate_root_goal_for_proof_tree_raw(
390 self,
391 canonical_goal: CanonicalInput<Self>,
392 ) -> (QueryResult<Self>, Self::Probe);
393}
394
395pub trait CollectAndApply<T, R>: Sized {
404 type Output;
405
406 fn collect_and_apply<I, F>(iter: I, f: F) -> Self::Output
411 where
412 I: Iterator<Item = Self>,
413 F: FnOnce(&[T]) -> R;
414}
415
416impl<T, R> CollectAndApply<T, R> for T {
418 type Output = R;
419
420 fn collect_and_apply<I, F>(mut iter: I, f: F) -> R
422 where
423 I: Iterator<Item = T>,
424 F: FnOnce(&[T]) -> R,
425 {
426 let Some(t0) = iter.next() else {
430 return f(&[]);
431 };
432
433 let Some(t1) = iter.next() else {
434 return f(&[t0]);
435 };
436
437 let Some(t2) = iter.next() else {
438 return f(&[t0, t1]);
439 };
440
441 let Some(t3) = iter.next() else {
442 return f(&[t0, t1, t2]);
443 };
444
445 let Some(t4) = iter.next() else {
446 return f(&[t0, t1, t2, t3]);
447 };
448
449 let Some(t5) = iter.next() else {
450 return f(&[t0, t1, t2, t3, t4]);
451 };
452
453 let Some(t6) = iter.next() else {
454 return f(&[t0, t1, t2, t3, t4, t5]);
455 };
456
457 let Some(t7) = iter.next() else {
458 return f(&[t0, t1, t2, t3, t4, t5, t6]);
459 };
460
461 let Some(t8) = iter.next() else {
462 return f(&[t0, t1, t2, t3, t4, t5, t6, t7]);
463 };
464
465 f(&[t0, t1, t2, t3, t4, t5, t6, t7, t8].into_iter().chain(iter).collect::<Vec<_>>())
466 }
467}
468
469impl<T, R, E> CollectAndApply<T, R> for Result<T, E> {
472 type Output = Result<R, E>;
473
474 fn collect_and_apply<I, F>(mut iter: I, f: F) -> Result<R, E>
476 where
477 I: Iterator<Item = Result<T, E>>,
478 F: FnOnce(&[T]) -> R,
479 {
480 let Some(t0) = iter.next() else {
484 return Ok(f(&[]));
485 };
486 let t0 = t0?;
487
488 let Some(t1) = iter.next() else {
489 return Ok(f(&[t0]));
490 };
491 let t1 = t1?;
492
493 let Some(t2) = iter.next() else {
494 return Ok(f(&[t0, t1]));
495 };
496 let t2 = t2?;
497
498 let Some(t3) = iter.next() else {
499 return Ok(f(&[t0, t1, t2]));
500 };
501 let t3 = t3?;
502
503 let Some(t4) = iter.next() else {
504 return Ok(f(&[t0, t1, t2, t3]));
505 };
506 let t4 = t4?;
507
508 let Some(t5) = iter.next() else {
509 return Ok(f(&[t0, t1, t2, t3, t4]));
510 };
511 let t5 = t5?;
512
513 let Some(t6) = iter.next() else {
514 return Ok(f(&[t0, t1, t2, t3, t4, t5]));
515 };
516 let t6 = t6?;
517
518 let Some(t7) = iter.next() else {
519 return Ok(f(&[t0, t1, t2, t3, t4, t5, t6]));
520 };
521 let t7 = t7?;
522
523 let Some(t8) = iter.next() else {
524 return Ok(f(&[t0, t1, t2, t3, t4, t5, t6, t7]));
525 };
526 let t8 = t8?;
527
528 Ok(f(&[Ok(t0), Ok(t1), Ok(t2), Ok(t3), Ok(t4), Ok(t5), Ok(t6), Ok(t7), Ok(t8)]
529 .into_iter()
530 .chain(iter)
531 .collect::<Result<Vec<_>, _>>()?))
532 }
533}
534
535impl<I: Interner> search_graph::Cx for I {
536 type Input = CanonicalInput<I>;
537 type Result = QueryResult<I>;
538
539 type DepNodeIndex = I::DepNodeIndex;
540 type Tracked<T: Debug + Clone> = I::Tracked<T>;
541 fn mk_tracked<T: Debug + Clone>(
542 self,
543 data: T,
544 dep_node_index: I::DepNodeIndex,
545 ) -> I::Tracked<T> {
546 I::mk_tracked(self, data, dep_node_index)
547 }
548 fn get_tracked<T: Debug + Clone>(self, tracked: &I::Tracked<T>) -> T {
549 I::get_tracked(self, tracked)
550 }
551 fn with_cached_task<T>(self, task: impl FnOnce() -> T) -> (T, I::DepNodeIndex) {
552 I::with_cached_task(self, task)
553 }
554 fn with_global_cache<R>(self, f: impl FnOnce(&mut search_graph::GlobalCache<Self>) -> R) -> R {
555 I::with_global_cache(self, f)
556 }
557 fn evaluation_is_concurrent(&self) -> bool {
558 self.evaluation_is_concurrent()
559 }
560}