]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/typeck/coherence/orphan.rs
57ce7f79e030a11b3d18790bf93ee1098999863f
[rust.git] / src / librustc / middle / typeck / coherence / orphan.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 /*!
12  * Orphan checker: every impl either implements a trait defined in this
13  * crate or pertains to a type defined in this crate.
14  */
15
16 use middle::traits;
17 use middle::ty;
18 use syntax::ast::{Item, ItemImpl};
19 use syntax::ast;
20 use syntax::ast_util;
21 use syntax::codemap::Span;
22 use syntax::visit;
23 use util::ppaux::Repr;
24
25 pub fn check(tcx: &ty::ctxt) {
26     let mut orphan = OrphanChecker { tcx: tcx };
27     visit::walk_crate(&mut orphan, tcx.map.krate());
28 }
29
30 struct OrphanChecker<'cx, 'tcx:'cx> {
31     tcx: &'cx ty::ctxt<'tcx>
32 }
33
34 impl<'cx, 'tcx> OrphanChecker<'cx, 'tcx> {
35     fn check_def_id(&self, span: Span, def_id: ast::DefId) {
36         if def_id.krate != ast::LOCAL_CRATE {
37             span_err!(self.tcx.sess, span, E0116,
38                       "cannot associate methods with a type outside the \
39                        crate the type is defined in; define and implement \
40                        a trait or new type instead");
41         }
42     }
43 }
44
45 impl<'cx, 'tcx,'v> visit::Visitor<'v> for OrphanChecker<'cx, 'tcx> {
46     fn visit_item(&mut self, item: &'v ast::Item) {
47         let def_id = ast_util::local_def(item.id);
48         match item.node {
49             ast::ItemImpl(_, None, _, _) => {
50                 // For inherent impls, self type must be a nominal type
51                 // defined in this crate.
52                 debug!("coherence2::orphan check: inherent impl {}", item.repr(self.tcx));
53                 let self_ty = ty::lookup_item_type(self.tcx, def_id).ty;
54                 match self_ty.sty {
55                     ty::ty_enum(def_id, _) |
56                     ty::ty_struct(def_id, _) => {
57                         self.check_def_id(item.span, def_id);
58                     }
59                     ty::ty_trait(box ty::TyTrait{ ref principal, ..}) => {
60                         self.check_def_id(item.span, principal.def_id);
61                     }
62                     _ => {
63                         span_err!(self.tcx.sess, item.span, E0118,
64                                   "no base type found for inherent implementation; \
65                                    implement a trait or new type instead");
66                     }
67                 }
68             }
69             ast::ItemImpl(_, Some(_), _, _) => {
70                 // "Trait" impl
71                 debug!("coherence2::orphan check: trait impl {}", item.repr(self.tcx));
72                 if traits::is_orphan_impl(self.tcx, def_id) {
73                     span_err!(self.tcx.sess, item.span, E0117,
74                               "cannot provide an extension implementation \
75                                where both trait and type are not defined in this crate");
76                 }
77             }
78             _ => {
79                 // Not an impl
80             }
81         }
82
83         visit::walk_item(self, item);
84     }
85 }