]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/coherence/overlap.rs
Auto merge of #45013 - chrisvittal:mir_pretty_printing_pr, r=nikomatsakis
[rust.git] / src / librustc_typeck / coherence / overlap.rs
1 // Copyright 2014 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 //! Overlap: No two impls for the same trait are implemented for the
12 //! same type. Likewise, no two inherent impls for a given type
13 //! constructor provide a method with the same name.
14
15 use rustc::traits;
16 use rustc::ty::{self, TyCtxt, TypeFoldable};
17 use syntax::ast;
18 use rustc::hir;
19 use rustc::hir::itemlikevisit::ItemLikeVisitor;
20
21 pub fn check_default_impls<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
22     let mut overlap = OverlapChecker { tcx };
23
24     // this secondary walk specifically checks for some other cases,
25     // like defaulted traits, for which additional overlap rules exist
26     tcx.hir.krate().visit_all_item_likes(&mut overlap);
27 }
28
29 pub fn check_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, node_id: ast::NodeId) {
30     let impl_def_id = tcx.hir.local_def_id(node_id);
31     let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
32     let trait_def_id = trait_ref.def_id;
33
34     if trait_ref.references_error() {
35         debug!("coherence: skipping impl {:?} with error {:?}",
36                impl_def_id, trait_ref);
37         return
38     }
39
40     // Trigger building the specialization graph for the trait of this impl.
41     // This will detect any overlap errors.
42     tcx.specialization_graph_of(trait_def_id);
43
44
45     // check for overlap with the automatic `impl Trait for Trait`
46     if let ty::TyDynamic(ref data, ..) = trait_ref.self_ty().sty {
47         // This is something like impl Trait1 for Trait2. Illegal
48         // if Trait1 is a supertrait of Trait2 or Trait2 is not object safe.
49
50         if data.principal().map_or(true, |p| !tcx.is_object_safe(p.def_id())) {
51             // This is an error, but it will be reported by wfcheck.  Ignore it here.
52             // This is tested by `coherence-impl-trait-for-trait-object-safe.rs`.
53         } else {
54             let mut supertrait_def_ids =
55                 traits::supertrait_def_ids(tcx,
56                                            data.principal().unwrap().def_id());
57             if supertrait_def_ids.any(|d| d == trait_def_id) {
58                 span_err!(tcx.sess,
59                           tcx.span_of_impl(impl_def_id).unwrap(),
60                           E0371,
61                           "the object type `{}` automatically \
62                            implements the trait `{}`",
63                           trait_ref.self_ty(),
64                           tcx.item_path_str(trait_def_id));
65             }
66         }
67     }
68 }
69
70 struct OverlapChecker<'cx, 'tcx: 'cx> {
71     tcx: TyCtxt<'cx, 'tcx, 'tcx>,
72 }
73
74 impl<'cx, 'tcx, 'v> ItemLikeVisitor<'v> for OverlapChecker<'cx, 'tcx> {
75     fn visit_item(&mut self, item: &'v hir::Item) {
76         match item.node {
77             hir::ItemDefaultImpl(..) => {
78                 // look for another default impl; note that due to the
79                 // general orphan/coherence rules, it must always be
80                 // in this crate.
81                 let impl_def_id = self.tcx.hir.local_def_id(item.id);
82                 let trait_ref = self.tcx.impl_trait_ref(impl_def_id).unwrap();
83
84                 let prev_id = self.tcx.hir.trait_default_impl(trait_ref.def_id).unwrap();
85                 if prev_id != item.id {
86                     let mut err = struct_span_err!(self.tcx.sess,
87                                                    self.tcx.span_of_impl(impl_def_id).unwrap(),
88                                                    E0521,
89                                                    "redundant default implementations of trait \
90                                                     `{}`:",
91                                                    trait_ref);
92                     err.span_note(self.tcx
93                                       .span_of_impl(self.tcx.hir.local_def_id(prev_id))
94                                       .unwrap(),
95                                   "redundant implementation is here:");
96                     err.emit();
97                 }
98             }
99             hir::ItemImpl(.., Some(_), _, _) => {
100             }
101             _ => {}
102         }
103     }
104
105     fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem) {
106     }
107
108     fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) {
109     }
110 }