]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/coherence/inherent_impls_overlap.rs
Auto merge of #69576 - matthiaskrgr:nightly_bootstrap_from_beta, r=Centril
[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.filter_by_name_unhygienic(item1.ident.name).any(|item2| {
27                 // Symbols and namespace match, compare hygienically.
28                 item1.kind.namespace() == item2.kind.namespace()
29                     && item1.ident.modern() == item2.ident.modern()
30             });
31
32             if collision {
33                 return true;
34             }
35         }
36
37         false
38     }
39
40     fn check_for_common_items_in_impls(
41         &self,
42         impl1: DefId,
43         impl2: DefId,
44         overlap: traits::OverlapResult<'_>,
45     ) {
46         let impl_items1 = self.tcx.associated_items(impl1);
47         let impl_items2 = self.tcx.associated_items(impl2);
48
49         for item1 in impl_items1.in_definition_order() {
50             let collision = impl_items2.filter_by_name_unhygienic(item1.ident.name).find(|item2| {
51                 // Symbols and namespace match, compare hygienically.
52                 item1.kind.namespace() == item2.kind.namespace()
53                     && item1.ident.modern() == item2.ident.modern()
54             });
55
56             if let Some(item2) = collision {
57                 let name = item1.ident.modern();
58                 let mut err = struct_span_err!(
59                     self.tcx.sess,
60                     self.tcx.span_of_impl(item1.def_id).unwrap(),
61                     E0592,
62                     "duplicate definitions with name `{}`",
63                     name
64                 );
65                 err.span_label(
66                     self.tcx.span_of_impl(item1.def_id).unwrap(),
67                     format!("duplicate definitions for `{}`", name),
68                 );
69                 err.span_label(
70                     self.tcx.span_of_impl(item2.def_id).unwrap(),
71                     format!("other definition for `{}`", name),
72                 );
73
74                 for cause in &overlap.intercrate_ambiguity_causes {
75                     cause.add_intercrate_ambiguity_hint(&mut err);
76                 }
77
78                 if overlap.involves_placeholder {
79                     traits::add_placeholder_note(&mut err);
80                 }
81
82                 err.emit();
83             }
84         }
85     }
86
87     fn check_for_overlapping_inherent_impls(&self, impl1_def_id: DefId, impl2_def_id: DefId) {
88         traits::overlapping_impls(
89             self.tcx,
90             impl1_def_id,
91             impl2_def_id,
92             // We go ahead and just skip the leak check for
93             // inherent impls without warning.
94             SkipLeakCheck::Yes,
95             |overlap| {
96                 self.check_for_common_items_in_impls(impl1_def_id, impl2_def_id, overlap);
97                 false
98             },
99             || true,
100         );
101     }
102 }
103
104 impl ItemLikeVisitor<'v> for InherentOverlapChecker<'tcx> {
105     fn visit_item(&mut self, item: &'v hir::Item<'v>) {
106         match item.kind {
107             hir::ItemKind::Enum(..)
108             | hir::ItemKind::Struct(..)
109             | hir::ItemKind::Trait(..)
110             | hir::ItemKind::Union(..) => {
111                 let ty_def_id = self.tcx.hir().local_def_id(item.hir_id);
112                 let impls = self.tcx.inherent_impls(ty_def_id);
113
114                 for (i, &impl1_def_id) in impls.iter().enumerate() {
115                     for &impl2_def_id in &impls[(i + 1)..] {
116                         if self.impls_have_common_items(impl1_def_id, impl2_def_id) {
117                             self.check_for_overlapping_inherent_impls(impl1_def_id, impl2_def_id);
118                         }
119                     }
120                 }
121             }
122             _ => {}
123         }
124     }
125
126     fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem<'v>) {}
127
128     fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem<'v>) {}
129 }