1use std::{debug_assert_matches, fmt};
4
5use rustc_attr_ir::lang_items::LangItem;
6use rustc_data_structures::intern::Interned;
7use rustc_errors::ErrorGuaranteed;
8use rustc_hir as hir;
9use rustc_hir::def::{CtorKind, DefKind};
10use rustc_hir::def_id::{DefId, LocalDefId};
11use rustc_span::{DUMMY_SP, Span, Symbol, bug};
12use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverProjectionLangItem, SolverTraitLangItem};
13use rustc_type_ir::solve::CanonicalInputData;
14use rustc_type_ir::{
15 BoundVar, CollectAndApply, DebruijnIndex, IncludeSizedImplicitBounds, Interner, RegionVid,
16 TypeFoldable, Unnormalized, VisitorResult, WithCachedTypeInfo, search_graph, try_visit,
17};
18
19use crate::dep_graph::{DepKind, DepNodeIndex};
20use crate::infer::canonical::CanonicalVarKinds;
21use crate::mir::interpret::Scalar;
22use crate::traits::cache::WithDepNode;
23use crate::traits::solve::{
24 self, CanonicalInput, ExternalConstraints, ExternalConstraintsData, QueryResult, inspect,
25};
26use crate::ty::{
27 self, BoundRegion, Clause, Const, List, ParamTy, Pattern, PolyExistentialPredicate, Predicate,
28 Region, RegionKind, RequiredDepth, Ty, TyCtxt,
29};
30
31#[allow(rustc::usage_of_ty_tykind)]
32impl<'tcx> Interner for TyCtxt<'tcx> {
33 fn next_trait_solver_globally(self) -> bool {
34 self.next_trait_solver_globally()
35 }
36
37 type DefId = DefId;
38 type LocalDefId = LocalDefId;
39 type TraitId = DefId;
40 type ForeignId = DefId;
41 type FunctionId = DefId;
42 type ClosureId = DefId;
43 type CoroutineClosureId = DefId;
44 type CoroutineId = DefId;
45 type AdtId = DefId;
46 type ImplId = DefId;
47 type AnonConstId = DefId;
48 type TraitAssocTyId = DefId;
49 type TraitAssocConstId = DefId;
50 type TraitAssocTermId = DefId;
51 type OpaqueTyId = DefId;
52 type LocalOpaqueTyId = LocalDefId;
53 type FreeTyAliasId = DefId;
54 type FreeConstAliasId = DefId;
55 type FreeTermAliasId = DefId;
56 type ImplOrTraitAssocTyId = DefId;
57 type ImplOrTraitAssocConstId = DefId;
58 type ImplOrTraitAssocTermId = DefId;
59 type InherentAssocTyId = DefId;
60 type InherentAssocConstId = DefId;
61 type InherentAssocTermId = DefId;
62 type Span = Span;
63
64 type GenericArgs = ty::GenericArgs<'tcx>;
65
66 type GenericArgsSlice<'a>
67 = &'a [ty::GenericArg<'tcx>]
68 where
69 Self: 'a;
70 type GenericArg = ty::GenericArg<'tcx>;
71 type Term = ty::Term<'tcx>;
72 type BoundVarKinds = &'tcx List<ty::BoundVariableKind<'tcx>>;
73 type TypingEnv = ty::TypingEnv<'tcx>;
74
75 type PredefinedOpaques = solve::PredefinedOpaques<'tcx>;
76
77 fn mk_predefined_opaques_in_body(
78 self,
79 data: &[(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)],
80 ) -> Self::PredefinedOpaques {
81 self.mk_predefined_opaques_in_body(data)
82 }
83 type LocalDefIds = &'tcx ty::List<LocalDefId>;
84 type CanonicalVarKinds = CanonicalVarKinds<'tcx>;
85 fn mk_canonical_var_kinds(
86 self,
87 kinds: &[ty::CanonicalVarKind<Self>],
88 ) -> Self::CanonicalVarKinds {
89 self.mk_canonical_var_kinds(kinds)
90 }
91
92 type ExternalConstraints = ExternalConstraints<'tcx>;
93 fn mk_external_constraints(
94 self,
95 data: ExternalConstraintsData<Self>,
96 ) -> ExternalConstraints<'tcx> {
97 self.mk_external_constraints(data)
98 }
99 type DepNodeIndex = DepNodeIndex;
100 fn with_cached_task<T>(self, task: impl FnOnce() -> T) -> (T, DepNodeIndex) {
101 self.dep_graph.with_anon_task(self, DepKind::TraitSelect, task)
102 }
103 type Ty = Ty<'tcx>;
104 type Tys = &'tcx List<Ty<'tcx>>;
105
106 type FnInputTys = &'tcx [Ty<'tcx>];
107 type ParamTy = ParamTy;
108 type Symbol = Symbol;
109
110 type ErrorGuaranteed = ErrorGuaranteed;
111 type BoundExistentialPredicates = &'tcx List<PolyExistentialPredicate<'tcx>>;
112
113 type AllocId = crate::mir::interpret::AllocId;
114 type Pat = Pattern<'tcx>;
115 type PatList = &'tcx List<Pattern<'tcx>>;
116 type Safety = hir::Safety;
117 type Consts = &'tcx List<ty::Const<'tcx>>;
118
119 type ParamConst = ty::ParamConst;
120 type ValueConst = ty::Value<'tcx>;
121 type ExprConst = ty::Expr<'tcx>;
122 type ValTree = ty::ValTree<'tcx>;
123 type ScalarInt = ty::ScalarInt;
124 type Scalar = Scalar;
125 type InternedRegionKind = Interned<'tcx, ty::RegionKind<'tcx>>;
126 type InternedConstKind = Interned<'tcx, WithCachedTypeInfo<ty::ConstKind<'tcx>>>;
127 type EarlyParamRegion = ty::EarlyParamRegion;
128 type LateParamRegionKind = ty::LateParamRegionKind;
129
130 type RegionAssumptions = &'tcx ty::List<ty::ArgOutlivesClause<'tcx>>;
131
132 type ParamEnv = ty::ParamEnv<'tcx>;
133 type Predicate = Predicate<'tcx>;
134
135 type Clause = Clause<'tcx>;
136 type Clauses = ty::Clauses<'tcx>;
137
138 type Tracked<T: fmt::Debug + Clone> = WithDepNode<T>;
139 fn mk_tracked<T: fmt::Debug + Clone>(
140 self,
141 data: T,
142 dep_node: DepNodeIndex,
143 ) -> Self::Tracked<T> {
144 WithDepNode::new(dep_node, data)
145 }
146 fn get_tracked<T: fmt::Debug + Clone>(self, tracked: &Self::Tracked<T>) -> T {
147 tracked.get(self)
148 }
149
150 fn with_global_cache<R>(self, f: impl FnOnce(&mut search_graph::GlobalCache<Self>) -> R) -> R {
151 f(&mut *self.caches.new_solver_evaluation_cache.lock())
152 }
153
154 fn with_canonical_param_env_cache<R>(
155 self,
156 f: impl FnOnce(&mut ty::CanonicalParamEnvCache<Self>) -> R,
157 ) -> R {
158 f(&mut *self.caches.new_solver_canonical_param_env_cache.lock())
159 }
160
161 fn assert_evaluation_is_concurrent(&self) {
162 }
165
166 fn erase_and_anonymize_regions<T: TypeFoldable<Self>>(self, value: T) -> T {
167 self.erase_and_anonymize_regions(value)
168 }
169
170 fn const_eval_resolve_for_typeck(
171 self,
172 typing_env: Self::TypingEnv,
173 ct: ty::AliasConst<'tcx>,
174 span: Self::Span,
175 ) -> ty::ConstEvalResult<Self> {
176 use crate::mir::interpret::ErrorHandled;
177
178 match self.const_eval_resolve_for_typeck(typing_env, ct, span) {
179 Ok(Ok(vt)) => Ok(vt),
180 Ok(Err(ty)) => Err(ty::ConstEvalErr::NonValtreeable(ty)),
181 Err(ErrorHandled::Reported(e, _)) => Err(ty::ConstEvalErr::Reported(e.into())),
182 Err(ErrorHandled::TooGeneric(_)) => Err(ty::ConstEvalErr::TooGeneric),
183 }
184 }
185
186 fn expand_abstract_consts<T: TypeFoldable<TyCtxt<'tcx>>>(self, t: T) -> T {
187 self.expand_abstract_consts(t)
188 }
189
190 type GenericsOf = &'tcx ty::Generics;
191
192 fn generics_of(self, def_id: DefId) -> &'tcx ty::Generics {
193 self.generics_of(def_id)
194 }
195
196 type VariancesOf = &'tcx [ty::Variance];
197
198 fn variances_of(self, def_id: DefId) -> Self::VariancesOf {
199 self.variances_of(def_id)
200 }
201
202 fn opt_alias_variances(
203 self,
204 kind: impl Into<ty::AliasTermKind<'tcx>>,
205 ) -> Option<&'tcx [ty::Variance]> {
206 self.opt_alias_variances(kind)
207 }
208
209 fn type_of(self, def_id: DefId) -> ty::EarlyBinder<'tcx, Ty<'tcx>> {
210 self.type_of(def_id)
211 }
212 fn type_of_opaque_hir_typeck(self, def_id: LocalDefId) -> ty::EarlyBinder<'tcx, Ty<'tcx>> {
213 self.type_of_opaque_hir_typeck(def_id)
214 }
215 fn is_direct_const(self, alias: ty::AliasConstKind<'tcx>) -> bool {
216 match alias {
217 ty::AliasConstKind::Projection { def_id }
218 | ty::AliasConstKind::InherentSelf { def_id }
219 | ty::AliasConstKind::InherentImpl { def_id }
220 | ty::AliasConstKind::Free { def_id } => self.is_direct_const(def_id),
221 ty::AliasConstKind::Anon { .. } => false,
222 }
223 }
224 fn const_of_item(
225 self,
226 alias: ty::AliasConstKind<'tcx>,
227 ) -> Option<ty::EarlyBinder<'tcx, Const<'tcx>>> {
228 match alias {
229 ty::AliasConstKind::Projection { def_id }
230 | ty::AliasConstKind::InherentSelf { def_id }
231 | ty::AliasConstKind::InherentImpl { def_id }
232 | ty::AliasConstKind::Free { def_id } => self.const_of_item(def_id),
233 ty::AliasConstKind::Anon { .. } => None,
234 }
235 }
236
237 fn anon_const_kind(self, def_id: DefId) -> ty::AnonConstKind {
238 self.anon_const_kind(def_id)
239 }
240
241 fn def_span(self, def_id: DefId) -> Span {
242 self.def_span(def_id)
243 }
244
245 type AdtDef = ty::AdtDef<'tcx>;
246 fn adt_def(self, adt_def_id: DefId) -> Self::AdtDef {
247 self.adt_def(adt_def_id)
248 }
249
250 fn trait_ref_and_own_args_for_alias<'a>(
251 self,
252 def_id: DefId,
253 args: &'a ty::GenericArgs<'tcx>,
254 ) -> (ty::TraitRef<'tcx>, &'a [ty::GenericArg<'tcx>]) {
255 if true {
{
match self.def_kind(def_id) {
DefKind::AssocTy | DefKind::AssocConst => {}
ref left_val => {
::core::panicking::assert_matches_failed(left_val,
"DefKind::AssocTy | DefKind::AssocConst",
::core::option::Option::None);
}
}
};
};debug_assert_matches!(self.def_kind(def_id), DefKind::AssocTy | DefKind::AssocConst);
256 let trait_def_id = self.parent(def_id);
257 if true {
{
match self.def_kind(trait_def_id) {
DefKind::Trait => {}
ref left_val => {
::core::panicking::assert_matches_failed(left_val,
"DefKind::Trait", ::core::option::Option::None);
}
}
};
};debug_assert_matches!(self.def_kind(trait_def_id), DefKind::Trait);
258 let trait_ref = ty::TraitRef::from_assoc(self, trait_def_id, *args);
259 (trait_ref, &args.as_slice()[trait_ref.args.len()..])
260 }
261
262 fn mk_args(self, args: &[Self::GenericArg]) -> ty::GenericArgs<'tcx> {
263 self.mk_args(args)
264 }
265
266 fn mk_args_from_iter<I, T>(self, args: I) -> T::Output
267 where
268 I: Iterator<Item = T>,
269 T: CollectAndApply<Self::GenericArg, ty::GenericArgs<'tcx>>,
270 {
271 self.mk_args_from_iter(args)
272 }
273
274 fn check_alias_term_args_compatible(
275 self,
276 kind: ty::AliasTermKind<'tcx>,
277 args: ty::GenericArgs<'tcx>,
278 ) -> bool {
279 self.check_alias_term_args_compatible(kind, args)
280 }
281
282 fn debug_assert_args_compatible(self, def_id: DefId, args: ty::GenericArgs<'tcx>) {
283 self.debug_assert_args_compatible(def_id, args);
284 }
285
286 fn debug_assert_alias_term_args_compatible(
287 self,
288 kind: ty::AliasTermKind<'tcx>,
289 args: ty::GenericArgs<'tcx>,
290 ) {
291 self.debug_assert_alias_term_args_compatible(kind, args);
292 }
293
294 fn debug_assert_existential_args_compatible(
298 self,
299 def_id: Self::DefId,
300 args: Self::GenericArgs,
301 ) {
302 if truecfg!(debug_assertions) {
305 self.debug_assert_args_compatible(
306 def_id,
307 self.mk_args_from_iter(
308 [self.types.trait_object_dummy_self.into()]
309 .into_iter()
310 .chain(args.as_slice().iter().copied()),
311 ),
312 );
313 }
314 }
315
316 fn mk_type_list_from_iter<I, T>(self, args: I) -> T::Output
317 where
318 I: Iterator<Item = T>,
319 T: CollectAndApply<Ty<'tcx>, &'tcx List<Ty<'tcx>>>,
320 {
321 self.mk_type_list_from_iter(args)
322 }
323
324 fn mk_ct_from_kind(self, kind: ty::ConstKind<'tcx>) -> ty::Const<'tcx> {
325 self.mk_ct_from_kind(kind)
326 }
327
328 fn projection_parent(self, def_id: Self::TraitAssocTermId) -> Self::TraitId {
329 self.parent(def_id)
330 }
331
332 fn impl_or_trait_assoc_term_parent(self, def_id: Self::ImplOrTraitAssocTyId) -> DefId {
333 self.parent(def_id)
334 }
335
336 fn inherent_alias_term_parent(self, def_id: Self::InherentAssocTermId) -> Self::ImplId {
337 self.parent(def_id)
338 }
339
340 fn recursion_limit(self) -> usize {
341 self.recursion_limit().0
342 }
343
344 type Features = &'tcx rustc_feature::Features;
345
346 fn features(self) -> Self::Features {
347 self.features()
348 }
349
350 fn assumptions_on_binders(self) -> bool {
351 self.assumptions_on_binders()
352 }
353
354 fn renormalize_rigid_aliases(self) -> bool {
355 self.renormalize_rigid_aliases()
356 }
357
358 fn coroutine_hidden_types(
359 self,
360 def_id: DefId,
361 ) -> ty::EarlyBinder<'tcx, ty::Binder<'tcx, ty::CoroutineWitnessTypes<TyCtxt<'tcx>>>> {
362 self.coroutine_hidden_types(def_id)
363 }
364
365 fn fn_sig(self, def_id: DefId) -> ty::EarlyBinder<'tcx, ty::PolyFnSig<'tcx>> {
366 self.fn_sig(def_id)
367 }
368
369 fn coroutine_movability(self, def_id: DefId) -> rustc_ast::Movability {
370 self.coroutine_movability(def_id)
371 }
372
373 fn coroutine_for_closure(self, def_id: DefId) -> DefId {
374 self.coroutine_for_closure(def_id)
375 }
376
377 fn generics_require_sized_self(self, def_id: DefId) -> bool {
378 self.generics_require_sized_self(def_id)
379 }
380
381 fn item_bounds(
382 self,
383 def_id: DefId,
384 ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
385 self.item_bounds(def_id).map_bound(IntoIterator::into_iter)
386 }
387
388 fn item_self_bounds(
389 self,
390 def_id: DefId,
391 ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
392 self.item_self_bounds(def_id).map_bound(IntoIterator::into_iter)
393 }
394
395 fn item_non_self_bounds(
396 self,
397 def_id: DefId,
398 ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
399 self.item_non_self_bounds(def_id).map_bound(IntoIterator::into_iter)
400 }
401
402 fn clauses_of(
403 self,
404 def_id: DefId,
405 ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
406 ty::EarlyBinder::bind_iter(
407 self.clauses_of(def_id)
408 .instantiate_identity(self)
409 .clauses
410 .into_iter()
411 .map(Unnormalized::skip_normalization),
412 )
413 }
414
415 fn own_clauses_of(
416 self,
417 def_id: DefId,
418 ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
419 ty::EarlyBinder::bind_iter(
420 self.clauses_of(def_id)
421 .instantiate_own_identity()
422 .map(|(clause, _)| clause.skip_normalization()),
423 )
424 }
425
426 fn explicit_super_clauses_of(
427 self,
428 def_id: DefId,
429 ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = (ty::Clause<'tcx>, Span)>> {
430 self.explicit_super_clauses_of(def_id).map_bound(|preds| preds.into_iter().copied())
431 }
432
433 fn explicit_implied_clauses_of(
434 self,
435 def_id: DefId,
436 ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = (ty::Clause<'tcx>, Span)>> {
437 self.explicit_implied_clauses_of(def_id).map_bound(|preds| preds.into_iter().copied())
438 }
439
440 fn impl_super_outlives(
441 self,
442 impl_def_id: DefId,
443 ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
444 self.impl_super_outlives(impl_def_id)
445 }
446
447 fn supertrait_def_ids(self, trait_def_id: DefId) -> impl Iterator<Item = DefId> {
448 rustc_type_ir::elaborate::supertrait_def_ids(self, trait_def_id)
449 }
450
451 fn impl_is_const(self, def_id: DefId) -> bool {
452 if true {
{
match self.def_kind(def_id) {
DefKind::Impl { of_trait: true } => {}
ref left_val => {
::core::panicking::assert_matches_failed(left_val,
"DefKind::Impl { of_trait: true }",
::core::option::Option::None);
}
}
};
};debug_assert_matches!(self.def_kind(def_id), DefKind::Impl { of_trait: true });
453 self.is_conditionally_const(def_id)
454 }
455
456 fn fn_is_const(self, def_id: DefId) -> bool {
457 if true {
{
match self.def_kind(def_id) {
DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn) =>
{}
ref left_val => {
::core::panicking::assert_matches_failed(left_val,
"DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn)",
::core::option::Option::None);
}
}
};
};debug_assert_matches!(
458 self.def_kind(def_id),
459 DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn)
460 );
461 self.is_conditionally_const(def_id)
462 }
463
464 fn closure_is_const(self, def_id: DefId) -> bool {
465 if true {
{
match self.def_kind(def_id) {
DefKind::Closure => {}
ref left_val => {
::core::panicking::assert_matches_failed(left_val,
"DefKind::Closure", ::core::option::Option::None);
}
}
};
};debug_assert_matches!(self.def_kind(def_id), DefKind::Closure);
466 #[allow(non_exhaustive_omitted_patterns)] match self.constness(def_id) {
hir::Constness::Const { always: false } => true,
_ => false,
}matches!(self.constness(def_id), hir::Constness::Const { always: false })
467 }
468
469 fn alias_has_const_conditions(self, def_id: DefId) -> bool {
470 if true {
{
match self.def_kind(def_id) {
DefKind::AssocTy | DefKind::OpaqueTy => {}
ref left_val => {
::core::panicking::assert_matches_failed(left_val,
"DefKind::AssocTy | DefKind::OpaqueTy",
::core::option::Option::None);
}
}
};
};debug_assert_matches!(self.def_kind(def_id), DefKind::AssocTy | DefKind::OpaqueTy);
471 self.is_conditionally_const(def_id)
472 }
473
474 fn const_conditions(
475 self,
476 def_id: DefId,
477 ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Binder<'tcx, ty::TraitRef<'tcx>>>> {
478 ty::EarlyBinder::bind_iter(
479 self.const_conditions(def_id)
480 .instantiate_identity(self)
481 .into_iter()
482 .map(|(c, _)| c.skip_normalization()),
483 )
484 }
485
486 fn explicit_implied_const_bounds(
487 self,
488 def_id: DefId,
489 ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Binder<'tcx, ty::TraitRef<'tcx>>>> {
490 ty::EarlyBinder::bind_iter(
491 self.explicit_implied_const_bounds(def_id)
492 .iter_identity_copied()
493 .map(Unnormalized::skip_normalization)
494 .map(|(c, _)| c),
495 )
496 }
497
498 fn impl_self_is_guaranteed_unsized(self, impl_def_id: DefId) -> bool {
499 self.impl_self_is_guaranteed_unsized(impl_def_id)
500 }
501
502 fn has_target_features(self, def_id: DefId) -> bool {
503 !self.codegen_fn_attrs(def_id).target_features.is_empty()
504 }
505
506 fn require_projection_lang_item(self, lang_item: SolverProjectionLangItem) -> DefId {
507 self.require_lang_item(solver_lang_item_to_lang_item(lang_item), DUMMY_SP)
508 }
509
510 fn require_trait_lang_item(self, lang_item: SolverTraitLangItem) -> DefId {
511 self.require_lang_item(solver_trait_lang_item_to_lang_item(lang_item), DUMMY_SP)
512 }
513
514 fn require_adt_lang_item(self, lang_item: SolverAdtLangItem) -> DefId {
515 self.require_lang_item(solver_adt_lang_item_to_lang_item(lang_item), DUMMY_SP)
516 }
517
518 fn is_projection_lang_item(self, def_id: DefId, lang_item: SolverProjectionLangItem) -> bool {
519 self.is_lang_item(def_id, solver_lang_item_to_lang_item(lang_item))
520 }
521
522 fn is_trait_lang_item(self, def_id: DefId, lang_item: SolverTraitLangItem) -> bool {
523 self.is_lang_item(def_id, solver_trait_lang_item_to_lang_item(lang_item))
524 }
525
526 fn is_default_trait(self, def_id: DefId) -> bool {
527 self.is_default_trait(def_id)
528 }
529
530 fn is_implicit_trait(
531 self,
532 def_id: Self::TraitId,
533 including_sized: IncludeSizedImplicitBounds,
534 ) -> bool {
535 self.is_implicit_trait(def_id, including_sized)
536 }
537
538 fn is_sizedness_trait(self, def_id: DefId) -> bool {
539 self.is_sizedness_trait(def_id)
540 }
541
542 fn as_projection_lang_item(self, def_id: DefId) -> Option<SolverProjectionLangItem> {
543 lang_item_to_solver_lang_item(self.lang_items().from_def_id(def_id)?)
544 }
545
546 fn as_trait_lang_item(self, def_id: DefId) -> Option<SolverTraitLangItem> {
547 lang_item_to_solver_trait_lang_item(self.lang_items().from_def_id(def_id)?)
548 }
549
550 fn as_adt_lang_item(self, def_id: DefId) -> Option<SolverAdtLangItem> {
551 lang_item_to_solver_adt_lang_item(self.lang_items().from_def_id(def_id)?)
552 }
553
554 fn associated_type_def_ids(self, def_id: DefId) -> impl IntoIterator<Item = DefId> {
555 self.associated_items(def_id)
556 .in_definition_order()
557 .filter(|assoc_item| assoc_item.is_type())
558 .map(|assoc_item| assoc_item.def_id)
559 }
560
561 fn for_each_relevant_impl<R: VisitorResult>(
564 self,
565 trait_ref: ty::TraitRef<'tcx>,
566 f: impl FnMut(DefId) -> R,
567 ) -> R {
568 let self_ty = trait_ref.args.type_at(0);
569 if true {
if !!#[allow(non_exhaustive_omitted_patterns)] match self_ty.kind() {
ty::Infer(ty::TyVar(_)) | ty::Param(_) | ty::Bound(_, _) =>
true,
_ => false,
} {
{
::core::panicking::panic_fmt(format_args!("we should not have them as self ty in the next solver"));
}
};
};debug_assert!(
570 !matches!(self_ty.kind(), ty::Infer(ty::TyVar(_)) | ty::Param(_) | ty::Bound(_, _)),
571 "we should not have them as self ty in the next solver"
572 );
573 TyCtxt::for_each_relevant_impl(self, trait_ref.def_id, self_ty, f)
574 }
575 fn for_each_blanket_impl<R: VisitorResult>(
576 self,
577 trait_def_id: DefId,
578 mut f: impl FnMut(DefId) -> R,
579 ) -> R {
580 let trait_impls = self.trait_impls_of(trait_def_id);
581 for &impl_def_id in trait_impls.blanket_impls() {
582 match ::rustc_ast_ir::visit::VisitorResult::branch(f(impl_def_id)) {
core::ops::ControlFlow::Continue(()) =>
(),
#[allow(unreachable_code)]
core::ops::ControlFlow::Break(r) => {
return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
}
};try_visit!(f(impl_def_id));
583 }
584
585 R::output()
586 }
587
588 fn has_item_definition(self, def_id: DefId) -> bool {
589 self.defaultness(def_id).has_value()
590 }
591
592 fn impl_specializes(self, impl_def_id: Self::DefId, victim_def_id: Self::DefId) -> bool {
593 self.specializes((impl_def_id, victim_def_id))
594 }
595
596 fn impl_is_default(self, impl_def_id: DefId) -> bool {
597 self.defaultness(impl_def_id).is_default()
598 }
599
600 fn impl_trait_ref(self, impl_def_id: DefId) -> ty::EarlyBinder<'tcx, ty::TraitRef<'tcx>> {
601 self.impl_trait_ref(impl_def_id)
602 }
603
604 fn impl_polarity(self, impl_def_id: DefId) -> ty::ImplPolarity {
605 self.impl_polarity(impl_def_id)
606 }
607
608 fn is_fully_generic_for_reflection(self, impl_def_id: Self::ImplId) -> bool {
609 self.impl_is_fully_generic_for_reflection(impl_def_id)
610 }
611
612 fn trait_is_auto(self, trait_def_id: DefId) -> bool {
613 self.trait_is_auto(trait_def_id)
614 }
615
616 fn trait_is_marker(self, trait_def_id: DefId) -> bool {
617 self.trait_def(trait_def_id).is_marker
618 }
619
620 fn trait_is_coinductive(self, trait_def_id: DefId) -> bool {
621 self.trait_is_coinductive(trait_def_id)
622 }
623
624 fn trait_is_alias(self, trait_def_id: DefId) -> bool {
625 self.trait_is_alias(trait_def_id)
626 }
627
628 fn trait_is_dyn_compatible(self, trait_def_id: DefId) -> bool {
629 self.is_dyn_compatible(trait_def_id)
630 }
631
632 fn trait_is_fundamental(self, def_id: DefId) -> bool {
633 self.trait_def(def_id).is_fundamental
634 }
635
636 fn trait_is_unsafe(self, trait_def_id: Self::DefId) -> bool {
637 self.trait_def(trait_def_id).safety.is_unsafe()
638 }
639
640 fn delay_bug(self, msg: impl ToString) -> ErrorGuaranteed {
641 self.dcx().span_delayed_bug(DUMMY_SP, msg.to_string())
642 }
643
644 fn span_delayed_bug(self, span: Self::Span, msg: impl ToString) -> ErrorGuaranteed {
645 self.dcx().span_delayed_bug(span, msg.to_string())
646 }
647
648 fn is_general_coroutine(self, coroutine_def_id: DefId) -> bool {
649 self.is_general_coroutine(coroutine_def_id)
650 }
651
652 fn coroutine_is_async(self, coroutine_def_id: DefId) -> bool {
653 self.coroutine_is_async(coroutine_def_id)
654 }
655
656 fn coroutine_is_gen(self, coroutine_def_id: DefId) -> bool {
657 self.coroutine_is_gen(coroutine_def_id)
658 }
659
660 fn coroutine_is_async_gen(self, coroutine_def_id: DefId) -> bool {
661 self.coroutine_is_async_gen(coroutine_def_id)
662 }
663
664 type UnsizingParams = &'tcx rustc_index::bit_set::DenseBitSet<u32>;
665 fn unsizing_params_for_adt(self, adt_def_id: DefId) -> Self::UnsizingParams {
666 self.unsizing_params_for_adt(adt_def_id)
667 }
668
669 fn anonymize_bound_vars<T: TypeFoldable<TyCtxt<'tcx>>>(
670 self,
671 binder: ty::Binder<'tcx, T>,
672 ) -> ty::Binder<'tcx, T> {
673 self.anonymize_bound_vars(binder)
674 }
675
676 fn opaque_types_defined_by(self, defining_anchor: LocalDefId) -> Self::LocalDefIds {
677 self.opaque_types_defined_by(defining_anchor)
678 }
679
680 fn opaque_types_and_coroutines_defined_by(
681 self,
682 defining_anchor: Self::LocalDefId,
683 ) -> Self::LocalDefIds {
684 let coroutines_defined_by = self
685 .nested_bodies_within(defining_anchor)
686 .iter()
687 .filter(|def_id| self.is_coroutine(def_id.to_def_id()));
688 self.mk_local_def_ids_from_iter(
689 self.opaque_types_defined_by(defining_anchor).iter().chain(coroutines_defined_by),
690 )
691 }
692
693 type Probe = &'tcx inspect::Probe<TyCtxt<'tcx>>;
694 fn mk_probe(self, probe: inspect::Probe<Self>) -> &'tcx inspect::Probe<TyCtxt<'tcx>> {
695 self.arena.alloc(probe)
696 }
697 type CanonicalInput = CanonicalInput<'tcx>;
698 fn mk_canonical_input(self, data: CanonicalInputData<Self>) -> CanonicalInput<'tcx> {
699 self.intern_canonical_input(data)
700 }
701 fn evaluate_root_goal_for_proof_tree_raw(
702 self,
703 canonical_goal: CanonicalInput<'tcx>,
704 root_depth: usize,
705 ) -> (QueryResult<'tcx>, &'tcx inspect::Probe<TyCtxt<'tcx>>, RequiredDepth) {
706 self.evaluate_root_goal_for_proof_tree_raw((canonical_goal, root_depth))
707 }
708
709 fn item_name(self, id: DefId) -> Symbol {
710 self.opt_item_name(id).unwrap_or_else(|| {
711 ::rustc_span::macros::bug_impl(None,
format_args!("item_name: no name for {0:?}", self.def_path(id)),
Location::caller());bug!("item_name: no name for {:?}", self.def_path(id));
712 })
713 }
714
715 fn get_re_static_lifetime(self) -> Region<'tcx> {
716 self.lifetimes.re_static
717 }
718
719 fn intern_re_var(self, rv: RegionVid) -> Region<'tcx> {
720 self.lifetimes
722 .re_vars
723 .get(rv.as_usize())
724 .copied()
725 .unwrap_or_else(|| self.intern_region(ty::ReVar(rv)))
726 }
727
728 fn intern_region(self, region_kind: RegionKind<'tcx>) -> Region<'tcx> {
729 self.intern_region(region_kind)
730 }
731
732 fn intern_bound_region(
733 self,
734 debruijn: DebruijnIndex,
735 bound_region: BoundRegion<'tcx>,
736 ) -> Region<'tcx> {
737 if let ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon } = bound_region
739 && let Some(inner) = self.lifetimes.re_bound_bound_anons.get(debruijn.as_usize())
740 && let Some(re) = inner.get(var.as_usize()).copied()
741 {
742 re
743 } else {
744 self.intern_region(ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), bound_region))
745 }
746 }
747
748 fn intern_canonical_bound(self, var: BoundVar) -> Region<'tcx> {
749 if let Some(re) = self.lifetimes.re_bound_canonical_anons.get(var.as_usize()).copied() {
751 re
752 } else {
753 self.intern_region(ty::ReBound(
754 ty::BoundVarIndexKind::Canonical,
755 BoundRegion { var, kind: ty::BoundRegionKind::Anon },
756 ))
757 }
758 }
759
760 fn intern_valtree(self, valtree_kind: ty::ValTreeKind<TyCtxt<'tcx>>) -> ty::ValTree<'tcx> {
761 self.intern_valtree(valtree_kind)
762 }
763
764 fn const_zst(self) -> ty::ValTree<'tcx> {
765 self.consts.valtree_zst
766 }
767}
768
769impl<'tcx, T: Clone + Copy> rustc_type_ir::intern::Interned<TyCtxt<'tcx>> for Interned<'tcx, T> {
770 type Value = T;
771 fn get(self) -> T {
772 *self.0
773 }
774}
775
776macro_rules! bidirectional_lang_item_map {
779 (
780 $solver_ty:ident, fn $to_solver:ident, fn $from_solver:ident;
781 $($name:ident),+ $(,)?
782 ) => {
783 fn $from_solver(lang_item: $solver_ty) -> LangItem {
784 match lang_item {
785 $($solver_ty::$name => LangItem::$name,)+
786 }
787 }
788
789 fn $to_solver(lang_item: LangItem) -> Option<$solver_ty> {
790 Some(match lang_item {
791 $(LangItem::$name => $solver_ty::$name,)+
792 _ => return None,
793 })
794 }
795 }
796}
797
798fn solver_lang_item_to_lang_item(lang_item: SolverProjectionLangItem)
-> LangItem {
match lang_item {
SolverProjectionLangItem::AsyncFnKindUpvars =>
LangItem::AsyncFnKindUpvars,
SolverProjectionLangItem::AsyncFnOnceOutput =>
LangItem::AsyncFnOnceOutput,
SolverProjectionLangItem::CallOnceFuture => LangItem::CallOnceFuture,
SolverProjectionLangItem::CallRefFuture => LangItem::CallRefFuture,
SolverProjectionLangItem::CoroutineReturn =>
LangItem::CoroutineReturn,
SolverProjectionLangItem::CoroutineYield => LangItem::CoroutineYield,
SolverProjectionLangItem::FieldBase => LangItem::FieldBase,
SolverProjectionLangItem::FieldType => LangItem::FieldType,
SolverProjectionLangItem::FutureOutput => LangItem::FutureOutput,
SolverProjectionLangItem::Metadata => LangItem::Metadata,
}
}
fn lang_item_to_solver_lang_item(lang_item: LangItem)
-> Option<SolverProjectionLangItem> {
Some(match lang_item {
LangItem::AsyncFnKindUpvars =>
SolverProjectionLangItem::AsyncFnKindUpvars,
LangItem::AsyncFnOnceOutput =>
SolverProjectionLangItem::AsyncFnOnceOutput,
LangItem::CallOnceFuture =>
SolverProjectionLangItem::CallOnceFuture,
LangItem::CallRefFuture =>
SolverProjectionLangItem::CallRefFuture,
LangItem::CoroutineReturn =>
SolverProjectionLangItem::CoroutineReturn,
LangItem::CoroutineYield =>
SolverProjectionLangItem::CoroutineYield,
LangItem::FieldBase => SolverProjectionLangItem::FieldBase,
LangItem::FieldType => SolverProjectionLangItem::FieldType,
LangItem::FutureOutput => SolverProjectionLangItem::FutureOutput,
LangItem::Metadata => SolverProjectionLangItem::Metadata,
_ => return None,
})
}bidirectional_lang_item_map! {
799 SolverProjectionLangItem, fn lang_item_to_solver_lang_item, fn solver_lang_item_to_lang_item;
800
801AsyncFnKindUpvars,
803 AsyncFnOnceOutput,
804 CallOnceFuture,
805 CallRefFuture,
806 CoroutineReturn,
807 CoroutineYield,
808 FieldBase,
809 FieldType,
810 FutureOutput,
811 Metadata,
812}
814
815fn solver_adt_lang_item_to_lang_item(lang_item: SolverAdtLangItem)
-> LangItem {
match lang_item {
SolverAdtLangItem::DynMetadata => LangItem::DynMetadata,
SolverAdtLangItem::Option => LangItem::Option,
SolverAdtLangItem::OwnedBox => LangItem::OwnedBox,
SolverAdtLangItem::Poll => LangItem::Poll,
}
}
fn lang_item_to_solver_adt_lang_item(lang_item: LangItem)
-> Option<SolverAdtLangItem> {
Some(match lang_item {
LangItem::DynMetadata => SolverAdtLangItem::DynMetadata,
LangItem::Option => SolverAdtLangItem::Option,
LangItem::OwnedBox => SolverAdtLangItem::OwnedBox,
LangItem::Poll => SolverAdtLangItem::Poll,
_ => return None,
})
}bidirectional_lang_item_map! {
816 SolverAdtLangItem, fn lang_item_to_solver_adt_lang_item, fn solver_adt_lang_item_to_lang_item;
817
818DynMetadata,
820 Option,
821 OwnedBox,
822 Poll,
823}
825
826fn solver_trait_lang_item_to_lang_item(lang_item: SolverTraitLangItem)
-> LangItem {
match lang_item {
SolverTraitLangItem::AsyncFn => LangItem::AsyncFn,
SolverTraitLangItem::AsyncFnKindHelper => LangItem::AsyncFnKindHelper,
SolverTraitLangItem::AsyncFnMut => LangItem::AsyncFnMut,
SolverTraitLangItem::AsyncFnOnce => LangItem::AsyncFnOnce,
SolverTraitLangItem::AsyncIterator => LangItem::AsyncIterator,
SolverTraitLangItem::BikeshedGuaranteedNoDrop =>
LangItem::BikeshedGuaranteedNoDrop,
SolverTraitLangItem::Clone => LangItem::Clone,
SolverTraitLangItem::Copy => LangItem::Copy,
SolverTraitLangItem::Coroutine => LangItem::Coroutine,
SolverTraitLangItem::Destruct => LangItem::Destruct,
SolverTraitLangItem::DiscriminantKind => LangItem::DiscriminantKind,
SolverTraitLangItem::Drop => LangItem::Drop,
SolverTraitLangItem::Field => LangItem::Field,
SolverTraitLangItem::Fn => LangItem::Fn,
SolverTraitLangItem::FnMut => LangItem::FnMut,
SolverTraitLangItem::FnOnce => LangItem::FnOnce,
SolverTraitLangItem::FnPtrTrait => LangItem::FnPtrTrait,
SolverTraitLangItem::Freeze => LangItem::Freeze,
SolverTraitLangItem::FusedIterator => LangItem::FusedIterator,
SolverTraitLangItem::Future => LangItem::Future,
SolverTraitLangItem::Iterator => LangItem::Iterator,
SolverTraitLangItem::MetaSized => LangItem::MetaSized,
SolverTraitLangItem::PointeeSized => LangItem::PointeeSized,
SolverTraitLangItem::PointeeTrait => LangItem::PointeeTrait,
SolverTraitLangItem::Sized => LangItem::Sized,
SolverTraitLangItem::TransmuteTrait => LangItem::TransmuteTrait,
SolverTraitLangItem::TrivialClone => LangItem::TrivialClone,
SolverTraitLangItem::TryAsDyn => LangItem::TryAsDyn,
SolverTraitLangItem::Tuple => LangItem::Tuple,
SolverTraitLangItem::Unpin => LangItem::Unpin,
SolverTraitLangItem::Unsize => LangItem::Unsize,
}
}
fn lang_item_to_solver_trait_lang_item(lang_item: LangItem)
-> Option<SolverTraitLangItem> {
Some(match lang_item {
LangItem::AsyncFn => SolverTraitLangItem::AsyncFn,
LangItem::AsyncFnKindHelper =>
SolverTraitLangItem::AsyncFnKindHelper,
LangItem::AsyncFnMut => SolverTraitLangItem::AsyncFnMut,
LangItem::AsyncFnOnce => SolverTraitLangItem::AsyncFnOnce,
LangItem::AsyncIterator => SolverTraitLangItem::AsyncIterator,
LangItem::BikeshedGuaranteedNoDrop =>
SolverTraitLangItem::BikeshedGuaranteedNoDrop,
LangItem::Clone => SolverTraitLangItem::Clone,
LangItem::Copy => SolverTraitLangItem::Copy,
LangItem::Coroutine => SolverTraitLangItem::Coroutine,
LangItem::Destruct => SolverTraitLangItem::Destruct,
LangItem::DiscriminantKind =>
SolverTraitLangItem::DiscriminantKind,
LangItem::Drop => SolverTraitLangItem::Drop,
LangItem::Field => SolverTraitLangItem::Field,
LangItem::Fn => SolverTraitLangItem::Fn,
LangItem::FnMut => SolverTraitLangItem::FnMut,
LangItem::FnOnce => SolverTraitLangItem::FnOnce,
LangItem::FnPtrTrait => SolverTraitLangItem::FnPtrTrait,
LangItem::Freeze => SolverTraitLangItem::Freeze,
LangItem::FusedIterator => SolverTraitLangItem::FusedIterator,
LangItem::Future => SolverTraitLangItem::Future,
LangItem::Iterator => SolverTraitLangItem::Iterator,
LangItem::MetaSized => SolverTraitLangItem::MetaSized,
LangItem::PointeeSized => SolverTraitLangItem::PointeeSized,
LangItem::PointeeTrait => SolverTraitLangItem::PointeeTrait,
LangItem::Sized => SolverTraitLangItem::Sized,
LangItem::TransmuteTrait => SolverTraitLangItem::TransmuteTrait,
LangItem::TrivialClone => SolverTraitLangItem::TrivialClone,
LangItem::TryAsDyn => SolverTraitLangItem::TryAsDyn,
LangItem::Tuple => SolverTraitLangItem::Tuple,
LangItem::Unpin => SolverTraitLangItem::Unpin,
LangItem::Unsize => SolverTraitLangItem::Unsize,
_ => return None,
})
}bidirectional_lang_item_map! {
827 SolverTraitLangItem, fn lang_item_to_solver_trait_lang_item, fn solver_trait_lang_item_to_lang_item;
828
829AsyncFn,
831 AsyncFnKindHelper,
832 AsyncFnMut,
833 AsyncFnOnce,
834 AsyncIterator,
835 BikeshedGuaranteedNoDrop,
836 Clone,
837 Copy,
838 Coroutine,
839 Destruct,
840 DiscriminantKind,
841 Drop,
842 Field,
843 Fn,
844 FnMut,
845 FnOnce,
846 FnPtrTrait,
847 Freeze,
848 FusedIterator,
849 Future,
850 Iterator,
851 MetaSized,
852 PointeeSized,
853 PointeeTrait,
854 Sized,
855 TransmuteTrait,
856 TrivialClone,
857 TryAsDyn,
858 Tuple,
859 Unpin,
860 Unsize,
861}