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.
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.
11 use namespace::Namespace;
12 use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
14 use rustc::hir::itemlikevisit::ItemLikeVisitor;
15 use rustc::traits::{self, IntercrateMode, FutureCompatOverlapErrorKind};
16 use rustc::ty::TyCtxt;
17 use rustc::ty::relate::TraitObjectMode;
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 });
28 struct InherentOverlapChecker<'a, 'tcx: 'a> {
29 tcx: TyCtxt<'a, 'tcx, 'tcx>
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>)
39 let name_and_namespace = |def_id| {
40 let item = self.tcx.associated_item(def_id);
41 (item.ident, Namespace::from(item.kind))
44 let impl_items1 = self.tcx.associated_item_def_ids(impl1);
45 let impl_items2 = self.tcx.associated_item_def_ids(impl2);
47 for &item1 in &impl_items1[..] {
48 let (name, namespace) = name_and_namespace(item1);
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,
61 self.tcx.struct_span_lint_node(
64 self.tcx.span_of_impl(item1).unwrap(),
65 &format!("duplicate definitions with name `{}` (E0592)", name)
69 struct_span_err!(self.tcx.sess,
70 self.tcx.span_of_impl(item1).unwrap(),
72 "duplicate definitions with name `{}`",
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));
82 for cause in &overlap.intercrate_ambiguity_causes {
83 cause.add_intercrate_ambiguity_hint(&mut err);
92 fn check_for_overlapping_inherent_impls(&self, ty_def_id: DefId) {
93 let impls = self.tcx.inherent_impls(ty_def_id);
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(
101 IntercrateMode::Issue43355,
102 TraitObjectMode::NoSquash,
104 self.check_for_common_items_in_impls(
108 Some(FutureCompatOverlapErrorKind::Issue43355),
115 if used_to_be_allowed {
116 used_to_be_allowed = traits::overlapping_impls(
120 IntercrateMode::Fixed,
121 TraitObjectMode::NoSquash,
123 self.check_for_common_items_in_impls(
135 if used_to_be_allowed {
136 traits::overlapping_impls(
140 IntercrateMode::Fixed,
141 TraitObjectMode::SquashAutoTraitsIssue33140,
143 self.check_for_common_items_in_impls(
147 Some(FutureCompatOverlapErrorKind::Issue33140),
159 impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for InherentOverlapChecker<'a, 'tcx> {
160 fn visit_item(&mut self, item: &'v hir::Item) {
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);
173 fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem) {
176 fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) {