]> git.proxmox.com Git - rustc.git/blame - src/librustc_typeck/coherence/inherent_impls_overlap.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / librustc_typeck / coherence / inherent_impls_overlap.rs
CommitLineData
cc61c64b
XL
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
abe05a73 11use namespace::Namespace;
cc61c64b
XL
12use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
13use rustc::hir;
14use rustc::hir::itemlikevisit::ItemLikeVisitor;
7cac9316 15use rustc::traits;
abe05a73 16use rustc::ty::TyCtxt;
cc61c64b 17
cc61c64b
XL
18pub fn crate_inherent_impls_overlap_check<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
19 crate_num: CrateNum) {
20 assert_eq!(crate_num, LOCAL_CRATE);
21 let krate = tcx.hir.krate();
22 krate.visit_all_item_likes(&mut InherentOverlapChecker { tcx });
23}
24
25struct InherentOverlapChecker<'a, 'tcx: 'a> {
26 tcx: TyCtxt<'a, 'tcx, 'tcx>
27}
28
29impl<'a, 'tcx> InherentOverlapChecker<'a, 'tcx> {
ea8adc8c
XL
30 fn check_for_common_items_in_impls(&self, impl1: DefId, impl2: DefId,
31 overlap: traits::OverlapResult) {
cc61c64b
XL
32
33 let name_and_namespace = |def_id| {
34 let item = self.tcx.associated_item(def_id);
abe05a73 35 (item.name, Namespace::from(item.kind))
cc61c64b
XL
36 };
37
38 let impl_items1 = self.tcx.associated_item_def_ids(impl1);
39 let impl_items2 = self.tcx.associated_item_def_ids(impl2);
40
41 for &item1 in &impl_items1[..] {
42 let (name, namespace) = name_and_namespace(item1);
43
44 for &item2 in &impl_items2[..] {
45 if (name, namespace) == name_and_namespace(item2) {
ea8adc8c
XL
46 let mut err = struct_span_err!(self.tcx.sess,
47 self.tcx.span_of_impl(item1).unwrap(),
48 E0592,
49 "duplicate definitions with name `{}`",
50 name);
51
52 err.span_label(self.tcx.span_of_impl(item1).unwrap(),
53 format!("duplicate definitions for `{}`", name));
54 err.span_label(self.tcx.span_of_impl(item2).unwrap(),
55 format!("other definition for `{}`", name));
56
57 for cause in &overlap.intercrate_ambiguity_causes {
58 cause.add_intercrate_ambiguity_hint(&mut err);
59 }
60
61 err.emit();
cc61c64b
XL
62 }
63 }
64 }
65 }
66
67 fn check_for_overlapping_inherent_impls(&self, ty_def_id: DefId) {
7cac9316 68 let impls = self.tcx.inherent_impls(ty_def_id);
cc61c64b
XL
69
70 for (i, &impl1_def_id) in impls.iter().enumerate() {
71 for &impl2_def_id in &impls[(i + 1)..] {
041b39d2 72 self.tcx.infer_ctxt().enter(|infcx| {
ea8adc8c
XL
73 if let Some(overlap) =
74 traits::overlapping_impls(&infcx, impl1_def_id, impl2_def_id) {
75 self.check_for_common_items_in_impls(impl1_def_id, impl2_def_id, overlap)
cc61c64b
XL
76 }
77 });
78 }
79 }
80 }
81}
82
83impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for InherentOverlapChecker<'a, 'tcx> {
84 fn visit_item(&mut self, item: &'v hir::Item) {
85 match item.node {
86 hir::ItemEnum(..) |
87 hir::ItemStruct(..) |
88 hir::ItemTrait(..) |
89 hir::ItemUnion(..) => {
90 let type_def_id = self.tcx.hir.local_def_id(item.id);
91 self.check_for_overlapping_inherent_impls(type_def_id);
92 }
93 _ => {}
94 }
95 }
96
97 fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem) {
98 }
99
100 fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) {
101 }
102}
103