diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs index 9b11301a9c494..8dd5ee2058552 100644 --- a/src/librustc/middle/lang_items.rs +++ b/src/librustc/middle/lang_items.rs @@ -158,6 +158,12 @@ impl LanguageItems { } } + pub fn is_builtin_kind(&self, id: def_id) -> bool { + Some(id) == self.freeze_trait() || + Some(id) == self.send_trait() || + Some(id) == self.sized_trait() + } + pub fn freeze_trait(&self) -> Option { self.items[FreezeTraitLangItem as uint] } diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 685699f781900..56c3616d6710f 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -3725,6 +3725,12 @@ pub fn impl_trait_ref(cx: ctxt, id: ast::def_id) -> Option<@TraitRef> { return ret; } +pub fn trait_ref_is_builtin_kind(tcx: ctxt, tr: &ast::trait_ref) -> bool { + let def = tcx.def_map.find(&tr.ref_id).expect("no def-map entry for trait"); + let def_id = ast_util::def_id_of_def(*def); + tcx.lang_items.is_builtin_kind(def_id) +} + pub fn ty_to_def_id(ty: t) -> Option { match get(ty).sty { ty_trait(id, _, _, _, _) | ty_struct(id, _) | ty_enum(id, _) => Some(id), diff --git a/src/librustc/middle/typeck/collect.rs b/src/librustc/middle/typeck/collect.rs index 7678a12b78a5f..2f6728891ff2a 100644 --- a/src/librustc/middle/typeck/collect.rs +++ b/src/librustc/middle/typeck/collect.rs @@ -869,6 +869,13 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::item) { &i_ty_generics, generics, parent_visibility); for t in opt_trait_ref.iter() { + // Prevent the builtin kind traits from being manually implemented. + if ty::trait_ref_is_builtin_kind(ccx.tcx, t) { + ccx.tcx.sess.span_err(it.span, + "cannot provide an explicit implementation \ + for a builtin kind"); + } + check_methods_against_trait(ccx, generics, rp, selfty, t, cms); } } diff --git a/src/test/compile-fail/cant-implement-builtin-kinds.rs b/src/test/compile-fail/cant-implement-builtin-kinds.rs new file mode 100644 index 0000000000000..c35ca098372d3 --- /dev/null +++ b/src/test/compile-fail/cant-implement-builtin-kinds.rs @@ -0,0 +1,19 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// See issue #8517 for why this should be illegal. + +struct X(T); + +impl Send for X { } //~ ERROR cannot provide an explicit implementation for a builtin kind +impl Sized for X { } //~ ERROR cannot provide an explicit implementation for a builtin kind +impl Freeze for X { } //~ ERROR cannot provide an explicit implementation for a builtin kind + +fn main() { }