]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/coherence/inherent_impls_overlap.rs
Rollup merge of #69554 - GuillaumeGomez:cleanup-e0374, r=Dylan-DPC
[rust.git] / src / librustc_typeck / coherence / inherent_impls_overlap.rs
1 use rustc::ty::TyCtxt;
2 use rustc_errors::struct_span_err;
3 use rustc_hir as hir;
4 use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
5 use rustc_hir::itemlikevisit::ItemLikeVisitor;
6 use rustc_infer::traits::{self, SkipLeakCheck};
7
8 pub fn crate_inherent_impls_overlap_check(tcx: TyCtxt<'_>, crate_num: CrateNum) {
9     assert_eq!(crate_num, LOCAL_CRATE);
10     let krate = tcx.hir().krate();
11     krate.visit_all_item_likes(&mut InherentOverlapChecker { tcx });
12 }
13
14 struct InherentOverlapChecker<'tcx> {
15     tcx: TyCtxt<'tcx>,
16 }
17
18 impl InherentOverlapChecker<'tcx> {
19     /// Checks whether any associated items in impls 1 and 2 share the same identifier and
20     /// namespace.
21     fn impls_have_common_items(&self, impl1: DefId, impl2: DefId) -> bool {
22         let impl_items1 = self.tcx.associated_items(impl1);
23         let impl_items2 = self.tcx.associated_items(impl2);
24
25         for item1 in impl_items1.in_definition_order() {
26             let collision = impl_items2
27                 .filter_by_name_unhygienic(item1.ident.name)
28                 .find(|item2| {
29                     // Symbols and namespace match, compare hygienically.
30                     item1.kind.namespace() == item2.kind.namespace()
31                         && item1.ident.modern() == item2.ident.modern()
32                 })
33                 .is_some();
34
35             if collision {
36                 return true;
37             }
38         }
39
40         false
41     }
42
43     fn check_for_common_items_in_impls(
44         &self,
45         impl1: DefId,
46         impl2: DefId,
47         overlap: traits::OverlapResult<'_>,
48     ) {
49         let impl_items1 = self.tcx.associated_items(impl1);
50         let impl_items2 = self.tcx.associated_items(impl2);
51
52         for item1 in impl_items1.in_definition_order() {
53             let collision = impl_items2.filter_by_name_unhygienic(item1.ident.name).find(|item2| {
54                 // Symbols and namespace match, compare hygienically.
55                 item1.kind.namespace() == item2.kind.namespace()
56                     && item1.ident.modern() == item2.ident.modern()
57             });
58
59             if let Some(item2) = collision {
60                 let name = item1.ident.modern();
61                 let mut err = struct_span_err!(
62                     self.tcx.sess,
63                     self.tcx.span_of_impl(item1.def_id).unwrap(),
64                     E0592,
65                     "duplicate definitions with name `{}`",
66                     name
67                 );
68                 err.span_label(
69                     self.tcx.span_of_impl(item1.def_id).unwrap(),
70                     format!("duplicate definitions for `{}`", name),
71                 );
72                 err.span_label(
73                     self.tcx.span_of_impl(item2.def_id).unwrap(),
74                     format!("other definition for `{}`", name),
75                 );
76
77                 for cause in &overlap.intercrate_ambiguity_causes {
78                     cause.add_intercrate_ambiguity_hint(&mut err);
79                 }
80
81                 if overlap.involves_placeholder {
82                     traits::add_placeholder_note(&mut err);
83                 }
84
85                 err.emit();
86             }
87         }
88     }
89
90     fn check_for_overlapping_inherent_impls(&self, impl1_def_id: DefId, impl2_def_id: DefId) {
91         traits::overlapping_impls(
92             self.tcx,
93             impl1_def_id,
94             impl2_def_id,
95             // We go ahead and just skip the leak check for
96             // inherent impls without warning.
97             SkipLeakCheck::Yes,
98             |overlap| {
99                 self.check_for_common_items_in_impls(impl1_def_id, impl2_def_id, overlap);
100                 false
101             },
102             || true,
103         );
104     }
105 }
106
107 impl ItemLikeVisitor<'v> for InherentOverlapChecker<'tcx> {
108     fn visit_item(&mut self, item: &'v hir::Item<'v>) {
109         match item.kind {
110             hir::ItemKind::Enum(..)
111             | hir::ItemKind::Struct(..)
112             | hir::ItemKind::Trait(..)
113             | hir::ItemKind::Union(..) => {
114                 let ty_def_id = self.tcx.hir().local_def_id(item.hir_id);
115                 let impls = self.tcx.inherent_impls(ty_def_id);
116
117                 for (i, &impl1_def_id) in impls.iter().enumerate() {
118                     for &impl2_def_id in &impls[(i + 1)..] {
119                         if self.impls_have_common_items(impl1_def_id, impl2_def_id) {
120                             self.check_for_overlapping_inherent_impls(impl1_def_id, impl2_def_id);
121                         }
122                     }
123                 }
124             }
125             _ => {}
126         }
127     }
128
129     fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem<'v>) {}
130
131     fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem<'v>) {}
132 }