Skip to main content

rustc_type_ir/
inherent.rs

1//! Set of traits which are used to emulate the inherent impls that are present in `rustc_middle`.
2//! It is customary to glob-import `rustc_type_ir::inherent::*` to bring all of these traits into
3//! scope when programming in interner-agnostic settings, and to avoid importing any of these
4//! directly elsewhere (i.e. specify the full path for an implementation downstream).
5
6use std::fmt::Debug;
7use std::hash::Hash;
8
9use rustc_ast_ir::Mutability;
10
11use crate::elaborate::Elaboratable;
12use crate::fold::{TypeFoldable, TypeSuperFoldable};
13use crate::relate::Relate;
14use crate::solve::{AdtDestructorKind, SizedTraitKind};
15use crate::visit::{Flags, TypeSuperVisitable, TypeVisitable};
16use crate::{
17    self as ty, ClauseKind, CollectAndApply, FieldInfo, Interner, PredicateKind, UpcastFrom,
18};
19
20#[rust_analyzer::prefer_underscore_import]
21pub trait Ty<I: Interner<Ty = Self>>:
22    Copy
23    + Debug
24    + Hash
25    + Eq
26    + Into<I::GenericArg>
27    + Into<I::Term>
28    + IntoKind<Kind = ty::TyKind<I>>
29    + TypeSuperVisitable<I>
30    + TypeSuperFoldable<I>
31    + Relate<I>
32    + Flags
33{
34    fn new_unit(interner: I) -> Self;
35
36    fn new_bool(interner: I) -> Self;
37
38    fn new_u8(interner: I) -> Self;
39
40    fn new_usize(interner: I) -> Self;
41
42    fn new_infer(interner: I, var: ty::InferTy) -> Self;
43
44    fn new_var(interner: I, var: ty::TyVid) -> Self;
45
46    fn new_param(interner: I, param: I::ParamTy) -> Self;
47
48    fn new_placeholder(interner: I, param: ty::PlaceholderType<I>) -> Self;
49
50    fn new_bound(interner: I, debruijn: ty::DebruijnIndex, var: ty::BoundTy<I>) -> Self;
51
52    fn new_anon_bound(interner: I, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self;
53
54    fn new_canonical_bound(interner: I, var: ty::BoundVar) -> Self;
55
56    fn new_alias(interner: I, is_rigid: ty::IsRigid, alias_ty: ty::AliasTy<I>) -> Self;
57
58    fn new_projection_from_args(
59        interner: I,
60        is_rigid: ty::IsRigid,
61        def_id: I::TraitAssocTyId,
62        args: I::GenericArgs,
63    ) -> Self {
64        Self::new_alias(
65            interner,
66            is_rigid,
67            ty::AliasTy::new_from_args(interner, ty::AliasTyKind::Projection { def_id }, args),
68        )
69    }
70
71    fn new_projection(
72        interner: I,
73        is_rigid: ty::IsRigid,
74        def_id: I::TraitAssocTyId,
75        args: impl IntoIterator<Item: Into<I::GenericArg>>,
76    ) -> Self {
77        Self::new_alias(
78            interner,
79            is_rigid,
80            ty::AliasTy::new(interner, ty::AliasTyKind::Projection { def_id }, args),
81        )
82    }
83
84    fn new_error(interner: I, guar: I::ErrorGuaranteed) -> Self;
85
86    fn new_adt(interner: I, adt_def: I::AdtDef, args: I::GenericArgs) -> Self;
87
88    fn new_foreign(interner: I, def_id: I::ForeignId) -> Self;
89
90    fn new_dynamic(interner: I, preds: I::BoundExistentialPredicates, region: I::Region) -> Self;
91
92    fn new_coroutine(interner: I, def_id: I::CoroutineId, args: I::GenericArgs) -> Self;
93
94    fn new_coroutine_closure(
95        interner: I,
96        def_id: I::CoroutineClosureId,
97        args: I::GenericArgs,
98    ) -> Self;
99
100    fn new_closure(interner: I, def_id: I::ClosureId, args: I::GenericArgs) -> Self;
101
102    fn new_coroutine_witness(interner: I, def_id: I::CoroutineId, args: I::GenericArgs) -> Self;
103
104    fn new_coroutine_witness_for_coroutine(
105        interner: I,
106        def_id: I::CoroutineId,
107        coroutine_args: I::GenericArgs,
108    ) -> Self;
109
110    fn new_ptr(interner: I, ty: Self, mutbl: Mutability) -> Self;
111
112    fn new_ref(interner: I, region: I::Region, ty: Self, mutbl: Mutability) -> Self;
113
114    fn new_array_with_const_len(interner: I, ty: Self, len: I::Const) -> Self;
115
116    fn new_slice(interner: I, ty: Self) -> Self;
117
118    fn new_tup(interner: I, tys: &[I::Ty]) -> Self;
119
120    fn new_tup_from_iter<It, T>(interner: I, iter: It) -> T::Output
121    where
122        It: Iterator<Item = T>,
123        T: CollectAndApply<Self, Self>;
124
125    fn new_fn_def(interner: I, def_id: I::FunctionId, args: I::GenericArgs) -> Self;
126
127    fn new_fn_ptr(interner: I, sig: ty::Binder<I, ty::FnSig<I>>) -> Self;
128
129    fn new_pat(interner: I, ty: Self, pat: I::Pat) -> Self;
130
131    fn new_unsafe_binder(interner: I, ty: ty::Binder<I, I::Ty>) -> Self;
132
133    fn tuple_fields(self) -> I::Tys;
134
135    fn to_opt_closure_kind(self) -> Option<ty::ClosureKind>;
136
137    fn from_closure_kind(interner: I, kind: ty::ClosureKind) -> Self;
138
139    fn from_coroutine_closure_kind(interner: I, kind: ty::ClosureKind) -> Self;
140
141    fn is_ty_var(self) -> bool {
142        #[allow(non_exhaustive_omitted_patterns)] match self.kind() {
    ty::Infer(ty::TyVar(_)) => true,
    _ => false,
}matches!(self.kind(), ty::Infer(ty::TyVar(_)))
143    }
144
145    fn is_ty_error(self) -> bool {
146        #[allow(non_exhaustive_omitted_patterns)] match self.kind() {
    ty::Error(_) => true,
    _ => false,
}matches!(self.kind(), ty::Error(_))
147    }
148
149    fn is_floating_point(self) -> bool {
150        #[allow(non_exhaustive_omitted_patterns)] match self.kind() {
    ty::Float(_) | ty::Infer(ty::FloatVar(_)) => true,
    _ => false,
}matches!(self.kind(), ty::Float(_) | ty::Infer(ty::FloatVar(_)))
151    }
152
153    fn is_integral(self) -> bool {
154        #[allow(non_exhaustive_omitted_patterns)] match self.kind() {
    ty::Infer(ty::IntVar(_)) | ty::Int(_) | ty::Uint(_) => true,
    _ => false,
}matches!(self.kind(), ty::Infer(ty::IntVar(_)) | ty::Int(_) | ty::Uint(_))
155    }
156
157    fn is_fn_ptr(self) -> bool {
158        #[allow(non_exhaustive_omitted_patterns)] match self.kind() {
    ty::FnPtr(..) => true,
    _ => false,
}matches!(self.kind(), ty::FnPtr(..))
159    }
160
161    /// Checks whether this type is an ADT that has unsafe fields.
162    fn has_unsafe_fields(self) -> bool;
163
164    fn fn_sig(self, interner: I) -> ty::Binder<I, ty::FnSig<I>> {
165        self.kind().fn_sig(interner)
166    }
167
168    fn discriminant_ty(self, interner: I) -> I::Ty;
169
170    fn is_known_rigid(self) -> bool {
171        self.kind().is_known_rigid()
172    }
173
174    fn is_guaranteed_unsized_raw(self) -> bool {
175        match self.kind() {
176            ty::Dynamic(_, _) | ty::Slice(_) | ty::Str => true,
177            ty::Bool
178            | ty::Char
179            | ty::Int(_)
180            | ty::Uint(_)
181            | ty::Float(_)
182            | ty::Adt(_, _)
183            | ty::Foreign(_)
184            | ty::Array(_, _)
185            | ty::Pat(_, _)
186            | ty::RawPtr(_, _)
187            | ty::Ref(_, _, _)
188            | ty::FnDef(_, _)
189            | ty::FnPtr(_, _)
190            | ty::UnsafeBinder(_)
191            | ty::Closure(_, _)
192            | ty::CoroutineClosure(_, _)
193            | ty::Coroutine(_, _)
194            | ty::CoroutineWitness(_, _)
195            | ty::Never
196            | ty::Tuple(_)
197            | ty::Alias(_, _)
198            | ty::Param(_)
199            | ty::Bound(_, _)
200            | ty::Placeholder(_)
201            | ty::Infer(_)
202            | ty::Error(_) => false,
203        }
204    }
205}
206
207#[rust_analyzer::prefer_underscore_import]
208pub trait Tys<I: Interner<Tys = Self>>:
209    Copy + Debug + Hash + Eq + SliceLike<Item = I::Ty> + TypeFoldable<I> + Default
210{
211    fn inputs(self) -> I::FnInputTys;
212
213    fn output(self) -> I::Ty;
214}
215
216#[rust_analyzer::prefer_underscore_import]
217pub trait Safety<I: Interner<Safety = Self>>: Copy + Debug + Hash + Eq {
218    /// The `safe` safety mode.
219    fn safe() -> Self;
220
221    /// The `unsafe` safety mode.
222    fn unsafe_mode() -> Self;
223
224    /// Is the safety mode `Safe`?
225    fn is_safe(self) -> bool;
226
227    /// The string prefix for this safety mode.
228    fn prefix_str(self) -> &'static str;
229}
230
231#[rust_analyzer::prefer_underscore_import]
232pub trait Region<I: Interner<Region = Self>>:
233    Copy
234    + Debug
235    + Hash
236    + Eq
237    + Into<I::GenericArg>
238    + IntoKind<Kind = ty::RegionKind<I>>
239    + Flags
240    + Relate<I>
241{
242    fn new_bound(interner: I, debruijn: ty::DebruijnIndex, var: ty::BoundRegion<I>) -> Self;
243
244    fn new_anon_bound(interner: I, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self;
245
246    fn new_canonical_bound(interner: I, var: ty::BoundVar) -> Self;
247
248    fn new_static(interner: I) -> Self;
249
250    fn new_placeholder(interner: I, var: ty::PlaceholderRegion<I>) -> Self;
251
252    fn is_bound(self) -> bool {
253        #[allow(non_exhaustive_omitted_patterns)] match self.kind() {
    ty::ReBound(..) => true,
    _ => false,
}matches!(self.kind(), ty::ReBound(..))
254    }
255}
256
257#[rust_analyzer::prefer_underscore_import]
258pub trait Const<I: Interner<Const = Self>>:
259    Copy
260    + Debug
261    + Hash
262    + Eq
263    + Into<I::GenericArg>
264    + Into<I::Term>
265    + IntoKind<Kind = ty::ConstKind<I>>
266    + TypeSuperVisitable<I>
267    + TypeSuperFoldable<I>
268    + Relate<I>
269    + Flags
270{
271    fn new_infer(interner: I, var: ty::InferConst) -> Self;
272
273    fn new_var(interner: I, var: ty::ConstVid) -> Self;
274
275    fn new_bound(interner: I, debruijn: ty::DebruijnIndex, bound_const: ty::BoundConst<I>) -> Self;
276
277    fn new_anon_bound(interner: I, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self;
278
279    fn new_canonical_bound(interner: I, var: ty::BoundVar) -> Self;
280
281    fn new_placeholder(interner: I, param: ty::PlaceholderConst<I>) -> Self;
282
283    fn new_alias(interner: I, is_rigid: ty::IsRigid, alias_const: ty::AliasConst<I>) -> Self;
284
285    fn new_expr(interner: I, expr: I::ExprConst) -> Self;
286
287    fn new_error(interner: I, guar: I::ErrorGuaranteed) -> Self;
288
289    fn new_error_with_message(interner: I, msg: impl ToString) -> Self {
290        Self::new_error(interner, interner.delay_bug(msg))
291    }
292
293    fn is_ct_var(self) -> bool {
294        #[allow(non_exhaustive_omitted_patterns)] match self.kind() {
    ty::ConstKind::Infer(ty::InferConst::Var(_)) => true,
    _ => false,
}matches!(self.kind(), ty::ConstKind::Infer(ty::InferConst::Var(_)))
295    }
296
297    fn is_ct_error(self) -> bool {
298        #[allow(non_exhaustive_omitted_patterns)] match self.kind() {
    ty::ConstKind::Error(_) => true,
    _ => false,
}matches!(self.kind(), ty::ConstKind::Error(_))
299    }
300}
301
302#[rust_analyzer::prefer_underscore_import]
303pub trait ValueConst<I: Interner<ValueConst = Self>>: Copy + Debug + Hash + Eq {
304    fn ty(self) -> I::Ty;
305    fn valtree(self) -> I::ValTree;
306}
307
308#[rust_analyzer::prefer_underscore_import]
309pub trait ExprConst<I: Interner<ExprConst = Self>>: Copy + Debug + Hash + Eq + Relate<I> {
310    fn args(self) -> I::GenericArgs;
311}
312
313#[rust_analyzer::prefer_underscore_import]
314pub trait GenericsOf<I: Interner<GenericsOf = Self>> {
315    fn count(&self) -> usize;
316}
317
318#[rust_analyzer::prefer_underscore_import]
319pub trait GenericArg<I: Interner<GenericArg = Self>>:
320    Copy
321    + Debug
322    + Hash
323    + Eq
324    + IntoKind<Kind = ty::GenericArgKind<I>>
325    + TypeVisitable<I>
326    + Relate<I>
327    + From<I::Ty>
328    + From<I::Region>
329    + From<I::Const>
330    + From<I::Term>
331{
332    fn as_term(&self) -> Option<I::Term> {
333        match self.kind() {
334            ty::GenericArgKind::Lifetime(_) => None,
335            ty::GenericArgKind::Type(ty) => Some(ty.into()),
336            ty::GenericArgKind::Const(ct) => Some(ct.into()),
337        }
338    }
339
340    fn as_type(&self) -> Option<I::Ty> {
341        if let ty::GenericArgKind::Type(ty) = self.kind() { Some(ty) } else { None }
342    }
343
344    fn expect_ty(&self) -> I::Ty {
345        self.as_type().expect("expected a type")
346    }
347
348    fn as_const(&self) -> Option<I::Const> {
349        if let ty::GenericArgKind::Const(c) = self.kind() { Some(c) } else { None }
350    }
351
352    fn expect_const(&self) -> I::Const {
353        self.as_const().expect("expected a const")
354    }
355
356    fn as_region(&self) -> Option<I::Region> {
357        if let ty::GenericArgKind::Lifetime(c) = self.kind() { Some(c) } else { None }
358    }
359
360    fn expect_region(&self) -> I::Region {
361        self.as_region().expect("expected a const")
362    }
363
364    fn is_non_region_infer(self) -> bool {
365        match self.kind() {
366            ty::GenericArgKind::Lifetime(_) => false,
367            ty::GenericArgKind::Type(ty) => ty.is_ty_var(),
368            ty::GenericArgKind::Const(ct) => ct.is_ct_var(),
369        }
370    }
371}
372
373#[rust_analyzer::prefer_underscore_import]
374pub trait Term<I: Interner<Term = Self>>:
375    Copy + Debug + Hash + Eq + IntoKind<Kind = ty::TermKind<I>> + TypeFoldable<I> + Relate<I>
376{
377    fn as_type(&self) -> Option<I::Ty> {
378        if let ty::TermKind::Ty(ty) = self.kind() { Some(ty) } else { None }
379    }
380
381    fn expect_ty(&self) -> I::Ty {
382        self.as_type().expect("expected a type, but found a const")
383    }
384
385    fn as_const(&self) -> Option<I::Const> {
386        if let ty::TermKind::Const(c) = self.kind() { Some(c) } else { None }
387    }
388
389    fn expect_const(&self) -> I::Const {
390        self.as_const().expect("expected a const, but found a type")
391    }
392
393    fn is_infer(self) -> bool {
394        match self.kind() {
395            ty::TermKind::Ty(ty) => ty.is_ty_var(),
396            ty::TermKind::Const(ct) => ct.is_ct_var(),
397        }
398    }
399
400    fn is_error(self) -> bool {
401        match self.kind() {
402            ty::TermKind::Ty(ty) => ty.is_ty_error(),
403            ty::TermKind::Const(ct) => ct.is_ct_error(),
404        }
405    }
406
407    fn to_alias_term(self) -> Option<ty::AliasTerm<I>> {
408        match self.kind() {
409            ty::TermKind::Ty(ty) => match ty.kind() {
410                ty::Alias(_, alias_ty) => Some(alias_ty.into()),
411                _ => None,
412            },
413            ty::TermKind::Const(ct) => match ct.kind() {
414                ty::ConstKind::Alias(_, alias_const) => Some(alias_const.into()),
415                _ => None,
416            },
417        }
418    }
419
420    fn is_non_rigid_alias(self) -> bool {
421        match self.kind() {
422            ty::TermKind::Ty(ty) => match ty.kind() {
423                ty::Alias(is_rigid, _) => is_rigid == ty::IsRigid::No,
424                _ => false,
425            },
426            ty::TermKind::Const(ct) => match ct.kind() {
427                ty::ConstKind::Alias(is_rigid, _) => is_rigid == ty::IsRigid::No,
428                _ => false,
429            },
430        }
431    }
432}
433
434#[rust_analyzer::prefer_underscore_import]
435pub trait GenericArgs<I: Interner<GenericArgs = Self>>:
436    Copy + Debug + Hash + Eq + SliceLike<Item = I::GenericArg> + Default + Relate<I>
437{
438    fn rebase_onto(
439        self,
440        interner: I,
441        source_def_id: I::DefId,
442        target: I::GenericArgs,
443    ) -> I::GenericArgs;
444
445    fn type_at(self, i: usize) -> I::Ty;
446
447    fn region_at(self, i: usize) -> I::Region;
448
449    fn const_at(self, i: usize) -> I::Const;
450
451    fn identity_for_item(interner: I, def_id: I::DefId) -> I::GenericArgs;
452
453    fn extend_with_error(
454        interner: I,
455        def_id: I::DefId,
456        original_args: &[I::GenericArg],
457    ) -> I::GenericArgs;
458
459    fn split_closure_args(self) -> ty::ClosureArgsParts<I>;
460    fn split_coroutine_closure_args(self) -> ty::CoroutineClosureArgsParts<I>;
461    fn split_coroutine_args(self) -> ty::CoroutineArgsParts<I>;
462
463    fn as_closure(self) -> ty::ClosureArgs<I> {
464        ty::ClosureArgs { args: self }
465    }
466    fn as_coroutine_closure(self) -> ty::CoroutineClosureArgs<I> {
467        ty::CoroutineClosureArgs { args: self }
468    }
469    fn as_coroutine(self) -> ty::CoroutineArgs<I> {
470        ty::CoroutineArgs { args: self }
471    }
472}
473
474#[rust_analyzer::prefer_underscore_import]
475pub trait Predicate<I: Interner<Predicate = Self>>:
476    Copy
477    + Debug
478    + Hash
479    + Eq
480    + TypeSuperVisitable<I>
481    + TypeSuperFoldable<I>
482    + Flags
483    + UpcastFrom<I, ty::PredicateKind<I>>
484    + UpcastFrom<I, ty::Binder<I, ty::PredicateKind<I>>>
485    + UpcastFrom<I, ty::ClauseKind<I>>
486    + UpcastFrom<I, ty::Binder<I, ty::ClauseKind<I>>>
487    + UpcastFrom<I, I::Clause>
488    + UpcastFrom<I, ty::NormalizesTo<I>>
489    + UpcastFrom<I, ty::TraitRef<I>>
490    + UpcastFrom<I, ty::Binder<I, ty::TraitRef<I>>>
491    + UpcastFrom<I, ty::TraitPredicate<I>>
492    + UpcastFrom<I, ty::ProjectionPredicate<I>>
493    + UpcastFrom<I, ty::OutlivesPredicate<I, I::Ty>>
494    + UpcastFrom<I, ty::OutlivesPredicate<I, I::Region>>
495    + IntoKind<Kind = ty::Binder<I, ty::PredicateKind<I>>>
496    + Elaboratable<I>
497{
498    fn as_clause(self) -> Option<I::Clause>;
499
500    fn allow_normalization(self) -> bool {
501        match self.kind().skip_binder() {
502            PredicateKind::Clause(ClauseKind::WellFormed(_)) => false,
503            PredicateKind::Clause(ClauseKind::Trait(_))
504            | PredicateKind::Clause(ClauseKind::HostEffect(..))
505            | PredicateKind::Clause(ClauseKind::RegionOutlives(_))
506            | PredicateKind::Clause(ClauseKind::TypeOutlives(_))
507            | PredicateKind::Clause(ClauseKind::Projection(_))
508            | PredicateKind::Clause(ClauseKind::ConstArgHasType(..))
509            | PredicateKind::Clause(ClauseKind::UnstableFeature(_))
510            | PredicateKind::DynCompatible(_)
511            | PredicateKind::Subtype(_)
512            | PredicateKind::Coerce(_)
513            | PredicateKind::Clause(ClauseKind::ConstEvaluatable(_))
514            | PredicateKind::ConstEquate(_, _)
515            | PredicateKind::NormalizesTo(..)
516            | PredicateKind::Ambiguous => true,
517        }
518    }
519}
520
521#[rust_analyzer::prefer_underscore_import]
522pub trait Clause<I: Interner<Clause = Self>>:
523    Copy
524    + Debug
525    + Hash
526    + Eq
527    + TypeFoldable<I>
528    + Flags
529    + UpcastFrom<I, ty::Binder<I, ty::ClauseKind<I>>>
530    + UpcastFrom<I, ty::TraitRef<I>>
531    + UpcastFrom<I, ty::Binder<I, ty::TraitRef<I>>>
532    + UpcastFrom<I, ty::TraitPredicate<I>>
533    + UpcastFrom<I, ty::Binder<I, ty::TraitPredicate<I>>>
534    + UpcastFrom<I, ty::ProjectionPredicate<I>>
535    + UpcastFrom<I, ty::Binder<I, ty::ProjectionPredicate<I>>>
536    + IntoKind<Kind = ty::Binder<I, ty::ClauseKind<I>>>
537    + Elaboratable<I>
538{
539    fn as_predicate(self) -> I::Predicate;
540
541    fn as_type_outlives_clause(self) -> Option<ty::Binder<I, ty::OutlivesPredicate<I, I::Ty>>> {
542        self.kind()
543            .map_bound(|clause| {
544                if let ty::ClauseKind::TypeOutlives(outlives) = clause {
545                    Some(outlives)
546                } else {
547                    None
548                }
549            })
550            .transpose()
551    }
552
553    fn as_trait_clause(self) -> Option<ty::Binder<I, ty::TraitPredicate<I>>> {
554        self.kind()
555            .map_bound(|clause| if let ty::ClauseKind::Trait(t) = clause { Some(t) } else { None })
556            .transpose()
557    }
558
559    fn as_host_effect_clause(self) -> Option<ty::Binder<I, ty::HostEffectPredicate<I>>> {
560        self.kind()
561            .map_bound(
562                |clause| if let ty::ClauseKind::HostEffect(t) = clause { Some(t) } else { None },
563            )
564            .transpose()
565    }
566
567    fn as_projection_clause(self) -> Option<ty::Binder<I, ty::ProjectionPredicate<I>>> {
568        self.kind()
569            .map_bound(
570                |clause| {
571                    if let ty::ClauseKind::Projection(p) = clause { Some(p) } else { None }
572                },
573            )
574            .transpose()
575    }
576
577    /// Performs a instantiation suitable for going from a
578    /// poly-trait-ref to supertraits that must hold if that
579    /// poly-trait-ref holds. This is slightly different from a normal
580    /// instantiation in terms of what happens with bound regions.
581    fn instantiate_supertrait(self, cx: I, trait_ref: ty::Binder<I, ty::TraitRef<I>>) -> Self;
582}
583
584#[rust_analyzer::prefer_underscore_import]
585pub trait Clauses<I: Interner<Clauses = Self>>:
586    Copy
587    + Debug
588    + Hash
589    + Eq
590    + TypeSuperVisitable<I>
591    + TypeSuperFoldable<I>
592    + Flags
593    + SliceLike<Item = I::Clause>
594{
595}
596
597#[rust_analyzer::prefer_underscore_import]
598pub trait IntoKind {
599    type Kind;
600
601    fn kind(self) -> Self::Kind;
602}
603
604#[rust_analyzer::prefer_underscore_import]
605pub trait ParamLike: Copy + Debug + Hash + Eq {
606    fn index(self) -> u32;
607}
608
609#[rust_analyzer::prefer_underscore_import]
610pub trait AdtDef<I: Interner>: Copy + Debug + Hash + Eq {
611    fn def_id(self) -> I::AdtId;
612
613    fn is_struct(self) -> bool;
614
615    fn is_packed(self) -> bool;
616
617    /// Returns the type of the struct tail.
618    ///
619    /// Expects the `AdtDef` to be a struct. If it is not, then this will panic.
620    fn struct_tail_ty(self, interner: I) -> Option<ty::EarlyBinder<I, I::Ty>>;
621
622    fn is_phantom_data(self) -> bool;
623
624    fn is_manually_drop(self) -> bool;
625
626    fn field_representing_type_info(
627        self,
628        interner: I,
629        args: I::GenericArgs,
630    ) -> Option<FieldInfo<I>>;
631
632    // FIXME: perhaps use `all_fields` and expose `FieldDef`.
633    fn all_field_tys(self, interner: I) -> ty::EarlyBinder<I, impl IntoIterator<Item = I::Ty>>;
634
635    fn sizedness_constraint(
636        self,
637        interner: I,
638        sizedness: SizedTraitKind,
639    ) -> Option<ty::EarlyBinder<I, I::Ty>>;
640
641    fn is_fundamental(self) -> bool;
642
643    fn destructor(self, interner: I) -> Option<AdtDestructorKind>;
644}
645
646#[rust_analyzer::prefer_underscore_import]
647pub trait ParamEnv<I: Interner>: Copy + Debug + Hash + Eq + TypeFoldable<I> {
648    fn caller_bounds(self) -> impl SliceLike<Item = I::Clause>;
649}
650
651#[rust_analyzer::prefer_underscore_import]
652pub trait Features<I: Interner>: Copy {
653    fn generic_const_exprs(self) -> bool;
654
655    fn generic_const_args(self) -> bool;
656
657    fn coroutine_clone(self) -> bool;
658
659    fn feature_bound_holds_in_crate(self, symbol: I::Symbol) -> bool;
660}
661
662#[rust_analyzer::prefer_underscore_import]
663pub trait DefId<I: Interner, Local = <I as Interner>::LocalDefId>:
664    Copy + Debug + Hash + Eq + TypeFoldable<I>
665{
666    fn is_local(self) -> bool;
667
668    fn as_local(self) -> Option<Local>;
669}
670
671pub trait SpecificDefId<I: Interner, Local = <I as Interner>::LocalDefId>:
672    DefId<I, Local> + Into<I::DefId> + TryFrom<I::DefId, Error: std::fmt::Debug>
673{
674}
675
676impl<
677    I: Interner,
678    T: DefId<I, Local> + Into<I::DefId> + TryFrom<I::DefId, Error: std::fmt::Debug>,
679    Local,
680> SpecificDefId<I, Local> for T
681{
682}
683
684#[rust_analyzer::prefer_underscore_import]
685pub trait BoundExistentialPredicates<I: Interner>:
686    Copy + Debug + Hash + Eq + Relate<I> + SliceLike<Item = ty::Binder<I, ty::ExistentialPredicate<I>>>
687{
688    fn principal_def_id(self) -> Option<I::TraitId>;
689
690    fn principal(self) -> Option<ty::Binder<I, ty::ExistentialTraitRef<I>>>;
691
692    fn auto_traits(self) -> impl IntoIterator<Item = I::TraitId>;
693
694    fn projection_bounds(
695        self,
696    ) -> impl IntoIterator<Item = ty::Binder<I, ty::ExistentialProjection<I>>>;
697}
698
699#[rust_analyzer::prefer_underscore_import]
700pub trait Span<I: Interner>: Copy + Debug + Hash + Eq + TypeFoldable<I> {
701    fn dummy() -> Self;
702}
703
704#[rust_analyzer::prefer_underscore_import]
705pub trait OpaqueTypeStorageEntries: Debug + Copy + Default {
706    /// Whether the number of opaques has changed in a way that necessitates
707    /// reevaluating a goal. For now, this is only when the number of non-duplicated
708    /// entries changed.
709    fn needs_reevaluation(self, canonicalized: usize) -> bool;
710}
711
712pub trait BoundVarKinds<I: Interner>:
713    Copy + Debug + Hash + Eq + SliceLike<Item = ty::BoundVariableKind<I>> + Default
714{
715    fn from_vars(cx: I, iter: impl IntoIterator<Item = ty::BoundVariableKind<I>>) -> Self;
716}
717
718pub trait SliceLike: Sized + Copy {
719    type Item: Copy;
720    type IntoIter: Iterator<Item = Self::Item> + DoubleEndedIterator;
721
722    fn iter(self) -> Self::IntoIter;
723
724    fn as_slice(&self) -> &[Self::Item];
725
726    fn get(self, idx: usize) -> Option<Self::Item> {
727        self.as_slice().get(idx).copied()
728    }
729
730    fn len(self) -> usize {
731        self.as_slice().len()
732    }
733
734    fn is_empty(self) -> bool {
735        self.len() == 0
736    }
737
738    fn contains(self, t: &Self::Item) -> bool
739    where
740        Self::Item: PartialEq,
741    {
742        self.as_slice().contains(t)
743    }
744
745    fn to_vec(self) -> Vec<Self::Item> {
746        self.as_slice().to_vec()
747    }
748
749    fn last(self) -> Option<Self::Item> {
750        self.as_slice().last().copied()
751    }
752
753    fn split_last(&self) -> Option<(&Self::Item, &[Self::Item])> {
754        self.as_slice().split_last()
755    }
756}
757
758impl<'a, T: Copy> SliceLike for &'a [T] {
759    type Item = T;
760    type IntoIter = std::iter::Copied<std::slice::Iter<'a, T>>;
761
762    fn iter(self) -> Self::IntoIter {
763        self.iter().copied()
764    }
765
766    fn as_slice(&self) -> &[Self::Item] {
767        *self
768    }
769}
770
771impl<'a, T: Copy, const N: usize> SliceLike for &'a [T; N] {
772    type Item = T;
773    type IntoIter = std::iter::Copied<std::slice::Iter<'a, T>>;
774
775    fn iter(self) -> Self::IntoIter {
776        self.into_iter().copied()
777    }
778
779    fn as_slice(&self) -> &[Self::Item] {
780        *self
781    }
782}
783
784impl<'a, S: SliceLike> SliceLike for &'a S {
785    type Item = S::Item;
786    type IntoIter = S::IntoIter;
787
788    fn iter(self) -> Self::IntoIter {
789        (*self).iter()
790    }
791
792    fn as_slice(&self) -> &[Self::Item] {
793        (*self).as_slice()
794    }
795}
796
797#[rust_analyzer::prefer_underscore_import]
798pub trait Symbol<I>: Copy + Hash + PartialEq + Eq + Debug {
799    fn is_kw_underscore_lifetime(self) -> bool;
800}