]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/coherence/inherent_impls_overlap.rs
8051056e4ba8dda09c351ddb2dcfc0565f80b896
[rust.git] / src / librustc_typeck / coherence / inherent_impls_overlap.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use namespace::Namespace;
12 use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
13 use rustc::hir;
14 use rustc::hir::itemlikevisit::ItemLikeVisitor;
15 use rustc::traits::{self, IntercrateMode, FutureCompatOverlapErrorKind};
16 use rustc::ty::TyCtxt;
17 use rustc::ty::relate::TraitObjectMode;
18
19 use lint;
20
21 pub fn crate_inherent_impls_overlap_check<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
22                                                     crate_num: CrateNum) {
23     assert_eq!(crate_num, LOCAL_CRATE);
24     let krate = tcx.hir().krate();
25     krate.visit_all_item_likes(&mut InherentOverlapChecker { tcx });
26 }
27
28 struct InherentOverlapChecker<'a, 'tcx: 'a> {
29     tcx: TyCtxt<'a, 'tcx, 'tcx>
30 }
31
32 impl<'a, 'tcx> InherentOverlapChecker<'a, 'tcx> {
33     fn check_for_common_items_in_impls(
34         &self, impl1: DefId, impl2: DefId,
35         overlap: traits::OverlapResult,
36         used_to_be_allowed: Option<FutureCompatOverlapErrorKind>)
37     {
38
39         let name_and_namespace = |def_id| {
40             let item = self.tcx.associated_item(def_id);
41             (item.ident, Namespace::from(item.kind))
42         };
43
44         let impl_items1 = self.tcx.associated_item_def_ids(impl1);
45         let impl_items2 = self.tcx.associated_item_def_ids(impl2);
46
47         for &item1 in &impl_items1[..] {
48             let (name, namespace) = name_and_namespace(item1);
49
50             for &item2 in &impl_items2[..] {
51                 if (name, namespace) == name_and_namespace(item2) {
52                     let node_id = self.tcx.hir().as_local_node_id(impl1);
53                     let mut err = match used_to_be_allowed {
54                         Some(kind) if node_id.is_some() => {
55                             let lint = match kind {
56                                 FutureCompatOverlapErrorKind::Issue43355 =>
57                                     lint::builtin::INCOHERENT_FUNDAMENTAL_IMPLS,
58                                 FutureCompatOverlapErrorKind::Issue33140 =>
59                                     lint::builtin::ORDER_DEPENDENT_TRAIT_OBJECTS,
60                             };
61                             self.tcx.struct_span_lint_node(
62                                 lint,
63                                 node_id.unwrap(),
64                                 self.tcx.span_of_impl(item1).unwrap(),
65                                 &format!("duplicate definitions with name `{}` (E0592)", name)
66                             )
67                         }
68                         _ => {
69                             struct_span_err!(self.tcx.sess,
70                                              self.tcx.span_of_impl(item1).unwrap(),
71                                              E0592,
72                                              "duplicate definitions with name `{}`",
73                                              name)
74                         }
75                     };
76
77                     err.span_label(self.tcx.span_of_impl(item1).unwrap(),
78                                    format!("duplicate definitions for `{}`", name));
79                     err.span_label(self.tcx.span_of_impl(item2).unwrap(),
80                                    format!("other definition for `{}`", name));
81
82                     for cause in &overlap.intercrate_ambiguity_causes {
83                         cause.add_intercrate_ambiguity_hint(&mut err);
84                     }
85
86                     err.emit();
87                 }
88             }
89         }
90     }
91
92     fn check_for_overlapping_inherent_impls(&self, ty_def_id: DefId) {
93         let impls = self.tcx.inherent_impls(ty_def_id);
94
95         for (i, &impl1_def_id) in impls.iter().enumerate() {
96             for &impl2_def_id in &impls[(i + 1)..] {
97                 let mut used_to_be_allowed = traits::overlapping_impls(
98                     self.tcx,
99                     impl1_def_id,
100                     impl2_def_id,
101                     IntercrateMode::Issue43355,
102                     TraitObjectMode::NoSquash,
103                     |overlap| {
104                         self.check_for_common_items_in_impls(
105                             impl1_def_id,
106                             impl2_def_id,
107                             overlap,
108                             Some(FutureCompatOverlapErrorKind::Issue43355),
109                         );
110                         false
111                     },
112                     || true,
113                 );
114
115                 if used_to_be_allowed {
116                     used_to_be_allowed = traits::overlapping_impls(
117                         self.tcx,
118                         impl1_def_id,
119                         impl2_def_id,
120                         IntercrateMode::Fixed,
121                         TraitObjectMode::NoSquash,
122                         |overlap| {
123                             self.check_for_common_items_in_impls(
124                                 impl1_def_id,
125                                 impl2_def_id,
126                                 overlap,
127                                 None,
128                             );
129                             false
130                         },
131                         || true,
132                     );
133                 }
134
135                 if used_to_be_allowed {
136                     traits::overlapping_impls(
137                         self.tcx,
138                         impl1_def_id,
139                         impl2_def_id,
140                         IntercrateMode::Fixed,
141                         TraitObjectMode::SquashAutoTraitsIssue33140,
142                         |overlap| {
143                             self.check_for_common_items_in_impls(
144                                 impl1_def_id,
145                                 impl2_def_id,
146                                 overlap,
147                                 Some(FutureCompatOverlapErrorKind::Issue33140),
148                             );
149                             false
150                         },
151                         || true,
152                     );
153                 }
154             }
155         }
156     }
157 }
158
159 impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for InherentOverlapChecker<'a, 'tcx> {
160     fn visit_item(&mut self, item: &'v hir::Item) {
161         match item.node {
162             hir::ItemKind::Enum(..) |
163             hir::ItemKind::Struct(..) |
164             hir::ItemKind::Trait(..) |
165             hir::ItemKind::Union(..) => {
166                 let type_def_id = self.tcx.hir().local_def_id(item.id);
167                 self.check_for_overlapping_inherent_impls(type_def_id);
168             }
169             _ => {}
170         }
171     }
172
173     fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem) {
174     }
175
176     fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) {
177     }
178 }