Skip to content

Commit 0419b4a

Browse files
committed
rollup merge of #21167: FlaPer87/coherence-impls
Fixes #21080 r? @nikomatsakis [breaking-change]
2 parents 7be39b0 + de7b3cf commit 0419b4a

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed
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+
}

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

src/libtest/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ pub struct TestDesc {
211211
pub should_fail: ShouldFail,
212212
}
213213

214+
unsafe impl Send for TestDesc {}
215+
214216
#[derive(Show)]
215217
pub struct TestDescAndFn {
216218
pub desc: TestDesc,
@@ -525,6 +527,8 @@ pub enum TestResult {
525527
TrBench(BenchSamples),
526528
}
527529

530+
unsafe impl Send for TestResult {}
531+
528532
enum OutputLocation<T> {
529533
Pretty(Box<term::Terminal<term::WriterWrapper> + Send>),
530534
Raw(T),
@@ -978,7 +982,6 @@ enum TestEvent {
978982

979983
pub type MonitorMsg = (TestDesc, TestResult, Vec<u8> );
980984

981-
unsafe impl Send for MonitorMsg {}
982985

983986
fn run_tests<F>(opts: &TestOpts,
984987
tests: Vec<TestDescAndFn> ,
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)