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
use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_middle::ty::{self, TyCtxt};
use rustc_trait_selection::traits::{self, SkipLeakCheck};
use smallvec::SmallVec;
pub fn crate_inherent_impls_overlap_check(tcx: TyCtxt<'_>, crate_num: CrateNum) {
assert_eq!(crate_num, LOCAL_CRATE);
let krate = tcx.hir().krate();
krate.visit_all_item_likes(&mut InherentOverlapChecker { tcx });
}
struct InherentOverlapChecker<'tcx> {
tcx: TyCtxt<'tcx>,
}
impl InherentOverlapChecker<'tcx> {
fn impls_have_common_items(
&self,
impl_items1: &ty::AssociatedItems<'_>,
impl_items2: &ty::AssociatedItems<'_>,
) -> bool {
let mut impl_items1 = &impl_items1;
let mut impl_items2 = &impl_items2;
if impl_items1.len() > impl_items2.len() {
std::mem::swap(&mut impl_items1, &mut impl_items2);
}
for item1 in impl_items1.in_definition_order() {
let collision = impl_items2.filter_by_name_unhygienic(item1.ident.name).any(|item2| {
item1.kind.namespace() == item2.kind.namespace()
&& item1.ident.normalize_to_macros_2_0()
== item2.ident.normalize_to_macros_2_0()
});
if collision {
return true;
}
}
false
}
fn check_for_common_items_in_impls(
&self,
impl1: DefId,
impl2: DefId,
overlap: traits::OverlapResult<'_>,
) {
let impl_items1 = self.tcx.associated_items(impl1);
let impl_items2 = self.tcx.associated_items(impl2);
for item1 in impl_items1.in_definition_order() {
let collision = impl_items2.filter_by_name_unhygienic(item1.ident.name).find(|item2| {
item1.kind.namespace() == item2.kind.namespace()
&& item1.ident.normalize_to_macros_2_0()
== item2.ident.normalize_to_macros_2_0()
});
if let Some(item2) = collision {
let name = item1.ident.normalize_to_macros_2_0();
let mut err = struct_span_err!(
self.tcx.sess,
self.tcx.span_of_impl(item1.def_id).unwrap(),
E0592,
"duplicate definitions with name `{}`",
name
);
err.span_label(
self.tcx.span_of_impl(item1.def_id).unwrap(),
format!("duplicate definitions for `{}`", name),
);
err.span_label(
self.tcx.span_of_impl(item2.def_id).unwrap(),
format!("other definition for `{}`", name),
);
for cause in &overlap.intercrate_ambiguity_causes {
cause.add_intercrate_ambiguity_hint(&mut err);
}
if overlap.involves_placeholder {
traits::add_placeholder_note(&mut err);
}
err.emit();
}
}
}
fn check_for_overlapping_inherent_impls(&self, impl1_def_id: DefId, impl2_def_id: DefId) {
traits::overlapping_impls(
self.tcx,
impl1_def_id,
impl2_def_id,
SkipLeakCheck::Yes,
|overlap| {
self.check_for_common_items_in_impls(impl1_def_id, impl2_def_id, overlap);
false
},
|| true,
);
}
}
impl ItemLikeVisitor<'v> for InherentOverlapChecker<'tcx> {
fn visit_item(&mut self, item: &'v hir::Item<'v>) {
match item.kind {
hir::ItemKind::Enum(..)
| hir::ItemKind::Struct(..)
| hir::ItemKind::Trait(..)
| hir::ItemKind::Union(..) => {
let ty_def_id = self.tcx.hir().local_def_id(item.hir_id);
let impls = self.tcx.inherent_impls(ty_def_id);
if impls.len() <= 1 {
return;
}
let impls_items = impls
.iter()
.map(|impl_def_id| (impl_def_id, self.tcx.associated_items(*impl_def_id)))
.collect::<SmallVec<[_; 8]>>();
for (i, &(&impl1_def_id, impl_items1)) in impls_items.iter().enumerate() {
for &(&impl2_def_id, impl_items2) in &impls_items[(i + 1)..] {
if self.impls_have_common_items(impl_items1, impl_items2) {
self.check_for_overlapping_inherent_impls(impl1_def_id, impl2_def_id);
}
}
}
}
_ => {}
}
}
fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem<'v>) {}
fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem<'v>) {}
}