Skip to content

Commit 369f7fa

Browse files
committed
Prevent Send, Freeze, and Sized from being manually implemented. Close #8517.
1 parent 7f26812 commit 369f7fa

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

src/librustc/middle/lang_items.rs

+6
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ impl LanguageItems {
158158
}
159159
}
160160

161+
pub fn is_builtin_kind(&self, id: def_id) -> bool {
162+
Some(id) == self.freeze_trait() ||
163+
Some(id) == self.send_trait() ||
164+
Some(id) == self.sized_trait()
165+
}
166+
161167
pub fn freeze_trait(&self) -> Option<def_id> {
162168
self.items[FreezeTraitLangItem as uint]
163169
}

src/librustc/middle/ty.rs

+6
Original file line numberDiff line numberDiff line change
@@ -3725,6 +3725,12 @@ pub fn impl_trait_ref(cx: ctxt, id: ast::def_id) -> Option<@TraitRef> {
37253725
return ret;
37263726
}
37273727

3728+
pub fn trait_ref_is_builtin_kind(tcx: ctxt, tr: &ast::trait_ref) -> bool {
3729+
let def = tcx.def_map.find(&tr.ref_id).expect("no def-map entry for trait");
3730+
let def_id = ast_util::def_id_of_def(*def);
3731+
tcx.lang_items.is_builtin_kind(def_id)
3732+
}
3733+
37283734
pub fn ty_to_def_id(ty: t) -> Option<ast::def_id> {
37293735
match get(ty).sty {
37303736
ty_trait(id, _, _, _, _) | ty_struct(id, _) | ty_enum(id, _) => Some(id),

src/librustc/middle/typeck/collect.rs

+7
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,13 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::item) {
869869
&i_ty_generics, generics,
870870
parent_visibility);
871871
for t in opt_trait_ref.iter() {
872+
// Prevent the builtin kind traits from being manually implemented.
873+
if ty::trait_ref_is_builtin_kind(ccx.tcx, t) {
874+
ccx.tcx.sess.span_err(it.span,
875+
"cannot provide an explicit implementation \
876+
for a builtin kind");
877+
}
878+
872879
check_methods_against_trait(ccx, generics, rp, selfty, t, cms);
873880
}
874881
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2013 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+
// See issue #8517 for why this should be illegal.
12+
13+
struct X<T>(T);
14+
15+
impl <T> Send for X<T> { } //~ ERROR cannot provide an explicit implementation for a builtin kind
16+
impl <T> Sized for X<T> { } //~ ERROR cannot provide an explicit implementation for a builtin kind
17+
impl <T> Freeze for X<T> { } //~ ERROR cannot provide an explicit implementation for a builtin kind
18+
19+
fn main() { }

0 commit comments

Comments
 (0)