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