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