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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use petgraph::prelude::*;
use crate::solve::Solver;
use crate::RustIrDatabase;
use chalk_ir::interner::Interner;
use chalk_ir::{self, ImplId, TraitId};
use std::collections::BTreeMap;
use std::fmt;
use std::sync::Arc;
pub mod orphan;
mod solve;
pub struct CoherenceSolver<'a, I: Interner> {
db: &'a dyn RustIrDatabase<I>,
solver_builder: &'a dyn Fn() -> Box<dyn Solver<I>>,
trait_id: TraitId<I>,
}
#[derive(Debug)]
pub enum CoherenceError<I: Interner> {
OverlappingImpls(TraitId<I>),
FailedOrphanCheck(TraitId<I>),
}
impl<I: Interner> fmt::Display for CoherenceError<I> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CoherenceError::OverlappingImpls(id) => {
write!(f, "overlapping impls of trait `{:?}`", id)
}
CoherenceError::FailedOrphanCheck(id) => {
write!(f, "impl for trait `{:?}` violates the orphan rules", id)
}
}
}
}
impl<I: Interner> std::error::Error for CoherenceError<I> {}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SpecializationPriorities<I: Interner> {
map: BTreeMap<ImplId<I>, SpecializationPriority>,
}
impl<I: Interner> SpecializationPriorities<I> {
pub fn new() -> Self {
Self {
map: BTreeMap::new(),
}
}
pub fn priority(&self, impl_id: ImplId<I>) -> SpecializationPriority {
self.map[&impl_id]
}
fn insert(&mut self, impl_id: ImplId<I>, p: SpecializationPriority) {
let old_value = self.map.insert(impl_id, p);
assert!(old_value.is_none());
}
}
#[derive(Copy, Clone, Default, PartialOrd, Ord, PartialEq, Eq, Debug)]
pub struct SpecializationPriority(usize);
impl<'a, I> CoherenceSolver<'a, I>
where
I: Interner,
{
pub fn new(
db: &'a dyn RustIrDatabase<I>,
solver_builder: &'a dyn Fn() -> Box<dyn Solver<I>>,
trait_id: TraitId<I>,
) -> Self {
Self {
db,
solver_builder,
trait_id,
}
}
pub fn specialization_priorities(
&self,
) -> Result<Arc<SpecializationPriorities<I>>, CoherenceError<I>> {
let mut result = SpecializationPriorities::<I>::new();
let forest = self.build_specialization_forest()?;
for root_idx in forest.externals(Direction::Incoming) {
self.set_priorities(root_idx, &forest, 0, &mut result);
}
Ok(Arc::new(result))
}
fn build_specialization_forest(&self) -> Result<Graph<ImplId<I>, ()>, CoherenceError<I>> {
let mut forest = DiGraphMap::new();
self.visit_specializations_of_trait(|less_special, more_special| {
forest.add_edge(less_special, more_special, ());
})?;
Ok(forest.into_graph())
}
fn set_priorities(
&self,
idx: NodeIndex,
forest: &Graph<ImplId<I>, ()>,
p: usize,
map: &mut SpecializationPriorities<I>,
) {
{
let impl_id = forest
.node_weight(idx)
.expect("index should be a valid index into graph");
map.insert(*impl_id, SpecializationPriority(p));
}
for child_idx in forest.neighbors(idx) {
self.set_priorities(child_idx, forest, p + 1, map);
}
}
}