Skip to content

Commit 72c6700

Browse files
committed
---
yaml --- r: 173617 b: refs/heads/master c: de7b3cf h: refs/heads/master i: 173615: a617415 v: v3
1 parent 7766d4c commit 72c6700

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: a7025582cc9c89ee37e38bcdf94482b75cf37cd8
2+
refs/heads/master: de7b3cf02e01be08863b712a666e24e7bfe8fca3
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 210f0dcf007104240b2e286ed0b80cb4a61d7bae
55
refs/heads/try: 957472483d3a2f43c0e4f7c2056280a1022af93c
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.
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+
//! Implementations checker: builtin traits and default impls are allowed just
12+
//! for structs and enums.
13+
14+
use middle::def;
15+
use middle::ty;
16+
use syntax::ast::{Item, ItemImpl};
17+
use syntax::ast;
18+
use syntax::ast_util;
19+
use syntax::visit;
20+
use util::ppaux::UserString;
21+
22+
pub fn check(tcx: &ty::ctxt) {
23+
let mut impls = ImplsChecker { tcx: tcx };
24+
visit::walk_crate(&mut impls, tcx.map.krate());
25+
}
26+
27+
struct ImplsChecker<'cx, 'tcx:'cx> {
28+
tcx: &'cx ty::ctxt<'tcx>
29+
}
30+
31+
impl<'cx, 'tcx,'v> visit::Visitor<'v> for ImplsChecker<'cx, 'tcx> {
32+
fn visit_item(&mut self, item: &'v ast::Item) {
33+
match item.node {
34+
ast::ItemImpl(_, _, _, Some(ref opt_trait), _, _) => {
35+
let trait_ref = ty::node_id_to_trait_ref(self.tcx, opt_trait.ref_id);
36+
if let Some(_) = self.tcx.lang_items.to_builtin_kind(trait_ref.def_id) {
37+
match trait_ref.self_ty().sty {
38+
ty::ty_struct(..) | ty::ty_enum(..) => {}
39+
_ => {
40+
self.tcx.sess.span_err(
41+
item.span,
42+
&format!("builtin traits can only be \
43+
implemented on structs or enums")[]);
44+
}
45+
}
46+
}
47+
}
48+
_ => {}
49+
}
50+
}
51+
}

trunk/src/librustc_typeck/coherence/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use syntax::visit;
4949
use util::nodemap::{DefIdMap, FnvHashMap};
5050
use util::ppaux::Repr;
5151

52+
mod impls;
5253
mod orphan;
5354
mod overlap;
5455
mod unsafety;
@@ -596,6 +597,7 @@ pub fn check_coherence(crate_context: &CrateCtxt) {
596597
inference_context: new_infer_ctxt(crate_context.tcx),
597598
inherent_impls: RefCell::new(FnvHashMap::new()),
598599
}.check(crate_context.tcx.map.krate());
600+
impls::check(crate_context.tcx);
599601
unsafety::check(crate_context.tcx);
600602
orphan::check(crate_context.tcx);
601603
overlap::check(crate_context.tcx);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.
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+
use std::marker::Send;
12+
13+
enum TestE {
14+
A
15+
}
16+
17+
struct MyType;
18+
19+
unsafe impl Send for TestE {}
20+
unsafe impl Send for MyType {}
21+
unsafe impl Send for (MyType, MyType) {}
22+
//~^ ERROR builtin traits can only be implemented on structs or enums
23+
24+
unsafe impl Send for &'static MyType {}
25+
//~^ ERROR builtin traits can only be implemented on structs or enums
26+
27+
unsafe impl Send for [MyType] {}
28+
//~^ ERROR builtin traits can only be implemented on structs or enums
29+
30+
unsafe impl Send for &'static [MyType] {}
31+
//~^ ERROR builtin traits can only be implemented on structs or enums
32+
33+
fn is_send<T: Send>() {}
34+
35+
fn main() {
36+
is_send::<(MyType, TestE)>();
37+
}

0 commit comments

Comments
 (0)