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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use middle::traits;
use middle::ty;
use middle::infer::{self, new_infer_ctxt};
use syntax::ast::DefId;
use syntax::ast::LOCAL_CRATE;
use syntax::ast;
use syntax::ast_util;
use syntax::visit;
use syntax::codemap::Span;
use util::nodemap::DefIdMap;
use util::ppaux::{Repr, UserString};
pub fn check(tcx: &ty::ctxt) {
let mut overlap = OverlapChecker { tcx: tcx, default_impls: DefIdMap() };
overlap.check_for_overlapping_impls();
visit::walk_crate(&mut overlap, tcx.map.krate());
}
struct OverlapChecker<'cx, 'tcx:'cx> {
tcx: &'cx ty::ctxt<'tcx>,
default_impls: DefIdMap<ast::NodeId>,
}
impl<'cx, 'tcx> OverlapChecker<'cx, 'tcx> {
fn check_for_overlapping_impls(&self) {
debug!("check_for_overlapping_impls");
let trait_defs: Vec<_> = self.tcx.trait_defs.borrow().values().cloned().collect();
for trait_def in trait_defs {
ty::populate_implementations_for_trait_if_necessary(
self.tcx,
trait_def.trait_ref.def_id);
self.check_for_overlapping_impls_of_trait(trait_def);
}
}
fn check_for_overlapping_impls_of_trait(&self,
trait_def: &'tcx ty::TraitDef<'tcx>)
{
debug!("check_for_overlapping_impls_of_trait(trait_def={})",
trait_def.repr(self.tcx));
let blanket_impls = trait_def.blanket_impls.borrow();
let nonblanket_impls = trait_def.nonblanket_impls.borrow();
let trait_def_id = trait_def.trait_ref.def_id;
for (i, &impl1_def_id) in blanket_impls.iter().enumerate() {
for &impl2_def_id in &blanket_impls[(i+1)..] {
self.check_if_impls_overlap(trait_def_id,
impl1_def_id,
impl2_def_id);
}
for v in nonblanket_impls.values() {
for &impl2_def_id in v {
self.check_if_impls_overlap(trait_def_id,
impl1_def_id,
impl2_def_id);
}
}
}
for impl_group in nonblanket_impls.values() {
for (i, &impl1_def_id) in impl_group.iter().enumerate() {
for &impl2_def_id in &impl_group[(i+1)..] {
self.check_if_impls_overlap(trait_def_id,
impl1_def_id,
impl2_def_id);
}
}
}
}
fn order_impls(&self, impl1_def_id: ast::DefId, impl2_def_id: ast::DefId)
-> Option<(ast::DefId, ast::DefId)> {
if impl1_def_id.krate != ast::LOCAL_CRATE {
if impl2_def_id.krate != ast::LOCAL_CRATE {
None
} else {
Some((impl2_def_id, impl1_def_id))
}
} else if impl2_def_id.krate != ast::LOCAL_CRATE {
Some((impl1_def_id, impl2_def_id))
} else if impl1_def_id.node < impl2_def_id.node {
Some((impl1_def_id, impl2_def_id))
} else {
Some((impl2_def_id, impl1_def_id))
}
}
fn check_if_impls_overlap(&self,
trait_def_id: ast::DefId,
impl1_def_id: ast::DefId,
impl2_def_id: ast::DefId)
{
if let Some((impl1_def_id, impl2_def_id)) = self.order_impls(
impl1_def_id, impl2_def_id)
{
debug!("check_if_impls_overlap({}, {}, {})",
trait_def_id.repr(self.tcx),
impl1_def_id.repr(self.tcx),
impl2_def_id.repr(self.tcx));
let infcx = infer::new_infer_ctxt(self.tcx);
if traits::overlapping_impls(&infcx, impl1_def_id, impl2_def_id) {
self.report_overlap_error(trait_def_id, impl1_def_id, impl2_def_id);
}
}
}
fn report_overlap_error(&self, trait_def_id: ast::DefId,
impl1: ast::DefId, impl2: ast::DefId) {
span_err!(self.tcx.sess, self.span_of_impl(impl1), E0119,
"conflicting implementations for trait `{}`",
ty::item_path_str(self.tcx, trait_def_id));
self.report_overlap_note(impl1, impl2);
}
fn report_overlap_note(&self, impl1: ast::DefId, impl2: ast::DefId) {
if impl2.krate == ast::LOCAL_CRATE {
span_note!(self.tcx.sess, self.span_of_impl(impl2),
"note conflicting implementation here");
} else {
let crate_store = &self.tcx.sess.cstore;
let cdata = crate_store.get_crate_data(impl2.krate);
span_note!(self.tcx.sess, self.span_of_impl(impl1),
"conflicting implementation in crate `{}`",
cdata.name);
}
}
fn span_of_impl(&self, impl_did: ast::DefId) -> Span {
assert_eq!(impl_did.krate, ast::LOCAL_CRATE);
self.tcx.map.span(impl_did.node)
}
}
impl<'cx, 'tcx,'v> visit::Visitor<'v> for OverlapChecker<'cx, 'tcx> {
fn visit_item(&mut self, item: &'v ast::Item) {
match item.node {
ast::ItemDefaultImpl(_, _) => {
let impl_def_id = ast_util::local_def(item.id);
let trait_ref = ty::impl_trait_ref(self.tcx, impl_def_id).unwrap();
let prev_default_impl = self.default_impls.insert(trait_ref.def_id, item.id);
match prev_default_impl {
Some(prev_id) => {
self.report_overlap_error(trait_ref.def_id,
impl_def_id,
ast_util::local_def(prev_id));
}
None => { }
}
}
ast::ItemImpl(_, _, _, Some(_), ref self_ty, _) => {
let impl_def_id = ast_util::local_def(item.id);
let trait_ref = ty::impl_trait_ref(self.tcx, impl_def_id).unwrap();
let trait_def_id = trait_ref.def_id;
match trait_ref.self_ty().sty {
ty::ty_trait(ref data) => {
if !traits::is_object_safe(self.tcx, data.principal_def_id()) {
span_err!(self.tcx.sess, self_ty.span, E0372,
"the trait `{}` cannot be made into an object",
ty::item_path_str(self.tcx, data.principal_def_id()));
} else {
let mut supertrait_def_ids =
traits::supertrait_def_ids(self.tcx, data.principal_def_id());
if supertrait_def_ids.any(|d| d == trait_def_id) {
span_err!(self.tcx.sess, item.span, E0371,
"the object type `{}` automatically \
implements the trait `{}`",
trait_ref.self_ty().user_string(self.tcx),
ty::item_path_str(self.tcx, trait_def_id));
}
}
}
_ => { }
}
}
_ => {
}
}
visit::walk_item(self, item);
}
}