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