Skip to content

Commit d6c1b85

Browse files
committed
auto merge of #14974 : Ryman/rust/non_trait_method, r=alexcrichton
Closes #3973.
2 parents 575710f + ab24d29 commit d6c1b85

File tree

7 files changed

+94
-13
lines changed

7 files changed

+94
-13
lines changed

src/librustc/middle/resolve.rs

+43-2
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
}
@@ -4021,6 +4044,9 @@ impl<'a> Resolver<'a> {
40214044

40224045
this.with_current_self_type(self_type, |this| {
40234046
for method in methods.iter() {
4047+
// If this is a trait impl, ensure the method exists in trait
4048+
this.check_trait_method(&**method);
4049+
40244050
// We also need a new scope for the method-specific type parameters.
40254051
this.resolve_method(MethodRibKind(id, Provided(method.id)),
40264052
&**method);
@@ -4030,6 +4056,21 @@ impl<'a> Resolver<'a> {
40304056
});
40314057
}
40324058

4059+
fn check_trait_method(&self, method: &Method) {
4060+
// If there is a TraitRef in scope for an impl, then the method must be in the trait.
4061+
for &(did, ref trait_ref) in self.current_trait_ref.iter() {
4062+
let method_name = method.ident.name;
4063+
4064+
if self.method_map.borrow().find(&(method_name, did)).is_none() {
4065+
let path_str = self.path_idents_to_str(&trait_ref.path);
4066+
self.resolve_error(method.span,
4067+
format!("method `{}` is not a member of trait `{}`",
4068+
token::get_name(method_name),
4069+
path_str).as_slice());
4070+
}
4071+
}
4072+
}
4073+
40334074
fn resolve_module(&mut self, module: &Mod, _span: Span,
40344075
_name: Ident, id: NodeId) {
40354076
// Write the implementations in scope into the module metadata.

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,8 @@ fn check_impl_methods_against_trait(ccx: &CrateCtxt,
784784
&impl_trait_ref.substs);
785785
}
786786
None => {
787-
tcx.sess.span_err(
787+
// This is span_bug as it should have already been caught in resolve.
788+
tcx.sess.span_bug(
788789
impl_method.span,
789790
format!(
790791
"method `{}` is not a member of trait `{}`",

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

+20
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

+3-2
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

+10-7
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,29 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// ignore-test
12-
13-
use std::io;
14-
1511
struct Point {
1612
x: f64,
1713
y: f64,
1814
}
1915

20-
impl ToStr for Point { //~ ERROR implements a method not defined in the trait
16+
trait NewTrait {
17+
fn a(&self) -> String;
18+
}
19+
20+
impl NewTrait for Point {
2121
fn new(x: f64, y: f64) -> Point {
22+
//~^ ERROR method `new` is not a member of trait `NewTrait`
2223
Point { x: x, y: y }
2324
}
2425

25-
fn to_str(&self) -> String {
26+
fn a(&self) -> String {
2627
format!("({}, {})", self.x, self.y)
2728
}
2829
}
2930

3031
fn main() {
3132
let p = Point::new(0.0, 0.0);
32-
println!("{}", p.to_str());
33+
//~^ ERROR unresolved name `Point::new`
34+
//~^^ ERROR failed to resolve. Use of undeclared module `Point`
35+
println!("{}", p.a());
3336
}

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

+14
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

+2-1
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)