Skip to content

Commit

Permalink
Prevent Send, Freeze, and Sized from being manually implemented. Close
Browse files Browse the repository at this point in the history
  • Loading branch information
bblum committed Aug 20, 2013
1 parent 7f26812 commit 369f7fa
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/librustc/middle/lang_items.rs
Expand Up @@ -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]
}
Expand Down
6 changes: 6 additions & 0 deletions src/librustc/middle/ty.rs
Expand Up @@ -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),
Expand Down
7 changes: 7 additions & 0 deletions src/librustc/middle/typeck/collect.rs
Expand Up @@ -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);
}
}
Expand Down
19 changes: 19 additions & 0 deletions 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 <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() { }

0 comments on commit 369f7fa

Please sign in to comment.