Prevent Send, Freeze, and Sized from being manually implemented. Close #8517.

This commit is contained in:
Ben Blum 2013-08-14 18:13:16 -04:00
parent 7f26812895
commit 369f7fa169
4 changed files with 38 additions and 0 deletions

View file

@ -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<def_id> {
self.items[FreezeTraitLangItem as uint]
}

View file

@ -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<ast::def_id> {
match get(ty).sty {
ty_trait(id, _, _, _, _) | ty_struct(id, _) | ty_enum(id, _) => Some(id),

View file

@ -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);
}
}

View file

@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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>(T);
impl <T> Send for X<T> { } //~ ERROR cannot provide an explicit implementation for a builtin kind
impl <T> Sized for X<T> { } //~ ERROR cannot provide an explicit implementation for a builtin kind
impl <T> Freeze for X<T> { } //~ ERROR cannot provide an explicit implementation for a builtin kind
fn main() { }