Skip to content

Commit 8494368

Browse files
committed
rollup merge of rust-lang#18447 : nick29581/dst-impl3
2 parents fff2b35 + f9e52fb commit 8494368

File tree

7 files changed

+100
-8
lines changed

7 files changed

+100
-8
lines changed

src/librustc/middle/resolve.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,10 @@ impl NameBindings {
627627
sp: Span) {
628628
// Merges the module with the existing type def or creates a new one.
629629
let modifiers = if is_public { PUBLIC } else { DefModifiers::empty() } | IMPORTABLE;
630-
let module_ = Rc::new(Module::new(parent_link, def_id, kind, external,
630+
let module_ = Rc::new(Module::new(parent_link,
631+
def_id,
632+
kind,
633+
external,
631634
is_public));
632635
let type_def = self.type_def.borrow().clone();
633636
match type_def {
@@ -1372,6 +1375,8 @@ impl<'a> Resolver<'a> {
13721375
// Create the module and add all methods.
13731376
match ty.node {
13741377
TyPath(ref path, _, _) if path.segments.len() == 1 => {
1378+
// FIXME(18446) we should distinguish between the name of
1379+
// a trait and the name of an impl of that trait.
13751380
let mod_name = path.segments.last().unwrap().identifier.name;
13761381

13771382
let parent_opt = parent.module().children.borrow()
@@ -1380,8 +1385,8 @@ impl<'a> Resolver<'a> {
13801385
// It already exists
13811386
Some(ref child) if child.get_module_if_available()
13821387
.is_some() &&
1383-
child.get_module().kind.get() ==
1384-
ImplModuleKind => {
1388+
(child.get_module().kind.get() == ImplModuleKind ||
1389+
child.get_module().kind.get() == TraitModuleKind) => {
13851390
ModuleReducedGraphParent(child.get_module())
13861391
}
13871392
Some(ref child) if child.get_module_if_available()

src/librustc/middle/traits/select.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
514514
// and `Rc<Baz>`. (Note that it is not a *coherence violation*
515515
// to have impls for both `Bar` and `Baz`, despite this
516516
// ambiguity). In this case, we report an error, listing all
517-
// the applicable impls. The use can explicitly "up-coerce"
517+
// the applicable impls. The user can explicitly "up-coerce"
518518
// to the type they want.
519519
//
520520
// Note that this coercion step only considers actual impls
@@ -1942,7 +1942,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
19421942

19431943
fn all_impls(&self, trait_def_id: ast::DefId) -> Vec<ast::DefId> {
19441944
/*!
1945-
* Returns se tof all impls for a given trait.
1945+
* Returns set of all impls for a given trait.
19461946
*/
19471947

19481948
ty::populate_implementations_for_trait_if_necessary(self.tcx(),

src/librustc/middle/typeck/coherence/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ impl<'a, 'tcx> CoherenceChecker<'a, 'tcx> {
207207
let impl_items = self.create_impl_from_item(item);
208208

209209
for associated_trait in associated_traits.iter() {
210-
let trait_ref = ty::node_id_to_trait_ref(
211-
self.crate_context.tcx, associated_trait.ref_id);
210+
let trait_ref = ty::node_id_to_trait_ref(self.crate_context.tcx,
211+
associated_trait.ref_id);
212212
debug!("(checking implementation) adding impl for trait '{}', item '{}'",
213213
trait_ref.repr(self.crate_context.tcx),
214214
token::get_ident(item.ident));

src/librustc/middle/typeck/coherence/orphan.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ impl<'cx, 'tcx,'v> visit::Visitor<'v> for OrphanChecker<'cx, 'tcx> {
4141
let self_ty = ty::lookup_item_type(self.tcx, def_id).ty;
4242
match ty::get(self_ty).sty {
4343
ty::ty_enum(def_id, _) |
44-
ty::ty_struct(def_id, _) => {
44+
ty::ty_struct(def_id, _) |
45+
ty::ty_trait(box ty::TyTrait{ def_id, ..}) => {
4546
if def_id.krate != ast::LOCAL_CRATE {
4647
span_err!(self.tcx.sess, item.span, E0116,
4748
"cannot associate methods with a type outside the \

src/test/compile-fail/trait-impl-1.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2014 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+
// Test calling methods on an impl for a bare trait. This test checks that the
12+
// trait impl is only applied to a trait object, not concrete types which implement
13+
// the trait.
14+
15+
trait T {}
16+
17+
impl<'a> T+'a {
18+
fn foo(&self) {}
19+
}
20+
21+
impl T for int {}
22+
23+
fn main() {
24+
let x = &42i;
25+
x.foo(); //~ERROR: type `&int` does not implement any method in scope named `foo`
26+
}

src/test/compile-fail/trait-impl-2.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2014 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+
// Test calling methods on an impl for a bare trait. This test checks trait impls
12+
// must be in the same module as the trait.
13+
14+
mod Foo {
15+
trait T {}
16+
}
17+
18+
mod Bar {
19+
impl<'a> ::Foo::T+'a { //~ERROR: inherent implementations may only be implemented in the same
20+
fn foo(&self) {}
21+
}
22+
}
23+
24+
fn main() {}

src/test/run-pass/trait-impl.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2014 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+
// Test calling methods on an impl for a bare trait.
12+
13+
static mut COUNT: uint = 1;
14+
15+
trait T {}
16+
17+
impl<'a> T+'a {
18+
fn foo(&self) {
19+
unsafe { COUNT *= 2; }
20+
}
21+
fn bar() {
22+
unsafe { COUNT *= 3; }
23+
}
24+
}
25+
26+
impl T for int {}
27+
28+
fn main() {
29+
let x: &T = &42i;
30+
31+
x.foo();
32+
T::foo(x);
33+
T::bar();
34+
35+
unsafe { assert!(COUNT == 12); }
36+
}

0 commit comments

Comments
 (0)