]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/coherence/impls.rs
Kill RacyCell in favor of marking SyncSender explicitly Send.
[rust.git] / src / librustc_typeck / coherence / impls.rs
1 // Copyright 2015 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 //! Implementations checker: builtin traits and default impls are allowed just
12 //! for structs and enums.
13
14 use middle::ty;
15 use syntax::ast::{Item, ItemImpl};
16 use syntax::ast;
17 use syntax::visit;
18
19 pub fn check(tcx: &ty::ctxt) {
20     let mut impls = ImplsChecker { tcx: tcx };
21     visit::walk_crate(&mut impls, tcx.map.krate());
22 }
23
24 struct ImplsChecker<'cx, 'tcx:'cx> {
25     tcx: &'cx ty::ctxt<'tcx>
26 }
27
28 impl<'cx, 'tcx,'v> visit::Visitor<'v> for ImplsChecker<'cx, 'tcx> {
29     fn visit_item(&mut self, item: &'v ast::Item) {
30         match item.node {
31             ast::ItemImpl(_, _, _, Some(ref opt_trait), _, _) => {
32                 let trait_ref = ty::node_id_to_trait_ref(self.tcx, opt_trait.ref_id);
33                 if let Some(_) = self.tcx.lang_items.to_builtin_kind(trait_ref.def_id) {
34                     match trait_ref.self_ty().sty {
35                         ty::ty_struct(..) | ty::ty_enum(..) => {}
36                         _ => {
37                             self.tcx.sess.span_err(
38                                 item.span,
39                                 &format!("builtin traits can only be \
40                                           implemented on structs or enums")[]);
41                         }
42                     }
43                 }
44             }
45             _ => {}
46         }
47     }
48 }