]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/coherence/inherent_impls_overlap.rs
Specify output filenames for compatibility with Windows
[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};
16 use rustc::ty::TyCtxt;
17
18 use lint;
19
20 pub fn crate_inherent_impls_overlap_check<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
21                                                     crate_num: CrateNum) {
22     assert_eq!(crate_num, LOCAL_CRATE);
23     let krate = tcx.hir.krate();
24     krate.visit_all_item_likes(&mut InherentOverlapChecker { tcx });
25 }
26
27 struct InherentOverlapChecker<'a, 'tcx: 'a> {
28     tcx: TyCtxt<'a, 'tcx, 'tcx>
29 }
30
31 impl<'a, 'tcx> InherentOverlapChecker<'a, 'tcx> {
32     fn check_for_common_items_in_impls(&self, impl1: DefId, impl2: DefId,
33                                        overlap: traits::OverlapResult,
34                                        used_to_be_allowed: bool) {
35
36         let name_and_namespace = |def_id| {
37             let item = self.tcx.associated_item(def_id);
38             (item.name, Namespace::from(item.kind))
39         };
40
41         let impl_items1 = self.tcx.associated_item_def_ids(impl1);
42         let impl_items2 = self.tcx.associated_item_def_ids(impl2);
43
44         for &item1 in &impl_items1[..] {
45             let (name, namespace) = name_and_namespace(item1);
46
47             for &item2 in &impl_items2[..] {
48                 if (name, namespace) == name_and_namespace(item2) {
49                     let node_id = self.tcx.hir.as_local_node_id(impl1);
50                     let mut err = if used_to_be_allowed && node_id.is_some() {
51                         self.tcx.struct_span_lint_node(
52                             lint::builtin::INCOHERENT_FUNDAMENTAL_IMPLS,
53                             node_id.unwrap(),
54                             self.tcx.span_of_impl(item1).unwrap(),
55                             &format!("duplicate definitions with name `{}` (E0592)", name)
56                         )
57                     } else {
58                         struct_span_err!(self.tcx.sess,
59                                          self.tcx.span_of_impl(item1).unwrap(),
60                                          E0592,
61                                          "duplicate definitions with name `{}`",
62                                          name)
63                     };
64
65                     err.span_label(self.tcx.span_of_impl(item1).unwrap(),
66                                    format!("duplicate definitions for `{}`", name));
67                     err.span_label(self.tcx.span_of_impl(item2).unwrap(),
68                                    format!("other definition for `{}`", name));
69
70                     for cause in &overlap.intercrate_ambiguity_causes {
71                         cause.add_intercrate_ambiguity_hint(&mut err);
72                     }
73
74                     err.emit();
75                 }
76             }
77         }
78     }
79
80     fn check_for_overlapping_inherent_impls(&self, ty_def_id: DefId) {
81         let impls = self.tcx.inherent_impls(ty_def_id);
82
83         for (i, &impl1_def_id) in impls.iter().enumerate() {
84             for &impl2_def_id in &impls[(i + 1)..] {
85                 let used_to_be_allowed = self.tcx.infer_ctxt().enter(|infcx| {
86                     if let Some(overlap) =
87                         traits::overlapping_impls(&infcx, impl1_def_id, impl2_def_id,
88                                                   IntercrateMode::Issue43355)
89                     {
90                         self.check_for_common_items_in_impls(
91                             impl1_def_id, impl2_def_id, overlap, false);
92                         false
93                     } else {
94                         true
95                     }
96                 });
97
98                 if used_to_be_allowed {
99                     self.tcx.infer_ctxt().enter(|infcx| {
100                         if let Some(overlap) =
101                             traits::overlapping_impls(&infcx, impl1_def_id, impl2_def_id,
102                                                       IntercrateMode::Fixed)
103                         {
104                             self.check_for_common_items_in_impls(
105                                 impl1_def_id, impl2_def_id, overlap, true);
106                         }
107                     });
108                 }
109             }
110         }
111     }
112 }
113
114 impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for InherentOverlapChecker<'a, 'tcx> {
115     fn visit_item(&mut self, item: &'v hir::Item) {
116         match item.node {
117             hir::ItemEnum(..) |
118             hir::ItemStruct(..) |
119             hir::ItemTrait(..) |
120             hir::ItemUnion(..) => {
121                 let type_def_id = self.tcx.hir.local_def_id(item.id);
122                 self.check_for_overlapping_inherent_impls(type_def_id);
123             }
124             _ => {}
125         }
126     }
127
128     fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem) {
129     }
130
131     fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) {
132     }
133 }