]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/coherence/inherent_impls_overlap.rs
Auto merge of #68506 - tmandry:rollup-kz9d33v, r=tmandry
[rust.git] / src / librustc_typeck / coherence / inherent_impls_overlap.rs
1 use crate::namespace::Namespace;
2 use rustc::traits::{self, IntercrateMode};
3 use rustc::ty::TyCtxt;
4 use rustc_errors::struct_span_err;
5 use rustc_hir as hir;
6 use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
7 use rustc_hir::itemlikevisit::ItemLikeVisitor;
8
9 pub fn crate_inherent_impls_overlap_check(tcx: TyCtxt<'_>, crate_num: CrateNum) {
10     assert_eq!(crate_num, LOCAL_CRATE);
11     let krate = tcx.hir().krate();
12     krate.visit_all_item_likes(&mut InherentOverlapChecker { tcx });
13 }
14
15 struct InherentOverlapChecker<'tcx> {
16     tcx: TyCtxt<'tcx>,
17 }
18
19 impl InherentOverlapChecker<'tcx> {
20     fn check_for_common_items_in_impls(
21         &self,
22         impl1: DefId,
23         impl2: DefId,
24         overlap: traits::OverlapResult<'_>,
25     ) {
26         let name_and_namespace = |def_id| {
27             let item = self.tcx.associated_item(def_id);
28             (item.ident.modern(), Namespace::from(item.kind))
29         };
30
31         let impl_items1 = self.tcx.associated_item_def_ids(impl1);
32         let impl_items2 = self.tcx.associated_item_def_ids(impl2);
33
34         for &item1 in &impl_items1[..] {
35             let (name, namespace) = name_and_namespace(item1);
36
37             for &item2 in &impl_items2[..] {
38                 if (name, namespace) == name_and_namespace(item2) {
39                     let mut err = struct_span_err!(
40                         self.tcx.sess,
41                         self.tcx.span_of_impl(item1).unwrap(),
42                         E0592,
43                         "duplicate definitions with name `{}`",
44                         name
45                     );
46                     err.span_label(
47                         self.tcx.span_of_impl(item1).unwrap(),
48                         format!("duplicate definitions for `{}`", name),
49                     );
50                     err.span_label(
51                         self.tcx.span_of_impl(item2).unwrap(),
52                         format!("other definition for `{}`", name),
53                     );
54
55                     for cause in &overlap.intercrate_ambiguity_causes {
56                         cause.add_intercrate_ambiguity_hint(&mut err);
57                     }
58
59                     if overlap.involves_placeholder {
60                         traits::add_placeholder_note(&mut err);
61                     }
62
63                     err.emit();
64                 }
65             }
66         }
67     }
68
69     fn check_for_overlapping_inherent_impls(&self, ty_def_id: DefId) {
70         let impls = self.tcx.inherent_impls(ty_def_id);
71
72         for (i, &impl1_def_id) in impls.iter().enumerate() {
73             for &impl2_def_id in &impls[(i + 1)..] {
74                 traits::overlapping_impls(
75                     self.tcx,
76                     impl1_def_id,
77                     impl2_def_id,
78                     IntercrateMode::Issue43355,
79                     |overlap| {
80                         self.check_for_common_items_in_impls(impl1_def_id, impl2_def_id, overlap);
81                         false
82                     },
83                     || true,
84                 );
85             }
86         }
87     }
88 }
89
90 impl ItemLikeVisitor<'v> for InherentOverlapChecker<'tcx> {
91     fn visit_item(&mut self, item: &'v hir::Item<'v>) {
92         match item.kind {
93             hir::ItemKind::Enum(..)
94             | hir::ItemKind::Struct(..)
95             | hir::ItemKind::Trait(..)
96             | hir::ItemKind::Union(..) => {
97                 let type_def_id = self.tcx.hir().local_def_id(item.hir_id);
98                 self.check_for_overlapping_inherent_impls(type_def_id);
99             }
100             _ => {}
101         }
102     }
103
104     fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem<'v>) {}
105
106     fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem<'v>) {}
107 }