Navigation Menu

Skip to content

Commit

Permalink
rustc: catch impl X for Y where X is not a trait in resolve.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryman committed Jun 23, 2014
1 parent 579a139 commit ab24d29
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 7 deletions.
27 changes: 25 additions & 2 deletions src/librustc/middle/resolve.rs
Expand Up @@ -3894,8 +3894,31 @@ impl<'a> Resolver<'a> {
self.resolve_error(trait_reference.path.span, msg.as_slice());
}
Some(def) => {
debug!("(resolving trait) found trait def: {:?}", def);
self.record_def(trait_reference.ref_id, def);
match def {
(DefTrait(_), _) => {
debug!("(resolving trait) found trait def: {:?}", def);
self.record_def(trait_reference.ref_id, def);
}
(def, _) => {
self.resolve_error(trait_reference.path.span,
format!("`{}` is not a trait",
self.path_idents_to_str(
&trait_reference.path)));

// If it's a typedef, give a note
match def {
DefTy(_) => {
self.session.span_note(
trait_reference.path.span,
format!("`type` aliases cannot \
be used for traits")
.as_slice());
}
_ => {}
}
}
}

}
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/compile-fail/issue-3907-2.rs
@@ -0,0 +1,20 @@
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:issue_3907.rs
extern crate issue_3907;

type Foo = issue_3907::Foo; //~ ERROR: reference to trait

struct S {
name: int
}

fn main() {}
5 changes: 3 additions & 2 deletions src/test/compile-fail/issue-3907.rs
@@ -1,4 +1,4 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -11,13 +11,14 @@
// aux-build:issue_3907.rs
extern crate issue_3907;

type Foo = issue_3907::Foo; //~ ERROR: reference to trait
type Foo = issue_3907::Foo;

struct S {
name: int
}

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

Expand Down
3 changes: 1 addition & 2 deletions src/test/compile-fail/issue-3973.rs
Expand Up @@ -31,7 +31,6 @@ impl NewTrait for Point {
fn main() {
let p = Point::new(0.0, 0.0);
//~^ ERROR unresolved name `Point::new`
//~^^ ERROR unresolved name
//~^^^ ERROR use of undeclared module `Point`
//~^^ ERROR failed to resolve. Use of undeclared module `Point`
println!("{}", p.a());
}
14 changes: 14 additions & 0 deletions src/test/compile-fail/issue-5035-2.rs
@@ -0,0 +1,14 @@
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

trait I {}
type K = I; //~ ERROR: reference to trait

fn main() {}
3 changes: 2 additions & 1 deletion src/test/compile-fail/issue-5035.rs
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

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

9 comments on commit ab24d29

@bors
Copy link
Contributor

@bors bors commented on ab24d29 Jun 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at Ryman@ab24d29

@bors
Copy link
Contributor

@bors bors commented on ab24d29 Jun 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging Ryman/rust/non_trait_method = ab24d29 into auto

@bors
Copy link
Contributor

@bors bors commented on ab24d29 Jun 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ryman/rust/non_trait_method = ab24d29 merged ok, testing candidate = 9c2c629b

@bors
Copy link
Contributor

@bors bors commented on ab24d29 Jun 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on ab24d29 Jun 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at Ryman@ab24d29

@bors
Copy link
Contributor

@bors bors commented on ab24d29 Jun 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging Ryman/rust/non_trait_method = ab24d29 into auto

@bors
Copy link
Contributor

@bors bors commented on ab24d29 Jun 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ryman/rust/non_trait_method = ab24d29 merged ok, testing candidate = d6c1b85

@bors
Copy link
Contributor

@bors bors commented on ab24d29 Jun 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = d6c1b85

Please sign in to comment.