]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/coherence/inherent_impls_overlap.rs
Do not show `::constructor` on tuple struct diagnostics
[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 rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
12 use rustc::hir;
13 use rustc::hir::itemlikevisit::ItemLikeVisitor;
14 use rustc::traits::{self, Reveal};
15 use rustc::ty::{self, TyCtxt};
16
17 use syntax_pos::DUMMY_SP;
18
19 pub fn crate_inherent_impls_overlap_check<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
20                                                     crate_num: CrateNum) {
21     assert_eq!(crate_num, LOCAL_CRATE);
22     let krate = tcx.hir.krate();
23     krate.visit_all_item_likes(&mut InherentOverlapChecker { tcx });
24 }
25
26 struct InherentOverlapChecker<'a, 'tcx: 'a> {
27     tcx: TyCtxt<'a, 'tcx, 'tcx>
28 }
29
30 impl<'a, 'tcx> InherentOverlapChecker<'a, 'tcx> {
31     fn check_for_common_items_in_impls(&self, impl1: DefId, impl2: DefId) {
32         #[derive(Copy, Clone, PartialEq)]
33         enum Namespace {
34             Type,
35             Value,
36         }
37
38         let name_and_namespace = |def_id| {
39             let item = self.tcx.associated_item(def_id);
40             (item.name, match item.kind {
41                 ty::AssociatedKind::Type => Namespace::Type,
42                 ty::AssociatedKind::Const |
43                 ty::AssociatedKind::Method => Namespace::Value,
44             })
45         };
46
47         let impl_items1 = self.tcx.associated_item_def_ids(impl1);
48         let impl_items2 = self.tcx.associated_item_def_ids(impl2);
49
50         for &item1 in &impl_items1[..] {
51             let (name, namespace) = name_and_namespace(item1);
52
53             for &item2 in &impl_items2[..] {
54                 if (name, namespace) == name_and_namespace(item2) {
55                     struct_span_err!(self.tcx.sess,
56                                      self.tcx.span_of_impl(item1).unwrap(),
57                                      E0592,
58                                      "duplicate definitions with name `{}`",
59                                      name)
60                         .span_label(self.tcx.span_of_impl(item1).unwrap(),
61                                     &format!("duplicate definitions for `{}`", name))
62                         .span_label(self.tcx.span_of_impl(item2).unwrap(),
63                                     &format!("other definition for `{}`", name))
64                         .emit();
65                 }
66             }
67         }
68     }
69
70     fn check_for_overlapping_inherent_impls(&self, ty_def_id: DefId) {
71         let impls = ty::queries::inherent_impls::get(self.tcx, DUMMY_SP, ty_def_id);
72
73         for (i, &impl1_def_id) in impls.iter().enumerate() {
74             for &impl2_def_id in &impls[(i + 1)..] {
75                 self.tcx.infer_ctxt((), Reveal::UserFacing).enter(|infcx| {
76                     if traits::overlapping_impls(&infcx, impl1_def_id, impl2_def_id).is_some() {
77                         self.check_for_common_items_in_impls(impl1_def_id, impl2_def_id)
78                     }
79                 });
80             }
81         }
82     }
83 }
84
85 impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for InherentOverlapChecker<'a, 'tcx> {
86     fn visit_item(&mut self, item: &'v hir::Item) {
87         match item.node {
88             hir::ItemEnum(..) |
89             hir::ItemStruct(..) |
90             hir::ItemTrait(..) |
91             hir::ItemUnion(..) => {
92                 let type_def_id = self.tcx.hir.local_def_id(item.id);
93                 self.check_for_overlapping_inherent_impls(type_def_id);
94             }
95             _ => {}
96         }
97     }
98
99     fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem) {
100     }
101
102     fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) {
103     }
104 }
105