Skip to main content

hir_ty/next_solver/
def_id.rs

1//! Definition of `SolverDefId`
2
3use hir_def::{
4    AdtId, AttrDefId, BuiltinDeriveImplId, CallableDefId, ConstId, DefWithBodyId, EnumId,
5    EnumVariantId, ExpressionStoreOwnerId, FunctionId, GenericDefId, ImplId, StaticId, StructId,
6    TraitId, TypeAliasId, UnionId, VariantId,
7    hir::ExprId,
8    signatures::{
9        ConstSignature, EnumSignature, FunctionSignature, StaticSignature, StructSignature,
10        TraitSignature, TypeAliasSignature, UnionSignature,
11    },
12    type_ref::{ConstRef, TypeRefId},
13};
14use rustc_type_ir::inherent;
15use salsa::SalsaValue;
16use stdx::impl_from;
17
18use crate::{
19    InferBodyId,
20    db::{
21        AnonConstId, GeneralConstId, InternedClosureId, InternedCoroutineClosureId,
22        InternedCoroutineId, InternedOpaqueTyId,
23    },
24};
25
26use super::DbInterner;
27
28#[derive(Debug, PartialOrd, Ord, Clone, Copy, PartialEq, Eq, Hash)]
29pub enum Ctor {
30    Struct(StructId),
31    Enum(EnumVariantId),
32}
33
34#[derive(PartialOrd, Ord, Clone, Copy, PartialEq, Eq, Hash, SalsaValue)]
35pub enum SolverDefId<'db> {
36    AdtId(AdtId),
37    ConstId(ConstId),
38    FunctionId(FunctionId),
39    ImplId(ImplId),
40    BuiltinDeriveImplId(BuiltinDeriveImplId),
41    StaticId(StaticId),
42    AnonConstId(AnonConstId<'db>),
43    TraitId(TraitId),
44    TypeAliasId(TypeAliasId),
45    InternedClosureId(InternedClosureId<'db>),
46    InternedCoroutineId(InternedCoroutineId<'db>),
47    InternedCoroutineClosureId(InternedCoroutineClosureId<'db>),
48    InternedOpaqueTyId(InternedOpaqueTyId<'db>),
49    EnumVariantId(EnumVariantId),
50    Ctor(Ctor),
51}
52
53impl std::fmt::Debug for SolverDefId<'_> {
54    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55        let interner = DbInterner::conjure();
56        let db = interner.db;
57        match *self {
58            SolverDefId::AdtId(AdtId::StructId(id)) => {
59                f.debug_tuple("AdtId").field(&StructSignature::of(db, id).name.as_str()).finish()
60            }
61            SolverDefId::AdtId(AdtId::EnumId(id)) => {
62                f.debug_tuple("AdtId").field(&EnumSignature::of(db, id).name.as_str()).finish()
63            }
64            SolverDefId::AdtId(AdtId::UnionId(id)) => {
65                f.debug_tuple("AdtId").field(&UnionSignature::of(db, id).name.as_str()).finish()
66            }
67            SolverDefId::ConstId(id) => f
68                .debug_tuple("ConstId")
69                .field(&ConstSignature::of(db, id).name.as_ref().map_or("_", |name| name.as_str()))
70                .finish(),
71            SolverDefId::FunctionId(id) => f
72                .debug_tuple("FunctionId")
73                .field(&FunctionSignature::of(db, id).name.as_str())
74                .finish(),
75            SolverDefId::ImplId(id) => f.debug_tuple("ImplId").field(&id).finish(),
76            SolverDefId::BuiltinDeriveImplId(id) => f.debug_tuple("ImplId").field(&id).finish(),
77            SolverDefId::StaticId(id) => {
78                f.debug_tuple("StaticId").field(&StaticSignature::of(db, id).name.as_str()).finish()
79            }
80            SolverDefId::TraitId(id) => {
81                f.debug_tuple("TraitId").field(&TraitSignature::of(db, id).name.as_str()).finish()
82            }
83            SolverDefId::TypeAliasId(id) => f
84                .debug_tuple("TypeAliasId")
85                .field(&TypeAliasSignature::of(db, id).name.as_str())
86                .finish(),
87            SolverDefId::InternedClosureId(id) => {
88                f.debug_tuple("InternedClosureId").field(&id).finish()
89            }
90            SolverDefId::InternedCoroutineId(id) => {
91                f.debug_tuple("InternedCoroutineId").field(&id).finish()
92            }
93            SolverDefId::InternedCoroutineClosureId(id) => {
94                f.debug_tuple("InternedCoroutineClosureId").field(&id).finish()
95            }
96            SolverDefId::InternedOpaqueTyId(id) => {
97                f.debug_tuple("InternedOpaqueTyId").field(&id).finish()
98            }
99            SolverDefId::EnumVariantId(id) => {
100                let parent_enum = id.loc(db).parent;
101                f.debug_tuple("EnumVariantId")
102                    .field(&format_args!(
103                        "\"{}::{}\"",
104                        EnumSignature::of(db, parent_enum).name.as_str(),
105                        parent_enum.enum_variants(db).variant_name_by_id(id).unwrap().as_str()
106                    ))
107                    .finish()
108            }
109            SolverDefId::AnonConstId(id) => f.debug_tuple("AnonConstId").field(&id).finish(),
110            SolverDefId::Ctor(Ctor::Struct(id)) => {
111                f.debug_tuple("Ctor").field(&StructSignature::of(db, id).name.as_str()).finish()
112            }
113            SolverDefId::Ctor(Ctor::Enum(id)) => {
114                let parent_enum = id.loc(db).parent;
115                f.debug_tuple("Ctor")
116                    .field(&format_args!(
117                        "\"{}::{}\"",
118                        EnumSignature::of(db, parent_enum).name.as_str(),
119                        parent_enum.enum_variants(db).variant_name_by_id(id).unwrap().as_str()
120                    ))
121                    .finish()
122            }
123        }
124    }
125}
126
127impl_from!(
128    impl<'db>
129    AdtId(StructId, EnumId, UnionId),
130    ConstId,
131    FunctionId,
132    ImplId,
133    BuiltinDeriveImplId,
134    StaticId,
135    AnonConstId<'db>,
136    TraitId,
137    TypeAliasId,
138    InternedClosureId<'db>,
139    InternedCoroutineId<'db>,
140    InternedCoroutineClosureId<'db>,
141    InternedOpaqueTyId<'db>,
142    EnumVariantId,
143    Ctor
144    for SolverDefId<'db>
145);
146
147impl_from!(
148    impl<'db>
149    GenericDefId { AdtId, ConstId, FunctionId, ImplId, StaticId, TraitId, TypeAliasId }
150    for SolverDefId<'db>
151);
152
153impl<'db> From<GeneralConstId<'db>> for SolverDefId<'db> {
154    #[inline]
155    fn from(value: GeneralConstId<'db>) -> Self {
156        match value {
157            GeneralConstId::ConstId(const_id) => SolverDefId::ConstId(const_id),
158            GeneralConstId::StaticId(static_id) => SolverDefId::StaticId(static_id),
159            GeneralConstId::AnonConstId(anon_const_id) => SolverDefId::AnonConstId(anon_const_id),
160        }
161    }
162}
163
164impl<'db> From<CallableDefId> for SolverDefId<'db> {
165    #[inline]
166    fn from(value: CallableDefId) -> Self {
167        match value {
168            CallableDefId::FunctionId(id) => id.into(),
169            CallableDefId::StructId(id) => id.into(),
170            CallableDefId::EnumVariantId(id) => id.into(),
171        }
172    }
173}
174
175impl<'db> From<DefWithBodyId> for SolverDefId<'db> {
176    #[inline]
177    fn from(value: DefWithBodyId) -> Self {
178        match value {
179            DefWithBodyId::FunctionId(id) => id.into(),
180            DefWithBodyId::StaticId(id) => id.into(),
181            DefWithBodyId::ConstId(id) => id.into(),
182            DefWithBodyId::VariantId(id) => id.into(),
183        }
184    }
185}
186
187impl<'db> From<InferBodyId<'db>> for SolverDefId<'db> {
188    #[inline]
189    fn from(value: InferBodyId<'db>) -> Self {
190        match value {
191            InferBodyId::DefWithBodyId(id) => id.into(),
192            InferBodyId::AnonConstId(id) => id.into(),
193        }
194    }
195}
196
197impl<'db> From<VariantId> for SolverDefId<'db> {
198    #[inline]
199    fn from(value: VariantId) -> Self {
200        match value {
201            VariantId::EnumVariantId(id) => id.into(),
202            VariantId::StructId(id) => id.into(),
203            VariantId::UnionId(id) => id.into(),
204        }
205    }
206}
207
208impl<'db> From<ExpressionStoreOwnerId> for SolverDefId<'db> {
209    #[inline]
210    fn from(value: ExpressionStoreOwnerId) -> Self {
211        match value {
212            ExpressionStoreOwnerId::Body(body_id) => body_id.into(),
213            ExpressionStoreOwnerId::Signature(sig_id) => sig_id.into(),
214            ExpressionStoreOwnerId::VariantFields(variant_id) => variant_id.into(),
215        }
216    }
217}
218
219impl TryFrom<SolverDefId<'_>> for AttrDefId {
220    type Error = ();
221    #[inline]
222    fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> {
223        match value {
224            SolverDefId::AdtId(it) => Ok(it.into()),
225            SolverDefId::ConstId(it) => Ok(it.into()),
226            SolverDefId::FunctionId(it) => Ok(it.into()),
227            SolverDefId::ImplId(it) => Ok(it.into()),
228            SolverDefId::StaticId(it) => Ok(it.into()),
229            SolverDefId::TraitId(it) => Ok(it.into()),
230            SolverDefId::TypeAliasId(it) => Ok(it.into()),
231            SolverDefId::EnumVariantId(it) => Ok(it.into()),
232            SolverDefId::Ctor(Ctor::Struct(it)) => Ok(it.into()),
233            SolverDefId::Ctor(Ctor::Enum(it)) => Ok(it.into()),
234            SolverDefId::BuiltinDeriveImplId(_)
235            | SolverDefId::InternedClosureId(_)
236            | SolverDefId::InternedCoroutineId(_)
237            | SolverDefId::InternedCoroutineClosureId(_)
238            | SolverDefId::InternedOpaqueTyId(_)
239            | SolverDefId::AnonConstId(_) => Err(()),
240        }
241    }
242}
243
244impl TryFrom<SolverDefId<'_>> for DefWithBodyId {
245    type Error = ();
246
247    #[inline]
248    fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> {
249        let id = match value {
250            SolverDefId::ConstId(id) => id.into(),
251            SolverDefId::FunctionId(id) => id.into(),
252            SolverDefId::StaticId(id) => id.into(),
253            SolverDefId::EnumVariantId(id) | SolverDefId::Ctor(Ctor::Enum(id)) => id.into(),
254            SolverDefId::InternedOpaqueTyId(_)
255            | SolverDefId::TraitId(_)
256            | SolverDefId::TypeAliasId(_)
257            | SolverDefId::ImplId(_)
258            | SolverDefId::BuiltinDeriveImplId(_)
259            | SolverDefId::InternedClosureId(_)
260            | SolverDefId::InternedCoroutineId(_)
261            | SolverDefId::InternedCoroutineClosureId(_)
262            | SolverDefId::Ctor(Ctor::Struct(_))
263            | SolverDefId::AnonConstId(_)
264            | SolverDefId::AdtId(_) => return Err(()),
265        };
266        Ok(id)
267    }
268}
269
270impl<'db> TryFrom<SolverDefId<'db>> for InferBodyId<'db> {
271    type Error = ();
272
273    #[inline]
274    fn try_from(value: SolverDefId<'db>) -> Result<Self, Self::Error> {
275        let id = match value {
276            SolverDefId::ConstId(id) => id.into(),
277            SolverDefId::FunctionId(id) => id.into(),
278            SolverDefId::StaticId(id) => id.into(),
279            SolverDefId::EnumVariantId(id) | SolverDefId::Ctor(Ctor::Enum(id)) => id.into(),
280            SolverDefId::AnonConstId(id) => id.into(),
281            SolverDefId::InternedOpaqueTyId(_)
282            | SolverDefId::TraitId(_)
283            | SolverDefId::TypeAliasId(_)
284            | SolverDefId::ImplId(_)
285            | SolverDefId::BuiltinDeriveImplId(_)
286            | SolverDefId::InternedClosureId(_)
287            | SolverDefId::InternedCoroutineId(_)
288            | SolverDefId::InternedCoroutineClosureId(_)
289            | SolverDefId::Ctor(Ctor::Struct(_))
290            | SolverDefId::AdtId(_) => return Err(()),
291        };
292        Ok(id)
293    }
294}
295
296impl TryFrom<SolverDefId<'_>> for GenericDefId {
297    type Error = ();
298
299    fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> {
300        Ok(match value {
301            SolverDefId::AdtId(adt_id) => GenericDefId::AdtId(adt_id),
302            SolverDefId::ConstId(const_id) => GenericDefId::ConstId(const_id),
303            SolverDefId::FunctionId(function_id) => GenericDefId::FunctionId(function_id),
304            SolverDefId::ImplId(impl_id) => GenericDefId::ImplId(impl_id),
305            SolverDefId::StaticId(static_id) => GenericDefId::StaticId(static_id),
306            SolverDefId::TraitId(trait_id) => GenericDefId::TraitId(trait_id),
307            SolverDefId::TypeAliasId(type_alias_id) => GenericDefId::TypeAliasId(type_alias_id),
308            SolverDefId::InternedClosureId(_)
309            | SolverDefId::InternedCoroutineId(_)
310            | SolverDefId::InternedCoroutineClosureId(_)
311            | SolverDefId::InternedOpaqueTyId(_)
312            | SolverDefId::EnumVariantId(_)
313            | SolverDefId::BuiltinDeriveImplId(_)
314            | SolverDefId::AnonConstId(_)
315            | SolverDefId::Ctor(_) => return Err(()),
316        })
317    }
318}
319
320impl<'db> inherent::DefId<DbInterner<'db>> for SolverDefId<'db> {
321    fn as_local(self) -> Option<SolverDefId<'db>> {
322        Some(self)
323    }
324    fn is_local(self) -> bool {
325        true
326    }
327}
328
329macro_rules! declare_id_wrapper {
330    ($name:ident, $wraps:ident) => {
331        declare_id_wrapper!($name, $wraps, SolverDefId<'db>);
332    };
333
334    ($name:ident, $wraps:ident, $local:ty) => {
335        declare_id_wrapper!($name, $wraps, $local, no_try_from);
336
337        impl TryFrom<SolverDefId<'_>> for $name {
338            type Error = ();
339
340            #[inline]
341            fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> {
342                match value {
343                    SolverDefId::$wraps(it) => Ok(Self(it)),
344                    _ => Err(()),
345                }
346            }
347        }
348    };
349
350    ($name:ident, $wraps:ident, $local:ty, no_try_from) => {
351        #[derive(Clone, Copy, PartialEq, Eq, Hash)]
352        pub struct $name(pub $wraps);
353
354        impl std::fmt::Debug for $name {
355            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
356                std::fmt::Debug::fmt(&SolverDefId::from(self.0), f)
357            }
358        }
359
360        impl From<$name> for $wraps {
361            #[inline]
362            fn from(value: $name) -> $wraps {
363                value.0
364            }
365        }
366
367        impl From<$wraps> for $name {
368            #[inline]
369            fn from(value: $wraps) -> $name {
370                Self(value)
371            }
372        }
373
374        impl<'db> From<$name> for SolverDefId<'db> {
375            #[inline]
376            fn from(value: $name) -> SolverDefId<'db> {
377                value.0.into()
378            }
379        }
380
381        impl<'db> inherent::DefId<DbInterner<'db>, $local> for $name {
382            fn as_local(self) -> Option<$local> {
383                Some(self.into())
384            }
385            fn is_local(self) -> bool {
386                true
387            }
388        }
389    };
390}
391
392/// This is similar to [`declare_id_wrapper`], but handles ids which have the `'db` lifetime.
393macro_rules! declare_id_wrapper_with_lt {
394    ($name:ident, $wraps:ident) => {
395        declare_id_wrapper_with_lt!($name, $wraps, SolverDefId<'db>);
396    };
397
398    ($name:ident, $wraps:ident, $local:ty) => {
399        declare_id_wrapper_with_lt!($name, $wraps, $local, no_try_from);
400
401        impl<'db> TryFrom<SolverDefId<'db>> for $name<'db> {
402            type Error = ();
403
404            #[inline]
405            fn try_from(value: SolverDefId<'db>) -> Result<Self, Self::Error> {
406                match value {
407                    SolverDefId::$wraps(it) => Ok(Self(it)),
408                    _ => Err(()),
409                }
410            }
411        }
412    };
413
414    ($name:ident, $wraps:ident, $local:ty, no_try_from) => {
415        #[derive(Clone, Copy, PartialEq, Eq, Hash)]
416        pub struct $name<'db>(pub $wraps<'db>);
417
418        impl std::fmt::Debug for $name<'_> {
419            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
420                std::fmt::Debug::fmt(&SolverDefId::from(self.0), f)
421            }
422        }
423
424        impl<'db> From<$name<'db>> for $wraps<'db> {
425            #[inline]
426            fn from(value: $name<'db>) -> $wraps<'db> {
427                value.0
428            }
429        }
430
431        impl<'db> From<$wraps<'db>> for $name<'db> {
432            #[inline]
433            fn from(value: $wraps<'db>) -> $name<'db> {
434                Self(value)
435            }
436        }
437
438        impl<'db> From<$name<'db>> for SolverDefId<'db> {
439            #[inline]
440            fn from(value: $name<'db>) -> SolverDefId<'db> {
441                value.0.into()
442            }
443        }
444
445        impl<'db> inherent::DefId<DbInterner<'db>, $local> for $name<'db> {
446            fn as_local(self) -> Option<$local> {
447                Some(self.into())
448            }
449            fn is_local(self) -> bool {
450                true
451            }
452        }
453    };
454}
455
456declare_id_wrapper!(TraitIdWrapper, TraitId);
457declare_id_wrapper!(TypeAliasIdWrapper, TypeAliasId);
458declare_id_wrapper_with_lt!(ClosureIdWrapper, InternedClosureId);
459declare_id_wrapper_with_lt!(CoroutineIdWrapper, InternedCoroutineId);
460declare_id_wrapper_with_lt!(CoroutineClosureIdWrapper, InternedCoroutineClosureId);
461declare_id_wrapper!(AdtIdWrapper, AdtId);
462declare_id_wrapper_with_lt!(OpaqueTyIdWrapper, InternedOpaqueTyId, OpaqueTyIdWrapper<'db>);
463
464macro_rules! declare_ty_const_pair {
465    ( $ty_id_name:ident, $const_id_name:ident, $term_id_name:ident ) => {
466        declare_id_wrapper!($ty_id_name, TypeAliasId);
467        declare_id_wrapper!($const_id_name, ConstId);
468        declare_id_wrapper!($term_id_name, TermId, SolverDefId<'db>, no_try_from);
469
470        impl TryFrom<SolverDefId<'_>> for $term_id_name {
471            type Error = ();
472
473            #[inline]
474            fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> {
475                match value {
476                    SolverDefId::TypeAliasId(it) => Ok(Self(TermId::TypeAliasId(it))),
477                    SolverDefId::ConstId(it) => Ok(Self(TermId::ConstId(it))),
478                    _ => Err(()),
479                }
480            }
481        }
482
483        impl From<$ty_id_name> for $term_id_name {
484            fn from(value: $ty_id_name) -> Self {
485                $term_id_name(TermId::TypeAliasId(value.0))
486            }
487        }
488
489        impl From<$const_id_name> for $term_id_name {
490            fn from(value: $const_id_name) -> Self {
491                $term_id_name(TermId::ConstId(value.0))
492            }
493        }
494
495        impl TryFrom<$term_id_name> for $ty_id_name {
496            type Error = ();
497
498            fn try_from(value: $term_id_name) -> Result<Self, Self::Error> {
499                match value.0 {
500                    TermId::TypeAliasId(id) => Ok($ty_id_name(id)),
501                    TermId::ConstId(_) => Err(()),
502                }
503            }
504        }
505
506        impl TryFrom<$term_id_name> for $const_id_name {
507            type Error = ();
508
509            fn try_from(value: $term_id_name) -> Result<Self, Self::Error> {
510                match value.0 {
511                    TermId::ConstId(id) => Ok($const_id_name(id)),
512                    TermId::TypeAliasId(_) => Err(()),
513                }
514            }
515        }
516
517        impl<'db> From<$const_id_name> for GeneralConstIdWrapper<'db> {
518            fn from(value: $const_id_name) -> Self {
519                GeneralConstIdWrapper(GeneralConstId::ConstId(value.0))
520            }
521        }
522    };
523}
524
525declare_ty_const_pair!(TraitAssocTyId, TraitAssocConstId, TraitAssocTermId);
526declare_ty_const_pair!(ImplOrTraitAssocTyId, ImplOrTraitAssocConstId, ImplOrTraitAssocTermId);
527declare_ty_const_pair!(FreeTyAliasId, FreeConstAliasId, FreeTermAliasId);
528declare_ty_const_pair!(InherentAssocTyId, InherentAssocConstId, InherentAssocTermId);
529
530#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
531pub enum TermId {
532    TypeAliasId(TypeAliasId),
533    ConstId(ConstId),
534}
535impl_from!(TypeAliasId, ConstId for TermId);
536
537impl<'db> From<TermId> for SolverDefId<'db> {
538    fn from(value: TermId) -> Self {
539        match value {
540            TermId::TypeAliasId(id) => id.into(),
541            TermId::ConstId(id) => id.into(),
542        }
543    }
544}
545
546#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
547pub enum TermRefId {
548    TypeRefId(TypeRefId),
549    ConstRef(ConstRef),
550}
551impl_from!(TypeRefId, ConstRef for TermRefId);
552
553#[derive(Copy, Clone, Hash, PartialEq, Eq)]
554pub struct TermRefIdPacked(u32);
555
556const _: () = assert!(std::mem::size_of::<TermRefIdPacked>() == std::mem::size_of::<u32>());
557
558impl TermRefIdPacked {
559    const CONST_REF_BIT: u32 = 1 << (u32::BITS - 1);
560    const INDEX_MASK: u32 = !Self::CONST_REF_BIT;
561
562    pub fn unpack(self) -> TermRefId {
563        let is_const_ref = self.0 & Self::CONST_REF_BIT != 0;
564        match is_const_ref {
565            true => {
566                let expr = ExprId::from_raw((self.0 & Self::INDEX_MASK).into());
567                TermRefId::ConstRef(ConstRef { expr })
568            }
569            false => TermRefId::TypeRefId(TypeRefId::from_raw(self.0.into())),
570        }
571    }
572}
573
574impl std::fmt::Debug for TermRefIdPacked {
575    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
576        self.unpack().fmt(f)
577    }
578}
579
580impl From<TypeRefId> for TermRefIdPacked {
581    fn from(value: TypeRefId) -> Self {
582        let value = value.into_raw().into_u32();
583        // virtually impossible to have IDs that high
584        debug_assert_eq!(value & Self::CONST_REF_BIT, 0);
585        Self(value)
586    }
587}
588
589impl From<ConstRef> for TermRefIdPacked {
590    fn from(value: ConstRef) -> Self {
591        let value = value.expr.into_raw().into_u32();
592        // virtually impossible to have IDs that high
593        debug_assert_eq!(value & Self::CONST_REF_BIT, 0);
594        Self(value | Self::CONST_REF_BIT)
595    }
596}
597
598#[derive(Clone, Copy, PartialEq, Eq, Hash)]
599pub struct GeneralConstIdWrapper<'db>(pub GeneralConstId<'db>);
600
601impl std::fmt::Debug for GeneralConstIdWrapper<'_> {
602    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
603        std::fmt::Debug::fmt(&self.0, f)
604    }
605}
606impl<'db> From<GeneralConstIdWrapper<'db>> for GeneralConstId<'db> {
607    #[inline]
608    fn from(value: GeneralConstIdWrapper<'db>) -> GeneralConstId<'db> {
609        value.0
610    }
611}
612impl<'db> From<GeneralConstId<'db>> for GeneralConstIdWrapper<'db> {
613    #[inline]
614    fn from(value: GeneralConstId<'db>) -> GeneralConstIdWrapper<'db> {
615        Self(value)
616    }
617}
618impl<'db> From<GeneralConstIdWrapper<'db>> for SolverDefId<'db> {
619    #[inline]
620    fn from(value: GeneralConstIdWrapper<'db>) -> SolverDefId<'db> {
621        match value.0 {
622            GeneralConstId::ConstId(id) => SolverDefId::ConstId(id),
623            GeneralConstId::StaticId(id) => SolverDefId::StaticId(id),
624            GeneralConstId::AnonConstId(id) => SolverDefId::AnonConstId(id),
625        }
626    }
627}
628impl<'db> TryFrom<SolverDefId<'db>> for GeneralConstIdWrapper<'db> {
629    type Error = ();
630    #[inline]
631    fn try_from(value: SolverDefId<'db>) -> Result<Self, Self::Error> {
632        match value {
633            SolverDefId::ConstId(it) => Ok(Self(it.into())),
634            SolverDefId::StaticId(it) => Ok(Self(it.into())),
635            SolverDefId::AnonConstId(it) => Ok(Self(it.into())),
636            _ => Err(()),
637        }
638    }
639}
640impl<'db> inherent::DefId<DbInterner<'db>> for GeneralConstIdWrapper<'db> {
641    fn as_local(self) -> Option<SolverDefId<'db>> {
642        Some(self.into())
643    }
644    fn is_local(self) -> bool {
645        true
646    }
647}
648
649#[derive(Clone, Copy, PartialEq, Eq, Hash)]
650pub struct CallableIdWrapper(pub CallableDefId);
651
652impl std::fmt::Debug for CallableIdWrapper {
653    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
654        std::fmt::Debug::fmt(&self.0, f)
655    }
656}
657impl From<CallableIdWrapper> for CallableDefId {
658    #[inline]
659    fn from(value: CallableIdWrapper) -> CallableDefId {
660        value.0
661    }
662}
663impl From<CallableDefId> for CallableIdWrapper {
664    #[inline]
665    fn from(value: CallableDefId) -> CallableIdWrapper {
666        Self(value)
667    }
668}
669impl<'db> From<CallableIdWrapper> for SolverDefId<'db> {
670    #[inline]
671    fn from(value: CallableIdWrapper) -> SolverDefId<'db> {
672        match value.0 {
673            CallableDefId::FunctionId(it) => it.into(),
674            CallableDefId::StructId(it) => Ctor::Struct(it).into(),
675            CallableDefId::EnumVariantId(it) => Ctor::Enum(it).into(),
676        }
677    }
678}
679impl TryFrom<SolverDefId<'_>> for CallableIdWrapper {
680    type Error = ();
681    #[inline]
682    fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> {
683        match value {
684            SolverDefId::FunctionId(it) => Ok(Self(it.into())),
685            SolverDefId::Ctor(Ctor::Struct(it)) => Ok(Self(it.into())),
686            SolverDefId::Ctor(Ctor::Enum(it)) => Ok(Self(it.into())),
687            _ => Err(()),
688        }
689    }
690}
691impl<'db> inherent::DefId<DbInterner<'db>> for CallableIdWrapper {
692    fn as_local(self) -> Option<SolverDefId<'db>> {
693        Some(self.into())
694    }
695    fn is_local(self) -> bool {
696        true
697    }
698}
699
700#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
701pub enum AnyImplId {
702    ImplId(ImplId),
703    BuiltinDeriveImplId(BuiltinDeriveImplId),
704}
705
706impl_from!(ImplId, BuiltinDeriveImplId for AnyImplId);
707
708impl<'db> From<AnyImplId> for SolverDefId<'db> {
709    #[inline]
710    fn from(value: AnyImplId) -> SolverDefId<'db> {
711        match value {
712            AnyImplId::ImplId(it) => it.into(),
713            AnyImplId::BuiltinDeriveImplId(it) => it.into(),
714        }
715    }
716}
717impl TryFrom<SolverDefId<'_>> for AnyImplId {
718    type Error = ();
719    #[inline]
720    fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> {
721        match value {
722            SolverDefId::ImplId(it) => Ok(it.into()),
723            SolverDefId::BuiltinDeriveImplId(it) => Ok(it.into()),
724            _ => Err(()),
725        }
726    }
727}
728impl<'db> inherent::DefId<DbInterner<'db>> for AnyImplId {
729    fn as_local(self) -> Option<SolverDefId<'db>> {
730        Some(self.into())
731    }
732    fn is_local(self) -> bool {
733        true
734    }
735}