aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/chalk_cast.rs
blob: df6492113f96d336b820b70410444e335f2519ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! Implementations of the Chalk `Cast` trait for our types.

use chalk_ir::{
    cast::{Cast, CastTo},
    interner::HasInterner,
};

use crate::{AliasEq, DomainGoal, GenericArg, GenericArgData, Interner, TraitRef, Ty, WhereClause};

macro_rules! has_interner {
    ($t:ty) => {
        impl HasInterner for $t {
            type Interner = crate::Interner;
        }
    };
}

has_interner!(WhereClause);
has_interner!(DomainGoal);
has_interner!(GenericArg);
has_interner!(Ty);

impl CastTo<WhereClause> for TraitRef {
    fn cast_to(self, _interner: &Interner) -> WhereClause {
        WhereClause::Implemented(self)
    }
}

impl CastTo<WhereClause> for AliasEq {
    fn cast_to(self, _interner: &Interner) -> WhereClause {
        WhereClause::AliasEq(self)
    }
}

impl CastTo<DomainGoal> for WhereClause {
    fn cast_to(self, _interner: &Interner) -> DomainGoal {
        DomainGoal::Holds(self)
    }
}

impl CastTo<GenericArg> for Ty {
    fn cast_to(self, interner: &Interner) -> GenericArg {
        GenericArg::new(interner, GenericArgData::Ty(self))
    }
}

macro_rules! transitive_impl {
    ($a:ty, $b:ty, $c:ty) => {
        impl CastTo<$c> for $a {
            fn cast_to(self, interner: &Interner) -> $c {
                self.cast::<$b>(interner).cast(interner)
            }
        }
    };
}

// In Chalk, these can be done as blanket impls, but that doesn't work here
// because of coherence

transitive_impl!(TraitRef, WhereClause, DomainGoal);
transitive_impl!(AliasEq, WhereClause, DomainGoal);

macro_rules! reflexive_impl {
    ($a:ty) => {
        impl CastTo<$a> for $a {
            fn cast_to(self, _interner: &Interner) -> $a {
                self
            }
        }
    };
}

reflexive_impl!(GenericArg);