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.
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 //! Implementations checker: builtin traits and default impls are allowed just
12 //! for structs and enums.
15 use syntax::ast::{Item, ItemImpl};
19 pub fn check(tcx: &ty::ctxt) {
20 let mut impls = ImplsChecker { tcx: tcx };
21 visit::walk_crate(&mut impls, tcx.map.krate());
24 struct ImplsChecker<'cx, 'tcx:'cx> {
25 tcx: &'cx ty::ctxt<'tcx>
28 impl<'cx, 'tcx,'v> visit::Visitor<'v> for ImplsChecker<'cx, 'tcx> {
29 fn visit_item(&mut self, item: &'v ast::Item) {
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(..) => {}
37 self.tcx.sess.span_err(
39 &format!("builtin traits can only be \
40 implemented on structs or enums")[]);