1 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2 use rustc_errors::struct_span_err;
4 use rustc_hir::def::DefKind;
5 use rustc_hir::def_id::DefId;
6 use rustc_index::vec::IndexVec;
7 use rustc_middle::traits::specialization_graph::OverlapMode;
8 use rustc_middle::ty::{self, TyCtxt};
9 use rustc_span::Symbol;
10 use rustc_trait_selection::traits::{self, SkipLeakCheck};
11 use smallvec::SmallVec;
12 use std::collections::hash_map::Entry;
14 pub fn crate_inherent_impls_overlap_check(tcx: TyCtxt<'_>, (): ()) {
15 let mut inherent_overlap_checker = InherentOverlapChecker { tcx };
16 for id in tcx.hir().items() {
17 inherent_overlap_checker.check_item(id);
21 struct InherentOverlapChecker<'tcx> {
25 impl<'tcx> InherentOverlapChecker<'tcx> {
26 /// Checks whether any associated items in impls 1 and 2 share the same identifier and
28 fn impls_have_common_items(
30 impl_items1: &ty::AssocItems<'_>,
31 impl_items2: &ty::AssocItems<'_>,
33 let mut impl_items1 = &impl_items1;
34 let mut impl_items2 = &impl_items2;
36 // Performance optimization: iterate over the smaller list
37 if impl_items1.len() > impl_items2.len() {
38 std::mem::swap(&mut impl_items1, &mut impl_items2);
41 for item1 in impl_items1.in_definition_order() {
42 let collision = impl_items2
43 .filter_by_name_unhygienic(item1.name)
44 .any(|item2| self.compare_hygienically(item1, item2));
54 fn compare_hygienically(&self, item1: &ty::AssocItem, item2: &ty::AssocItem) -> bool {
55 // Symbols and namespace match, compare hygienically.
56 item1.kind.namespace() == item2.kind.namespace()
57 && item1.ident(self.tcx).normalize_to_macros_2_0()
58 == item2.ident(self.tcx).normalize_to_macros_2_0()
61 fn check_for_common_items_in_impls(
65 overlap: traits::OverlapResult<'_>,
67 let impl_items1 = self.tcx.associated_items(impl1);
68 let impl_items2 = self.tcx.associated_items(impl2);
70 for item1 in impl_items1.in_definition_order() {
71 let collision = impl_items2
72 .filter_by_name_unhygienic(item1.name)
73 .find(|item2| self.compare_hygienically(item1, item2));
75 if let Some(item2) = collision {
76 let name = item1.ident(self.tcx).normalize_to_macros_2_0();
77 let mut err = struct_span_err!(
79 self.tcx.span_of_impl(item1.def_id).unwrap(),
81 "duplicate definitions with name `{}`",
85 self.tcx.span_of_impl(item1.def_id).unwrap(),
86 format!("duplicate definitions for `{}`", name),
89 self.tcx.span_of_impl(item2.def_id).unwrap(),
90 format!("other definition for `{}`", name),
93 for cause in &overlap.intercrate_ambiguity_causes {
94 cause.add_intercrate_ambiguity_hint(&mut err);
97 if overlap.involves_placeholder {
98 traits::add_placeholder_note(&mut err);
106 fn check_for_overlapping_inherent_impls(
108 overlap_mode: OverlapMode,
112 traits::overlapping_impls(
116 // We go ahead and just skip the leak check for
117 // inherent impls without warning.
121 self.check_for_common_items_in_impls(impl1_def_id, impl2_def_id, overlap);
128 fn check_item(&mut self, id: hir::ItemId) {
129 let def_kind = self.tcx.def_kind(id.def_id);
130 if !matches!(def_kind, DefKind::Enum | DefKind::Struct | DefKind::Trait | DefKind::Union) {
134 let impls = self.tcx.inherent_impls(id.def_id);
136 // If there is only one inherent impl block,
137 // there is nothing to overlap check it with
138 if impls.len() <= 1 {
142 let overlap_mode = OverlapMode::get(self.tcx, id.def_id.to_def_id());
144 let impls_items = impls
146 .map(|impl_def_id| (impl_def_id, self.tcx.associated_items(*impl_def_id)))
147 .collect::<SmallVec<[_; 8]>>();
149 // Perform a O(n^2) algorithm for small n,
150 // otherwise switch to an allocating algorithm with
151 // faster asymptotic runtime.
152 const ALLOCATING_ALGO_THRESHOLD: usize = 500;
153 if impls.len() < ALLOCATING_ALGO_THRESHOLD {
154 for (i, &(&impl1_def_id, impl_items1)) in impls_items.iter().enumerate() {
155 for &(&impl2_def_id, impl_items2) in &impls_items[(i + 1)..] {
156 if self.impls_have_common_items(impl_items1, impl_items2) {
157 self.check_for_overlapping_inherent_impls(
166 // Build a set of connected regions of impl blocks.
167 // Two impl blocks are regarded as connected if they share
168 // an item with the same unhygienic identifier.
169 // After we have assembled the connected regions,
170 // run the O(n^2) algorithm on each connected region.
171 // This is advantageous to running the algorithm over the
172 // entire graph when there are many connected regions.
174 rustc_index::newtype_index! {
175 pub struct RegionId {
179 struct ConnectedRegion {
180 idents: SmallVec<[Symbol; 8]>,
181 impl_blocks: FxHashSet<usize>,
183 let mut connected_regions: IndexVec<RegionId, _> = Default::default();
184 // Reverse map from the Symbol to the connected region id.
185 let mut connected_region_ids = FxHashMap::default();
187 for (i, &(&_impl_def_id, impl_items)) in impls_items.iter().enumerate() {
188 if impl_items.len() == 0 {
191 // First obtain a list of existing connected region ids
192 let mut idents_to_add = SmallVec::<[Symbol; 8]>::new();
193 let mut ids = impl_items
194 .in_definition_order()
196 let entry = connected_region_ids.entry(item.name);
197 if let Entry::Occupied(e) = &entry {
200 idents_to_add.push(item.name);
204 .collect::<SmallVec<[RegionId; 8]>>();
205 // Sort the id list so that the algorithm is deterministic
210 // Create a new connected region
212 let id_to_set = connected_regions.next_index();
213 // Update the connected region ids
214 for ident in &idents_to_add {
215 connected_region_ids.insert(*ident, id_to_set);
217 connected_regions.insert(
220 idents: idents_to_add,
221 impl_blocks: std::iter::once(i).collect(),
225 // Take the only id inside the list
227 let region = connected_regions[id_to_set].as_mut().unwrap();
228 region.impl_blocks.insert(i);
229 region.idents.extend_from_slice(&idents_to_add);
230 // Update the connected region ids
231 for ident in &idents_to_add {
232 connected_region_ids.insert(*ident, id_to_set);
235 // We have multiple connected regions to merge.
236 // In the worst case this might add impl blocks
237 // one by one and can thus be O(n^2) in the size
238 // of the resulting final connected region, but
239 // this is no issue as the final step to check
240 // for overlaps runs in O(n^2) as well.
241 &[id_to_set, ..] => {
242 let mut region = connected_regions.remove(id_to_set).unwrap();
243 region.impl_blocks.insert(i);
244 region.idents.extend_from_slice(&idents_to_add);
245 // Update the connected region ids
246 for ident in &idents_to_add {
247 connected_region_ids.insert(*ident, id_to_set);
250 // Remove other regions from ids.
251 for &id in ids.iter() {
255 let r = connected_regions.remove(id).unwrap();
256 for ident in r.idents.iter() {
257 connected_region_ids.insert(*ident, id_to_set);
259 region.idents.extend_from_slice(&r.idents);
260 region.impl_blocks.extend(r.impl_blocks);
263 connected_regions.insert(id_to_set, region);
269 "churning through {} components (sum={}, avg={}, var={}, max={})",
270 connected_regions.len(),
272 impls.len() / connected_regions.len(),
274 let avg = impls.len() / connected_regions.len();
275 let s = connected_regions
278 .map(|r| r.impl_blocks.len() as isize - avg as isize)
279 .map(|v| v.abs() as usize)
281 s / connected_regions.len()
283 connected_regions.iter().flatten().map(|r| r.impl_blocks.len()).max().unwrap()
285 // List of connected regions is built. Now, run the overlap check
286 // for each pair of impl blocks in the same connected region.
287 for region in connected_regions.into_iter().flatten() {
288 let mut impl_blocks =
289 region.impl_blocks.into_iter().collect::<SmallVec<[usize; 8]>>();
290 impl_blocks.sort_unstable();
291 for (i, &impl1_items_idx) in impl_blocks.iter().enumerate() {
292 let &(&impl1_def_id, impl_items1) = &impls_items[impl1_items_idx];
293 for &impl2_items_idx in impl_blocks[(i + 1)..].iter() {
294 let &(&impl2_def_id, impl_items2) = &impls_items[impl2_items_idx];
295 if self.impls_have_common_items(impl_items1, impl_items2) {
296 self.check_for_overlapping_inherent_impls(