Skip to content

Commit ab24d29

Browse files
committed
rustc: catch impl X for Y where X is not a trait in resolve.
1 parent 579a139 commit ab24d29

File tree

6 files changed

+65
-7
lines changed

6 files changed

+65
-7
lines changed

src/librustc/middle/resolve.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3894,8 +3894,31 @@ impl<'a> Resolver<'a> {
38943894
self.resolve_error(trait_reference.path.span, msg.as_slice());
38953895
}
38963896
Some(def) => {
3897-
debug!("(resolving trait) found trait def: {:?}", def);
3898-
self.record_def(trait_reference.ref_id, def);
3897+
match def {
3898+
(DefTrait(_), _) => {
3899+
debug!("(resolving trait) found trait def: {:?}", def);
3900+
self.record_def(trait_reference.ref_id, def);
3901+
}
3902+
(def, _) => {
3903+
self.resolve_error(trait_reference.path.span,
3904+
format!("`{}` is not a trait",
3905+
self.path_idents_to_str(
3906+
&trait_reference.path)));
3907+
3908+
// If it's a typedef, give a note
3909+
match def {
3910+
DefTy(_) => {
3911+
self.session.span_note(
3912+
trait_reference.path.span,
3913+
format!("`type` aliases cannot \
3914+
be used for traits")
3915+
.as_slice());
3916+
}
3917+
_ => {}
3918+
}
3919+
}
3920+
}
3921+
38993922
}
39003923
}
39013924
}

src/test/compile-fail/issue-3907-2.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2013-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+
// aux-build:issue_3907.rs
12+
extern crate issue_3907;
13+
14+
type Foo = issue_3907::Foo; //~ ERROR: reference to trait
15+
16+
struct S {
17+
name: int
18+
}
19+
20+
fn main() {}

src/test/compile-fail/issue-3907.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -11,13 +11,14 @@
1111
// aux-build:issue_3907.rs
1212
extern crate issue_3907;
1313

14-
type Foo = issue_3907::Foo; //~ ERROR: reference to trait
14+
type Foo = issue_3907::Foo;
1515

1616
struct S {
1717
name: int
1818
}
1919

2020
impl Foo for S { //~ ERROR: `Foo` is not a trait
21+
//~^ NOTE: `type` aliases cannot be used for traits
2122
fn bar() { }
2223
}
2324

src/test/compile-fail/issue-3973.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ impl NewTrait for Point {
3131
fn main() {
3232
let p = Point::new(0.0, 0.0);
3333
//~^ ERROR unresolved name `Point::new`
34-
//~^^ ERROR unresolved name
35-
//~^^^ ERROR use of undeclared module `Point`
34+
//~^^ ERROR failed to resolve. Use of undeclared module `Point`
3635
println!("{}", p.a());
3736
}

src/test/compile-fail/issue-5035-2.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2013-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+
trait I {}
12+
type K = I; //~ ERROR: reference to trait
13+
14+
fn main() {}

src/test/compile-fail/issue-5035.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
trait I {}
12-
type K = I; //~ ERROR: reference to trait
12+
type K = I;
1313
impl K for int {} //~ ERROR: `K` is not a trait
14+
//~^ NOTE: `type` aliases cannot be used for traits
1415
fn main() {}

0 commit comments

Comments
 (0)